KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > table > MetaTable


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.table;
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStreamReader JavaDoc;
10 import java.io.Reader JavaDoc;
11 import java.sql.DatabaseMetaData JavaDoc;
12 import java.sql.ResultSet JavaDoc;
13 import java.sql.SQLException JavaDoc;
14 import java.text.Collator JavaDoc;
15 import java.util.Locale JavaDoc;
16
17 import org.h2.constraint.Constraint;
18 import org.h2.constraint.ConstraintCheck;
19 import org.h2.constraint.ConstraintReferential;
20 import org.h2.constraint.ConstraintUnique;
21 import org.h2.engine.Constants;
22 import org.h2.engine.Database;
23 import org.h2.engine.DbObject;
24 import org.h2.engine.FunctionAlias;
25 import org.h2.engine.Mode;
26 import org.h2.engine.Right;
27 import org.h2.engine.Role;
28 import org.h2.engine.Session;
29 import org.h2.engine.Setting;
30 import org.h2.engine.User;
31 import org.h2.engine.UserDataType;
32 import org.h2.expression.ValueExpression;
33 import org.h2.index.Index;
34 import org.h2.index.IndexType;
35 import org.h2.index.MetaIndex;
36 import org.h2.message.Message;
37 import org.h2.result.Row;
38 import org.h2.result.SearchRow;
39 import org.h2.schema.Constant;
40 import org.h2.schema.Schema;
41 import org.h2.schema.Sequence;
42 import org.h2.schema.TriggerObject;
43 import org.h2.store.DiskFile;
44 import org.h2.store.InDoubtTransaction;
45 import org.h2.tools.Csv;
46 import org.h2.util.ObjectArray;
47 import org.h2.util.Resources;
48 import org.h2.util.StringUtils;
49 import org.h2.value.CompareMode;
50 import org.h2.value.DataType;
51 import org.h2.value.Value;
52 import org.h2.value.ValueNull;
53 import org.h2.value.ValueString;
54
55 /**
56  * @author Thomas
57  */

58
59 public class MetaTable extends Table {
60
61     // TODO INFORMATION_SCHEMA.tables: select table_name from INFORMATION_SCHEMA.tables where TABLE_TYPE = 'BASE TABLE'
62

63     public static final int TABLES = 0, COLUMNS = 1, INDEXES = 2, TABLE_TYPES=3,
64     TYPE_INFO=4, CATALOGS=5, SETTINGS=6, HELP=7, SEQUENCES=8, USERS=9,
65     ROLES=10, RIGHTS=11, FUNCTION_ALIASES = 12, SCHEMATA = 13, TABLE_PRIVILEGES = 14,
66     COLUMN_PRIVILEGES = 15, COLLATIONS = 16, VIEWS = 17, IN_DOUBT = 18, CROSS_REFERENCES = 19,
67     CONSTRAINTS = 20, FUNCTION_COLUMNS = 21, CONSTANTS = 22, DOMAINS = 23, TRIGGERS = 24;
68
69     private int type;
70     private MetaIndex index;
71     private int indexColumn;
72
73     public MetaTable(Schema schema, int type) throws SQLException JavaDoc {
74         // tableName will be set later
75
super(schema, 0, null, true);
76         this.type = type;
77         Column[] cols;
78         String JavaDoc indexColumnName = null;
79         switch(type) {
80         case TABLES:
81             setObjectName("TABLES");
82             cols = createColumns(new String JavaDoc[]{
83                     "TABLE_CATALOG",
84                     "TABLE_SCHEMA",
85                     "TABLE_NAME",
86                     "TABLE_TYPE",
87                     // extensions
88
"STORAGE_TYPE",
89                     "SQL",
90                     "REMARKS"
91             });
92             indexColumnName = "TABLE_NAME";
93             break;
94         case COLUMNS:
95             setObjectName("COLUMNS");
96             cols = createColumns(new String JavaDoc[]{
97                     "TABLE_CATALOG",
98                     "TABLE_SCHEMA",
99                     "TABLE_NAME",
100                     "COLUMN_NAME",
101                     "ORDINAL_POSITION INT",
102                     "COLUMN_DEFAULT",
103                     "IS_NULLABLE",
104                     "DATA_TYPE SMALLINT",
105                     "CHARACTER_MAXIMUM_LENGTH INT",
106                     "CHARACTER_OCTET_LENGTH INT",
107                     "NUMERIC_PRECISION INT",
108                     "NUMERIC_PRECISION_RADIX INT",
109                     "NUMERIC_SCALE INT",
110                     "CHARACTER_SET_NAME",
111                     "COLLATION_NAME",
112                     // extensions
113
"TYPE_NAME",
114                     "NULLABLE SMALLINT",
115                     "IS_COMPUTED BIT",
116                     "SELECTIVITY INT",
117                     "CHECK_CONSTRAINT",
118                     "REMARKS"
119             });
120             indexColumnName = "TABLE_NAME";
121             break;
122         case INDEXES:
123             setObjectName("INDEXES");
124             cols = createColumns(new String JavaDoc[]{
125                     "TABLE_CATALOG",
126                     "TABLE_SCHEMA",
127                     "TABLE_NAME",
128                     "NON_UNIQUE BIT",
129                     "INDEX_NAME",
130                     "ORDINAL_POSITION SMALLINT",
131                     "COLUMN_NAME",
132                     "CARDINALITY INT",
133                     "PRIMARY_KEY BIT",
134                     "INDEX_TYPE_NAME",
135                     "IS_GENERATED BIT",
136                     "INDEX_TYPE SMALLINT",
137                     "ASC_OR_DESC",
138                     "PAGES INT",
139                     "FILTER_CONDITION",
140                     "REMARKS"
141             });
142             indexColumnName = "TABLE_NAME";
143             break;
144         case TABLE_TYPES:
145             setObjectName("TABLE_TYPES");
146             cols = createColumns(
147                     new String JavaDoc[]{"TYPE"});
148             break;
149         case TYPE_INFO:
150             setObjectName("TYPE_INFO");
151             cols = createColumns(new String JavaDoc[]{
152                 "TYPE_NAME",
153                 "DATA_TYPE SMALLINT",
154                 "PRECISION INT",
155                 "PREFIX",
156                 "SUFFIX",
157                 "PARAMS",
158                 "AUTO_INCREMENT BIT",
159                 "MINIMUM_SCALE SMALLINT",
160                 "MAXIMUM_SCALE SMALLINT",
161                 "RADIX INT",
162                 "POS INT",
163                 "CASE_SENSITIVE BIT",
164                 "NULLABLE SMALLINT",
165                 "SEARCHABLE SMALLINT"
166             });
167             break;
168         case CATALOGS:
169             setObjectName("CATALOGS");
170             cols = createColumns(
171                     new String JavaDoc[]{"CATALOG_NAME"});
172             break;
173         case SETTINGS:
174             setObjectName("SETTINGS");
175             cols = createColumns(
176                     new String JavaDoc[]{"NAME", "VALUE"});
177             break;
178         case HELP:
179             setObjectName("HELP");
180             cols = createColumns(new String JavaDoc[]{
181                     "ID INT",
182                     "SECTION",
183                     "TOPIC",
184                     "SYNTAX",
185                     "TEXT",
186                     "EXAMPLE"
187             });
188             break;
189         case SEQUENCES:
190             setObjectName("SEQUENCES");
191             cols = createColumns(new String JavaDoc[]{
192                     "SEQUENCE_CATALOG",
193                     "SEQUENCE_SCHEMA",
194                     "SEQUENCE_NAME",
195                     "CURRENT_VALUE BIGINT",
196                     "INCREMENT BIGINT",
197                     "IS_GENERATED BIT",
198                     "REMARKS"
199             });
200             break;
201         case USERS:
202             setObjectName("USERS");
203             cols = createColumns(new String JavaDoc[]{
204                     "NAME",
205                     "ADMIN",
206                     "REMARKS"
207             });
208             break;
209         case ROLES:
210             setObjectName("ROLES");
211             cols = createColumns(new String JavaDoc[]{
212                     "NAME",
213                     "REMARKS"
214             });
215             break;
216         case RIGHTS:
217             setObjectName("RIGHTS");
218             cols = createColumns(new String JavaDoc[]{
219                     "GRANTEE",
220                     "GRANTEETYPE",
221                     "GRANTEDROLE",
222                     "RIGHTS",
223                     "TABLE_SCHEMA",
224                     "TABLE_NAME"
225             });
226             indexColumnName = "TABLE_NAME";
227             break;
228         case FUNCTION_ALIASES:
229             setObjectName("FUNCTION_ALIASES");
230             cols = createColumns(new String JavaDoc[]{
231                     "ALIAS_CATALOG",
232                     "ALIAS_SCHEMA",
233                     "ALIAS_NAME",
234                     "JAVA_CLASS",
235                     "JAVA_METHOD",
236                     "DATA_TYPE INT",
237                     "COLUMN_COUNT INT",
238                     "RETURNS_RESULT SMALLINT",
239                     "REMARKS"
240             });
241             break;
242         case FUNCTION_COLUMNS:
243             setObjectName("FUNCTION_COLUMNS");
244             cols = createColumns(new String JavaDoc[]{
245                     "ALIAS_CATALOG",
246                     "ALIAS_SCHEMA",
247                     "ALIAS_NAME",
248                     "JAVA_CLASS",
249                     "JAVA_METHOD",
250                     "POS INT",
251                     "COLUMN_NAME",
252                     "DATA_TYPE INT",
253                     "TYPE_NAME",
254                     "PRECISION INT",
255                     "SCALE SMALLINT",
256                     "RADIX SMALLINT",
257                     "NULLABLE SMALLINT",
258                     "COLUMN_TYPE SMALLINT",
259                     "REMARKS"
260             });
261             break;
262         case SCHEMATA:
263             setObjectName("SCHEMATA");
264             cols = createColumns(new String JavaDoc[]{
265                     "CATALOG_NAME",
266                     "SCHEMA_NAME",
267                     "SCHEMA_OWNER",
268                     "DEFAULT_CHARACTER_SET_NAME",
269                     "DEFAULT_COLLATION_NAME",
270                     "IS_DEFAULT BIT",
271                     "REMARKS"
272             });
273             break;
274         case TABLE_PRIVILEGES:
275             setObjectName("TABLE_PRIVILEGES");
276             cols = createColumns(new String JavaDoc[]{
277                     "GRANTOR",
278                     "GRANTEE",
279                     "TABLE_CATALOG",
280                     "TABLE_SCHEMA",
281                     "TABLE_NAME",
282                     "PRIVILEGE_TYPE",
283                     "IS_GRANTABLE",
284             });
285             indexColumnName = "TABLE_NAME";
286             break;
287         case COLUMN_PRIVILEGES:
288             setObjectName("COLUMN_PRIVILEGES");
289             cols = createColumns(new String JavaDoc[]{
290                     "GRANTOR",
291                     "GRANTEE",
292                     "TABLE_CATALOG",
293                     "TABLE_SCHEMA",
294                     "TABLE_NAME",
295                     "COLUMN_NAME",
296                     "PRIVILEGE_TYPE",
297                     "IS_GRANTABLE",
298             });
299             indexColumnName = "TABLE_NAME";
300             break;
301         case COLLATIONS:
302             setObjectName("COLLATIONS");
303             cols = createColumns(new String JavaDoc[]{
304                     "NAME",
305                     "KEY"
306             });
307             break;
308         case VIEWS:
309             setObjectName("VIEWS");
310             cols = createColumns(new String JavaDoc[]{
311                     "TABLE_CATALOG",
312                     "TABLE_SCHEMA",
313                     "TABLE_NAME",
314                     "VIEW_DEFINITION",
315                     "CHECK_OPTION",
316                     "IS_UPDATABLE",
317                     "STATUS",
318                     "REMARKS"
319             });
320             indexColumnName = "TABLE_NAME";
321             break;
322         case IN_DOUBT:
323             setObjectName("IN_DOUBT");
324             cols = createColumns(new String JavaDoc[]{
325                     "TRANSACTION",
326                     "STATE",
327             });
328             break;
329         case CROSS_REFERENCES:
330             setObjectName("CROSS_REFERENCES");
331             cols = createColumns(new String JavaDoc[]{
332                     "PKTABLE_CATALOG",
333                     "PKTABLE_SCHEMA",
334                     "PKTABLE_NAME",
335                     "PKCOLUMN_NAME",
336                     "FKTABLE_CATALOG",
337                     "FKTABLE_SCHEMA",
338                     "FKTABLE_NAME",
339                     "FKCOLUMN_NAME",
340                     "ORDINAL_POSITION SMALLINT",
341                     "UPDATE_RULE SMALLINT",
342                     "DELETE_RULE SMALLINT",
343                     "FK_NAME",
344                     "PK_NAME",
345                     "DEFERRABILITY SMALLINT"
346             });
347             indexColumnName = "PKTABLE_NAME";
348             break;
349         case CONSTRAINTS:
350             setObjectName("CONSTRAINTS");
351             cols = createColumns(new String JavaDoc[]{
352                     "CONSTRAINT_CATALOG",
353                     "CONSTRAINT_SCHEMA",
354                     "CONSTRAINT_NAME",
355                     "CONSTRAINT_TYPE",
356                     "TABLE_CATALOG",
357                     "TABLE_SCHEMA",
358                     "TABLE_NAME",
359                     "CHECK_EXPRESSION",
360                     "COLUMN_LIST",
361                     "REMARKS",
362                     "SQL",
363             });
364             indexColumnName = "TABLE_NAME";
365             break;
366         case CONSTANTS:
367             setObjectName("CONSTANTS");
368             cols = createColumns(new String JavaDoc[]{
369                     "CONSTANT_CATALOG",
370                     "CONSTANT_SCHEMA",
371                     "CONSTANT_NAME",
372                     "DATA_TYPE SMALLINT",
373                     "REMARKS",
374                     "SQL",
375             });
376             break;
377         case DOMAINS:
378             setObjectName("DOMAINS");
379             cols = createColumns(new String JavaDoc[]{
380                     "DOMAIN_CATALOG",
381                     "DOMAIN_SCHEMA",
382                     "DOMAIN_NAME",
383                     "COLUMN_DEFAULT",
384                     "IS_NULLABLE",
385                     "DATA_TYPE SMALLINT",
386                     "PRECISION INT",
387                     "SCALE INT",
388                     "TYPE_NAME",
389                     "SELECTIVITY INT",
390                     "CHECK_CONSTRAINT",
391                     "REMARKS",
392                     "SQL",
393             });
394             break;
395         case TRIGGERS:
396             setObjectName("TRIGGERS");
397             cols = createColumns(new String JavaDoc[]{
398                     "TRIGGER_CATALOG",
399                     "TRIGGER_SCHEMA",
400                     "TRIGGER_NAME",
401                     "TRIGGER_TYPE",
402                     "TABLE_CATALOG",
403                     "TABLE_SCHEMA",
404                     "TABLE_NAME",
405                     "BEFORE BIT",
406                     "JAVA_CLASS",
407                     "QUEUE_SIZE INT",
408                     "NO_WAIT BIT",
409                     "REMARKS",
410                     "SQL",
411             });
412             break;
413         default:
414             throw Message.getInternalError("type="+type);
415         }
416         setColumns(cols);
417
418         if(indexColumnName == null) {
419             indexColumn = -1;
420         } else {
421             indexColumn = getColumn(indexColumnName).getColumnId();
422             Column[] indexCols = new Column[]{cols[indexColumn]};
423             index = new MetaIndex(this, indexCols, false);
424         }
425     }
426
427     private Column[] createColumns(String JavaDoc[] names) {
428         Column[] cols = new Column[names.length];
429         for(int i=0; i<names.length; i++) {
430             String JavaDoc nameType = names[i];
431             int idx = nameType.indexOf(' ');
432             int type;
433             String JavaDoc name;
434             if(idx < 0) {
435                 type = Value.STRING;
436                 name = nameType;
437             } else {
438                 type = DataType.getTypeByName(nameType.substring(idx+1)).type;
439                 name = nameType.substring(0, idx);
440             }
441             cols[i] = new Column(name, type, 0, 0);
442         }
443         return cols;
444     }
445
446     public String JavaDoc getCreateSQL() {
447         return null;
448     }
449
450     public Index addIndex(Session session, String JavaDoc indexName, int indexId, Column[] cols, IndexType indexType, int headPos, String JavaDoc comment) throws SQLException JavaDoc {
451         throw Message.getUnsupportedException();
452     }
453
454     public void lock(Session session, boolean exclusive) throws SQLException JavaDoc {
455         // nothing to do
456
}
457
458     public boolean isLockedExclusively() {
459         return false;
460     }
461
462     private String JavaDoc identifier(String JavaDoc s) {
463         if(Mode.getCurrentMode().lowerCaseIdentifiers) {
464             s = (s==null ? null : StringUtils.toLowerEnglish(s));
465         }
466         return s;
467     }
468
469     private ObjectArray getAllTables(Session session) {
470         ObjectArray tables = database.getAllSchemaObjects(DbObject.TABLE_OR_VIEW);
471         ObjectArray tempTables = session.getLocalTempTables();
472         tables.addAll(tempTables);
473         return tables;
474     }
475
476     private boolean checkIndex(Session session, String JavaDoc value, Value indexFrom, Value indexTo) throws SQLException JavaDoc {
477         if(value == null || (indexFrom == null && indexTo == null)) {
478             return true;
479         }
480         Database db = session.getDatabase();
481         Value v = ValueString.get(value);
482         if(indexFrom != null && db.compare(v, indexFrom) < 0) {
483             return false;
484         }
485         if(indexTo != null && db.compare(v, indexTo) > 0) {
486             return false;
487         }
488         return true;
489     }
490
491     private String JavaDoc replaceNullWithEmpty(String JavaDoc s) {
492         return s == null ? "" : s;
493     }
494
495     public ObjectArray generateRows(Session session, SearchRow first, SearchRow last) throws SQLException JavaDoc {
496         Value indexFrom = null, indexTo = null;
497         if(indexColumn >= 0) {
498             if(first != null) {
499                 indexFrom = first.getValue(indexColumn);
500             }
501             if(last != null) {
502                 indexTo = last.getValue(indexColumn);
503             }
504         }
505         ObjectArray rows = new ObjectArray();
506         String JavaDoc catalog = identifier(database.getShortName());
507         switch(type) {
508         case TABLES: {
509             ObjectArray tables = getAllTables(session);
510             for(int i=0; i<tables.size(); i++) {
511                 Table table = (Table) tables.get(i);
512                 String JavaDoc tableName = identifier(table.getName());
513                 if(!checkIndex(session, tableName, indexFrom, indexTo)) {
514                     continue;
515                 }
516                 String JavaDoc storageType;
517                 if(table.getTemporary()) {
518                     if(table.getGlobalTemporary()) {
519                         storageType = "GLOBAL TEMPORARY";
520                     } else {
521                         storageType = "LOCAL TEMPORARY";
522                     }
523                 } else {
524                     storageType = table.isPersistent() ? "CACHED" : "MEMORY";
525                 }
526                 add(rows, new String JavaDoc[]{
527                         catalog, // TABLE_CATALOG
528
identifier(table.getSchema().getName()), // TABLE_SCHEMA
529
tableName, // TABLE_NAME
530
table.getTableType(), // TABLE_TYPE
531
storageType, // STORAGE_TYPE
532
table.getCreateSQL(), // SQL
533
replaceNullWithEmpty(table.getComment()) // REMARKS
534
});
535             }
536             break;
537         }
538         case COLUMNS: {
539             ObjectArray tables = getAllTables(session);
540             for(int i=0; i<tables.size(); i++) {
541                 Table table = (Table) tables.get(i);
542                 String JavaDoc tableName = identifier(table.getName());
543                 if(!checkIndex(session, tableName, indexFrom, indexTo)) {
544                     continue;
545                 }
546                 Column[] cols = table.getColumns();
547                 String JavaDoc collation = database.getCompareMode().getName();
548                 for(int j=0; j<cols.length; j++) {
549                     Column c = cols[j];
550                     add(rows, new String JavaDoc[]{
551                             catalog, // TABLE_CATALOG
552
identifier(table.getSchema().getName()), // TABLE_SCHEMA
553
tableName, // TABLE_NAME
554
identifier(c.getName()), // COLUMN_NAME
555
String.valueOf(j + 1), // ORDINAL_POSITION
556
c.getDefaultSQL(), // COLUMN_DEFAULT
557
c.getNullable() ? "YES" : "NO", // IS_NULLABLE
558
"" + DataType.convertTypeToSQLType(c.getType()), // DATA_TYPE
559
"" + c.getPrecisionAsInt(), // CHARACTER_MAXIMUM_LENGTH
560
"" + c.getPrecisionAsInt(), // CHARACTER_OCTET_LENGTH
561
"" + c.getPrecisionAsInt(), // NUMERIC_PRECISION
562
"10", // NUMERIC_PRECISION_RADIX
563
"" + c.getScale(), // NUMERIC_SCALE
564
Constants.CHARACTER_SET_NAME, // CHARACTER_SET_NAME
565
collation, // COLLATION_NAME
566
identifier(DataType.getDataType(c.getType()).name), // TYPE_NAME
567
"" + (c.getNullable() ? DatabaseMetaData.columnNullable : DatabaseMetaData.columnNoNulls) , // NULLABLE
568
"" + (c.getComputed() ? "TRUE" : "FALSE"), // IS_COMPUTED
569
"" + (c.getSelectivity()), // SELECTIVITY
570
c.getCheckConstraintSQL(session, c.getName()), // CHECK_CONSTRAINT
571
replaceNullWithEmpty(c.getComment()) // REMARKS
572
});
573                 }
574             }
575             break;
576         }
577         case INDEXES: {
578             ObjectArray tables = getAllTables(session);
579             for(int i=0; i<tables.size(); i++) {
580                 Table table = (Table) tables.get(i);
581                 String JavaDoc tableName = identifier(table.getName());
582                 if(!checkIndex(session, tableName, indexFrom, indexTo)) {
583                     continue;
584                 }
585                 ObjectArray idx = table.getIndexes();
586                 for(int j=0; idx != null && j<idx.size(); j++) {
587                     Index index = (Index) idx.get(j);
588                     if(index.getCreateSQL() == null) {
589                         continue;
590                     }
591                     Column[] cols = index.getColumns();
592                     for(int k=0; k<cols.length; k++) {
593                         Column column = cols[k];
594                         add(rows,new String JavaDoc[]{
595                                 catalog, // TABLE_CATALOG
596
identifier(table.getSchema().getName()), // TABLE_SCHEMA
597
tableName, // TABLE_NAME
598
index.getIndexType().isUnique() ? "FALSE" : "TRUE", // NON_UNIQUE
599
identifier(index.getName()), // INDEX_NAME
600
"" + (k+1), // ORDINAL_POSITION
601
identifier(column.getName()), // COLUMN_NAME
602
"0", // CARDINALITY
603
index.getIndexType().isPrimaryKey() ? "TRUE" : "FALSE", // PRIMARY_KEY
604
index.getIndexType().getSQL(), // INDEX_TYPE_NAME
605
index.getIndexType().belongsToConstraint() ? "TRUE" : "FALSE", // IS_GENERATED
606
"" + DatabaseMetaData.tableIndexOther, // INDEX_TYPE
607
"A", // ASC_OR_DESC
608
"0", // PAGES
609
"", // FILTER_CONDITION
610
replaceNullWithEmpty(index.getComment()) // REMARKS
611
});
612                     }
613                 }
614             }
615             break;
616         }
617         case TABLE_TYPES: {
618             add(rows,new String JavaDoc[]{Table.TABLE});
619             add(rows,new String JavaDoc[]{Table.TABLE_LINK});
620             add(rows,new String JavaDoc[]{Table.SYSTEM_TABLE});
621             add(rows,new String JavaDoc[]{Table.VIEW});
622             break;
623         }
624         case CATALOGS: {
625             add(rows,new String JavaDoc[]{catalog});
626             break;
627         }
628         case SETTINGS: {
629             ObjectArray list = database.getAllSettings();
630             for(int i=0; i<list.size(); i++) {
631                 Setting s = (Setting)list.get(i);
632                 String JavaDoc value = s.getStringValue();
633                 if(value == null) {
634                     value = "" + s.getIntValue();
635                 }
636                 add(rows,new String JavaDoc[]{
637                         identifier(s.getName()),
638                         value
639                 });
640             }
641             add(rows, new String JavaDoc[]{"MODE", Mode.getCurrentMode().getName()});
642             DiskFile dataFile = database.getDataFile();
643             if(dataFile != null) {
644                 add(rows, new String JavaDoc[]{"CACHE_TYPE", dataFile.getCache().getTypeName()});
645                 if(session.getUser().getAdmin()) {
646                     add(rows, new String JavaDoc[]{"FILE_DISK_WRITE", "" + dataFile.getWriteCount()});
647                     add(rows, new String JavaDoc[]{"FILE_DISK_READ", "" + dataFile.getReadCount()});
648                     add(rows, new String JavaDoc[]{"FILE_INDEX_WRITE", "" + database.getIndexFile().getWriteCount()});
649                     add(rows, new String JavaDoc[]{"FILE_INDEX_READ", "" + database.getIndexFile().getReadCount()});
650                 }
651             }
652             add(rows, new String JavaDoc[]{"h2.check", "" + Constants.CHECK});
653             add(rows, new String JavaDoc[]{"h2.check2", "" + Constants.CHECK2});
654             add(rows, new String JavaDoc[]{"h2.lobFilesInDirectories", "" + Constants.LOB_FILES_IN_DIRECTORIES});
655             add(rows, new String JavaDoc[]{"h2.lobFilesPerDirectory", "" + Constants.LOB_FILES_PER_DIRECTORY});
656             add(rows, new String JavaDoc[]{"h2.multiThreadedKernel", "" + Constants.MULTI_THREADED_KERNEL});
657             add(rows, new String JavaDoc[]{"h2.runFinalizers", "" + Constants.RUN_FINALIZERS});
658             add(rows, new String JavaDoc[]{"h2.optimizeMinMax", "" + Constants.OPTIMIZE_MIN_MAX});
659             add(rows, new String JavaDoc[]{"h2.optimizeIn", "" + Constants.OPTIMIZE_IN});
660             add(rows, new String JavaDoc[]{"h2.redoBufferSize", "" + Constants.REDO_BUFFER_SIZE});
661             add(rows, new String JavaDoc[]{"h2.recompileAlways", "" + Constants.RECOMPILE_ALWAYS});
662             add(rows, new String JavaDoc[]{"h2.optimizeSubqueryCache", "" + Constants.OPTIMIZE_SUBQUERY_CACHE});
663             add(rows, new String JavaDoc[]{"h2.overflowExceptions", "" + Constants.OVERFLOW_EXCEPTIONS});
664             add(rows, new String JavaDoc[]{"h2.logAllErrors", "" + Constants.LOG_ALL_ERRORS});
665             add(rows, new String JavaDoc[]{"h2.logAllErrorsFile", "" + Constants.LOG_ALL_ERRORS_FILE});
666             add(rows, new String JavaDoc[]{"h2.serverCachedObjects", "" + Constants.SERVER_CACHED_OBJECTS});
667             add(rows, new String JavaDoc[]{"h2.serverSmallResultSetSize", "" + Constants.SERVER_SMALL_RESULTSET_SIZE});
668             add(rows, new String JavaDoc[]{"h2.emergencySpaceInitial", "" + Constants.EMERGENCY_SPACE_INITIAL});
669             add(rows, new String JavaDoc[]{"h2.emergencySpaceMin", "" + Constants.EMERGENCY_SPACE_MIN});
670             add(rows, new String JavaDoc[]{"h2.objectCache", "" + Constants.OBJECT_CACHE});
671             add(rows, new String JavaDoc[]{"h2.objectCacheSize", "" + Constants.OBJECT_CACHE_SIZE});
672             add(rows, new String JavaDoc[]{"h2.objectCacheMaxPerElementSize", "" + Constants.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE});
673             add(rows, new String JavaDoc[]{"h2.clientTraceDirectory", Constants.CLIENT_TRACE_DIRECTORY});
674             add(rows, new String JavaDoc[]{"h2.scriptDirectory", Constants.SCRIPT_DIRECTORY});
675             break;
676         }
677         case TYPE_INFO: {
678             ObjectArray types = DataType.getTypes();
679             for(int i=0; i<types.size(); i++) {
680                 DataType t = (DataType) types.get(i);
681                 if(t.hidden || t.sqlType == Value.NULL) {
682                     continue;
683                 }
684                 add(rows,new String JavaDoc[]{
685                         t.name, // TYPE_NAME
686
String.valueOf(t.sqlType), // DATA_TYPE
687
String.valueOf(t.maxPrecision), // PRECISION
688
t.prefix, // PREFIX
689
t.suffix, // SUFFIX
690
t.params, // PARAMS
691
String.valueOf(t.autoInc), // AUTO_INCREMENT
692
String.valueOf(t.minScale), // MINIMUM_SCALE
693
String.valueOf(t.maxScale), // MAXIMUM_SCALE
694
t.decimal ? "10" : null, // RADIX
695
String.valueOf(t.order), // POS
696
String.valueOf(t.caseSensitive), // CASE_SENSITIVE
697
"" + DatabaseMetaData.typeNullable, // NULLABLE
698
"" + DatabaseMetaData.typeSearchable // SEARCHABLE
699
});
700             }
701             break;
702         }
703         case HELP: {
704             try {
705                 byte[] data = Resources.get("/org/h2/res/help.csv");
706                 Reader JavaDoc reader = new InputStreamReader JavaDoc(new ByteArrayInputStream JavaDoc(data));
707                 ResultSet JavaDoc rs = Csv.getInstance().read(reader, null);
708                 for(int i=0; rs.next(); i++) {
709                     add(rows, new String JavaDoc[]{
710                         String.valueOf(i), // ID
711
rs.getString(1).trim(), // SECTION
712
rs.getString(2).trim(), // TOPIC
713
rs.getString(3).trim(), // SYNTAX
714
rs.getString(4).trim(), // TEXT
715
rs.getString(5).trim(), // EXAMPLE
716
});
717                 }
718             } catch (IOException JavaDoc e) {
719                 throw Message.convert(e);
720             }
721             break;
722         }
723         case SEQUENCES: {
724             ObjectArray sequences = database.getAllSchemaObjects(DbObject.SEQUENCE);
725             for(int i=0; i<sequences.size(); i++) {
726                 Sequence s = (Sequence) sequences.get(i);
727                 add(rows,new String JavaDoc[]{
728                         catalog, // SEQUENCE_CATALOG
729
identifier(s.getSchema().getName()), // SEQUENCE_SCHEMA
730
identifier(s.getName()), // SEQUENCE_NAME
731
String.valueOf(s.getCurrentValue()), // CURRENT_VALUE
732
String.valueOf(s.getIncrement()), // INCREMENT
733
s.getBelongsToTable() ? "TRUE" : "FALSE", // IS_GENERATED
734
replaceNullWithEmpty(s.getComment()) // REMARKS
735
});
736             }
737             break;
738         }
739         case USERS: {
740             ObjectArray users = database.getAllUsers();
741             for(int i=0; i<users.size(); i++) {
742                 User u = (User) users.get(i);
743                 add(rows,new String JavaDoc[]{
744                         identifier(u.getName()), // NAME
745
String.valueOf(u.getAdmin()), // ADMIN
746
replaceNullWithEmpty(u.getComment()) // REMARKS
747
});
748             }
749             break;
750         }
751         case ROLES: {
752             ObjectArray roles = database.getAllRoles();
753             for(int i=0; i<roles.size(); i++) {
754                 Role r = (Role) roles.get(i);
755                 add(rows,new String JavaDoc[]{
756                         identifier(r.getName()), // NAME
757
replaceNullWithEmpty(r.getComment()) // REMARKS
758
});
759             }
760             break;
761         }
762         case RIGHTS: {
763             ObjectArray rights = database.getAllRights();
764             for(int i=0; i<rights.size(); i++) {
765                 Right r = (Right) rights.get(i);
766                 // "GRANTEE", "GRANTEETYPE", "GRANTEDROLE", "RIGHTS", "TABLE"
767
Role role = r.getGrantedRole();
768                 DbObject grantee = r.getGrantee();
769                 String JavaDoc type = grantee.getType() == DbObject.USER ? "USER" : "ROLE";
770                 if(role == null) {
771                     Table granted = r.getGrantedTable();
772                     String JavaDoc tableName = identifier(granted.getName());
773                     if(!checkIndex(session, tableName, indexFrom, indexTo)) {
774                         continue;
775                     }
776                     add(rows,new String JavaDoc[]{
777                             identifier(grantee.getName()),
778                             type,
779                             "",
780                             r.getRights(),
781                             identifier(granted.getSchema().getName()),
782                             identifier(granted.getName())
783                     });
784                 } else {
785                     add(rows,new String JavaDoc[]{
786                             identifier(grantee.getName()),
787                             type,
788                             identifier(role.getName()),
789                             "",
790                             "",
791                             ""
792                     });
793                 }
794             }
795             break;
796         }
797         case FUNCTION_ALIASES: {
798             ObjectArray aliases = database.getAllFunctionAliases();
799             for(int i=0; i<aliases.size(); i++) {
800                 FunctionAlias alias = (FunctionAlias) aliases.get(i);
801                 int returnsResult = alias.getDataType() == Value.NULL ? DatabaseMetaData.procedureNoResult : DatabaseMetaData.procedureReturnsResult;
802                 add(rows,new String JavaDoc[]{
803                         catalog, // ALIAS_CATALOG
804
Constants.SCHEMA_MAIN, // ALIAS_SCHEMA
805
identifier(alias.getName()), // ALIAS_NAME
806
alias.getJavaClassName(), // JAVA_CLASS
807
alias.getJavaMethodName(), // JAVA_METHOD
808
""+DataType.convertTypeToSQLType(alias.getDataType()), // DATA_TYPE
809
""+ alias.getColumnClasses().length, // COLUMN_COUNT INT
810
""+ returnsResult, // RETURNS_RESULT SMALLINT
811
replaceNullWithEmpty(alias.getComment()) // REMARKS
812
});
813             }
814             break;
815         }
816         case FUNCTION_COLUMNS: {
817             ObjectArray aliases = database.getAllFunctionAliases();
818             for(int i=0; i<aliases.size(); i++) {
819                 FunctionAlias alias = (FunctionAlias) aliases.get(i);
820                 Class JavaDoc[] columns = alias.getColumnClasses();
821                 for(int j=0; j<columns.length; j++) {
822                     Class JavaDoc clazz = columns[j];
823                     int type = DataType.getTypeFromClass(clazz);
824                     DataType dt = DataType.getDataType(type);
825                     int nullable = clazz.isPrimitive() ? DatabaseMetaData.columnNoNulls : DatabaseMetaData.columnNullable;
826                     add(rows,new String JavaDoc[]{
827                             catalog, // ALIAS_CATALOG
828
Constants.SCHEMA_MAIN, // ALIAS_SCHEMA
829
identifier(alias.getName()), // ALIAS_NAME
830
alias.getJavaClassName(), // JAVA_CLASS
831
alias.getJavaMethodName(), // JAVA_METHOD
832
"" + j, // POS INT
833
"P" + (j+1), // COLUMN_NAME
834
"" + DataType.convertTypeToSQLType(dt.type), // DATA_TYPE
835
dt.name, // TYPE_NAME
836
"" + dt.defaultPrecision, // PRECISION
837
"" + dt.defaultScale, // SCALE
838
"10", // RADIX
839
"" + nullable, // NULLABLE SMALLINT
840
"" + DatabaseMetaData.procedureColumnIn, // COLUMN_TYPE
841
"" // REMARKS
842
});
843                 }
844             }
845             break;
846         }
847         case SCHEMATA: {
848             ObjectArray schemas = database.getAllSchemas();
849             String JavaDoc collation = database.getCompareMode().getName();
850             for(int i=0; i<schemas.size(); i++) {
851                 Schema schema = (Schema) schemas.get(i);
852                 add(rows,new String JavaDoc[]{
853                         catalog, // CATALOG_NAME
854
identifier(schema.getName()), // SCHEMA_NAME
855
identifier(schema.getOwner().getName()), // SCHEMA_OWNER
856
Constants.CHARACTER_SET_NAME, // DEFAULT_CHARACTER_SET_NAME
857
collation, // DEFAULT_COLLATION_NAME
858
Constants.SCHEMA_MAIN.equals(schema.getName()) ? "TRUE" : "FALSE", // IS_DEFAULT
859
replaceNullWithEmpty(schema.getComment()) // REMARKS
860
});
861             }
862             break;
863         }
864         case TABLE_PRIVILEGES: {
865             ObjectArray rights = database.getAllRights();
866             for(int i=0; i<rights.size(); i++) {
867                 Right r = (Right) rights.get(i);
868                 Table table = r.getGrantedTable();
869                 if(table == null) {
870                     continue;
871                 }
872                 String JavaDoc tableName = identifier(table.getName());
873                 if(!checkIndex(session, tableName, indexFrom, indexTo)) {
874                     continue;
875                 }
876                 addPrivileges(rows, r.getGrantee(), catalog, table, null, r.getRightMask());
877             }
878             break;
879         }
880         case COLUMN_PRIVILEGES: {
881             ObjectArray rights = database.getAllRights();
882             for(int i=0; i<rights.size(); i++) {
883                 Right r = (Right) rights.get(i);
884                 Table table = r.getGrantedTable();
885                 if(table == null) {
886                     continue;
887                 }
888                 String JavaDoc tableName = identifier(table.getName());
889                 if(!checkIndex(session, tableName, indexFrom, indexTo)) {
890                     continue;
891                 }
892                 DbObject grantee = r.getGrantee();
893                 int mask = r.getRightMask();
894                 Column[] columns = table.getColumns();
895                 for(int j=0; j<columns.length; j++) {
896                     String JavaDoc column = columns[j].getName();
897                     addPrivileges(rows, grantee, catalog, table, column, mask);
898                 }
899             }
900             break;
901         }
902         case COLLATIONS: {
903             Locale JavaDoc[] locales = Collator.getAvailableLocales();
904             for(int i=0; i<locales.length; i++) {
905                 Locale JavaDoc l = locales[i];
906                 add(rows,new String JavaDoc[]{
907                         CompareMode.getName(l), // NAME
908
l.toString(), // KEY
909
});
910             }
911             break;
912         }
913         case VIEWS: {
914             ObjectArray tables = getAllTables(session);
915             for(int i=0; i<tables.size(); i++) {
916                 Table table = (Table) tables.get(i);
917                 if(!table.getTableType().equals(Table.VIEW)) {
918                     continue;
919                 }
920                 String JavaDoc tableName = identifier(table.getName());
921                 if(!checkIndex(session, tableName, indexFrom, indexTo)) {
922                     continue;
923                 }
924                 TableView view = (TableView)table;
925                 add(rows, new String JavaDoc[]{
926                         catalog, // TABLE_CATALOG
927
identifier(table.getSchema().getName()), // TABLE_SCHEMA
928
tableName, // TABLE_NAME
929
table.getCreateSQL(), // VIEW_DEFINITION
930
"NONE", // CHECK_OPTION
931
"NO", // IS_UPDATABLE
932
view.getInvalid() ? "INVALID" : "VALID", // STATUS
933
replaceNullWithEmpty(view.getComment()) // REMARKS
934
});
935             }
936             break;
937         }
938         case IN_DOUBT: {
939             ObjectArray prepared = database.getLog().getInDoubtTransactions();
940             for(int i=0; prepared != null && i<prepared.size(); i++) {
941                 InDoubtTransaction prep = (InDoubtTransaction) prepared.get(i);
942                 add(rows, new String JavaDoc[] {
943                         prep.getTransaction(), // TRANSACTION
944
prep.getState(), // STATE
945
});
946             }
947             break;
948         }
949         case CROSS_REFERENCES: {
950             ObjectArray constraints = database.getAllSchemaObjects(DbObject.CONSTRAINT);
951             for(int i=0; i<constraints.size(); i++) {
952                 Constraint constraint = (Constraint) constraints.get(i);
953                 if(!(constraint.getConstraintType().equals(Constraint.REFERENTIAL))) {
954                     continue;
955                 }
956                 ConstraintReferential ref = (ConstraintReferential) constraint;
957                 Column[] cols = ref.getColumns();
958                 Column[] refCols = ref.getRefColumns();
959                 Table tab = ref.getTable();
960                 Table refTab = ref.getRefTable();
961                 String JavaDoc tableName = identifier(refTab.getName());
962                 if(!checkIndex(session, tableName, indexFrom, indexTo)) {
963                     continue;
964                 }
965                 int upd = getRefAction(ref.getUpdateAction());
966                 int del = getRefAction(ref.getDeleteAction());
967                 for(int j=0; j<cols.length; j++) {
968                     add(rows, new String JavaDoc[] {
969                             catalog, // PKTABLE_CATALOG
970
identifier(refTab.getSchema().getName()), // PKTABLE_SCHEMA
971
identifier(refTab.getName()), // PKTABLE_NAME
972
identifier(refCols[j].getName()), // PKCOLUMN_NAME
973
catalog, // FKTABLE_CATALOG
974
identifier(tab.getSchema().getName()), // FKTABLE_SCHEMA
975
identifier(tab.getName()), // FKTABLE_NAME
976
identifier(cols[j].getName()), // FKCOLUMN_NAME
977
String.valueOf(j + 1), // ORDINAL_POSITION
978
String.valueOf(upd), // UPDATE_RULE SMALLINT
979
String.valueOf(del), // DELETE_RULE SMALLINT
980
identifier(ref.getName()), // FK_NAME
981
null, // PK_NAME
982
"" + DatabaseMetaData.importedKeyNotDeferrable, // DEFERRABILITY
983
});
984                 }
985             }
986             break;
987         }
988         case CONSTRAINTS: {
989             ObjectArray constraints = database.getAllSchemaObjects(DbObject.CONSTRAINT);
990             for(int i=0; i<constraints.size(); i++) {
991                 Constraint constraint = (Constraint) constraints.get(i);
992                 String JavaDoc type = constraint.getConstraintType();
993                 String JavaDoc checkExpression = null;
994                 Column[] columns = null;
995                 Table table = constraint.getTable();
996                 String JavaDoc tableName = identifier(table.getName());
997                 if(!checkIndex(session, tableName, indexFrom, indexTo)) {
998                     continue;
999                 }
1000                if(type.equals(Constraint.CHECK)) {
1001                    checkExpression = ((ConstraintCheck)constraint).getExpression().getSQL();
1002                } else if(type.equals(Constraint.UNIQUE)) {
1003                    columns = ((ConstraintUnique)constraint).getColumns();
1004                } else if(type.equals(Constraint.REFERENTIAL)) {
1005                    columns = ((ConstraintReferential)constraint).getColumns();
1006                }
1007                String JavaDoc columnList = null;
1008                if(columns != null) {
1009                    columnList = "";
1010                    for(int j=0; j<columns.length; j++) {
1011                        if(j>0) {
1012                            columnList += ",";
1013                        }
1014                        columnList += columns[j].getName();
1015                    }
1016                }
1017                add(rows, new String JavaDoc[] {
1018                        catalog, // CONSTRAINT_CATALOG
1019
identifier(constraint.getSchema().getName()), // CONSTRAINT_SCHEMA
1020
identifier(constraint.getName()), // CONSTRAINT_NAME
1021
type, // CONSTRAINT_TYPE
1022
catalog, // TABLE_CATALOG
1023
identifier(table.getSchema().getName()), // TABLE_SCHEMA
1024
tableName, // TABLE_NAME
1025
checkExpression, // CHECK_EXPRESSION
1026
columnList, // COLUMN_LIST
1027
replaceNullWithEmpty(constraint.getComment()), // REMARKS
1028
constraint.getCreateSQL(), // SQL
1029
});
1030            }
1031            break;
1032        }
1033        case CONSTANTS: {
1034            ObjectArray constants = database.getAllSchemaObjects(DbObject.CONSTANT);
1035            for(int i=0; i<constants.size(); i++) {
1036                Constant constant = (Constant) constants.get(i);
1037                ValueExpression expr = constant.getValue();
1038                add(rows, new String JavaDoc[] {
1039                        catalog, // CONSTANT_CATALOG
1040
identifier(constant.getSchema().getName()), // CONSTANT_SCHEMA
1041
identifier(constant.getName()), // CONSTANT_NAME
1042
"" + DataType.convertTypeToSQLType(expr.getType()), // CONSTANT_TYPE
1043
replaceNullWithEmpty(constant.getComment()), // REMARKS
1044
expr.getSQL(), // SQL
1045
});
1046            }
1047            break;
1048        }
1049        case DOMAINS: {
1050            ObjectArray userDataTypes = database.getAllUserDataTypes();
1051            for(int i=0; i<userDataTypes.size(); i++) {
1052                UserDataType dt = (UserDataType) userDataTypes.get(i);
1053                Column col = dt.getColumn();
1054                add(rows, new String JavaDoc[] {
1055                        catalog, // DOMAIN_CATALOG
1056
Constants.SCHEMA_MAIN, // DOMAIN_SCHEMA
1057
identifier(dt.getName()), // DOMAIN_NAME
1058
col.getDefaultSQL(), // COLUMN_DEFAULT
1059
col.getNullable() ? "YES" : "NO", // IS_NULLABLE
1060
"" + col.getDataType().sqlType, // DATA_TYPE
1061
"" + col.getPrecisionAsInt(), // PRECISION INT
1062
"" + col.getScale(), // SCALE INT
1063
col.getDataType().name, // TYPE_NAME
1064
"" + col.getSelectivity(), // SELECTIVITY INT
1065
"" + col.getCheckConstraintSQL(session, "VALUE"), // CHECK_CONSTRAINT
1066
replaceNullWithEmpty(dt.getComment()), // REMARKS
1067
"" + dt.getCreateSQL() // SQL
1068
});
1069            }
1070            break;
1071        }
1072        case TRIGGERS: {
1073            ObjectArray triggers = database.getAllSchemaObjects(DbObject.TRIGGER);
1074            for(int i=0; i<triggers.size(); i++) {
1075                TriggerObject trigger = (TriggerObject) triggers.get(i);
1076                Table table = trigger.getTable();
1077                add(rows, new String JavaDoc[] {
1078                        catalog, // TRIGGER_CATALOG
1079
identifier(trigger.getSchema().getName()), // TRIGGER_SCHEMA
1080
identifier(trigger.getName()), // TRIGGER_NAME
1081
trigger.getTypeNameList(), // TRIGGER_TYPE
1082
catalog, // TABLE_CATALOG
1083
identifier(table.getSchema().getName()), // TABLE_SCHEMA
1084
identifier(table.getName()), // TABLE_NAME
1085
"" + trigger.getBefore(), // BEFORE BIT
1086
trigger.getTriggerClassName(), // JAVA_CLASS
1087
"" + trigger.getQueueSize(), // QUEUE_SIZE INT
1088
"" + trigger.getNoWait(), // NO_WAIT BIT
1089
replaceNullWithEmpty(trigger.getComment()), // REMARKS
1090
trigger.getSQL() // SQL
1091
});
1092            }
1093            break;
1094        }
1095        default:
1096            throw Message.getInternalError("type="+type);
1097        }
1098        return rows;
1099    }
1100
1101    private int getRefAction(int action) {
1102        switch(action) {
1103        case ConstraintReferential.CASCADE:
1104            return DatabaseMetaData.importedKeyCascade;
1105        case ConstraintReferential.RESTRICT:
1106            return DatabaseMetaData.importedKeyRestrict;
1107        case ConstraintReferential.SET_DEFAULT:
1108            return DatabaseMetaData.importedKeySetDefault;
1109        case ConstraintReferential.SET_NULL:
1110            return DatabaseMetaData.importedKeySetNull;
1111        default:
1112            throw Message.getInternalError("action="+action);
1113        }
1114    }
1115
1116    public void removeRow(Session session, Row row) throws SQLException JavaDoc {
1117        throw Message.getUnsupportedException();
1118    }
1119
1120    public void addRow(Session session, Row row) throws SQLException JavaDoc {
1121        throw Message.getUnsupportedException();
1122    }
1123
1124    public void removeIndex(String JavaDoc indexName) throws SQLException JavaDoc {
1125        throw Message.getUnsupportedException();
1126    }
1127
1128    public void removeChildrenAndResources(Session session) throws SQLException JavaDoc {
1129        throw Message.getUnsupportedException();
1130    }
1131
1132    public void close(Session session) throws SQLException JavaDoc {
1133        // nothing to do
1134
}
1135
1136    public void unlock(Session s) {
1137        // nothing to do
1138
}
1139
1140    private void addPrivileges(ObjectArray rows, DbObject grantee, String JavaDoc catalog, Table table, String JavaDoc column, int rightMask) throws SQLException JavaDoc {
1141        if((rightMask & Right.SELECT) != 0) {
1142            addPrivileg(rows, grantee, catalog, table, column, "SELECT");
1143        }
1144        if((rightMask & Right.INSERT) != 0) {
1145            addPrivileg(rows, grantee, catalog, table, column, "INSERT");
1146        }
1147        if((rightMask & Right.UPDATE) != 0) {
1148            addPrivileg(rows, grantee, catalog, table, column, "UPDATE");
1149        }
1150        if((rightMask & Right.DELETE) != 0) {
1151            addPrivileg(rows, grantee, catalog, table, column, "DELETE");
1152        }
1153    }
1154
1155    private void addPrivileg(ObjectArray rows, DbObject grantee, String JavaDoc catalog, Table table, String JavaDoc column, String JavaDoc right) throws SQLException JavaDoc {
1156        String JavaDoc isGrantable = "NO";
1157        if(grantee.getType() == DbObject.USER) {
1158            User user = (User)grantee;
1159            if(user.getAdmin()) {
1160                // the right is grantable if the grantee is an admin
1161
isGrantable = "YES";
1162            }
1163        }
1164        if(column == null) {
1165            add(rows,new String JavaDoc[]{
1166                    null, // GRANTOR
1167
identifier(grantee.getName()), // GRANTEE
1168
catalog, // TABLE_CATALOG
1169
identifier(table.getSchema().getName()), // TABLE_SCHEMA
1170
identifier(table.getName()), // TABLE_NAME
1171
right, // PRIVILEGE_TYPE
1172
isGrantable // IS_GRANTABLE
1173
});
1174        } else {
1175            add(rows,new String JavaDoc[]{
1176                    null, // GRANTOR
1177
identifier(grantee.getName()), // GRANTEE
1178
catalog, // TABLE_CATALOG
1179
identifier(table.getSchema().getName()), // TABLE_SCHEMA
1180
identifier(table.getName()), // TABLE_NAME
1181
identifier(column), // COLUMN_NAME
1182
right, // PRIVILEGE_TYPE
1183
isGrantable // IS_GRANTABLE
1184
});
1185        }
1186    }
1187
1188    private void add(ObjectArray rows, String JavaDoc[] strings) throws SQLException JavaDoc {
1189        Value[] values = new Value[strings.length];
1190        for(int i=0; i<strings.length; i++) {
1191            String JavaDoc s = strings[i];
1192            Value v = (s == null) ? (Value)ValueNull.INSTANCE : ValueString.get(s);
1193            Column col = columns[i];
1194            v = v.convertTo(col.getType());
1195            values[i] = v;
1196        }
1197        Row row = new Row(values);
1198        row.setPos(rows.size());
1199        rows.add(row);
1200    }
1201
1202    public void checkRename() throws SQLException JavaDoc {
1203        throw Message.getUnsupportedException();
1204    }
1205
1206    public void checkSupportAlter() throws SQLException JavaDoc {
1207        throw Message.getUnsupportedException();
1208    }
1209
1210    public void truncate(Session session) throws SQLException JavaDoc {
1211        throw Message.getUnsupportedException();
1212    }
1213
1214    public int getRowCount() {
1215        throw Message.getInternalError();
1216    }
1217
1218    public boolean canGetRowCount() {
1219        return false;
1220    }
1221
1222    public boolean canDrop() {
1223        return false;
1224    }
1225
1226    public String JavaDoc getTableType() {
1227        return Table.SYSTEM_TABLE;
1228    }
1229
1230    public Index getScanIndex(Session session) throws SQLException JavaDoc {
1231        return new MetaIndex(this, columns, true);
1232    }
1233
1234    public ObjectArray getIndexes() {
1235        if(index == null) {
1236            return null;
1237        }
1238        ObjectArray list = new ObjectArray();
1239        list.add(new MetaIndex(this, columns, true));
1240        // TODO fixed scan index
1241
list.add(index);
1242        return list;
1243    }
1244
1245    public long getMaxDataModificationId() {
1246        return database.getModificationDataId();
1247    }
1248
1249    public Index getUniqueIndex() {
1250        return null;
1251    }
1252
1253}
1254
Popular Tags