0001: /*
0002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
0003: * (http://h2database.com/html/license.html).
0004: * Initial Developer: H2 Group
0005: */
0006: package org.h2.table;
0007:
0008: import java.io.BufferedReader;
0009: import java.io.ByteArrayInputStream;
0010: import java.io.IOException;
0011: import java.io.InputStreamReader;
0012: import java.io.Reader;
0013: import java.sql.DatabaseMetaData;
0014: import java.sql.ResultSet;
0015: import java.sql.SQLException;
0016: import java.sql.Timestamp;
0017: import java.text.Collator;
0018: import java.util.Locale;
0019:
0020: import org.h2.command.Command;
0021: import org.h2.constant.SysProperties;
0022: import org.h2.constraint.Constraint;
0023: import org.h2.constraint.ConstraintCheck;
0024: import org.h2.constraint.ConstraintReferential;
0025: import org.h2.constraint.ConstraintUnique;
0026: import org.h2.engine.Constants;
0027: import org.h2.engine.Database;
0028: import org.h2.engine.DbObject;
0029: import org.h2.engine.FunctionAlias;
0030: import org.h2.engine.Right;
0031: import org.h2.engine.Role;
0032: import org.h2.engine.Session;
0033: import org.h2.engine.Setting;
0034: import org.h2.engine.User;
0035: import org.h2.engine.UserAggregate;
0036: import org.h2.engine.UserDataType;
0037: import org.h2.expression.ValueExpression;
0038: import org.h2.index.Index;
0039: import org.h2.index.IndexType;
0040: import org.h2.index.MetaIndex;
0041: import org.h2.log.InDoubtTransaction;
0042: import org.h2.message.Message;
0043: import org.h2.result.Row;
0044: import org.h2.result.SearchRow;
0045: import org.h2.result.SortOrder;
0046: import org.h2.schema.Constant;
0047: import org.h2.schema.Schema;
0048: import org.h2.schema.Sequence;
0049: import org.h2.schema.TriggerObject;
0050: import org.h2.store.DiskFile;
0051: import org.h2.tools.Csv;
0052: import org.h2.util.ObjectArray;
0053: import org.h2.util.Resources;
0054: import org.h2.util.StringUtils;
0055: import org.h2.value.CompareMode;
0056: import org.h2.value.DataType;
0057: import org.h2.value.Value;
0058: import org.h2.value.ValueNull;
0059: import org.h2.value.ValueString;
0060:
0061: /**
0062: * This class is responsible to build the database meta data pseudo tables.
0063: */
0064: public class MetaTable extends Table {
0065:
0066: // TODO INFORMATION_SCHEMA.tables: select table_name
0067: // from INFORMATION_SCHEMA.tables where TABLE_TYPE = 'BASE TABLE'
0068:
0069: public static final int TABLES = 0, COLUMNS = 1, INDEXES = 2,
0070: TABLE_TYPES = 3, TYPE_INFO = 4, CATALOGS = 5, SETTINGS = 6,
0071: HELP = 7, SEQUENCES = 8, USERS = 9, ROLES = 10,
0072: RIGHTS = 11, FUNCTION_ALIASES = 12, SCHEMATA = 13,
0073: TABLE_PRIVILEGES = 14, COLUMN_PRIVILEGES = 15,
0074: COLLATIONS = 16, VIEWS = 17, IN_DOUBT = 18,
0075: CROSS_REFERENCES = 19, CONSTRAINTS = 20,
0076: FUNCTION_COLUMNS = 21, CONSTANTS = 22, DOMAINS = 23,
0077: TRIGGERS = 24, SESSIONS = 25, LOCKS = 26;
0078:
0079: private final int type;
0080: private final int indexColumn;
0081: private MetaIndex index;
0082:
0083: public MetaTable(Schema schema, int id, int type)
0084: throws SQLException {
0085: // tableName will be set later
0086: super (schema, id, null, true);
0087: this .type = type;
0088: Column[] cols;
0089: String indexColumnName = null;
0090: switch (type) {
0091: case TABLES:
0092: setObjectName("TABLES");
0093: cols = createColumns(new String[] { "TABLE_CATALOG",
0094: "TABLE_SCHEMA", "TABLE_NAME", "TABLE_TYPE",
0095: // extensions
0096: "STORAGE_TYPE", "SQL", "REMARKS", "ID INT" });
0097: indexColumnName = "TABLE_NAME";
0098: break;
0099: case COLUMNS:
0100: setObjectName("COLUMNS");
0101: cols = createColumns(new String[] { "TABLE_CATALOG",
0102: "TABLE_SCHEMA", "TABLE_NAME", "COLUMN_NAME",
0103: "ORDINAL_POSITION INT", "COLUMN_DEFAULT",
0104: "IS_NULLABLE", "DATA_TYPE SMALLINT",
0105: "CHARACTER_MAXIMUM_LENGTH INT",
0106: "CHARACTER_OCTET_LENGTH INT",
0107: "NUMERIC_PRECISION INT",
0108: "NUMERIC_PRECISION_RADIX INT", "NUMERIC_SCALE INT",
0109: "CHARACTER_SET_NAME",
0110: "COLLATION_NAME",
0111: // extensions
0112: "TYPE_NAME", "NULLABLE SMALLINT",
0113: "IS_COMPUTED BIT", "SELECTIVITY INT",
0114: "CHECK_CONSTRAINT", "REMARKS" });
0115: indexColumnName = "TABLE_NAME";
0116: break;
0117: case INDEXES:
0118: setObjectName("INDEXES");
0119: cols = createColumns(new String[] { "TABLE_CATALOG",
0120: "TABLE_SCHEMA", "TABLE_NAME", "NON_UNIQUE BIT",
0121: "INDEX_NAME", "ORDINAL_POSITION SMALLINT",
0122: "COLUMN_NAME", "CARDINALITY INT",
0123: "PRIMARY_KEY BIT", "INDEX_TYPE_NAME",
0124: "IS_GENERATED BIT", "INDEX_TYPE SMALLINT",
0125: "ASC_OR_DESC", "PAGES INT", "FILTER_CONDITION",
0126: "REMARKS", "SQL", "ID INT", "SORT_TYPE INT" });
0127: indexColumnName = "TABLE_NAME";
0128: break;
0129: case TABLE_TYPES:
0130: setObjectName("TABLE_TYPES");
0131: cols = createColumns(new String[] { "TYPE" });
0132: break;
0133: case TYPE_INFO:
0134: setObjectName("TYPE_INFO");
0135: cols = createColumns(new String[] { "TYPE_NAME",
0136: "DATA_TYPE SMALLINT", "PRECISION INT", "PREFIX",
0137: "SUFFIX", "PARAMS", "AUTO_INCREMENT BIT",
0138: "MINIMUM_SCALE SMALLINT", "MAXIMUM_SCALE SMALLINT",
0139: "RADIX INT", "POS INT", "CASE_SENSITIVE BIT",
0140: "NULLABLE SMALLINT", "SEARCHABLE SMALLINT" });
0141: break;
0142: case CATALOGS:
0143: setObjectName("CATALOGS");
0144: cols = createColumns(new String[] { "CATALOG_NAME" });
0145: break;
0146: case SETTINGS:
0147: setObjectName("SETTINGS");
0148: cols = createColumns(new String[] { "NAME", "VALUE" });
0149: break;
0150: case HELP:
0151: setObjectName("HELP");
0152: cols = createColumns(new String[] { "ID INT", "SECTION",
0153: "TOPIC", "SYNTAX", "TEXT", "EXAMPLE" });
0154: break;
0155: case SEQUENCES:
0156: setObjectName("SEQUENCES");
0157: cols = createColumns(new String[] { "SEQUENCE_CATALOG",
0158: "SEQUENCE_SCHEMA", "SEQUENCE_NAME",
0159: "CURRENT_VALUE BIGINT", "INCREMENT BIGINT",
0160: "IS_GENERATED BIT", "REMARKS", "CACHE BIGINT",
0161: "ID INT" });
0162: break;
0163: case USERS:
0164: setObjectName("USERS");
0165: cols = createColumns(new String[] { "NAME", "ADMIN",
0166: "REMARKS", "ID INT" });
0167: break;
0168: case ROLES:
0169: setObjectName("ROLES");
0170: cols = createColumns(new String[] { "NAME", "REMARKS",
0171: "ID INT" });
0172: break;
0173: case RIGHTS:
0174: setObjectName("RIGHTS");
0175: cols = createColumns(new String[] { "GRANTEE",
0176: "GRANTEETYPE", "GRANTEDROLE", "RIGHTS",
0177: "TABLE_SCHEMA", "TABLE_NAME", "ID INT" });
0178: indexColumnName = "TABLE_NAME";
0179: break;
0180: case FUNCTION_ALIASES:
0181: setObjectName("FUNCTION_ALIASES");
0182: cols = createColumns(new String[] { "ALIAS_CATALOG",
0183: "ALIAS_SCHEMA", "ALIAS_NAME", "JAVA_CLASS",
0184: "JAVA_METHOD", "DATA_TYPE INT", "COLUMN_COUNT INT",
0185: "RETURNS_RESULT SMALLINT", "REMARKS", "ID INT" });
0186: break;
0187: case FUNCTION_COLUMNS:
0188: setObjectName("FUNCTION_COLUMNS");
0189: cols = createColumns(new String[] { "ALIAS_CATALOG",
0190: "ALIAS_SCHEMA", "ALIAS_NAME", "JAVA_CLASS",
0191: "JAVA_METHOD", "POS INT", "COLUMN_NAME",
0192: "DATA_TYPE INT", "TYPE_NAME", "PRECISION INT",
0193: "SCALE SMALLINT", "RADIX SMALLINT",
0194: "NULLABLE SMALLINT", "COLUMN_TYPE SMALLINT",
0195: "REMARKS" });
0196: break;
0197: case SCHEMATA:
0198: setObjectName("SCHEMATA");
0199: cols = createColumns(new String[] { "CATALOG_NAME",
0200: "SCHEMA_NAME", "SCHEMA_OWNER",
0201: "DEFAULT_CHARACTER_SET_NAME",
0202: "DEFAULT_COLLATION_NAME", "IS_DEFAULT BIT",
0203: "REMARKS", "ID INT" });
0204: break;
0205: case TABLE_PRIVILEGES:
0206: setObjectName("TABLE_PRIVILEGES");
0207: cols = createColumns(new String[] { "GRANTOR", "GRANTEE",
0208: "TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME",
0209: "PRIVILEGE_TYPE", "IS_GRANTABLE", });
0210: indexColumnName = "TABLE_NAME";
0211: break;
0212: case COLUMN_PRIVILEGES:
0213: setObjectName("COLUMN_PRIVILEGES");
0214: cols = createColumns(new String[] { "GRANTOR", "GRANTEE",
0215: "TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME",
0216: "COLUMN_NAME", "PRIVILEGE_TYPE", "IS_GRANTABLE", });
0217: indexColumnName = "TABLE_NAME";
0218: break;
0219: case COLLATIONS:
0220: setObjectName("COLLATIONS");
0221: cols = createColumns(new String[] { "NAME", "KEY" });
0222: break;
0223: case VIEWS:
0224: setObjectName("VIEWS");
0225: cols = createColumns(new String[] { "TABLE_CATALOG",
0226: "TABLE_SCHEMA", "TABLE_NAME", "VIEW_DEFINITION",
0227: "CHECK_OPTION", "IS_UPDATABLE", "STATUS",
0228: "REMARKS", "ID INT" });
0229: indexColumnName = "TABLE_NAME";
0230: break;
0231: case IN_DOUBT:
0232: setObjectName("IN_DOUBT");
0233: cols = createColumns(new String[] { "TRANSACTION", "STATE", });
0234: break;
0235: case CROSS_REFERENCES:
0236: setObjectName("CROSS_REFERENCES");
0237: cols = createColumns(new String[] { "PKTABLE_CATALOG",
0238: "PKTABLE_SCHEMA", "PKTABLE_NAME", "PKCOLUMN_NAME",
0239: "FKTABLE_CATALOG", "FKTABLE_SCHEMA",
0240: "FKTABLE_NAME", "FKCOLUMN_NAME",
0241: "ORDINAL_POSITION SMALLINT",
0242: "UPDATE_RULE SMALLINT", "DELETE_RULE SMALLINT",
0243: "FK_NAME", "PK_NAME", "DEFERRABILITY SMALLINT" });
0244: indexColumnName = "PKTABLE_NAME";
0245: break;
0246: case CONSTRAINTS:
0247: setObjectName("CONSTRAINTS");
0248: cols = createColumns(new String[] { "CONSTRAINT_CATALOG",
0249: "CONSTRAINT_SCHEMA", "CONSTRAINT_NAME",
0250: "CONSTRAINT_TYPE", "TABLE_CATALOG", "TABLE_SCHEMA",
0251: "TABLE_NAME", "CHECK_EXPRESSION", "COLUMN_LIST",
0252: "REMARKS", "SQL", "ID INT" });
0253: indexColumnName = "TABLE_NAME";
0254: break;
0255: case CONSTANTS:
0256: setObjectName("CONSTANTS");
0257: cols = createColumns(new String[] { "CONSTANT_CATALOG",
0258: "CONSTANT_SCHEMA", "CONSTANT_NAME",
0259: "DATA_TYPE SMALLINT", "REMARKS", "SQL", "ID INT" });
0260: break;
0261: case DOMAINS:
0262: setObjectName("DOMAINS");
0263: cols = createColumns(new String[] { "DOMAIN_CATALOG",
0264: "DOMAIN_SCHEMA", "DOMAIN_NAME", "COLUMN_DEFAULT",
0265: "IS_NULLABLE", "DATA_TYPE SMALLINT",
0266: "PRECISION INT", "SCALE INT", "TYPE_NAME",
0267: "SELECTIVITY INT", "CHECK_CONSTRAINT", "REMARKS",
0268: "SQL", "ID INT" });
0269: break;
0270: case TRIGGERS:
0271: setObjectName("TRIGGERS");
0272: cols = createColumns(new String[] { "TRIGGER_CATALOG",
0273: "TRIGGER_SCHEMA", "TRIGGER_NAME", "TRIGGER_TYPE",
0274: "TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME",
0275: "BEFORE BIT", "JAVA_CLASS", "QUEUE_SIZE INT",
0276: "NO_WAIT BIT", "REMARKS", "SQL", "ID INT" });
0277: break;
0278: case SESSIONS: {
0279: setObjectName("SESSIONS");
0280: cols = createColumns(new String[] { "ID INT", "USER_NAME",
0281: "SESSION_START", "STATEMENT", "STATEMENT_START" });
0282: break;
0283: }
0284: case LOCKS: {
0285: setObjectName("LOCKS");
0286: cols = createColumns(new String[] { "TABLE_SCHEMA",
0287: "TABLE_NAME", "SESSION_ID INT", "LOCK_TYPE", });
0288: break;
0289: }
0290: default:
0291: throw Message.getInternalError("type=" + type);
0292: }
0293: setColumns(cols);
0294:
0295: if (indexColumnName == null) {
0296: indexColumn = -1;
0297: } else {
0298: indexColumn = getColumn(indexColumnName).getColumnId();
0299: IndexColumn[] indexCols = IndexColumn
0300: .wrap(new Column[] { cols[indexColumn] });
0301: index = new MetaIndex(this , indexCols, false);
0302: }
0303: }
0304:
0305: private Column[] createColumns(String[] names) {
0306: Column[] cols = new Column[names.length];
0307: for (int i = 0; i < names.length; i++) {
0308: String nameType = names[i];
0309: int idx = nameType.indexOf(' ');
0310: int type;
0311: String name;
0312: if (idx < 0) {
0313: type = Value.STRING;
0314: name = nameType;
0315: } else {
0316: type = DataType.getTypeByName(nameType
0317: .substring(idx + 1)).type;
0318: name = nameType.substring(0, idx);
0319: }
0320: cols[i] = new Column(name, type);
0321: }
0322: return cols;
0323: }
0324:
0325: public String getDropSQL() {
0326: return null;
0327: }
0328:
0329: public String getCreateSQL() {
0330: return null;
0331: }
0332:
0333: public Index addIndex(Session session, String indexName,
0334: int indexId, IndexColumn[] cols, IndexType indexType,
0335: int headPos, String comment) throws SQLException {
0336: throw Message.getUnsupportedException();
0337: }
0338:
0339: public void lock(Session session, boolean exclusive, boolean force)
0340: throws SQLException {
0341: // nothing to do
0342: }
0343:
0344: public boolean isLockedExclusively() {
0345: return false;
0346: }
0347:
0348: private String identifier(String s) {
0349: if (database.getMode().lowerCaseIdentifiers) {
0350: s = (s == null ? null : StringUtils.toLowerEnglish(s));
0351: }
0352: return s;
0353: }
0354:
0355: private ObjectArray getAllTables(Session session) {
0356: ObjectArray tables = database
0357: .getAllSchemaObjects(DbObject.TABLE_OR_VIEW);
0358: ObjectArray tempTables = session.getLocalTempTables();
0359: tables.addAll(tempTables);
0360: return tables;
0361: }
0362:
0363: private boolean checkIndex(Session session, String value,
0364: Value indexFrom, Value indexTo) throws SQLException {
0365: if (value == null || (indexFrom == null && indexTo == null)) {
0366: return true;
0367: }
0368: Database db = session.getDatabase();
0369: Value v = ValueString.get(value);
0370: if (indexFrom != null && db.compare(v, indexFrom) < 0) {
0371: return false;
0372: }
0373: if (indexTo != null && db.compare(v, indexTo) > 0) {
0374: return false;
0375: }
0376: return true;
0377: }
0378:
0379: private String replaceNullWithEmpty(String s) {
0380: return s == null ? "" : s;
0381: }
0382:
0383: public ObjectArray generateRows(Session session, SearchRow first,
0384: SearchRow last) throws SQLException {
0385: Value indexFrom = null, indexTo = null;
0386:
0387: if (indexColumn >= 0) {
0388: if (first != null) {
0389: indexFrom = first.getValue(indexColumn);
0390: }
0391: if (last != null) {
0392: indexTo = last.getValue(indexColumn);
0393: }
0394: }
0395:
0396: ObjectArray rows = new ObjectArray();
0397: String catalog = identifier(database.getShortName());
0398: switch (type) {
0399: case TABLES: {
0400: ObjectArray tables = getAllTables(session);
0401: for (int i = 0; i < tables.size(); i++) {
0402: Table table = (Table) tables.get(i);
0403: String tableName = identifier(table.getName());
0404: if (!checkIndex(session, tableName, indexFrom, indexTo)) {
0405: continue;
0406: }
0407: String storageType;
0408: if (table.getTemporary()) {
0409: if (table.getGlobalTemporary()) {
0410: storageType = "GLOBAL TEMPORARY";
0411: } else {
0412: storageType = "LOCAL TEMPORARY";
0413: }
0414: } else {
0415: storageType = table.isPersistent() ? "CACHED"
0416: : "MEMORY";
0417: }
0418: add(rows, new String[] { catalog, // TABLE_CATALOG
0419: identifier(table.getSchema().getName()), // TABLE_SCHEMA
0420: tableName, // TABLE_NAME
0421: table.getTableType(), // TABLE_TYPE
0422: storageType, // STORAGE_TYPE
0423: table.getCreateSQL(), // SQL
0424: replaceNullWithEmpty(table.getComment()), // REMARKS
0425: "" + table.getId() // ID
0426: });
0427: }
0428: break;
0429: }
0430: case COLUMNS: {
0431: ObjectArray tables = getAllTables(session);
0432: for (int i = 0; i < tables.size(); i++) {
0433: Table table = (Table) tables.get(i);
0434: String tableName = identifier(table.getName());
0435: if (!checkIndex(session, tableName, indexFrom, indexTo)) {
0436: continue;
0437: }
0438: Column[] cols = table.getColumns();
0439: String collation = database.getCompareMode().getName();
0440: for (int j = 0; j < cols.length; j++) {
0441: Column c = cols[j];
0442: add(
0443: rows,
0444: new String[] {
0445: catalog, // TABLE_CATALOG
0446: identifier(table.getSchema()
0447: .getName()), // TABLE_SCHEMA
0448: tableName, // TABLE_NAME
0449: identifier(c.getName()), // COLUMN_NAME
0450: String.valueOf(j + 1), // ORDINAL_POSITION
0451: c.getDefaultSQL(), // COLUMN_DEFAULT
0452: c.getNullable() ? "YES" : "NO", // IS_NULLABLE
0453: ""
0454: + DataType
0455: .convertTypeToSQLType(c
0456: .getType()), // DATA_TYPE
0457: "" + c.getPrecisionAsInt(), // CHARACTER_MAXIMUM_LENGTH
0458: "" + c.getPrecisionAsInt(), // CHARACTER_OCTET_LENGTH
0459: "" + c.getPrecisionAsInt(), // NUMERIC_PRECISION
0460: "10", // NUMERIC_PRECISION_RADIX
0461: "" + c.getScale(), // NUMERIC_SCALE
0462: Constants.CHARACTER_SET_NAME, // CHARACTER_SET_NAME
0463: collation, // COLLATION_NAME
0464: identifier(DataType.getDataType(c
0465: .getType()).name), // TYPE_NAME
0466: ""
0467: + (c.getNullable() ? DatabaseMetaData.columnNullable
0468: : DatabaseMetaData.columnNoNulls), // NULLABLE
0469: ""
0470: + (c.getComputed() ? "TRUE"
0471: : "FALSE"), // IS_COMPUTED
0472: "" + (c.getSelectivity()), // SELECTIVITY
0473: c.getCheckConstraintSQL(session, c
0474: .getName()), // CHECK_CONSTRAINT
0475: replaceNullWithEmpty(c.getComment()) // REMARKS
0476: });
0477: }
0478: }
0479: break;
0480: }
0481: case INDEXES: {
0482: ObjectArray tables = getAllTables(session);
0483: for (int i = 0; i < tables.size(); i++) {
0484: Table table = (Table) tables.get(i);
0485: String tableName = identifier(table.getName());
0486: if (!checkIndex(session, tableName, indexFrom, indexTo)) {
0487: continue;
0488: }
0489: ObjectArray idx = table.getIndexes();
0490: for (int j = 0; idx != null && j < idx.size(); j++) {
0491: Index index = (Index) idx.get(j);
0492: if (index.getCreateSQL() == null) {
0493: continue;
0494: }
0495: IndexColumn[] cols = index.getIndexColumns();
0496: for (int k = 0; k < cols.length; k++) {
0497: IndexColumn idxCol = cols[k];
0498: Column column = idxCol.column;
0499: add(
0500: rows,
0501: new String[] {
0502: catalog, // TABLE_CATALOG
0503: identifier(table.getSchema()
0504: .getName()), // TABLE_SCHEMA
0505: tableName, // TABLE_NAME
0506: index.getIndexType().isUnique() ? "FALSE"
0507: : "TRUE", // NON_UNIQUE
0508: identifier(index.getName()), // INDEX_NAME
0509: "" + (k + 1), // ORDINAL_POSITION
0510: identifier(column.getName()), // COLUMN_NAME
0511: "0", // CARDINALITY
0512: index.getIndexType()
0513: .isPrimaryKey() ? "TRUE"
0514: : "FALSE", // PRIMARY_KEY
0515: index.getIndexType().getSQL(), // INDEX_TYPE_NAME
0516: index.getIndexType()
0517: .belongsToConstraint() ? "TRUE"
0518: : "FALSE", // IS_GENERATED
0519: ""
0520: + DatabaseMetaData.tableIndexOther, // INDEX_TYPE
0521: (idxCol.sortType & SortOrder.DESCENDING) != 0 ? "D"
0522: : "A", // ASC_OR_DESC
0523: "0", // PAGES
0524: "", // FILTER_CONDITION
0525: replaceNullWithEmpty(index
0526: .getComment()), // REMARKS
0527: index.getSQL(), // SQL
0528: "" + index.getId(), // ID
0529: "" + idxCol.sortType, // SORT_TYPE
0530: });
0531: }
0532: }
0533: }
0534: break;
0535: }
0536: case TABLE_TYPES: {
0537: add(rows, new String[] { Table.TABLE });
0538: add(rows, new String[] { Table.TABLE_LINK });
0539: add(rows, new String[] { Table.SYSTEM_TABLE });
0540: add(rows, new String[] { Table.VIEW });
0541: break;
0542: }
0543: case CATALOGS: {
0544: add(rows, new String[] { catalog });
0545: break;
0546: }
0547: case SETTINGS: {
0548: ObjectArray list = database.getAllSettings();
0549: for (int i = 0; i < list.size(); i++) {
0550: Setting s = (Setting) list.get(i);
0551: String value = s.getStringValue();
0552: if (value == null) {
0553: value = "" + s.getIntValue();
0554: }
0555: add(rows,
0556: new String[] { identifier(s.getName()), value });
0557: }
0558: add(rows, new String[] { "info.BUILD_ID",
0559: "" + Constants.BUILD_ID });
0560: add(rows, new String[] { "info.VERSION_MAJOR",
0561: "" + Constants.VERSION_MAJOR });
0562: add(rows, new String[] { "info.VERSION_MINOR",
0563: "" + Constants.VERSION_MINOR });
0564: add(rows, new String[] { "info.VERSION",
0565: "" + Constants.getVersion() });
0566: if (session.getUser().getAdmin()) {
0567: String[] settings = new String[] {
0568: "java.runtime.version", "java.vm.name",
0569: "java.vendor", "os.name", "os.arch",
0570: "os.version", "sun.os.patch.level",
0571: "file.separator", "path.separator",
0572: "line.separator", "user.country",
0573: "user.language", "user.variant",
0574: "file.encoding" };
0575: for (int i = 0; i < settings.length; i++) {
0576: String s = settings[i];
0577: add(rows, new String[] { "property." + s,
0578: SysProperties.getStringSetting(s, "") });
0579: }
0580: }
0581: add(rows, new String[] {
0582: "EXCLUSIVE",
0583: database.getExclusiveSession() == null ? "FALSE"
0584: : "TRUE" });
0585: add(rows, new String[] { "MODE",
0586: database.getMode().getName() });
0587: add(rows, new String[] { "MULTI_THREADED",
0588: database.getMultiThreaded() ? "1" : "0" });
0589: add(rows, new String[] { "MVCC",
0590: database.isMultiVersion() ? "TRUE" : "FALSE" });
0591: add(rows, new String[] { "QUERY_TIMEOUT",
0592: "" + session.getQueryTimeout() });
0593: // the setting for the current database
0594: add(rows, new String[] { "LOB_FILES_IN_DIRECTORIES",
0595: "" + database.getLobFilesInDirectories() });
0596: add(rows, new String[] { "h2.allowBigDecimalExtensions",
0597: "" + SysProperties.ALLOW_BIG_DECIMAL_EXTENSIONS });
0598: add(rows, new String[] { "h2.baseDir",
0599: "" + SysProperties.getBaseDir() });
0600: add(rows, new String[] { "h2.check",
0601: "" + SysProperties.CHECK });
0602: add(rows, new String[] { "h2.check2",
0603: "" + SysProperties.CHECK2 });
0604: add(rows, new String[] { "h2.clientTraceDirectory",
0605: SysProperties.CLIENT_TRACE_DIRECTORY });
0606: add(rows, new String[] {
0607: SysProperties.H2_COLLATOR_CACHE_SIZE,
0608: "" + SysProperties.getCollatorCacheSize() });
0609: add(rows, new String[] { "h2.defaultMaxMemoryUndo",
0610: "" + SysProperties.DEFAULT_MAX_MEMORY_UNDO });
0611: add(rows, new String[] { "h2.emergencySpaceInitial",
0612: "" + SysProperties.EMERGENCY_SPACE_INITIAL });
0613: add(rows, new String[] { "h2.emergencySpaceMin",
0614: "" + SysProperties.EMERGENCY_SPACE_MIN });
0615: add(rows, new String[] { "h2.lobFilesInDirectories",
0616: "" + SysProperties.LOB_FILES_IN_DIRECTORIES });
0617: add(rows, new String[] { "h2.lobFilesPerDirectory",
0618: "" + SysProperties.LOB_FILES_PER_DIRECTORY });
0619: add(rows, new String[] { "h2.logAllErrors",
0620: "" + SysProperties.LOG_ALL_ERRORS });
0621: add(rows, new String[] { "h2.logAllErrorsFile",
0622: "" + SysProperties.LOG_ALL_ERRORS_FILE });
0623: add(rows, new String[] { "h2.maxFileRetry",
0624: "" + SysProperties.MAX_FILE_RETRY });
0625: add(rows, new String[] {
0626: SysProperties.H2_MAX_QUERY_TIMEOUT,
0627: "" + SysProperties.getMaxQueryTimeout() });
0628: add(rows, new String[] { "h2.lobCloseBetweenReads",
0629: "" + SysProperties.lobCloseBetweenReads });
0630: add(rows, new String[] { "h2.objectCache",
0631: "" + SysProperties.OBJECT_CACHE });
0632: add(rows, new String[] { "h2.objectCacheSize",
0633: "" + SysProperties.OBJECT_CACHE_SIZE });
0634: add(
0635: rows,
0636: new String[] {
0637: "h2.objectCacheMaxPerElementSize",
0638: ""
0639: + SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE });
0640: add(rows, new String[] { "h2.optimizeIn",
0641: "" + SysProperties.OPTIMIZE_IN });
0642: add(rows, new String[] { "h2.optimizeInJoin",
0643: "" + SysProperties.OPTIMIZE_IN_JOIN });
0644: add(rows, new String[] { "h2.optimizeMinMax",
0645: "" + SysProperties.OPTIMIZE_MIN_MAX });
0646: add(rows, new String[] { "h2.optimizeSubqueryCache",
0647: "" + SysProperties.OPTIMIZE_SUBQUERY_CACHE });
0648: add(rows, new String[] { "h2.overflowExceptions",
0649: "" + SysProperties.OVERFLOW_EXCEPTIONS });
0650: add(rows, new String[] { "h2.recompileAlways",
0651: "" + SysProperties.RECOMPILE_ALWAYS });
0652: add(rows, new String[] { "h2.redoBufferSize",
0653: "" + SysProperties.REDO_BUFFER_SIZE });
0654: add(rows, new String[] { "h2.runFinalize",
0655: "" + SysProperties.runFinalize });
0656: add(rows, new String[] { "h2.scriptDirectory",
0657: SysProperties.scriptDirectory });
0658: add(rows, new String[] { "h2.serverCachedObjects",
0659: "" + SysProperties.SERVER_CACHED_OBJECTS });
0660: add(rows, new String[] { "h2.serverResultSetFetchSize",
0661: "" + SysProperties.SERVER_RESULT_SET_FETCH_SIZE });
0662: DiskFile dataFile = database.getDataFile();
0663: if (dataFile != null) {
0664: add(rows, new String[] { "CACHE_TYPE",
0665: dataFile.getCache().getTypeName() });
0666: if (session.getUser().getAdmin()) {
0667: add(rows, new String[] { "info.FILE_DISK_WRITE",
0668: "" + dataFile.getWriteCount() });
0669: add(rows, new String[] { "info.FILE_DISK_READ",
0670: "" + dataFile.getReadCount() });
0671: add(rows, new String[] {
0672: "info.FILE_INDEX_WRITE",
0673: ""
0674: + database.getIndexFile()
0675: .getWriteCount() });
0676: add(rows,
0677: new String[] {
0678: "info.FILE_INDEX_READ",
0679: ""
0680: + database.getIndexFile()
0681: .getReadCount() });
0682: }
0683: }
0684: break;
0685: }
0686: case TYPE_INFO: {
0687: ObjectArray types = DataType.getTypes();
0688: for (int i = 0; i < types.size(); i++) {
0689: DataType t = (DataType) types.get(i);
0690: if (t.hidden || t.sqlType == Value.NULL) {
0691: continue;
0692: }
0693: add(rows, new String[] { t.name, // TYPE_NAME
0694: String.valueOf(t.sqlType), // DATA_TYPE
0695: String.valueOf(t.maxPrecision), // PRECISION
0696: t.prefix, // PREFIX
0697: t.suffix, // SUFFIX
0698: t.params, // PARAMS
0699: String.valueOf(t.autoInc), // AUTO_INCREMENT
0700: String.valueOf(t.minScale), // MINIMUM_SCALE
0701: String.valueOf(t.maxScale), // MAXIMUM_SCALE
0702: t.decimal ? "10" : null, // RADIX
0703: String.valueOf(t.sqlTypePos), // POS
0704: String.valueOf(t.caseSensitive), // CASE_SENSITIVE
0705: "" + DatabaseMetaData.typeNullable, // NULLABLE
0706: "" + DatabaseMetaData.typeSearchable // SEARCHABLE
0707: });
0708: }
0709: break;
0710: }
0711: case HELP: {
0712: String resource = "/org/h2/res/help.csv";
0713: try {
0714: byte[] data = Resources.get(resource);
0715: Reader reader = new BufferedReader(
0716: new InputStreamReader(new ByteArrayInputStream(
0717: data)));
0718: ResultSet rs = Csv.getInstance().read(reader, null);
0719: for (int i = 0; rs.next(); i++) {
0720: add(rows, new String[] { String.valueOf(i), // ID
0721: rs.getString(1).trim(), // SECTION
0722: rs.getString(2).trim(), // TOPIC
0723: rs.getString(3).trim(), // SYNTAX
0724: rs.getString(4).trim(), // TEXT
0725: rs.getString(5).trim(), // EXAMPLE
0726: });
0727: }
0728: } catch (IOException e) {
0729: throw Message.convertIOException(e, resource);
0730: }
0731: break;
0732: }
0733: case SEQUENCES: {
0734: ObjectArray sequences = database
0735: .getAllSchemaObjects(DbObject.SEQUENCE);
0736: for (int i = 0; i < sequences.size(); i++) {
0737: Sequence s = (Sequence) sequences.get(i);
0738: add(rows, new String[] { catalog, // SEQUENCE_CATALOG
0739: identifier(s.getSchema().getName()), // SEQUENCE_SCHEMA
0740: identifier(s.getName()), // SEQUENCE_NAME
0741: String.valueOf(s.getCurrentValue()), // CURRENT_VALUE
0742: String.valueOf(s.getIncrement()), // INCREMENT
0743: s.getBelongsToTable() ? "TRUE" : "FALSE", // IS_GENERATED
0744: replaceNullWithEmpty(s.getComment()), // REMARKS
0745: String.valueOf(s.getCacheSize()), // CACHE
0746: "" + s.getId() // ID
0747: });
0748: }
0749: break;
0750: }
0751: case USERS: {
0752: ObjectArray users = database.getAllUsers();
0753: for (int i = 0; i < users.size(); i++) {
0754: User u = (User) users.get(i);
0755: add(rows, new String[] { identifier(u.getName()), // NAME
0756: String.valueOf(u.getAdmin()), // ADMIN
0757: replaceNullWithEmpty(u.getComment()), // REMARKS
0758: "" + u.getId() //
0759: });
0760: }
0761: break;
0762: }
0763: case ROLES: {
0764: ObjectArray roles = database.getAllRoles();
0765: for (int i = 0; i < roles.size(); i++) {
0766: Role r = (Role) roles.get(i);
0767: add(rows, new String[] { identifier(r.getName()), // NAME
0768: replaceNullWithEmpty(r.getComment()), // REMARKS
0769: "" + r.getId() // ID
0770: });
0771: }
0772: break;
0773: }
0774: case RIGHTS: {
0775: ObjectArray rights = database.getAllRights();
0776: for (int i = 0; i < rights.size(); i++) {
0777: Right r = (Right) rights.get(i);
0778: Role role = r.getGrantedRole();
0779: DbObject grantee = r.getGrantee();
0780: String type = grantee.getType() == DbObject.USER ? "USER"
0781: : "ROLE";
0782: if (role == null) {
0783: Table granted = r.getGrantedTable();
0784: String tableName = identifier(granted.getName());
0785: if (!checkIndex(session, tableName, indexFrom,
0786: indexTo)) {
0787: continue;
0788: }
0789: add(rows, new String[] {
0790: identifier(grantee.getName()), // GRANTEE
0791: type, // GRANTEETYPE
0792: "", // GRANTEDROLE
0793: r.getRights(), // RIGHTS
0794: identifier(granted.getSchema().getName()), // TABLE_SCHEMA
0795: identifier(granted.getName()), // TABLE_NAME
0796: "" + r.getId() // ID
0797: });
0798: } else {
0799: add(rows, new String[] {
0800: identifier(grantee.getName()), // GRANTEE
0801: type, // GRANTEETYPE
0802: identifier(role.getName()), // GRANTEDROLE
0803: "", // RIGHTS
0804: "", // TABLE_SCHEMA
0805: "", // TABLE_NAME
0806: "" + r.getId() // ID
0807: });
0808: }
0809: }
0810: break;
0811: }
0812: case FUNCTION_ALIASES: {
0813: ObjectArray aliases = database.getAllFunctionAliases();
0814: for (int i = 0; i < aliases.size(); i++) {
0815: FunctionAlias alias = (FunctionAlias) aliases.get(i);
0816: int returnsResult = alias.getDataType() == Value.NULL ? DatabaseMetaData.procedureNoResult
0817: : DatabaseMetaData.procedureReturnsResult;
0818: add(rows, new String[] {
0819: catalog, // ALIAS_CATALOG
0820: Constants.SCHEMA_MAIN, // ALIAS_SCHEMA
0821: identifier(alias.getName()), // ALIAS_NAME
0822: alias.getJavaClassName(), // JAVA_CLASS
0823: alias.getJavaMethodName(), // JAVA_METHOD
0824: ""
0825: + DataType.convertTypeToSQLType(alias
0826: .getDataType()), // DATA_TYPE
0827: "" + alias.getColumnClasses().length, // COLUMN_COUNT INT
0828: "" + returnsResult, // RETURNS_RESULT SMALLINT
0829: replaceNullWithEmpty(alias.getComment()), // REMARKS
0830: "" + alias.getId() // ID
0831: });
0832: }
0833: ObjectArray aggregates = database.getAllAggregates();
0834: for (int i = 0; i < aggregates.size(); i++) {
0835: UserAggregate agg = (UserAggregate) aggregates.get(i);
0836: int returnsResult = DatabaseMetaData.procedureReturnsResult;
0837: add(rows, new String[] { catalog, // ALIAS_CATALOG
0838: Constants.SCHEMA_MAIN, // ALIAS_SCHEMA
0839: identifier(agg.getName()), // ALIAS_NAME
0840: agg.getJavaClassName(), // JAVA_CLASS
0841: "", // JAVA_METHOD
0842: "" + DataType.convertTypeToSQLType(Value.NULL), // DATA_TYPE
0843: "1", // COLUMN_COUNT INT
0844: "" + returnsResult, // RETURNS_RESULT SMALLINT
0845: replaceNullWithEmpty(agg.getComment()), // REMARKS
0846: "" + agg.getId() // ID
0847: });
0848: }
0849: break;
0850: }
0851: case FUNCTION_COLUMNS: {
0852: ObjectArray aliases = database.getAllFunctionAliases();
0853: for (int i = 0; i < aliases.size(); i++) {
0854: FunctionAlias alias = (FunctionAlias) aliases.get(i);
0855: Class[] columns = alias.getColumnClasses();
0856: for (int j = 0; j < columns.length; j++) {
0857: Class clazz = columns[j];
0858: int type = DataType.getTypeFromClass(clazz);
0859: DataType dt = DataType.getDataType(type);
0860: int nullable = clazz.isPrimitive() ? DatabaseMetaData.columnNoNulls
0861: : DatabaseMetaData.columnNullable;
0862: add(
0863: rows,
0864: new String[] {
0865: catalog, // ALIAS_CATALOG
0866: Constants.SCHEMA_MAIN, // ALIAS_SCHEMA
0867: identifier(alias.getName()), // ALIAS_NAME
0868: alias.getJavaClassName(), // JAVA_CLASS
0869: alias.getJavaMethodName(), // JAVA_METHOD
0870: "" + j, // POS INT
0871: "P" + (j + 1), // COLUMN_NAME
0872: ""
0873: + DataType
0874: .convertTypeToSQLType(dt.type), // DATA_TYPE
0875: dt.name, // TYPE_NAME
0876: "" + dt.defaultPrecision, // PRECISION
0877: "" + dt.defaultScale, // SCALE
0878: "10", // RADIX
0879: "" + nullable, // NULLABLE SMALLINT
0880: ""
0881: + DatabaseMetaData.procedureColumnIn, // COLUMN_TYPE
0882: "" // REMARKS
0883: });
0884: }
0885: }
0886: break;
0887: }
0888: case SCHEMATA: {
0889: ObjectArray schemas = database.getAllSchemas();
0890: String collation = database.getCompareMode().getName();
0891: for (int i = 0; i < schemas.size(); i++) {
0892: Schema schema = (Schema) schemas.get(i);
0893: add(
0894: rows,
0895: new String[] {
0896: catalog, // CATALOG_NAME
0897: identifier(schema.getName()), // SCHEMA_NAME
0898: identifier(schema.getOwner().getName()), // SCHEMA_OWNER
0899: Constants.CHARACTER_SET_NAME, // DEFAULT_CHARACTER_SET_NAME
0900: collation, // DEFAULT_COLLATION_NAME
0901: Constants.SCHEMA_MAIN.equals(schema
0902: .getName()) ? "TRUE" : "FALSE", // IS_DEFAULT
0903: replaceNullWithEmpty(schema
0904: .getComment()), // REMARKS
0905: "" + schema.getId() // ID
0906: });
0907: }
0908: break;
0909: }
0910: case TABLE_PRIVILEGES: {
0911: ObjectArray rights = database.getAllRights();
0912: for (int i = 0; i < rights.size(); i++) {
0913: Right r = (Right) rights.get(i);
0914: Table table = r.getGrantedTable();
0915: if (table == null) {
0916: continue;
0917: }
0918: String tableName = identifier(table.getName());
0919: if (!checkIndex(session, tableName, indexFrom, indexTo)) {
0920: continue;
0921: }
0922: addPrivileges(rows, r.getGrantee(), catalog, table,
0923: null, r.getRightMask());
0924: }
0925: break;
0926: }
0927: case COLUMN_PRIVILEGES: {
0928: ObjectArray rights = database.getAllRights();
0929: for (int i = 0; i < rights.size(); i++) {
0930: Right r = (Right) rights.get(i);
0931: Table table = r.getGrantedTable();
0932: if (table == null) {
0933: continue;
0934: }
0935: String tableName = identifier(table.getName());
0936: if (!checkIndex(session, tableName, indexFrom, indexTo)) {
0937: continue;
0938: }
0939: DbObject grantee = r.getGrantee();
0940: int mask = r.getRightMask();
0941: Column[] columns = table.getColumns();
0942: for (int j = 0; j < columns.length; j++) {
0943: String column = columns[j].getName();
0944: addPrivileges(rows, grantee, catalog, table,
0945: column, mask);
0946: }
0947: }
0948: break;
0949: }
0950: case COLLATIONS: {
0951: Locale[] locales = Collator.getAvailableLocales();
0952: for (int i = 0; i < locales.length; i++) {
0953: Locale l = locales[i];
0954: add(rows, new String[] { CompareMode.getName(l), // NAME
0955: l.toString(), // KEY
0956: });
0957: }
0958: break;
0959: }
0960: case VIEWS: {
0961: ObjectArray tables = getAllTables(session);
0962: for (int i = 0; i < tables.size(); i++) {
0963: Table table = (Table) tables.get(i);
0964: if (!table.getTableType().equals(Table.VIEW)) {
0965: continue;
0966: }
0967: String tableName = identifier(table.getName());
0968: if (!checkIndex(session, tableName, indexFrom, indexTo)) {
0969: continue;
0970: }
0971: TableView view = (TableView) table;
0972: add(rows, new String[] { catalog, // TABLE_CATALOG
0973: identifier(table.getSchema().getName()), // TABLE_SCHEMA
0974: tableName, // TABLE_NAME
0975: table.getCreateSQL(), // VIEW_DEFINITION
0976: "NONE", // CHECK_OPTION
0977: "NO", // IS_UPDATABLE
0978: view.getInvalid() ? "INVALID" : "VALID", // STATUS
0979: replaceNullWithEmpty(view.getComment()), // REMARKS
0980: "" + view.getId() // ID
0981: });
0982: }
0983: break;
0984: }
0985: case IN_DOUBT: {
0986: ObjectArray prepared = database.getLog()
0987: .getInDoubtTransactions();
0988: for (int i = 0; prepared != null && i < prepared.size(); i++) {
0989: InDoubtTransaction prep = (InDoubtTransaction) prepared
0990: .get(i);
0991: add(rows, new String[] { prep.getTransaction(), // TRANSACTION
0992: prep.getState(), // STATE
0993: });
0994: }
0995: break;
0996: }
0997: case CROSS_REFERENCES: {
0998: ObjectArray constraints = database
0999: .getAllSchemaObjects(DbObject.CONSTRAINT);
1000: for (int i = 0; i < constraints.size(); i++) {
1001: Constraint constraint = (Constraint) constraints.get(i);
1002: if (!(constraint.getConstraintType()
1003: .equals(Constraint.REFERENTIAL))) {
1004: continue;
1005: }
1006: ConstraintReferential ref = (ConstraintReferential) constraint;
1007: IndexColumn[] cols = ref.getColumns();
1008: IndexColumn[] refCols = ref.getRefColumns();
1009: Table tab = ref.getTable();
1010: Table refTab = ref.getRefTable();
1011: String tableName = identifier(refTab.getName());
1012: if (!checkIndex(session, tableName, indexFrom, indexTo)) {
1013: continue;
1014: }
1015: int update = getRefAction(ref.getUpdateAction());
1016: int delete = getRefAction(ref.getDeleteAction());
1017: for (int j = 0; j < cols.length; j++) {
1018: add(
1019: rows,
1020: new String[] {
1021: catalog, // PKTABLE_CATALOG
1022: identifier(refTab.getSchema()
1023: .getName()), // PKTABLE_SCHEMA
1024: identifier(refTab.getName()), // PKTABLE_NAME
1025: identifier(refCols[j].column
1026: .getName()), // PKCOLUMN_NAME
1027: catalog, // FKTABLE_CATALOG
1028: identifier(tab.getSchema()
1029: .getName()), // FKTABLE_SCHEMA
1030: identifier(tab.getName()), // FKTABLE_NAME
1031: identifier(cols[j].column.getName()), // FKCOLUMN_NAME
1032: String.valueOf(j + 1), // ORDINAL_POSITION
1033: String.valueOf(update), // UPDATE_RULE SMALLINT
1034: String.valueOf(delete), // DELETE_RULE SMALLINT
1035: identifier(ref.getName()), // FK_NAME
1036: null, // PK_NAME
1037: ""
1038: + DatabaseMetaData.importedKeyNotDeferrable, // DEFERRABILITY
1039: });
1040: }
1041: }
1042: break;
1043: }
1044: case CONSTRAINTS: {
1045: ObjectArray constraints = database
1046: .getAllSchemaObjects(DbObject.CONSTRAINT);
1047: for (int i = 0; i < constraints.size(); i++) {
1048: Constraint constraint = (Constraint) constraints.get(i);
1049: String type = constraint.getConstraintType();
1050: String checkExpression = null;
1051: IndexColumn[] columns = null;
1052: Table table = constraint.getTable();
1053: String tableName = identifier(table.getName());
1054: if (!checkIndex(session, tableName, indexFrom, indexTo)) {
1055: continue;
1056: }
1057: if (type.equals(Constraint.CHECK)) {
1058: checkExpression = ((ConstraintCheck) constraint)
1059: .getExpression().getSQL();
1060: } else if (type.equals(Constraint.UNIQUE)
1061: || type.equals(Constraint.PRIMARY_KEY)) {
1062: columns = ((ConstraintUnique) constraint)
1063: .getColumns();
1064: } else if (type.equals(Constraint.REFERENTIAL)) {
1065: columns = ((ConstraintReferential) constraint)
1066: .getColumns();
1067: }
1068: String columnList = null;
1069: if (columns != null) {
1070: StringBuffer buff = new StringBuffer();
1071: for (int j = 0; j < columns.length; j++) {
1072: if (j > 0) {
1073: buff.append(',');
1074: }
1075: buff.append(columns[j].column.getName());
1076: }
1077: columnList = buff.toString();
1078: }
1079: add(rows, new String[] { catalog, // CONSTRAINT_CATALOG
1080: identifier(constraint.getSchema().getName()), // CONSTRAINT_SCHEMA
1081: identifier(constraint.getName()), // CONSTRAINT_NAME
1082: type, // CONSTRAINT_TYPE
1083: catalog, // TABLE_CATALOG
1084: identifier(table.getSchema().getName()), // TABLE_SCHEMA
1085: tableName, // TABLE_NAME
1086: checkExpression, // CHECK_EXPRESSION
1087: columnList, // COLUMN_LIST
1088: replaceNullWithEmpty(constraint.getComment()), // REMARKS
1089: constraint.getCreateSQL(), // SQL
1090: "" + constraint.getId() // ID
1091: });
1092: }
1093: break;
1094: }
1095: case CONSTANTS: {
1096: ObjectArray constants = database
1097: .getAllSchemaObjects(DbObject.CONSTANT);
1098: for (int i = 0; i < constants.size(); i++) {
1099: Constant constant = (Constant) constants.get(i);
1100: ValueExpression expr = constant.getValue();
1101: add(rows, new String[] {
1102: catalog, // CONSTANT_CATALOG
1103: identifier(constant.getSchema().getName()), // CONSTANT_SCHEMA
1104: identifier(constant.getName()), // CONSTANT_NAME
1105: ""
1106: + DataType.convertTypeToSQLType(expr
1107: .getType()), // CONSTANT_TYPE
1108: replaceNullWithEmpty(constant.getComment()), // REMARKS
1109: expr.getSQL(), // SQL
1110: "" + constant.getId() // ID
1111: });
1112: }
1113: break;
1114: }
1115: case DOMAINS: {
1116: ObjectArray userDataTypes = database.getAllUserDataTypes();
1117: for (int i = 0; i < userDataTypes.size(); i++) {
1118: UserDataType dt = (UserDataType) userDataTypes.get(i);
1119: Column col = dt.getColumn();
1120: add(rows, new String[] {
1121: catalog, // DOMAIN_CATALOG
1122: Constants.SCHEMA_MAIN, // DOMAIN_SCHEMA
1123: identifier(dt.getName()), // DOMAIN_NAME
1124: col.getDefaultSQL(), // COLUMN_DEFAULT
1125: col.getNullable() ? "YES" : "NO", // IS_NULLABLE
1126: "" + col.getDataType().sqlType, // DATA_TYPE
1127: "" + col.getPrecisionAsInt(), // PRECISION INT
1128: "" + col.getScale(), // SCALE INT
1129: col.getDataType().name, // TYPE_NAME
1130: "" + col.getSelectivity(), // SELECTIVITY INT
1131: ""
1132: + col.getCheckConstraintSQL(session,
1133: "VALUE"), // CHECK_CONSTRAINT
1134: replaceNullWithEmpty(dt.getComment()), // REMARKS
1135: "" + dt.getCreateSQL(), // SQL
1136: "" + dt.getId() // ID
1137: });
1138: }
1139: break;
1140: }
1141: case TRIGGERS: {
1142: ObjectArray triggers = database
1143: .getAllSchemaObjects(DbObject.TRIGGER);
1144: for (int i = 0; i < triggers.size(); i++) {
1145: TriggerObject trigger = (TriggerObject) triggers.get(i);
1146: Table table = trigger.getTable();
1147: add(rows, new String[] { catalog, // TRIGGER_CATALOG
1148: identifier(trigger.getSchema().getName()), // TRIGGER_SCHEMA
1149: identifier(trigger.getName()), // TRIGGER_NAME
1150: trigger.getTypeNameList(), // TRIGGER_TYPE
1151: catalog, // TABLE_CATALOG
1152: identifier(table.getSchema().getName()), // TABLE_SCHEMA
1153: identifier(table.getName()), // TABLE_NAME
1154: "" + trigger.getBefore(), // BEFORE BIT
1155: trigger.getTriggerClassName(), // JAVA_CLASS
1156: "" + trigger.getQueueSize(), // QUEUE_SIZE INT
1157: "" + trigger.getNoWait(), // NO_WAIT BIT
1158: replaceNullWithEmpty(trigger.getComment()), // REMARKS
1159: trigger.getCreateSQL(), // SQL
1160: "" + trigger.getId() // ID
1161: });
1162: }
1163: break;
1164: }
1165: case SESSIONS: {
1166: Session[] sessions = database.getSessions();
1167: boolean admin = session.getUser().getAdmin();
1168: for (int i = 0; i < sessions.length; i++) {
1169: Session s = sessions[i];
1170: if (admin || s == session) {
1171: Command command = s.getCurrentCommand();
1172: add(rows,
1173: new String[] {
1174: "" + s.getId(), // ID
1175: s.getUser().getName(), // USER_NAME
1176: new Timestamp(s.getSessionStart())
1177: .toString(), // SESSION_START
1178: command == null ? null : command
1179: .toString(), // STATEMENT
1180: new Timestamp(s
1181: .getCurrentCommandStart())
1182: .toString() // STATEMENT_START
1183: });
1184: }
1185: }
1186: break;
1187: }
1188: case LOCKS: {
1189: Session[] sessions = database.getSessions();
1190: boolean admin = session.getUser().getAdmin();
1191: for (int i = 0; i < sessions.length; i++) {
1192: Session s = sessions[i];
1193: if (admin || s == session) {
1194: Table[] locks = s.getLocks();
1195: for (int j = 0; j < locks.length; j++) {
1196: Table table = locks[j];
1197: add(rows, new String[] {
1198: table.getSchema().getName(), // TABLE_SCHEMA
1199: table.getName(), // TABLE_NAME
1200: "" + s.getId(), // SESSION_ID
1201: table.isLockExclusive(s) ? "WRITE"
1202: : "READ", // LOCK_TYPE
1203: });
1204: }
1205: }
1206: }
1207: break;
1208: }
1209: default:
1210: throw Message.getInternalError("type=" + type);
1211: }
1212: return rows;
1213: }
1214:
1215: private int getRefAction(int action) {
1216: switch (action) {
1217: case ConstraintReferential.CASCADE:
1218: return DatabaseMetaData.importedKeyCascade;
1219: case ConstraintReferential.RESTRICT:
1220: return DatabaseMetaData.importedKeyRestrict;
1221: case ConstraintReferential.SET_DEFAULT:
1222: return DatabaseMetaData.importedKeySetDefault;
1223: case ConstraintReferential.SET_NULL:
1224: return DatabaseMetaData.importedKeySetNull;
1225: default:
1226: throw Message.getInternalError("action=" + action);
1227: }
1228: }
1229:
1230: public void removeRow(Session session, Row row) throws SQLException {
1231: throw Message.getUnsupportedException();
1232: }
1233:
1234: public void addRow(Session session, Row row) throws SQLException {
1235: throw Message.getUnsupportedException();
1236: }
1237:
1238: public void removeIndex(String indexName) throws SQLException {
1239: throw Message.getUnsupportedException();
1240: }
1241:
1242: public void removeChildrenAndResources(Session session)
1243: throws SQLException {
1244: throw Message.getUnsupportedException();
1245: }
1246:
1247: public void close(Session session) throws SQLException {
1248: // nothing to do
1249: }
1250:
1251: public void unlock(Session s) {
1252: // nothing to do
1253: }
1254:
1255: private void addPrivileges(ObjectArray rows, DbObject grantee,
1256: String catalog, Table table, String column, int rightMask)
1257: throws SQLException {
1258: if ((rightMask & Right.SELECT) != 0) {
1259: addPrivilege(rows, grantee, catalog, table, column,
1260: "SELECT");
1261: }
1262: if ((rightMask & Right.INSERT) != 0) {
1263: addPrivilege(rows, grantee, catalog, table, column,
1264: "INSERT");
1265: }
1266: if ((rightMask & Right.UPDATE) != 0) {
1267: addPrivilege(rows, grantee, catalog, table, column,
1268: "UPDATE");
1269: }
1270: if ((rightMask & Right.DELETE) != 0) {
1271: addPrivilege(rows, grantee, catalog, table, column,
1272: "DELETE");
1273: }
1274: }
1275:
1276: private void addPrivilege(ObjectArray rows, DbObject grantee,
1277: String catalog, Table table, String column, String right)
1278: throws SQLException {
1279: String isGrantable = "NO";
1280: if (grantee.getType() == DbObject.USER) {
1281: User user = (User) grantee;
1282: if (user.getAdmin()) {
1283: // the right is grantable if the grantee is an admin
1284: isGrantable = "YES";
1285: }
1286: }
1287: if (column == null) {
1288: add(rows, new String[] { null, // GRANTOR
1289: identifier(grantee.getName()), // GRANTEE
1290: catalog, // TABLE_CATALOG
1291: identifier(table.getSchema().getName()), // TABLE_SCHEMA
1292: identifier(table.getName()), // TABLE_NAME
1293: right, // PRIVILEGE_TYPE
1294: isGrantable // IS_GRANTABLE
1295: });
1296: } else {
1297: add(rows, new String[] { null, // GRANTOR
1298: identifier(grantee.getName()), // GRANTEE
1299: catalog, // TABLE_CATALOG
1300: identifier(table.getSchema().getName()), // TABLE_SCHEMA
1301: identifier(table.getName()), // TABLE_NAME
1302: identifier(column), // COLUMN_NAME
1303: right, // PRIVILEGE_TYPE
1304: isGrantable // IS_GRANTABLE
1305: });
1306: }
1307: }
1308:
1309: private void add(ObjectArray rows, String[] strings)
1310: throws SQLException {
1311: Value[] values = new Value[strings.length];
1312: for (int i = 0; i < strings.length; i++) {
1313: String s = strings[i];
1314: Value v = (s == null) ? (Value) ValueNull.INSTANCE
1315: : ValueString.get(s);
1316: Column col = columns[i];
1317: v = v.convertTo(col.getType());
1318: values[i] = v;
1319: }
1320: Row row = new Row(values, 0);
1321: row.setPos(rows.size());
1322: rows.add(row);
1323: }
1324:
1325: public void checkRename() throws SQLException {
1326: throw Message.getUnsupportedException();
1327: }
1328:
1329: public void checkSupportAlter() throws SQLException {
1330: throw Message.getUnsupportedException();
1331: }
1332:
1333: public void truncate(Session session) throws SQLException {
1334: throw Message.getUnsupportedException();
1335: }
1336:
1337: public long getRowCount(Session session) {
1338: throw Message.getInternalError();
1339: }
1340:
1341: public boolean canGetRowCount() {
1342: return false;
1343: }
1344:
1345: public boolean canDrop() {
1346: return false;
1347: }
1348:
1349: public String getTableType() {
1350: return Table.SYSTEM_TABLE;
1351: }
1352:
1353: public Index getScanIndex(Session session) throws SQLException {
1354: return new MetaIndex(this , IndexColumn.wrap(columns), true);
1355: }
1356:
1357: public ObjectArray getIndexes() {
1358: if (index == null) {
1359: return null;
1360: }
1361: ObjectArray list = new ObjectArray();
1362: list.add(new MetaIndex(this , IndexColumn.wrap(columns), true));
1363: // TODO fixed scan index
1364: list.add(index);
1365: return list;
1366: }
1367:
1368: public long getMaxDataModificationId() {
1369: return database.getModificationDataId();
1370: }
1371:
1372: public Index getUniqueIndex() {
1373: return null;
1374: }
1375:
1376: }
|