1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
pgsqldatabase.php
См. документацию.
1<?php
2
3namespace Bitrix\Perfmon;
4
6{
10 public function getTables($full = true)
11 {
12 $tables = [];
13
14 if ($full)
15 {
16 $tableList = $this->connection->query("
17 select
18 table_name
19 ,pg_table_size(quote_ident(table_name)) data_length
20 ,pg_indexes_size(quote_ident(table_name)) index_length
21 ,pg_catalog.pg_class.reltuples row_count
22 from
23 information_schema.tables
24 left join pg_catalog.pg_class on pg_catalog.pg_class.oid = quote_ident(table_name)::regclass::oid
25 where
26 table_schema = 'public'
27 ");
28 while ($table = $tableList->fetch())
29 {
30 $tables[] = [
31 'TABLE_NAME' => $table['TABLE_NAME'],
32 'ENGINE_TYPE' => '',
33 'NUM_ROWS' => $table['ROW_COUNT'],
34 'BYTES' => $table['DATA_LENGTH'],
35 'BYTES_INDEX' => $table['INDEX_LENGTH'],
36 ];
37 }
38 }
39 else
40 {
41 $tableList = $this->connection->query("
42 select
43 table_name
44 from
45 information_schema.tables
46 where
47 table_schema = 'public'
48 ");
49 while ($table = $tableList->fetch())
50 {
51 $tables[] = [
52 'TABLE_NAME' => $table['TABLE_NAME'],
53 'ENGINE_TYPE' => '',
54 'NUM_ROWS' => '',
55 'BYTES' => '',
56 'BYTES_INDEX' => '',
57 ];
58 }
59 }
60
61 $result = new \CDBResult();
62 $result->InitFromArray($tables);
63
64 return $result;
65 }
66
70 protected function fillTableIndexes($tableName)
71 {
72 $table = $this->schema->tables->search($tableName);
73 if (!$table)
74 {
75 $table = new \Bitrix\Perfmon\Sql\Table($tableName);
76
77 $sqlHelper = $this->connection->getSqlHelper();
78 $indexColumns = [];
79 $unique = [];
80 $fulltext = [];
81 try
82 {
83 $tableColumns = [];
84 $columnList = $this->connection->query("
85 SELECT a.attnum, a.attname
86 FROM pg_class t
87 LEFT JOIN pg_attribute a ON a.attrelid = t.oid
88 WHERE t.relname = '" . $sqlHelper->forSql($tableName) . "'
89 ");
90 while ($column = $columnList->fetch())
91 {
92 if ($column['ATTNUM'] > 0)
93 {
94 $tableColumns[$column['ATTNUM']] = $column['ATTNAME'];
95 }
96 }
97
98 $indexList = $this->connection->query("
99 SELECT relname, indkey, indisprimary, indisunique, pg_get_expr(pg_index.indexprs, pg_index.indrelid) full_text
100 FROM pg_class, pg_index
101 WHERE pg_class.oid = pg_index.indexrelid
102 AND pg_class.oid IN (
103 SELECT indexrelid
104 FROM pg_index, pg_class
105 WHERE pg_class.relname = '" . $sqlHelper->forSql($tableName) . "'
106 AND pg_class.oid = pg_index.indrelid
107 )
108 ORDER BY indisprimary desc
109 ");
110
111 while ($indexColumn = $indexList->fetch())
112 {
113 $indexColumns[$indexColumn['RELNAME']] = [];
114 $unique[$indexColumn['RELNAME']] = $indexColumn['INDISPRIMARY'] === 't' || $indexColumn['INDISUNIQUE'] === 't';
115 $fulltext[$indexColumn['RELNAME']] = !empty($indexColumn['FULL_TEXT']);
116 if ($indexColumn['FULL_TEXT'])
117 {
118 $match = [];
119 if (preg_match_all('/,\s*([a-z0-9_]+)/i', $indexColumn['FULL_TEXT'], $match))
120 {
121 foreach ($match[1] as $i => $colName)
122 {
123 $indexColumns[$indexColumn['RELNAME']][$i] = mb_strtoupper($colName);
124 }
125 }
126 }
127 else
128 {
129 foreach (explode(' ', $indexColumn['INDKEY']) as $i => $indkey)
130 {
131 $indexColumns[$indexColumn['RELNAME']][$i] = mb_strtoupper($tableColumns[$indkey]);
132 }
133 }
134 }
135 }
136 catch (\Bitrix\Main\DB\SqlQueryException $_)
137 {
138 }
139
140 foreach ($indexColumns as $indexName => $columns)
141 {
142 $index = new \Bitrix\Perfmon\Sql\Index($indexName, $unique[$indexName], $fulltext[$indexName]);
143 $index->columns = array_values($columns);
144 $table->indexes->add($index);
145 }
146 $this->schema->tables->add($table);
147 }
148
149 return $table;
150 }
151
155 public function getTableFields($tableName = false)
156 {
157 $sqlHelper = $this->connection->getSqlHelper();
158
159 $strSql = "
160 SELECT *
161 FROM information_schema.columns
162 WHERE table_schema = 'public'
163 AND table_name = '" . $sqlHelper->forSql($tableName) . "'
164 ";
165 $columnList = $this->connection->query($strSql);
166 $result = [];
167 $resultExt = [];
168 while ($column = $columnList->fetch())
169 {
170 $canSort = true;
171 $match = [];
172 switch ($column['DATA_TYPE'])
173 {
174 case 'character varying':
175 $dataType = 'string';
176 $ormDataType = 'string';
177 break;
178 case 'character':
179 $dataType = 'string';
180 if (
181 $column['CHARACTER_MAXIMUM_LENGTH'] == 1
182 && (
183 substr($column['COLUMN_DEFAULT'], 0, 3) === "'N'"
184 || substr($column['COLUMN_DEFAULT'], 0, 3) === "'Y'"
185 )
186 )
187 {
188 $column['COLUMN_DEFAULT'] = $column['COLUMN_DEFAULT'][1];
189 $ormDataType = 'boolean';
190 }
191 else
192 {
193 $ormDataType = 'string';
194 }
195 break;
196 case 'text':
197 case 'bytea':
198 $canSort = false;
199 $dataType = 'string';
200 $ormDataType = 'string';
201 break;
202 case 'bigint':
203 case 'bigserial':
204 case 'int':
205 case 'int2':
206 case 'int4':
207 case 'int8':
208 case 'integer':
209 case 'serial':
210 case 'serial2':
211 case 'serial4':
212 case 'serial8':
213 case 'smallint':
214 case 'smallserial':
215 $dataType = 'int';
216 $ormDataType = 'integer';
217 break;
218 case 'double precision':
219 case 'float4':
220 case 'float8':
221 case 'numeric':
222 case 'real':
223 $dataType = 'double';
224 $ormDataType = 'float';
225 break;
226 case 'timestamp without time zone':
227 $dataType = 'datetime';
228 $ormDataType = 'datetime';
229 break;
230 case 'date':
231 $dataType = 'date';
232 $ormDataType = 'date';
233 break;
234 default:
235 $canSort = false;
236 $dataType = 'unknown';
237 $ormDataType = 'UNKNOWN';
238 break;
239 }
240
241 $result[mb_strtoupper($column['COLUMN_NAME'])] = $dataType;
242 $resultExt[mb_strtoupper($column['COLUMN_NAME'])] = [
243 'type' => $dataType,
244 'length' => $column['CHARACTER_MAXIMUM_LENGTH'],
245 'nullable' => $column['IS_NULLABLE'] !== 'NO',
246 'default' => preg_match('/^\'(.*)\'::/', $column['COLUMN_DEFAULT'], $match) ? $match[1] : $column['COLUMN_DEFAULT'],
247 'sortable' => $canSort,
248 'orm_type' => $ormDataType,
249 'increment' => strpos($column['COLUMN_DEFAULT'], 'nextval(') !== false || $column['IS_IDENTITY'] === 'YES',
250 'info' => $column,
251 ];
252 }
253
254 return [$result, $resultExt];
255 }
256}
fillTableIndexes($tableName)
Определения pgsqldatabase.php:70
getTableFields($tableName=false)
Определения pgsqldatabase.php:155
getTables($full=true)
Определения pgsqldatabase.php:10
$result
Определения get_property_values.php:14
$i
Определения factura.php:643
$columnList
Определения template.php:276