18 using System.Collections.Generic;
28 namespace Deveel.Data.Sql.Tables {
31 return query.
Session.ResolveTableName(tableName);
35 var schema = query.CurrentSchema();
36 if (String.IsNullOrEmpty(schema))
37 throw new InvalidOperationException(
"Default schema not specified in the query.");
39 var objSchemaName = query.ResolveSchemaName(schema);
40 if (objSchemaName == null)
41 throw new InvalidOperationException(
42 String.Format(
"The default schema of the session '{0}' is not defined in the database.", schema));
45 if (objName.Parent == null)
46 objName =
new ObjectName(objSchemaName, objName.Name);
48 return query.ResolveTableName(objName);
52 return query.ObjectExists(
DbObjectType.Table, tableName);
56 CreateTable(query, tableInfo,
false);
60 CreateTable(query, tableInfo, onlyIfNotExists,
false);
64 if (tableInfo == null)
65 throw new ArgumentNullException(
"tableInfo");
69 if (!query.UserCanCreateTable(tableName))
72 if (query.TableExists(tableName)) {
74 throw new InvalidOperationException(
75 String.Format(
"The table {0} already exists and the IF NOT EXISTS clause was not specified.", tableName));
80 query.
Session.CreateTable(tableInfo, temporary);
82 using (var systemContext = query.Direct()) {
83 systemContext.GrantToUserOnTable(tableInfo.
TableName, query.User().Name,
Privileges.TableAll);
88 return context.
Session.GetTableQueryInfo(tableName, alias);
92 if (tableInfo == null)
93 throw new ArgumentNullException(
"tableInfo");
97 if (!query.UserCanCreateTable(tableName))
100 query.
Session.CreateTable(tableInfo,
false);
104 DropTables(query, tableNames,
false);
107 public static void DropTables(
this IQuery query, IEnumerable<ObjectName> tableNames,
bool onlyIfExists) {
108 var tableNameList = tableNames.ToList();
109 foreach (var tableName
in tableNameList) {
110 if (!query.UserCanDropObject(
DbObjectType.Table, tableName))
115 foreach (var tableName
in tableNameList) {
116 var refs = query.GetTableImportedForeignKeys(tableName);
118 foreach (var reference
in refs) {
120 if (!tableNameList.Contains(reference.TableName)) {
122 String.Format(
"Constraint violation ({0}) dropping table '{1}' because of referential link from '{2}'",
123 reference.ConstraintName, tableName, reference.TableName));
132 foreach (var tableName
in tableNameList) {
134 if (!query.TableExists(tableName)) {
135 throw new InvalidOperationException(String.Format(
"The table '{0}' does not exist and cannot be dropped.",
141 foreach (var tname
in tableNameList) {
143 if (query.TableExists(tname)) {
148 query.RevokeAllGrantsOnTable(tname);
151 query.DropAllTableConstraints(tname);
157 DropTable(query, tableName,
false);
161 query.DropTables(
new[] { tableName }, onlyIfExists);
165 query.AlterObject(tableInfo);
175 table = query.
Session.GetTable(tableName);
178 query.CacheTable(tableName.
FullName, table);
190 var tableCache = query.TableCache();
191 if (tableCache == null)
195 if (!tableCache.TryGet(cacheKey, out obj))
202 var tableCache = query.TableCache();
203 if (tableCache == null)
206 tableCache.Set(cacheKey, table);
210 var tableCache = query.TableCache();
211 if (tableCache == null)
220 AddPrimaryKey(query, tableName, columnNames, null);
224 if (!query.UserCanAlterTable(tableName))
231 AddPrimaryKey(query, tableName, columnName, null);
235 query.AddPrimaryKey(tableName,
new[] { columnName }, constraintName);
240 String constraintName) {
241 AddForeignKey(query, table, columns, refTable, refColumns, deleteRule, updateRule,
ConstraintDeferrability.InitiallyImmediate, constraintName);
246 String constraintName) {
247 if (!query.UserCanAlterTable(table))
249 if (!query.UserCanReferenceTable(refTable))
252 query.
Session.AddForeignKey(table, columns, refTable, refColumns, deleteRule, updateRule, deferred, constraintName);
256 AddUniqueKey(query, tableName, columns, null);
265 AddUniqueKey(query, tableName, columns, deferrability, null);
270 if (!query.UserCanAlterTable(tableName))
273 query.
Session.AddUniqueKey(tableName, columns, deferrability, constraintName);
281 query.
Session.AddCheck(tableName, expression, deferred, constraintName);
285 query.
Session.DropAllTableConstraints(tableName);
289 return query.
Session.DropTableConstraint(tableName, constraintName);
295 if (columnNames.Length > 1)
296 throw new ArgumentException();
298 query.AddPrimaryKey(tableName, columnNames[0], constraintInfo.
ConstraintName);
310 return query.
Session.DropTablePrimaryKey(tableName, constraintName);
314 query.
Session.CheckConstraintViolations(tableName);
318 return query.
Session.QueryTableCheckExpressions(tableName);
322 return query.
Session.QueryTableImportedForeignKeys(tableName);
326 return query.
Session.QueryTableForeignKeys(tableName);
330 return query.
Session.QueryTablePrimaryKey(tableName);
334 return query.
Session.QueryTableUniqueKeys(tableName);
static void DropTables(this IQuery query, IEnumerable< ObjectName > tableNames, bool onlyIfExists)
static void AddConstraint(this IQuery query, ObjectName tableName, ConstraintInfo constraintInfo)
static void AddPrimaryKey(this IQuery query, ObjectName tableName, string columnName, string constraintName)
static ConstraintInfo[] GetTableUniqueKeys(this IQuery query, ObjectName tableName)
Defines the contract to access the data contained into a table of a database.
Enumerates a known set of codes in a SQL Model
A wrapper around a table that fires triggers on table events.
static bool TableExists(this IQuery query, ObjectName tableName)
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
A database exception that represents a constraint violation.
static void AddCheck(this IQuery query, ObjectName tableName, SqlExpression expression, string constraintName)
static void AlterTable(this IQuery query, TableInfo tableInfo)
static void DropTable(this IQuery query, ObjectName tableName, bool onlyIfExists)
static void AddCheck(this IQuery query, ObjectName tableName, SqlExpression expression, ConstraintDeferrability deferred, string constraintName)
static ConstraintInfo[] GetTableForeignKeys(this IQuery query, ObjectName tableName)
static void AddUniqueKey(this IQuery query, ObjectName tableName, string[] columns, ConstraintDeferrability deferrability, string constraintName)
SqlExpression CheckExpression
static void AddUniqueKey(this IQuery query, ObjectName tableName, string[] columns)
Describes the name of an object within a database.
ConstraintDeferrability
The type of deferrance of a constraint.
static void AddForeignKey(this IQuery query, ObjectName table, string[] columns, ObjectName refTable, string[] refColumns, ForeignKeyAction deleteRule, ForeignKeyAction updateRule, String constraintName)
static void DropTable(this IQuery query, ObjectName tableName)
ObjectName TableName
Gets the fully qualified name of the table that is ensured to be unique within the system...
ForeignKeyAction
Enumerates the foreign key referential trigger actions.
static void ClearCachedTables(this IQuery query)
static void CreateTable(this IQuery query, TableInfo tableInfo, bool onlyIfNotExists, bool temporary)
ConstraintType
An enumeration of all the supported kinds of constraints within a table or a schema.
static IMutableTable GetMutableTable(this IQuery query, ObjectName tableName)
static ObjectName ResolveTableName(this IQuery query, string name)
static ConstraintInfo GetTablePrimaryKey(this IQuery query, ObjectName tableName)
static ConstraintInfo[] GetTableCheckExpressions(this IQuery query, ObjectName tableName)
static ITable GetTable(this IQuery query, ObjectName tableName)
ForeignKeyAction OnDelete
static void CheckConstraints(this IQuery query, ObjectName tableName)
string FullName
Gets the full reference name formatted.
static void DropTables(this IQuery query, IEnumerable< ObjectName > tableNames)
static ITableQueryInfo GetTableQueryInfo(this IQuery context, ObjectName tableName, ObjectName alias)
string[] ForeignColumnNames
static void CreateTable(this IQuery query, TableInfo tableInfo, bool onlyIfNotExists)
const int DropTableViolation
Tried to drop a table that is referenced by another source.
ForeignKeyAction OnUpdate
static void CreateTable(this IQuery query, TableInfo tableInfo)
static ITable GetCachedTable(this IQuery query, string cacheKey)
ConstraintType ConstraintType
Provides a contract to access a caching system.
static void AddUniqueKey(this IQuery query, ObjectName tableName, string[] columns, ConstraintDeferrability deferrability)
static void CreateSystemTable(this IQuery query, TableInfo tableInfo)
static ICache TableCache(this IQuery query)
Defines the base class for instances that represent SQL expression tree nodes.
static int DropConstraint(this IQuery query, ObjectName tableName, string constraintName)
static void CacheTable(this IQuery query, string cacheKey, ITable table)
DbObjectType
The kind of objects that can be handled by a database system and its managers
static ConstraintInfo[] GetTableImportedForeignKeys(this IQuery query, ObjectName tableName)
static void AddForeignKey(this IQuery query, ObjectName table, string[] columns, ObjectName refTable, string[] refColumns, ForeignKeyAction deleteRule, ForeignKeyAction updateRule, ConstraintDeferrability deferred, String constraintName)
Defines the metadata properties of a table existing within a database.
static void DropAllTableConstraints(this IQuery query, ObjectName tableName)
static ObjectName ResolveTableName(this IQuery query, ObjectName tableName)
new IQueryContext Context
An interface that defines contracts to alter the contents of a table.
static void AddPrimaryKey(this IQuery query, ObjectName tableName, string[] columnNames, string constraintName)
static void AddPrimaryKey(this IQuery query, ObjectName tableName, string[] columnNames)
static void AddUniqueKey(this IQuery query, ObjectName tableName, string[] columns, string constraintName)
static bool DropPrimaryKey(this IQuery query, ObjectName tableName, string constraintName)
static void AddPrimaryKey(this IQuery query, ObjectName tableName, string columnName)