DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SystemSchema.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 
21 using Deveel.Data.Security;
22 using Deveel.Data.Sql;
23 using Deveel.Data.Sql.Objects;
24 using Deveel.Data.Sql.Tables;
26 using Deveel.Data.Types;
27 using Deveel.Data.Util;
28 
29 namespace Deveel.Data {
37  public static class SystemSchema {
38  static SystemSchema() {
39  // SYSTEM.TABLE_INFO
40  TableInfoTableInfo = new TableInfo(TableInfoTableName);
41  TableInfoTableInfo.AddColumn("catalog", PrimitiveTypes.String());
42  TableInfoTableInfo.AddColumn("schema", PrimitiveTypes.String());
43  TableInfoTableInfo.AddColumn("name", PrimitiveTypes.String());
44  TableInfoTableInfo.AddColumn("type", PrimitiveTypes.String());
45  TableInfoTableInfo.AddColumn("other", PrimitiveTypes.String());
46  TableInfoTableInfo = TableInfoTableInfo.AsReadOnly();
47 
48  // SYSTEM.TABLE_COLUMNS
49  TableColumnsTableInfo = new TableInfo(TableColumnsTableName);
50  TableColumnsTableInfo.AddColumn("schema", PrimitiveTypes.String());
51  TableColumnsTableInfo.AddColumn("table", PrimitiveTypes.String());
52  TableColumnsTableInfo.AddColumn("column", PrimitiveTypes.String());
53  TableColumnsTableInfo.AddColumn("sql_type", PrimitiveTypes.Numeric());
54  TableColumnsTableInfo.AddColumn("type_desc", PrimitiveTypes.String());
55  TableColumnsTableInfo.AddColumn("size", PrimitiveTypes.Numeric());
56  TableColumnsTableInfo.AddColumn("scale", PrimitiveTypes.Numeric());
57  TableColumnsTableInfo.AddColumn("not_null", PrimitiveTypes.Boolean());
58  TableColumnsTableInfo.AddColumn("default", PrimitiveTypes.String());
59  TableColumnsTableInfo.AddColumn("index_str", PrimitiveTypes.String());
60  TableColumnsTableInfo.AddColumn("seq_no", PrimitiveTypes.Numeric());
61  TableColumnsTableInfo = TableColumnsTableInfo.AsReadOnly();
62 
63  // SYSTEM.VARIABLES
64  VariablesTableInfo = new TableInfo(VariablesTableName);
65  VariablesTableInfo.AddColumn("var", PrimitiveTypes.String());
66  VariablesTableInfo.AddColumn("type", PrimitiveTypes.String());
67  VariablesTableInfo.AddColumn("value", PrimitiveTypes.String());
68  VariablesTableInfo.AddColumn("constant", PrimitiveTypes.Boolean());
69  VariablesTableInfo.AddColumn("not_null", PrimitiveTypes.Boolean());
70  VariablesTableInfo.AddColumn("is_set", PrimitiveTypes.Boolean());
71  VariablesTableInfo = VariablesTableInfo.AsReadOnly();
72 
73  // SYSTEM.PRODUCT_INFO
74  ProductInfoTableInfo = new TableInfo(ProductInfoTableName);
75  ProductInfoTableInfo.AddColumn("var", PrimitiveTypes.String());
76  ProductInfoTableInfo.AddColumn("value", PrimitiveTypes.String());
77  ProductInfoTableInfo = ProductInfoTableInfo.AsReadOnly();
78 
79  // SYSTEM.STATS
80  StatisticsTableInfo = new TableInfo(StatisticsTableName);
81  StatisticsTableInfo.AddColumn("stat_name", PrimitiveTypes.String());
82  StatisticsTableInfo.AddColumn("value", PrimitiveTypes.String());
83  StatisticsTableInfo = StatisticsTableInfo.AsReadOnly();
84 
85  // SYSTEM.SQL_TYPES
86  SqlTypesTableInfo = new TableInfo(SqlTypesTableName);
87  SqlTypesTableInfo.AddColumn("TYPE_NAME", PrimitiveTypes.String());
88  SqlTypesTableInfo.AddColumn("DATA_TYPE", PrimitiveTypes.Numeric());
89  SqlTypesTableInfo.AddColumn("PRECISION", PrimitiveTypes.Numeric());
90  SqlTypesTableInfo.AddColumn("LITERAL_PREFIX", PrimitiveTypes.String());
91  SqlTypesTableInfo.AddColumn("LITERAL_SUFFIX", PrimitiveTypes.String());
92  SqlTypesTableInfo.AddColumn("CREATE_PARAMS", PrimitiveTypes.String());
93  SqlTypesTableInfo.AddColumn("NULLABLE", PrimitiveTypes.Numeric());
94  SqlTypesTableInfo.AddColumn("CASE_SENSITIVE", PrimitiveTypes.Boolean());
95  SqlTypesTableInfo.AddColumn("SEARCHABLE", PrimitiveTypes.Numeric());
96  SqlTypesTableInfo.AddColumn("UNSIGNED_ATTRIBUTE", PrimitiveTypes.Boolean());
97  SqlTypesTableInfo.AddColumn("FIXED_PREC_SCALE", PrimitiveTypes.Boolean());
98  SqlTypesTableInfo.AddColumn("AUTO_INCREMENT", PrimitiveTypes.Boolean());
99  SqlTypesTableInfo.AddColumn("LOCAL_TYPE_NAME", PrimitiveTypes.String());
100  SqlTypesTableInfo.AddColumn("MINIMUM_SCALE", PrimitiveTypes.Numeric());
101  SqlTypesTableInfo.AddColumn("MAXIMUM_SCALE", PrimitiveTypes.Numeric());
102  SqlTypesTableInfo.AddColumn("SQL_DATA_TYPE", PrimitiveTypes.String());
103  SqlTypesTableInfo.AddColumn("SQL_DATETIME_SUB", PrimitiveTypes.String());
104  SqlTypesTableInfo.AddColumn("NUM_PREC_RADIX", PrimitiveTypes.Numeric());
105  SqlTypesTableInfo = SqlTypesTableInfo.AsReadOnly();
106 
107  // SYSTEM.OPEN_SESSIONS
108  OpenSessionsTableInfo = new TableInfo(OpenSessionsTableName);
109  OpenSessionsTableInfo.AddColumn("username", PrimitiveTypes.String());
110  OpenSessionsTableInfo.AddColumn("host_string", PrimitiveTypes.String());
111  OpenSessionsTableInfo.AddColumn("last_command", PrimitiveTypes.DateTime());
112  OpenSessionsTableInfo.AddColumn("time_connected", PrimitiveTypes.DateTime());
113  OpenSessionsTableInfo = OpenSessionsTableInfo.AsReadOnly();
114 
115  // SYSTEM.SESSION_INFO
116  SessionInfoTableInfo = new TableInfo(SessionInfoTableName);
117  SessionInfoTableInfo.AddColumn("var", PrimitiveTypes.String());
118  SessionInfoTableInfo.AddColumn("value", PrimitiveTypes.String());
119  SessionInfoTableInfo = SessionInfoTableInfo.AsReadOnly();
120 
121  // SYSTEM.PRIVS
122  PrivilegesTableInfo = new TableInfo(PrivilegesTableName);
123  PrivilegesTableInfo.AddColumn("priv_bit", PrimitiveTypes.Numeric());
124  PrivilegesTableInfo.AddColumn("description", PrimitiveTypes.String());
125  PrivilegesTableInfo = PrivilegesTableInfo.AsReadOnly();
126  }
127 
132  public const string Name = "SYSTEM";
133 
137  public static readonly ObjectName SchemaName = new ObjectName(Name);
138 
139  #region Table Names
140 
141  public static readonly ObjectName SchemaInfoTableName = new ObjectName(SchemaName, "schema_info");
142 
143  public static readonly ObjectName TableInfoTableName = new ObjectName(SchemaName, "table_info");
144 
145  public static readonly ObjectName TableColumnsTableName = new ObjectName(SchemaName, "table_cols");
146 
147  public static readonly ObjectName ViewTableName = new ObjectName(SchemaName, "view");
148 
149  public static readonly ObjectName RoutineTableName = new ObjectName(SchemaName, "routine");
150 
151  public static readonly ObjectName TriggerTableName = new ObjectName(SchemaName, "trigger");
152 
153  public static readonly ObjectName RoutineParameterTableName = new ObjectName(SchemaName, "routine_params");
154 
155  public static readonly ObjectName VariablesTableName = new ObjectName(SchemaName, "vars");
156 
157  public static readonly ObjectName ProductInfoTableName = new ObjectName(SchemaName, "product_info");
158 
159  public static readonly ObjectName StatisticsTableName = new ObjectName(SchemaName, "stats");
160 
164  public static readonly ObjectName SequenceInfoTableName = new ObjectName(SchemaName, "sequence_info");
165 
168  public static readonly ObjectName SequenceTableName = new ObjectName(SchemaName, "sequence");
169 
170  public static readonly ObjectName PrimaryKeyInfoTableName = new ObjectName(SchemaName, "pkey_info");
171 
172  public static readonly ObjectName PrimaryKeyColumnsTableName = new ObjectName(SchemaName, "pkey_cols");
173 
174  public static readonly ObjectName ForeignKeyInfoTableName = new ObjectName(SchemaName, "fkey_info");
175 
176  public static readonly ObjectName ForeignKeyColumnsTableName = new ObjectName(SchemaName, "fkey_cols");
177 
178  public static readonly ObjectName UniqueKeyInfoTableName = new ObjectName(SchemaName, "unique_info");
179 
180  public static readonly ObjectName UniqueKeyColumnsTableName = new ObjectName(SchemaName, "unique_cols");
181 
182  public static readonly ObjectName CheckInfoTableName = new ObjectName(SchemaName, "check_info");
183 
184  public static readonly ObjectName OldTriggerTableName = new ObjectName(SchemaName, "OLD");
185 
186  public static readonly ObjectName NewTriggerTableName = new ObjectName(SchemaName, "NEW");
187 
188  public static readonly ObjectName SqlTypesTableName = new ObjectName(SchemaName, "sql_types");
189 
190  public static readonly ObjectName SessionInfoTableName = new ObjectName(SchemaName, "session_info");
191 
192  public static readonly ObjectName OpenSessionsTableName = new ObjectName(SchemaName, "open_sessions");
193 
194  public static readonly ObjectName PrivilegesTableName = new ObjectName(SchemaName, "privs");
195 
196 
200  public static readonly ObjectName UserTableName = new ObjectName(SchemaName, "user");
201 
202  public static readonly ObjectName PasswordTableName = new ObjectName(SchemaName, "password");
203 
204  //public static readonly ObjectName UserConnectPrivilegesTableName = new ObjectName(SchemaName, "user_connect_priv");
205 
206  public static readonly ObjectName GroupsTableName = new ObjectName(SchemaName, "group");
207 
208  public static readonly ObjectName UserGroupTableName = new ObjectName(SchemaName, "user_group");
209 
210  public static readonly ObjectName UserGrantsTableName = new ObjectName(SchemaName, "grants");
211 
212  public static readonly ObjectName GroupGrantsTable = new ObjectName(SchemaName, "group_grants");
213 
214  #endregion
215 
216  #region Table Info
217 
218  internal static readonly TableInfo TableInfoTableInfo;
219 
220  internal static readonly TableInfo TableColumnsTableInfo;
221 
222  internal static readonly TableInfo SqlTypesTableInfo;
223 
224  internal static readonly TableInfo VariablesTableInfo;
225 
226  internal static readonly TableInfo ProductInfoTableInfo;
227 
228  internal static readonly TableInfo StatisticsTableInfo;
229 
230  internal static readonly TableInfo SessionInfoTableInfo;
231 
232  internal static readonly TableInfo OpenSessionsTableInfo;
233 
234  internal static readonly TableInfo PrivilegesTableInfo;
235 
236  #endregion
237 
238  private static void CreateSecurityTables(IQuery context) {
239  var tableInfo = new TableInfo(UserTableName);
240  tableInfo.AddColumn("name", PrimitiveTypes.String());
241  // TODO: User table must be completed ...
242  tableInfo = tableInfo.AsReadOnly();
243  context.CreateSystemTable(tableInfo);
244 
245  context.AddPrimaryKey(UserTableName, new []{"name"}, "SYSTEM_USER_PK");
246 
247  tableInfo = new TableInfo(PasswordTableName);
248  tableInfo.AddColumn("user", PrimitiveTypes.String());
249  tableInfo.AddColumn("method", PrimitiveTypes.String());
250  tableInfo.AddColumn("method_args", PrimitiveTypes.Binary());
251  tableInfo.AddColumn("identifier", PrimitiveTypes.String());
252  tableInfo = tableInfo.AsReadOnly();
253  context.CreateSystemTable(tableInfo);
254 
255  tableInfo = new TableInfo(UserGroupTableName);
256  tableInfo.AddColumn("user", PrimitiveTypes.String());
257  tableInfo.AddColumn("group", PrimitiveTypes.String());
258  tableInfo.AddColumn("admin", PrimitiveTypes.Boolean());
259  tableInfo = tableInfo.AsReadOnly();
260  context.CreateSystemTable(tableInfo);
261 
262  tableInfo = new TableInfo(GroupsTableName);
263  tableInfo.AddColumn("name", PrimitiveTypes.String(), true);
264  tableInfo = tableInfo.AsReadOnly();
265  context.CreateSystemTable(tableInfo);
266 
267  context.AddPrimaryKey(GroupsTableName, new[] { "name" }, "SYSTEM_GROUP_PK");
268 
269  tableInfo = new TableInfo(UserGrantsTableName);
270  tableInfo.AddColumn("priv_bit", PrimitiveTypes.Numeric());
271  tableInfo.AddColumn("object", PrimitiveTypes.Numeric());
272  tableInfo.AddColumn("name", PrimitiveTypes.String());
273  tableInfo.AddColumn("user", PrimitiveTypes.String());
274  tableInfo.AddColumn("grant_option", PrimitiveTypes.Boolean());
275  tableInfo.AddColumn("granter", PrimitiveTypes.String());
276  tableInfo = tableInfo.AsReadOnly();
277  context.CreateSystemTable(tableInfo);
278 
279  tableInfo = new TableInfo(GroupGrantsTable);
280  tableInfo.AddColumn("priv_bit", PrimitiveTypes.Numeric());
281  tableInfo.AddColumn("object", PrimitiveTypes.Numeric());
282  tableInfo.AddColumn("name", PrimitiveTypes.String());
283  tableInfo.AddColumn("group", PrimitiveTypes.String());
284  tableInfo.AddColumn("grant_option", PrimitiveTypes.Boolean());
285  tableInfo.AddColumn("granter", PrimitiveTypes.String());
286 
287  var fkCol = new[] {"user"};
288  var gfkCol = new[] {"group"};
289  var refCol = new[] {"name"};
290  const ForeignKeyAction onUpdate = ForeignKeyAction.NoAction;
291  const ForeignKeyAction onDelete = ForeignKeyAction.Cascade;
292  context.AddForeignKey(PasswordTableName, fkCol, UserTableName, refCol, onDelete, onUpdate, "USER_PASSWORD_FK");
293  context.AddForeignKey(UserGroupTableName, fkCol, UserTableName, refCol, onDelete, onUpdate, "USER_PRIV_FK");
294  context.AddForeignKey(UserGroupTableName, gfkCol, GroupsTableName, refCol, onDelete, onUpdate, "USER_GROUP_FK");
295  context.AddForeignKey(UserGrantsTableName, fkCol, UserTableName, refCol, onDelete, onUpdate, "USER_GRANTS_FK");
296  context.AddForeignKey(GroupGrantsTable, gfkCol, GroupsTableName, refCol, onDelete, onUpdate, "GROUP_GRANTS_FK");
297  }
298 
299  /*
300  private static void CreateRoutineTables(IQueryContext context) {
301  var tableInfo = new TableInfo(RoutineTableName);
302  tableInfo.AddColumn("schema", PrimitiveTypes.String());
303  tableInfo.AddColumn("name", PrimitiveTypes.String());
304  tableInfo.AddColumn("type", PrimitiveTypes.Numeric());
305  tableInfo.AddColumn("return_type", PrimitiveTypes.String());
306  tableInfo.AddColumn("body", PrimitiveTypes.Binary());
307  tableInfo = tableInfo.AsReadOnly();
308  context.CreateTable(tableInfo);
309 
310  tableInfo = new TableInfo(RoutineParameterTableName);
311  tableInfo.AddColumn("routine_schema", PrimitiveTypes.String());
312  tableInfo.AddColumn("routine_name", PrimitiveTypes.String());
313  tableInfo.AddColumn("name", PrimitiveTypes.String());
314  tableInfo.AddColumn("type", PrimitiveTypes.String());
315  tableInfo.AddColumn("flags", PrimitiveTypes.Numeric());
316  tableInfo.AddColumn("default", PrimitiveTypes.String());
317  tableInfo = tableInfo.AsReadOnly();
318  context.CreateTable(tableInfo);
319 
320  var fkCol = new[] { "routine_schema", "routine_name" };
321  var refCol = new[] { "schema", "name" };
322  const ForeignKeyAction onUpdate = ForeignKeyAction.NoAction;
323  const ForeignKeyAction onDelete = ForeignKeyAction.Cascade;
324 
325  context.AddForeignKey(RoutineParameterTableName, fkCol, RoutineTableName, refCol, onDelete, onUpdate, "ROUTINE_PARAMS_FK");
326  }
327  */
328 
329  public static void CreateTables(IQuery context) {
330  CreateSecurityTables(context);
331  //CreateRoutineTables(context);
332  }
333 
334  public static void GrantToPublic(IQuery context) {
335  context.GrantToUserOnTable(ProductInfoTableName, User.PublicName, Privileges.TableRead);
336  context.GrantToUserOnTable(SqlTypesTableName, User.PublicName, Privileges.TableRead);
337  context.GrantToUserOnTable(PrivilegesTableName, User.PublicName, Privileges.TableRead);
338  context.GrantToUserOnTable(StatisticsTableName, User.PublicName, Privileges.TableRead);
339  context.GrantToUserOnTable(VariablesTableName, User.PublicName, Privileges.TableRead);
340  context.GrantToUserOnTable(RoutineTableName, User.PublicName, Privileges.TableRead);
341  context.GrantToUserOnTable(RoutineParameterTableName, User.PublicName, Privileges.TableRead);
342  context.GrantToUserOnTable(SessionInfoTableName, User.PublicName, Privileges.TableRead);
343  }
344 
345  public static void Setup(ITransaction transaction) {
346  // -- Primary Keys --
347  // The 'id' columns are primary keys on all the system tables,
348  var idCol = new[] { "id" };
349  transaction.AddPrimaryKey(PrimaryKeyInfoTableName, idCol, "SYSTEM_PK_PK");
350  transaction.AddPrimaryKey(ForeignKeyInfoTableName, idCol, "SYSTEM_FK_PK");
351  transaction.AddPrimaryKey(UniqueKeyInfoTableName, idCol, "SYSTEM_UNIQUE_PK");
352  transaction.AddPrimaryKey(CheckInfoTableName, idCol, "SYSTEM_CHECK_PK");
353  transaction.AddPrimaryKey(SchemaInfoTableName, idCol, "SYSTEM_SCHEMA_PK");
354 
355  // -- Foreign Keys --
356  // Create the foreign key references,
357  var fkCol = new string[1];
358  var fkRefCol = new[] { "id" };
359 
360  fkCol[0] = "pk_id";
361  transaction.AddForeignKey(PrimaryKeyColumnsTableName, fkCol, PrimaryKeyInfoTableName, fkRefCol, "SYSTEM_PK_FK");
362 
363  fkCol[0] = "fk_id";
364  transaction.AddForeignKey(ForeignKeyColumnsTableName, fkCol, ForeignKeyInfoTableName, fkRefCol, "SYSTEM_FK_FK");
365 
366  fkCol[0] = "un_id";
367  transaction.AddForeignKey(UniqueKeyColumnsTableName, fkCol, UniqueKeyInfoTableName, fkRefCol, "SYSTEM_UNIQUE_FK");
368 
369  // pkey_info 'schema', 'table' column is a unique set,
370  // (You are only allowed one primary key per table).
371  var columns = new[] { "schema", "table" };
372  transaction.AddUniqueKey(PrimaryKeyInfoTableName, columns, "SYSTEM_PKEY_ST_UNIQUE");
373 
374  // schema_info 'name' column is a unique column,
375  columns = new String[] { "name" };
376  transaction.AddUniqueKey(SchemaInfoTableName, columns, "SYSTEM_SCHEMA_UNIQUE");
377 
378  // columns = new String[] { "name" };
379  columns = new String[] { "name", "schema" };
380  // pkey_info 'name' column is a unique column,
381  transaction.AddUniqueKey(PrimaryKeyInfoTableName, columns, "SYSTEM_PKEY_UNIQUE");
382 
383  // fkey_info 'name' column is a unique column,
384  transaction.AddUniqueKey(ForeignKeyInfoTableName, columns, "SYSTEM_FKEY_UNIQUE");
385 
386  // unique_info 'name' column is a unique column,
387  transaction.AddUniqueKey(UniqueKeyInfoTableName, columns, "SYSTEM_UNIQUE_UNIQUE");
388 
389  // check_info 'name' column is a unique column,
390  transaction.AddUniqueKey(CheckInfoTableName, columns, "SYSTEM_CHECK_UNIQUE");
391  }
392 
393  public static ITable GetTableInfoTable(ITransaction transaction) {
394  return new TableInfoTable(transaction);
395  }
396 
397  public static ITable GetTableColumnsTable(ITransaction transaction) {
398  return new TableColumnsTable(transaction);
399  }
400 
401  public static ITable GetSqlTypesTable(ITransaction transaction) {
402  return new SqlTypesTable(transaction);
403  }
404 
405  public static ITable GetProductInfoTable(ITransaction transaction) {
406  return new ProductInfoTable(transaction);
407  }
408 
409  public static ITable GetOpenSessionsTable(ITransaction transaction) {
410  return new OpenSessionsTable(transaction);
411  }
412 
413  public static ITable GetVariablesTable(ITransaction transaction) {
414  return new VariablesTable(transaction);
415  }
416 
417  public static ITable GetPrivilegesTable(ITransaction transaction) {
418  return new PrivilegesTable(transaction);
419  }
420 
421  public static ITable GetSessionInfoTable(IQuery context) {
422  return new SessionInfoTable(context.Session);
423  }
424 
425  public static ITable GetStatisticsTable(ITransaction transaction) {
426  return new StatisticsTable(transaction);
427  }
428 
429  #region TableInfoTable
430 
432  private List<TableInfoObject> tableInfoObjects;
433  private int rowCount;
434 
435  public TableInfoTable(ITransaction transaction)
436  : base(transaction.Database.Context) {
437  Transaction = transaction;
438  tableInfoObjects = new List<TableInfoObject>();
439 
440  Init();
441  }
442 
443  public ITransaction Transaction { get; private set; }
444 
445  public override TableInfo TableInfo {
446  get { return TableInfoTableInfo; }
447  }
448 
449  public override int RowCount {
450  get { return rowCount; }
451  }
452 
453  private void Init() {
454  // All the tables
455  var manager = Transaction.GetTableManager();
456  var tableNames = manager.GetTableNames();
457 
458  var tableList = tableNames.ToArray();
459  Array.Sort(tableList);
460  rowCount = tableList.Length;
461 
462  foreach (var tableName in tableList) {
463  string curType = Transaction.GetTableType(tableName);
464 
465  // If the table is in the SYSTEM schema, the type is defined as a
466  // SYSTEM TABLE.
467  if (curType.Equals("TABLE") &&
468  tableName.Parent.Name.Equals(Name)) {
469  curType = "SYSTEM TABLE";
470  }
471 
472  tableInfoObjects.Add(new TableInfoObject(null, tableName.Parent.Name, tableName.Name, curType, null));
473  }
474  }
475 
476  public override DataObject GetValue(long rowNumber, int columnOffset) {
477  if (rowNumber < 0 || rowNumber >= tableInfoObjects.Count)
478  throw new ArgumentOutOfRangeException("rowNumber");
479 
480  var tableInfo = tableInfoObjects[(int) rowNumber];
481 
482  switch (columnOffset) {
483  case 0:
484  return DataObject.String(tableInfo.Catalog);
485  case 1:
486  return DataObject.String(tableInfo.Schema);
487  case 2:
488  return DataObject.String(tableInfo.Name);
489  case 3:
490  return DataObject.String(tableInfo.Type);
491  case 4:
492  return DataObject.String(tableInfo.Comments);
493  default:
494  throw new ArgumentOutOfRangeException("columnOffset");
495  }
496  }
497 
498  #region TableInfoObject
499 
501  public TableInfoObject(string catalog, string schema, string name, string type, string comments) {
502  Catalog = catalog;
503  Schema = schema;
504  Name = name;
505  Type = type;
506  Comments = comments;
507  }
508 
509  public string Name { get; private set; }
510  public string Schema { get; private set; }
511  public string Catalog { get; private set; }
512  public string Type { get; private set; }
513  public string Comments { get; private set; }
514  }
515  #endregion
516  }
517 
518  #endregion
519 
520  #region TableColumnsTable
521 
524 
525  public TableColumnsTable(ITransaction transaction)
526  : base(transaction.Database.Context) {
527  this.transaction = transaction;
528  }
529 
530  public override TableInfo TableInfo {
531  get { return TableColumnsTableInfo; }
532  }
533 
534  public override int RowCount {
535  get { return GetRowCount(); }
536  }
537 
538  private int GetRowCount() {
539  // All the tables
540  var tableManager = transaction.GetTableManager();
541  var list = tableManager.GetTableNames();
542 
543  int colCount = 0;
544  foreach (var tableName in list) {
545  var info = tableManager.GetTableInfo(tableName);
546  if (info == null)
547  throw new InvalidOperationException(String.Format("Table information not found for '{0}'.", tableName));
548 
549  colCount += info.ColumnCount;
550  }
551 
552  return colCount;
553  }
554 
555  public override DataObject GetValue(long rowNumber, int columnOffset) {
556  // All the tables
557  var tableManager = transaction.GetTableManager();
558  var list = tableManager.GetTableNames();
559  var visibleTables = list.Select(name => transaction.GetTableInfo(name));
560 
561  int rs = 0;
562  foreach (var info in visibleTables) {
563  var schemaName = info.SchemaName == null ? null : info.SchemaName.FullName;
564 
565  int b = rs;
566  rs += info.ColumnCount;
567  if (rowNumber >= b && rowNumber < rs) {
568  // This is the column that was requested,
569  var seqNo = rowNumber - b;
570  var colInfo = info[(int)seqNo];
571 
572  var defaultExpression = colInfo.HasDefaultExpression ? colInfo.DefaultExpression.ToString() : null;
573 
574  switch (columnOffset) {
575  case 0: // schema
576  return GetColumnValue(columnOffset, new SqlString(schemaName));
577  case 1: // table
578  return GetColumnValue(columnOffset, new SqlString(info.Name));
579  case 2: // column
580  return GetColumnValue(columnOffset, new SqlString(colInfo.ColumnName));
581  case 3: // sql_type
582  return GetColumnValue(columnOffset, new SqlNumber((int)colInfo.ColumnType.TypeCode));
583  case 4: // type_desc
584  return GetColumnValue(columnOffset, new SqlString(colInfo.ColumnType.ToString()));
585  case 5: // size
586  return GetColumnValue(columnOffset, new SqlNumber(colInfo.Size));
587  case 6: // scale
588  return GetColumnValue(columnOffset, new SqlNumber(colInfo.Scale));
589  case 7: // not_null
590  return GetColumnValue(columnOffset, (SqlBoolean) colInfo.IsNotNull);
591  case 8: // default
592  return GetColumnValue(columnOffset, new SqlString(defaultExpression));
593  case 9: // index_str
594  return GetColumnValue(columnOffset, new SqlString(colInfo.IndexType));
595  case 10: // seq_no
596  return GetColumnValue(columnOffset, new SqlNumber(seqNo));
597  default:
598  throw new ArgumentOutOfRangeException("columnOffset");
599  }
600  }
601 
602  } // for each visible table
603 
604  throw new ArgumentOutOfRangeException("rowNumber", "Row out of bounds.");
605  }
606 
607  protected override void Dispose(bool disposing) {
608  transaction = null;
609  base.Dispose(disposing);
610  }
611  }
612 
613  #endregion
614 
615  #region SqlTypesTable
616 
619  private List<SqlTypeInfo> sqlTypes;
620 
621  public SqlTypesTable(ITransaction transaction)
622  : base(transaction.Database.Context) {
623  this.transaction = transaction;
624 
625  sqlTypes = new List<SqlTypeInfo>();
626 
627  Init();
628  }
629 
630  public override TableInfo TableInfo {
631  get { return SqlTypesTableInfo; }
632  }
633 
634  public override int RowCount {
635  get { return sqlTypes.Count; }
636  }
637 
638  private void AddType(string name, string localName, SqlTypeCode type, byte precision, string prefix, string suffix, bool searchable) {
639  sqlTypes.Add(new SqlTypeInfo {
640  TypeName = name,
641  LocalName = localName,
642  Type = type,
643  Precision = precision,
644  LiteralPrefix = prefix,
645  LiteralSuffix = suffix,
646  Searchable = (byte)(searchable ? 3 : 0)
647  });
648  }
649 
650  private void Init() {
651  AddType("BIT", "BOOLEAN", SqlTypeCode.Bit, 1, null, null, true);
652  AddType("BOOLEAN", "BOOLEAN", SqlTypeCode.Boolean, 1, null, null, true);
653  AddType("TINYINT", "NUMBER", SqlTypeCode.TinyInt, 9, null, null, true);
654  AddType("SMALLINT", "NUMBER", SqlTypeCode.SmallInt, 9, null, null, true);
655  AddType("INTEGER", "NUMBER", SqlTypeCode.Integer, 9, null, null, true);
656  AddType("BIGINT", "NUMBER", SqlTypeCode.BigInt, 9, null, null, true);
657  AddType("FLOAT", "NUMBER", SqlTypeCode.Float, 9, null, null, true);
658  AddType("REAL", "NUMBER", SqlTypeCode.Real, 9, null, null, true);
659  AddType("DOUBLE", "NUMBER", SqlTypeCode.Double, 9, null, null, true);
660  AddType("NUMERIC", "NUMBER", SqlTypeCode.Numeric, 9, null, null, true);
661  AddType("DECIMAL", "NUMBER", SqlTypeCode.Decimal, 9, null, null, true);
662  AddType("CHAR", "STRING", SqlTypeCode.Char, 9, "'", "'", true);
663  AddType("VARCHAR", "STRING", SqlTypeCode.VarChar, 9, "'", "'", true);
664  AddType("LONGVARCHAR", "STRING", SqlTypeCode.LongVarChar, 9, "'", "'", true);
665  AddType("DATE", "DATETIME", SqlTypeCode.Date, 9, null, null, true);
666  AddType("TIME", "DATETIME", SqlTypeCode.Time, 9, null, null, true);
667  AddType("TIMESTAMP", "DATETIME", SqlTypeCode.TimeStamp, 9, null, null, true);
668  AddType("BINARY", "BINARY", SqlTypeCode.Binary, 9, null, null, false);
669  AddType("VARBINARY", "BINARY", SqlTypeCode.VarBinary, 9, null, null, false);
670  AddType("LONGVARBINARY", "BINARY", SqlTypeCode.LongVarBinary, 9, null, null, false);
671  AddType("OBJECT", "OBJECT", SqlTypeCode.Object, 9, null, null, false);
672  AddType("TYPE", "TYPE", SqlTypeCode.Type, 9, null, null, false);
673  }
674 
675  public override DataObject GetValue(long rowNumber, int columnOffset) {
676  // TODO: handle also the user-types here?
677 
678  if (rowNumber < 0 || rowNumber >= sqlTypes.Count)
679  throw new ArgumentOutOfRangeException("rowNumber");
680 
681  var typeInfo = sqlTypes[(int)rowNumber];
682  switch (columnOffset) {
683  case 0: // type_name
684  return GetColumnValue(columnOffset, new SqlString(typeInfo.TypeName));
685  case 1: // data_type
686  return GetColumnValue(columnOffset, new SqlNumber((int)typeInfo.Type));
687  case 2: // precision
688  return GetColumnValue(columnOffset, new SqlNumber(typeInfo.Precision));
689  case 3: // literal_prefix
690  return GetColumnValue(columnOffset, new SqlString(typeInfo.LiteralPrefix));
691  case 4: // literal_suffix
692  return GetColumnValue(columnOffset, new SqlString(typeInfo.LiteralSuffix));
693  case 5: // create_params
694  return GetColumnValue(columnOffset, SqlString.Null);
695  case 6: // nullable
696  return GetColumnValue(columnOffset, SqlNumber.One);
697  case 7: // case_sensitive
698  return GetColumnValue(columnOffset, SqlBoolean.True);
699  case 8: // searchable
700  return GetColumnValue(columnOffset, new SqlNumber(typeInfo.Searchable));
701  case 9: // unsigned_attribute
702  return GetColumnValue(columnOffset, SqlBoolean.False);
703  case 10: // fixed_prec_scale
704  return GetColumnValue(columnOffset, SqlBoolean.False);
705  case 11: // auto_increment
706  return GetColumnValue(columnOffset, SqlBoolean.False);
707  case 12: // local_type_name
708  return GetColumnValue(columnOffset, new SqlString(typeInfo.LocalName));
709  case 13: // minimum_scale
710  return GetColumnValue(columnOffset, SqlNumber.Zero);
711  case 14: // maximum_scale
712  return GetColumnValue(columnOffset, new SqlNumber(10000000));
713  case 15: // sql_data_type
714  return GetColumnValue(columnOffset, SqlNull.Value);
715  case 16: // sql_datetype_sub
716  return GetColumnValue(columnOffset, SqlNull.Value);
717  case 17: // num_prec_radix
718  return GetColumnValue(columnOffset, new SqlNumber(10));
719  default:
720  throw new ArgumentOutOfRangeException("columnOffset");
721 
722  }
723  }
724 
725  protected override void Dispose(bool disposing) {
726  transaction = null;
727  sqlTypes = null;
728 
729  base.Dispose(disposing);
730  }
731 
732  #region SqlTypeInfo
733 
734  class SqlTypeInfo {
735  public string TypeName;
736  public string LocalName;
738  public byte Precision;
739  public string LiteralPrefix;
740  public string LiteralSuffix;
741  public byte Searchable;
742  }
743 
744  #endregion
745  }
746 
747  #endregion
748 
749  #region OpenSessionsTable
750 
753 
754  public OpenSessionsTable(ITransaction transaction)
755  : base(transaction.Database.Context) {
756  this.transaction = transaction;
757  }
758 
759  public override TableInfo TableInfo {
760  get { return OpenSessionsTableInfo; }
761  }
762 
763  public override int RowCount {
764  get { return transaction.Database.Sessions.Count; }
765  }
766 
767  public override DataObject GetValue(long rowNumber, int columnOffset) {
768  if (rowNumber < 0 || rowNumber >= transaction.Database.Sessions.Count)
769  throw new ArgumentOutOfRangeException("rowNumber");
770 
771  var session = transaction.Database.Sessions[(int) rowNumber];
772  var lastCommandTime = session.LastCommandTime == null
773  ? SqlDateTime.Null
774  : (SqlDateTime) session.LastCommandTime.Value;
775 
776  switch (columnOffset) {
777  case 0:
778  return GetColumnValue(0, new SqlString(session.User.Name));
779  case 1:
780  return GetColumnValue(1, SqlString.Null);
781  case 2:
782  return GetColumnValue(2, lastCommandTime);
783  case 3:
784  return GetColumnValue(3, (SqlDateTime)session.StartedOn);
785  default:
786  throw new ArgumentOutOfRangeException("columnOffset");
787  }
788  }
789 
790  protected override void Dispose(bool disposing) {
791  transaction = null;
792  base.Dispose(disposing);
793  }
794  }
795 
796  #endregion
797 
798  #region ProductInfoTable
799 
801  private List<ISqlString> keyValuePairs;
802 
803  public ProductInfoTable(ITransaction transaction)
804  : base(transaction.Database.Context) {
805  Init();
806  }
807 
808  public override TableInfo TableInfo {
809  get { return ProductInfoTableInfo; }
810  }
811 
812  public override int RowCount {
813  get { return keyValuePairs.Count/2; }
814  }
815 
816  private void Init() {
817  keyValuePairs = new List<ISqlString>();
818 
819  var productInfo = ProductInfo.Current;
820 
821  // Set up the product variables.
822  keyValuePairs.Add(new SqlString("title"));
823  keyValuePairs.Add(new SqlString(productInfo.Title));
824 
825  keyValuePairs.Add(new SqlString("version"));
826  keyValuePairs.Add(new SqlString(productInfo.Version.ToString()));
827 
828  keyValuePairs.Add(new SqlString("copyright"));
829  keyValuePairs.Add(new SqlString(productInfo.Copyright));
830 
831  keyValuePairs.Add(new SqlString("description"));
832  keyValuePairs.Add(new SqlString(productInfo.Description));
833 
834  keyValuePairs.Add(new SqlString("company"));
835  keyValuePairs.Add(new SqlString(productInfo.Company));
836  }
837 
838  public override DataObject GetValue(long rowNumber, int columnOffset) {
839  switch (columnOffset) {
840  case 0: // var
841  return GetColumnValue(columnOffset, keyValuePairs[(int)rowNumber * 2]);
842  case 1: // value
843  return GetColumnValue(columnOffset, keyValuePairs[(int)(rowNumber * 2) + 1]);
844  default:
845  throw new ArgumentOutOfRangeException("columnOffset");
846  }
847  }
848  }
849 
850  #endregion
851 
852  #region VariablesTable
853 
856 
857  public VariablesTable(ITransaction transaction)
858  : base(transaction.Database.Context) {
859  this.transaction = transaction;
860  }
861 
862  public override TableInfo TableInfo {
863  get { return VariablesTableInfo; }
864  }
865 
866  public override int RowCount {
867  get { throw new NotImplementedException(); }
868  }
869 
870  public override DataObject GetValue(long rowNumber, int columnOffset) {
871  throw new NotImplementedException();
872  }
873 
874  protected override void Dispose(bool disposing) {
875  transaction = null;
876  base.Dispose(disposing);
877  }
878  }
879 
880  #endregion
881 
882  #region PrivilegesTable
883 
885  private readonly IList<KeyValuePair<string, int>> privBits;
886 
887  public PrivilegesTable(ITransaction transaction)
888  : base(transaction.Database.Context) {
889  privBits = FormPrivilegesValues();
890  }
891 
892  private IList<KeyValuePair<string, int>> FormPrivilegesValues() {
893  var names = Enum.GetNames(typeof (Privileges));
894  var values = Enum.GetValues(typeof (Privileges));
895 
896  return names.Select((t, i) => new KeyValuePair<string, int>(t, (int) values.GetValue(i))).ToList();
897  }
898 
899  public override TableInfo TableInfo {
900  get { return PrivilegesTableInfo; }
901  }
902 
903  public override int RowCount {
904  get { return privBits.Count; }
905  }
906 
907  public override DataObject GetValue(long rowNumber, int columnOffset) {
908  if (rowNumber < 0 || rowNumber >= privBits.Count)
909  throw new ArgumentOutOfRangeException("rowNumber");
910 
911  var pair = privBits[(int) rowNumber];
912  switch (columnOffset) {
913  case 0:
914  return DataObject.Integer(pair.Value);
915  case 1:
916  return DataObject.VarChar(pair.Key);
917  default:
918  throw new ArgumentOutOfRangeException("columnOffset");
919  }
920  }
921  }
922 
923  #endregion
924 
925  #region SessionInfoTable
926 
928  public SessionInfoTable(ISession session)
929  : base(session.Context) {
930  }
931 
932  public override TableInfo TableInfo {
933  get { throw new NotImplementedException(); }
934  }
935 
936  public override int RowCount {
937  get { throw new NotImplementedException(); }
938  }
939 
940  public override DataObject GetValue(long rowNumber, int columnOffset) {
941  throw new NotImplementedException();
942  }
943  }
944 
945  #endregion
946 
947  #region StatisticsTable
948 
950  public StatisticsTable(ITransaction transaction)
951  : base(transaction.Database.Context) {
952  }
953 
954  public override TableInfo TableInfo {
955  get { throw new NotImplementedException(); }
956  }
957 
958  public override int RowCount {
959  get { throw new NotImplementedException(); }
960  }
961 
962  public override DataObject GetValue(long rowNumber, int columnOffset) {
963  throw new NotImplementedException();
964  }
965  }
966 
967  #endregion
968  }
969 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static readonly TableInfo SqlTypesTableInfo
static ITable GetPrivilegesTable(ITransaction transaction)
static void Setup(ITransaction transaction)
static DataObject Integer(int value)
Definition: DataObject.cs:576
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static void CreateTables(IQuery context)
static ITable GetOpenSessionsTable(ITransaction transaction)
readonly IList< KeyValuePair< string, int > > privBits
TableColumnsTable(ITransaction transaction)
A long string in the system.
The system implementation of a transaction model that handles isolated operations within a database c...
Definition: Transaction.cs:35
override void Dispose(bool disposing)
static ITable GetSqlTypesTable(ITransaction transaction)
const string PublicName
The name of the PUBLIC special user.
Definition: User.cs:47
static void CreateSecurityTables(IQuery context)
TableInfoTable(ITransaction transaction)
static BooleanType Boolean()
override void Dispose(bool disposing)
static readonly TableInfo TableInfoTableInfo
SqlTypesTable(ITransaction transaction)
static BinaryType Binary(int maxSize)
TableInfoObject(string catalog, string schema, string name, string type, string comments)
The default implementation of a database in a system.
Definition: Database.cs:38
Describes the name of an object within a database.
Definition: ObjectName.cs:44
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
IDatabase Database
Gets the database this transaction belongs to.
Definition: ITransaction.cs:48
static readonly TableInfo TableColumnsTableInfo
override void Dispose(bool disposing)
static ITable GetVariablesTable(ITransaction transaction)
static DataObject String(string s)
Definition: DataObject.cs:592
static readonly TableInfo PrivilegesTableInfo
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
static readonly SqlNull Value
Definition: SqlNull.cs:24
ForeignKeyAction
Enumerates the foreign key referential trigger actions.
static ITable GetStatisticsTable(ITransaction transaction)
static readonly TableInfo ProductInfoTableInfo
A user-defined TYPE that holds complex objects in a database column.
VariablesTable(ITransaction transaction)
static readonly SqlNumber Zero
Definition: SqlNumber.cs:29
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
static NumericType Numeric()
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
static readonly TableInfo StatisticsTableInfo
ISession Session
Definition: IQuery.cs:23
static void GrantToPublic(IQuery context)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
ProductInfoTable(ITransaction transaction)
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
static readonly TableInfo VariablesTableInfo
static readonly SqlDateTime Null
Definition: SqlDateTime.cs:24
int Count
Gets the count of the open connections.
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
Provides utilities and properties for handling the SYSTEN schema of a database.
Definition: SystemSchema.cs:37
A SCHEMA object, that is a named container of multiple types of objects (eg. TABLE, PROCEDURE, VIEW, etc.).
static readonly TableInfo OpenSessionsTableInfo
IList< KeyValuePair< string, int > > FormPrivilegesValues()
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
ActiveSessionList Sessions
Gets a list of all the open sessions to the database.
Definition: IDatabase.cs:65
List< TableInfoObject > tableInfoObjects
static DataObject VarChar(string s)
Definition: DataObject.cs:622
static readonly SqlNumber One
Definition: SqlNumber.cs:30
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
PrivilegesTable(ITransaction transaction)
void AddType(string name, string localName, SqlTypeCode type, byte precision, string prefix, string suffix, bool searchable)
override void Dispose(bool disposing)
The simplest implementation of a transaction.
Definition: ITransaction.cs:30
static ITable GetSessionInfoTable(IQuery context)
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
static ITable GetTableColumnsTable(ITransaction transaction)
Provides the information for a user in a database system
Definition: User.cs:27
OpenSessionsTable(ITransaction transaction)
static ITable GetTableInfoTable(ITransaction transaction)
static readonly TableInfo SessionInfoTableInfo
Deveel.Data.Sql.Objects.SqlString SqlString
Definition: DataObject.cs:27
static ITable GetProductInfoTable(ITransaction transaction)
static ProductInfo Current
Definition: ProductInfo.cs:31
StatisticsTable(ITransaction transaction)
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...