| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mode; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.HashSet; |
| 10 |
import java.util.List; |
| 11 |
|
| 12 |
import org.h2.constraint.Constraint; |
| 13 |
import org.h2.engine.Constants; |
| 14 |
import org.h2.engine.RightOwner; |
| 15 |
import org.h2.engine.SessionLocal; |
| 16 |
import org.h2.engine.User; |
| 17 |
import org.h2.index.Index; |
| 18 |
import org.h2.message.DbException; |
| 19 |
import org.h2.result.Row; |
| 20 |
import org.h2.result.SearchRow; |
| 21 |
import org.h2.schema.Schema; |
| 22 |
import org.h2.schema.TriggerObject; |
| 23 |
import org.h2.server.pg.PgServer; |
| 24 |
import org.h2.table.Column; |
| 25 |
import org.h2.table.MetaTable; |
| 26 |
import org.h2.table.Table; |
| 27 |
import org.h2.util.StringUtils; |
| 28 |
import org.h2.util.Utils; |
| 29 |
import org.h2.value.DataType; |
| 30 |
import org.h2.value.TypeInfo; |
| 31 |
import org.h2.value.Value; |
| 32 |
import org.h2.value.ValueArray; |
| 33 |
import org.h2.value.ValueBoolean; |
| 34 |
import org.h2.value.ValueDouble; |
| 35 |
import org.h2.value.ValueInteger; |
| 36 |
import org.h2.value.ValueSmallint; |
| 37 |
|
| 38 |
/** |
| 39 |
* This class is responsible to build the pg_catalog tables. |
| 40 |
*/ |
| 41 |
public final class PgCatalogTable extends MetaTable { |
| 42 |
|
| 43 |
private static final int PG_AM = 0; |
| 44 |
|
| 45 |
private static final int PG_ATTRDEF = PG_AM + 1; |
| 46 |
|
| 47 |
private static final int PG_ATTRIBUTE = PG_ATTRDEF + 1; |
| 48 |
|
| 49 |
private static final int PG_AUTHID = PG_ATTRIBUTE + 1; |
| 50 |
|
| 51 |
private static final int PG_CLASS = PG_AUTHID + 1; |
| 52 |
|
| 53 |
private static final int PG_CONSTRAINT = PG_CLASS + 1; |
| 54 |
|
| 55 |
private static final int PG_DATABASE = PG_CONSTRAINT + 1; |
| 56 |
|
| 57 |
private static final int PG_DESCRIPTION = PG_DATABASE + 1; |
| 58 |
|
| 59 |
private static final int PG_GROUP = PG_DESCRIPTION + 1; |
| 60 |
|
| 61 |
private static final int PG_INDEX = PG_GROUP + 1; |
| 62 |
|
| 63 |
private static final int PG_INHERITS = PG_INDEX + 1; |
| 64 |
|
| 65 |
private static final int PG_NAMESPACE = PG_INHERITS + 1; |
| 66 |
|
| 67 |
private static final int PG_PROC = PG_NAMESPACE + 1; |
| 68 |
|
| 69 |
private static final int PG_ROLES = PG_PROC + 1; |
| 70 |
|
| 71 |
private static final int PG_SETTINGS = PG_ROLES + 1; |
| 72 |
|
| 73 |
private static final int PG_TABLESPACE = PG_SETTINGS + 1; |
| 74 |
|
| 75 |
private static final int PG_TRIGGER = PG_TABLESPACE + 1; |
| 76 |
|
| 77 |
private static final int PG_TYPE = PG_TRIGGER + 1; |
| 78 |
|
| 79 |
private static final int PG_USER = PG_TYPE + 1; |
| 80 |
|
| 81 |
/** |
| 82 |
* The number of meta table types. Supported meta table types are |
| 83 |
* {@code 0..META_TABLE_TYPE_COUNT - 1}. |
| 84 |
*/ |
| 85 |
public static final int META_TABLE_TYPE_COUNT = PG_USER + 1; |
| 86 |
|
| 87 |
private static final Object[][] PG_EXTRA_TYPES = { |
| 88 |
{ 18, "char", 1, 0 }, |
| 89 |
{ 19, "name", 64, 18 }, |
| 90 |
{ 22, "int2vector", -1, 21 }, |
| 91 |
{ 24, "regproc", 4, 0 }, |
| 92 |
{ PgServer.PG_TYPE_INT2_ARRAY, "_int2", -1, PgServer.PG_TYPE_INT2 }, |
| 93 |
{ PgServer.PG_TYPE_INT4_ARRAY, "_int4", -1, PgServer.PG_TYPE_INT4 }, |
| 94 |
{ PgServer.PG_TYPE_VARCHAR_ARRAY, "_varchar", -1, PgServer.PG_TYPE_VARCHAR }, |
| 95 |
{ 2205, "regclass", 4, 0 }, |
| 96 |
}; |
| 97 |
|
| 98 |
/** |
| 99 |
* Create a new metadata table. |
| 100 |
* |
| 101 |
* @param schema |
| 102 |
* the schema |
| 103 |
* @param id |
| 104 |
* the object id |
| 105 |
* @param type |
| 106 |
* the meta table type |
| 107 |
*/ |
| 108 |
public PgCatalogTable(Schema schema, int id, int type) { |
| 109 |
super(schema, id, type); |
| 110 |
Column[] cols; |
| 111 |
switch (type) { |
| 112 |
case PG_AM: |
| 113 |
setMetaTableName("PG_AM"); |
| 114 |
cols = new Column[] { // |
| 115 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 116 |
column("AMNAME", TypeInfo.TYPE_VARCHAR), // |
| 117 |
}; |
| 118 |
break; |
| 119 |
case PG_ATTRDEF: |
| 120 |
setMetaTableName("PG_ATTRDEF"); |
| 121 |
cols = new Column[] { // |
| 122 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 123 |
column("ADSRC", TypeInfo.TYPE_INTEGER), // |
| 124 |
column("ADRELID", TypeInfo.TYPE_INTEGER), // |
| 125 |
column("ADNUM", TypeInfo.TYPE_INTEGER), // |
| 126 |
column("ADBIN", TypeInfo.TYPE_VARCHAR), // pg_node_tree |
| 127 |
}; |
| 128 |
break; |
| 129 |
case PG_ATTRIBUTE: |
| 130 |
setMetaTableName("PG_ATTRIBUTE"); |
| 131 |
cols = new Column[] { // |
| 132 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 133 |
column("ATTRELID", TypeInfo.TYPE_INTEGER), // |
| 134 |
column("ATTNAME", TypeInfo.TYPE_VARCHAR), // |
| 135 |
column("ATTTYPID", TypeInfo.TYPE_INTEGER), // |
| 136 |
column("ATTLEN", TypeInfo.TYPE_INTEGER), // |
| 137 |
column("ATTNUM", TypeInfo.TYPE_INTEGER), // |
| 138 |
column("ATTTYPMOD", TypeInfo.TYPE_INTEGER), // |
| 139 |
column("ATTNOTNULL", TypeInfo.TYPE_BOOLEAN), // |
| 140 |
column("ATTISDROPPED", TypeInfo.TYPE_BOOLEAN), // |
| 141 |
column("ATTHASDEF", TypeInfo.TYPE_BOOLEAN), // |
| 142 |
}; |
| 143 |
break; |
| 144 |
case PG_AUTHID: |
| 145 |
setMetaTableName("PG_AUTHID"); |
| 146 |
cols = new Column[] { // |
| 147 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 148 |
column("ROLNAME", TypeInfo.TYPE_VARCHAR), // |
| 149 |
column("ROLSUPER", TypeInfo.TYPE_BOOLEAN), // |
| 150 |
column("ROLINHERIT", TypeInfo.TYPE_BOOLEAN), // |
| 151 |
column("ROLCREATEROLE", TypeInfo.TYPE_BOOLEAN), // |
| 152 |
column("ROLCREATEDB", TypeInfo.TYPE_BOOLEAN), // |
| 153 |
column("ROLCATUPDATE", TypeInfo.TYPE_BOOLEAN), // |
| 154 |
column("ROLCANLOGIN", TypeInfo.TYPE_BOOLEAN), // |
| 155 |
column("ROLCONNLIMIT", TypeInfo.TYPE_BOOLEAN), // |
| 156 |
column("ROLPASSWORD", TypeInfo.TYPE_BOOLEAN), // |
| 157 |
column("ROLVALIDUNTIL", TypeInfo.TYPE_TIMESTAMP_TZ), // |
| 158 |
column("ROLCONFIG", TypeInfo.getTypeInfo(Value.ARRAY, -1L, 0, TypeInfo.TYPE_VARCHAR)), // |
| 159 |
}; |
| 160 |
break; |
| 161 |
case PG_CLASS: |
| 162 |
setMetaTableName("PG_CLASS"); |
| 163 |
cols = new Column[] { // |
| 164 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 165 |
column("RELNAME", TypeInfo.TYPE_VARCHAR), // |
| 166 |
column("RELNAMESPACE", TypeInfo.TYPE_INTEGER), // |
| 167 |
column("RELKIND", TypeInfo.TYPE_CHAR), // |
| 168 |
column("RELAM", TypeInfo.TYPE_INTEGER), // |
| 169 |
column("RELTUPLES", TypeInfo.TYPE_DOUBLE), // |
| 170 |
column("RELTABLESPACE", TypeInfo.TYPE_INTEGER), // |
| 171 |
column("RELPAGES", TypeInfo.TYPE_INTEGER), // |
| 172 |
column("RELHASINDEX", TypeInfo.TYPE_BOOLEAN), // |
| 173 |
column("RELHASRULES", TypeInfo.TYPE_BOOLEAN), // |
| 174 |
column("RELHASOIDS", TypeInfo.TYPE_BOOLEAN), // |
| 175 |
column("RELCHECKS", TypeInfo.TYPE_SMALLINT), // |
| 176 |
column("RELTRIGGERS", TypeInfo.TYPE_INTEGER), // |
| 177 |
}; |
| 178 |
break; |
| 179 |
case PG_CONSTRAINT: |
| 180 |
setMetaTableName("PG_CONSTRAINT"); |
| 181 |
cols = new Column[] { // |
| 182 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 183 |
column("CONNAME", TypeInfo.TYPE_VARCHAR), // |
| 184 |
column("CONTYPE", TypeInfo.TYPE_VARCHAR), // |
| 185 |
column("CONRELID", TypeInfo.TYPE_INTEGER), // |
| 186 |
column("CONFRELID", TypeInfo.TYPE_INTEGER), // |
| 187 |
column("CONKEY", TypeInfo.getTypeInfo(Value.ARRAY, -1L, 0, TypeInfo.TYPE_SMALLINT)), // |
| 188 |
}; |
| 189 |
break; |
| 190 |
case PG_DATABASE: |
| 191 |
setMetaTableName("PG_DATABASE"); |
| 192 |
cols = new Column[] { // |
| 193 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 194 |
column("DATNAME", TypeInfo.TYPE_VARCHAR), // |
| 195 |
column("ENCODING", TypeInfo.TYPE_INTEGER), // |
| 196 |
column("DATLASTSYSOID", TypeInfo.TYPE_INTEGER), // |
| 197 |
column("DATALLOWCONN", TypeInfo.TYPE_BOOLEAN), // |
| 198 |
column("DATCONFIG", TypeInfo.getTypeInfo(Value.ARRAY, -1L, 0, TypeInfo.TYPE_VARCHAR)), // |
| 199 |
column("DATACL", TypeInfo.getTypeInfo(Value.ARRAY, -1L, 0, TypeInfo.TYPE_VARCHAR)), // aclitem[] |
| 200 |
column("DATDBA", TypeInfo.TYPE_INTEGER), // |
| 201 |
column("DATTABLESPACE", TypeInfo.TYPE_INTEGER), // |
| 202 |
}; |
| 203 |
break; |
| 204 |
case PG_DESCRIPTION: |
| 205 |
setMetaTableName("PG_DESCRIPTION"); |
| 206 |
cols = new Column[] { // |
| 207 |
column("OBJOID", TypeInfo.TYPE_INTEGER), // |
| 208 |
column("OBJSUBID", TypeInfo.TYPE_INTEGER), // |
| 209 |
column("CLASSOID", TypeInfo.TYPE_INTEGER), // |
| 210 |
column("DESCRIPTION", TypeInfo.TYPE_VARCHAR), // |
| 211 |
}; |
| 212 |
break; |
| 213 |
case PG_GROUP: |
| 214 |
setMetaTableName("PG_GROUP"); |
| 215 |
cols = new Column[] { // |
| 216 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 217 |
column("GRONAME", TypeInfo.TYPE_VARCHAR), // |
| 218 |
}; |
| 219 |
break; |
| 220 |
case PG_INDEX: |
| 221 |
setMetaTableName("PG_INDEX"); |
| 222 |
cols = new Column[] { // |
| 223 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 224 |
column("INDEXRELID", TypeInfo.TYPE_INTEGER), // |
| 225 |
column("INDRELID", TypeInfo.TYPE_INTEGER), // |
| 226 |
column("INDISCLUSTERED", TypeInfo.TYPE_BOOLEAN), // |
| 227 |
column("INDISUNIQUE", TypeInfo.TYPE_BOOLEAN), // |
| 228 |
column("INDISPRIMARY", TypeInfo.TYPE_BOOLEAN), // |
| 229 |
column("INDEXPRS", TypeInfo.TYPE_VARCHAR), // |
| 230 |
column("INDKEY", TypeInfo.getTypeInfo(Value.ARRAY, -1L, 0, TypeInfo.TYPE_INTEGER)), // |
| 231 |
column("INDPRED", TypeInfo.TYPE_VARCHAR), // pg_node_tree |
| 232 |
}; |
| 233 |
break; |
| 234 |
case PG_INHERITS: |
| 235 |
setMetaTableName("PG_INHERITS"); |
| 236 |
cols = new Column[] { // |
| 237 |
column("INHRELID", TypeInfo.TYPE_INTEGER), // |
| 238 |
column("INHPARENT", TypeInfo.TYPE_INTEGER), // |
| 239 |
column("INHSEQNO", TypeInfo.TYPE_INTEGER), // |
| 240 |
}; |
| 241 |
break; |
| 242 |
case PG_NAMESPACE: |
| 243 |
setMetaTableName("PG_NAMESPACE"); |
| 244 |
cols = new Column[] { // |
| 245 |
column("ID", TypeInfo.TYPE_INTEGER), // |
| 246 |
column("NSPNAME", TypeInfo.TYPE_VARCHAR), // |
| 247 |
}; |
| 248 |
break; |
| 249 |
case PG_PROC: |
| 250 |
setMetaTableName("PG_PROC"); |
| 251 |
cols = new Column[] { // |
| 252 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 253 |
column("PRONAME", TypeInfo.TYPE_VARCHAR), // |
| 254 |
column("PRORETTYPE", TypeInfo.TYPE_INTEGER), // |
| 255 |
column("PROARGTYPES", TypeInfo.getTypeInfo(Value.ARRAY, -1L, 0, TypeInfo.TYPE_INTEGER)), // |
| 256 |
column("PRONAMESPACE", TypeInfo.TYPE_INTEGER), // |
| 257 |
}; |
| 258 |
break; |
| 259 |
case PG_ROLES: |
| 260 |
setMetaTableName("PG_ROLES"); |
| 261 |
cols = new Column[] { // |
| 262 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 263 |
column("ROLNAME", TypeInfo.TYPE_VARCHAR), // |
| 264 |
column("ROLSUPER", TypeInfo.TYPE_CHAR), // |
| 265 |
column("ROLCREATEROLE", TypeInfo.TYPE_CHAR), // |
| 266 |
column("ROLCREATEDB", TypeInfo.TYPE_CHAR), // |
| 267 |
}; |
| 268 |
break; |
| 269 |
case PG_SETTINGS: |
| 270 |
setMetaTableName("PG_SETTINGS"); |
| 271 |
cols = new Column[] { // |
| 272 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 273 |
column("NAME", TypeInfo.TYPE_VARCHAR), // |
| 274 |
column("SETTING", TypeInfo.TYPE_VARCHAR), // |
| 275 |
}; |
| 276 |
break; |
| 277 |
case PG_TABLESPACE: |
| 278 |
setMetaTableName("PG_TABLESPACE"); |
| 279 |
cols = new Column[] { // |
| 280 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 281 |
column("SPCNAME", TypeInfo.TYPE_VARCHAR), // |
| 282 |
column("SPCLOCATION", TypeInfo.TYPE_VARCHAR), // |
| 283 |
column("SPCOWNER", TypeInfo.TYPE_INTEGER), // |
| 284 |
column("SPCACL", TypeInfo.getTypeInfo(Value.ARRAY, -1L, 0, TypeInfo.TYPE_VARCHAR)), // ACLITEM[] |
| 285 |
}; |
| 286 |
break; |
| 287 |
case PG_TRIGGER: |
| 288 |
setMetaTableName("PG_TRIGGER"); |
| 289 |
cols = new Column[] { // |
| 290 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 291 |
column("TGCONSTRRELID", TypeInfo.TYPE_INTEGER), // |
| 292 |
column("TGFOID", TypeInfo.TYPE_INTEGER), // |
| 293 |
column("TGARGS", TypeInfo.TYPE_INTEGER), // |
| 294 |
column("TGNARGS", TypeInfo.TYPE_INTEGER), // |
| 295 |
column("TGDEFERRABLE", TypeInfo.TYPE_BOOLEAN), // |
| 296 |
column("TGINITDEFERRED", TypeInfo.TYPE_BOOLEAN), // |
| 297 |
column("TGCONSTRNAME", TypeInfo.TYPE_VARCHAR), // |
| 298 |
column("TGRELID", TypeInfo.TYPE_INTEGER), // |
| 299 |
}; |
| 300 |
break; |
| 301 |
case PG_TYPE: |
| 302 |
setMetaTableName("PG_TYPE"); |
| 303 |
cols = new Column[] { // |
| 304 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 305 |
column("TYPNAME", TypeInfo.TYPE_VARCHAR), // |
| 306 |
column("TYPNAMESPACE", TypeInfo.TYPE_INTEGER), // |
| 307 |
column("TYPLEN", TypeInfo.TYPE_INTEGER), // |
| 308 |
column("TYPTYPE", TypeInfo.TYPE_VARCHAR), // |
| 309 |
column("TYPDELIM", TypeInfo.TYPE_VARCHAR), // |
| 310 |
column("TYPRELID", TypeInfo.TYPE_INTEGER), // |
| 311 |
column("TYPELEM", TypeInfo.TYPE_INTEGER), // |
| 312 |
column("TYPBASETYPE", TypeInfo.TYPE_INTEGER), // |
| 313 |
column("TYPTYPMOD", TypeInfo.TYPE_INTEGER), // |
| 314 |
column("TYPNOTNULL", TypeInfo.TYPE_BOOLEAN), // |
| 315 |
column("TYPINPUT", TypeInfo.TYPE_VARCHAR), // |
| 316 |
}; |
| 317 |
break; |
| 318 |
case PG_USER: |
| 319 |
setMetaTableName("PG_USER"); |
| 320 |
cols = new Column[] { // |
| 321 |
column("OID", TypeInfo.TYPE_INTEGER), // |
| 322 |
column("USENAME", TypeInfo.TYPE_VARCHAR), // |
| 323 |
column("USECREATEDB", TypeInfo.TYPE_BOOLEAN), // |
| 324 |
column("USESUPER", TypeInfo.TYPE_BOOLEAN), // |
| 325 |
}; |
| 326 |
break; |
| 327 |
default: |
| 328 |
throw DbException.getInternalError("type=" + type); |
| 329 |
} |
| 330 |
setColumns(cols); |
| 331 |
indexColumn = -1; |
| 332 |
metaIndex = null; |
| 333 |
} |
| 334 |
|
| 335 |
@Override |
| 336 |
public ArrayList<Row> generateRows(SessionLocal session, SearchRow first, SearchRow last) { |
| 337 |
ArrayList<Row> rows = Utils.newSmallArrayList(); |
| 338 |
String catalog = database.getShortName(); |
| 339 |
boolean admin = session.getUser().isAdmin(); |
| 340 |
switch (type) { |
| 341 |
case PG_AM: { |
| 342 |
String[] am = { "btree", "hash" }; |
| 343 |
for (int i = 0, l = am.length; i < l; i++) { |
| 344 |
add(session, rows, |
| 345 |
// OID |
| 346 |
ValueInteger.get(i), |
| 347 |
// AMNAME |
| 348 |
am[i]); |
| 349 |
} |
| 350 |
break; |
| 351 |
} |
| 352 |
case PG_ATTRDEF: |
| 353 |
break; |
| 354 |
case PG_ATTRIBUTE: |
| 355 |
for (Schema schema : database.getAllSchemas()) { |
| 356 |
for (Table table : schema.getAllTablesAndViews(session)) { |
| 357 |
if (!hideTable(table, session)) { |
| 358 |
pgAttribute(session, rows, table); |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
for (Table table: session.getLocalTempTables()) { |
| 363 |
if (!hideTable(table, session)) { |
| 364 |
pgAttribute(session, rows, table); |
| 365 |
} |
| 366 |
} |
| 367 |
break; |
| 368 |
case PG_AUTHID: |
| 369 |
break; |
| 370 |
case PG_CLASS: |
| 371 |
for (Schema schema : database.getAllSchemas()) { |
| 372 |
for (Table table : schema.getAllTablesAndViews(session)) { |
| 373 |
if (!hideTable(table, session)) { |
| 374 |
pgClass(session, rows, table); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
for (Table table: session.getLocalTempTables()) { |
| 379 |
if (!hideTable(table, session)) { |
| 380 |
pgClass(session, rows, table); |
| 381 |
} |
| 382 |
} |
| 383 |
break; |
| 384 |
case PG_CONSTRAINT: |
| 385 |
pgConstraint(session, rows); |
| 386 |
break; |
| 387 |
case PG_DATABASE: { |
| 388 |
int uid = Integer.MAX_VALUE; |
| 389 |
for (RightOwner rightOwner : database.getAllUsersAndRoles()) { |
| 390 |
if (rightOwner instanceof User && ((User) rightOwner).isAdmin()) { |
| 391 |
int id = rightOwner.getId(); |
| 392 |
if (id < uid) { |
| 393 |
uid = id; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
add(session, rows, |
| 398 |
// OID |
| 399 |
ValueInteger.get(100_001), |
| 400 |
// DATNAME |
| 401 |
catalog, |
| 402 |
// ENCODING INT, |
| 403 |
ValueInteger.get(6), // UTF-8 |
| 404 |
// DATLASTSYSOID INT, |
| 405 |
ValueInteger.get(100_000), |
| 406 |
// DATALLOWCONN BOOLEAN, |
| 407 |
ValueBoolean.TRUE, |
| 408 |
// DATCONFIG ARRAY, -- TEXT[] |
| 409 |
null, |
| 410 |
// DATACL ARRAY, -- ACLITEM[] |
| 411 |
null, |
| 412 |
// DATDBA INT, |
| 413 |
ValueInteger.get(uid), |
| 414 |
// DATTABLESPACE INT |
| 415 |
ValueInteger.get(0)); |
| 416 |
break; |
| 417 |
} |
| 418 |
case PG_DESCRIPTION: |
| 419 |
add(session, rows, |
| 420 |
// OBJOID |
| 421 |
ValueInteger.get(0), |
| 422 |
// OBJSUBID |
| 423 |
ValueInteger.get(0), |
| 424 |
// CLASSOID |
| 425 |
ValueInteger.get(-1), |
| 426 |
// DESCRIPTION |
| 427 |
catalog); |
| 428 |
break; |
| 429 |
case PG_GROUP: |
| 430 |
// The next one returns no rows due to MS Access problem opening |
| 431 |
// tables with primary key |
| 432 |
case PG_INDEX: |
| 433 |
case PG_INHERITS: |
| 434 |
break; |
| 435 |
case PG_NAMESPACE: |
| 436 |
for (Schema schema : database.getAllSchemas()) { |
| 437 |
add(session, rows, |
| 438 |
// ID |
| 439 |
ValueInteger.get(schema.getId()), |
| 440 |
// NSPNAME |
| 441 |
schema.getName()); |
| 442 |
} |
| 443 |
break; |
| 444 |
case PG_PROC: |
| 445 |
break; |
| 446 |
case PG_ROLES: |
| 447 |
for (RightOwner rightOwner : database.getAllUsersAndRoles()) { |
| 448 |
if (admin || session.getUser() == rightOwner) { |
| 449 |
String r = rightOwner instanceof User && ((User) rightOwner).isAdmin() ? "t" : "f"; |
| 450 |
add(session, rows, |
| 451 |
// OID |
| 452 |
ValueInteger.get(rightOwner.getId()), |
| 453 |
// ROLNAME |
| 454 |
identifier(rightOwner.getName()), |
| 455 |
// ROLSUPER |
| 456 |
r, |
| 457 |
// ROLCREATEROLE |
| 458 |
r, |
| 459 |
// ROLCREATEDB; |
| 460 |
r); |
| 461 |
} |
| 462 |
} |
| 463 |
break; |
| 464 |
case PG_SETTINGS: { |
| 465 |
String[][] settings = { { "autovacuum", "on" }, { "stats_start_collector", "on" }, |
| 466 |
{ "stats_row_level", "on" } }; |
| 467 |
for (int i = 0, l = settings.length; i < l; i++) { |
| 468 |
String[] setting = settings[i]; |
| 469 |
add(session, rows, |
| 470 |
// OID |
| 471 |
ValueInteger.get(i), |
| 472 |
// NAME |
| 473 |
setting[0], |
| 474 |
// SETTING |
| 475 |
setting[1]); |
| 476 |
} |
| 477 |
break; |
| 478 |
} |
| 479 |
case PG_TABLESPACE: |
| 480 |
add(session, rows, |
| 481 |
// OID INTEGER |
| 482 |
ValueInteger.get(0), |
| 483 |
// SPCNAME |
| 484 |
"main", |
| 485 |
// SPCLOCATION |
| 486 |
"?", |
| 487 |
// SPCOWNER |
| 488 |
ValueInteger.get(0), |
| 489 |
// SPCACL |
| 490 |
null); |
| 491 |
break; |
| 492 |
case PG_TRIGGER: |
| 493 |
break; |
| 494 |
case PG_TYPE: { |
| 495 |
HashSet<Integer> types = new HashSet<>(); |
| 496 |
for (int i = 1, l = Value.TYPE_COUNT; i < l; i++) { |
| 497 |
DataType t = DataType.getDataType(i); |
| 498 |
if (t.type == Value.ARRAY) { |
| 499 |
continue; |
| 500 |
} |
| 501 |
int pgType = PgServer.convertType(TypeInfo.getTypeInfo(t.type)); |
| 502 |
if (pgType == PgServer.PG_TYPE_UNKNOWN || !types.add(pgType)) { |
| 503 |
continue; |
| 504 |
} |
| 505 |
add(session, rows, |
| 506 |
// OID |
| 507 |
ValueInteger.get(pgType), |
| 508 |
// TYPNAME |
| 509 |
Value.getTypeName(t.type), |
| 510 |
// TYPNAMESPACE |
| 511 |
ValueInteger.get(Constants.PG_CATALOG_SCHEMA_ID), |
| 512 |
// TYPLEN |
| 513 |
ValueInteger.get(-1), |
| 514 |
// TYPTYPE |
| 515 |
"b", |
| 516 |
// TYPDELIM |
| 517 |
",", |
| 518 |
// TYPRELID |
| 519 |
ValueInteger.get(0), |
| 520 |
// TYPELEM |
| 521 |
ValueInteger.get(0), |
| 522 |
// TYPBASETYPE |
| 523 |
ValueInteger.get(0), |
| 524 |
// TYPTYPMOD |
| 525 |
ValueInteger.get(-1), |
| 526 |
// TYPNOTNULL |
| 527 |
ValueBoolean.FALSE, |
| 528 |
// TYPINPUT |
| 529 |
null); |
| 530 |
} |
| 531 |
for (Object[] pgType : PG_EXTRA_TYPES) { |
| 532 |
add(session, rows, |
| 533 |
// OID |
| 534 |
ValueInteger.get((int) pgType[0]), |
| 535 |
// TYPNAME |
| 536 |
pgType[1], |
| 537 |
// TYPNAMESPACE |
| 538 |
ValueInteger.get(Constants.PG_CATALOG_SCHEMA_ID), |
| 539 |
// TYPLEN |
| 540 |
ValueInteger.get((int) pgType[2]), |
| 541 |
// TYPTYPE |
| 542 |
"b", |
| 543 |
// TYPDELIM |
| 544 |
",", |
| 545 |
// TYPRELID |
| 546 |
ValueInteger.get(0), |
| 547 |
// TYPELEM |
| 548 |
ValueInteger.get((int) pgType[3]), |
| 549 |
// TYPBASETYPE |
| 550 |
ValueInteger.get(0), |
| 551 |
// TYPTYPMOD |
| 552 |
ValueInteger.get(-1), |
| 553 |
// TYPNOTNULL |
| 554 |
ValueBoolean.FALSE, |
| 555 |
// TYPINPUT |
| 556 |
null); |
| 557 |
} |
| 558 |
break; |
| 559 |
} |
| 560 |
case PG_USER: |
| 561 |
for (RightOwner rightOwner : database.getAllUsersAndRoles()) { |
| 562 |
if (rightOwner instanceof User) { |
| 563 |
User u = (User) rightOwner; |
| 564 |
if (admin || session.getUser() == u) { |
| 565 |
ValueBoolean r = ValueBoolean.get(u.isAdmin()); |
| 566 |
add(session, rows, |
| 567 |
// OID |
| 568 |
ValueInteger.get(u.getId()), |
| 569 |
// USENAME |
| 570 |
identifier(u.getName()), |
| 571 |
// USECREATEDB |
| 572 |
r, |
| 573 |
// USESUPER; |
| 574 |
r); |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
break; |
| 579 |
default: |
| 580 |
throw DbException.getInternalError("type=" + type); |
| 581 |
} |
| 582 |
return rows; |
| 583 |
|
| 584 |
} |
| 585 |
|
| 586 |
private void pgAttribute(SessionLocal session, ArrayList<Row> rows, Table table) { |
| 587 |
Column[] cols = table.getColumns(); |
| 588 |
int tableId = table.getId(); |
| 589 |
for (int i = 0; i < cols.length;) { |
| 590 |
Column column = cols[i++]; |
| 591 |
addAttribute(session, rows, tableId * 10_000 + i, tableId, column, i); |
| 592 |
} |
| 593 |
for (Index index : table.getIndexes()) { |
| 594 |
if (index.getCreateSQL() == null) { |
| 595 |
continue; |
| 596 |
} |
| 597 |
cols = index.getColumns(); |
| 598 |
for (int i = 0; i < cols.length;) { |
| 599 |
Column column = cols[i++]; |
| 600 |
int indexId = index.getId(); |
| 601 |
addAttribute(session, rows, 1_000_000 * indexId + tableId * 10_000 + i, indexId, column, i); |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
private void pgClass(SessionLocal session, ArrayList<Row> rows, Table table) { |
| 607 |
ArrayList<TriggerObject> triggers = table.getTriggers(); |
| 608 |
addClass(session, rows, table.getId(), table.getName(), table.getSchema().getId(), |
| 609 |
table.isView() ? "v" : "r", false, triggers != null ? triggers.size() : 0); |
| 610 |
ArrayList<Index> indexes = table.getIndexes(); |
| 611 |
if (indexes != null) { |
| 612 |
for (Index index : indexes) { |
| 613 |
if (index.getCreateSQL() == null) { |
| 614 |
continue; |
| 615 |
} |
| 616 |
addClass(session, rows, index.getId(), index.getName(), index.getSchema().getId(), "i", true, |
| 617 |
0); |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
private void pgConstraint(SessionLocal session, ArrayList<Row> rows) { |
| 623 |
for (Schema schema : database.getAllSchemasNoMeta()) { |
| 624 |
for (Constraint constraint : schema.getAllConstraints()) { |
| 625 |
Constraint.Type constraintType = constraint.getConstraintType(); |
| 626 |
if (constraintType == Constraint.Type.DOMAIN) { |
| 627 |
continue; |
| 628 |
} |
| 629 |
Table table = constraint.getTable(); |
| 630 |
if (hideTable(table, session)) { |
| 631 |
continue; |
| 632 |
} |
| 633 |
List<ValueSmallint> conkey = new ArrayList<>(); |
| 634 |
for (Column column : constraint.getReferencedColumns(table)) { |
| 635 |
conkey.add(ValueSmallint.get((short) (column.getColumnId() + 1))); |
| 636 |
} |
| 637 |
Table refTable = constraint.getRefTable(); |
| 638 |
add(session, |
| 639 |
rows, |
| 640 |
// OID |
| 641 |
ValueInteger.get(constraint.getId()), |
| 642 |
// CONNAME |
| 643 |
constraint.getName(), |
| 644 |
// CONTYPE |
| 645 |
StringUtils.toLowerEnglish(constraintType.getSqlName().substring(0, 1)), |
| 646 |
// CONRELID |
| 647 |
ValueInteger.get(table.getId()), |
| 648 |
// CONFRELID |
| 649 |
ValueInteger.get(refTable != null && refTable != table |
| 650 |
&& !hideTable(refTable, session) ? table.getId() : 0), |
| 651 |
// CONKEY |
| 652 |
ValueArray.get(TypeInfo.TYPE_SMALLINT, conkey.toArray(Value.EMPTY_VALUES), null) |
| 653 |
); |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
private void addAttribute(SessionLocal session, ArrayList<Row> rows, int id, int relId, Column column, |
| 659 |
int ordinal) { |
| 660 |
long precision = column.getType().getPrecision(); |
| 661 |
add(session, rows, |
| 662 |
// OID |
| 663 |
ValueInteger.get(id), |
| 664 |
// ATTRELID |
| 665 |
ValueInteger.get(relId), |
| 666 |
// ATTNAME |
| 667 |
column.getName(), |
| 668 |
// ATTTYPID |
| 669 |
ValueInteger.get(PgServer.convertType(column.getType())), |
| 670 |
// ATTLEN |
| 671 |
ValueInteger.get(precision > 255 ? -1 : (int) precision), |
| 672 |
// ATTNUM |
| 673 |
ValueInteger.get(ordinal), |
| 674 |
// ATTTYPMOD |
| 675 |
ValueInteger.get(-1), |
| 676 |
// ATTNOTNULL |
| 677 |
ValueBoolean.get(!column.isNullable()), |
| 678 |
// ATTISDROPPED |
| 679 |
ValueBoolean.FALSE, |
| 680 |
// ATTHASDEF |
| 681 |
ValueBoolean.FALSE); |
| 682 |
} |
| 683 |
|
| 684 |
private void addClass(SessionLocal session, ArrayList<Row> rows, int id, String name, int schema, String kind, |
| 685 |
boolean index, int triggers) { |
| 686 |
add(session, rows, |
| 687 |
// OID |
| 688 |
ValueInteger.get(id), |
| 689 |
// RELNAME |
| 690 |
name, |
| 691 |
// RELNAMESPACE |
| 692 |
ValueInteger.get(schema), |
| 693 |
// RELKIND |
| 694 |
kind, |
| 695 |
// RELAM |
| 696 |
ValueInteger.get(0), |
| 697 |
// RELTUPLES |
| 698 |
ValueDouble.get(0d), |
| 699 |
// RELTABLESPACE |
| 700 |
ValueInteger.get(0), |
| 701 |
// RELPAGES |
| 702 |
ValueInteger.get(0), |
| 703 |
// RELHASINDEX |
| 704 |
ValueBoolean.get(index), |
| 705 |
// RELHASRULES |
| 706 |
ValueBoolean.FALSE, |
| 707 |
// RELHASOIDS |
| 708 |
ValueBoolean.FALSE, |
| 709 |
// RELCHECKS |
| 710 |
ValueSmallint.get((short) 0), |
| 711 |
// RELTRIGGERS |
| 712 |
ValueInteger.get(triggers)); |
| 713 |
} |
| 714 |
|
| 715 |
@Override |
| 716 |
public long getMaxDataModificationId() { |
| 717 |
return database.getModificationDataId(); |
| 718 |
} |
| 719 |
|
| 720 |
} |