2 using System.Collections.Generic;
12 namespace Deveel.Data.Sql.Tables {
13 public static partial class QueryContextExtensions {
15 return context.Session().ResolveTableName(tableName);
19 var schema = context.CurrentSchema;
20 if (String.IsNullOrEmpty(schema))
21 throw new InvalidOperationException(
"Default schema not specified in the context.");
23 var objSchemaName = context.ResolveSchemaName(schema);
24 if (objSchemaName == null)
25 throw new InvalidOperationException(
26 String.Format(
"The default schema of the session '{0}' is not defined in the database.", schema));
29 if (objName.Parent == null)
30 objName =
new ObjectName(objSchemaName, objName.Name);
32 return context.ResolveTableName(objName);
36 return context.ObjectExists(
DbObjectType.Table, tableName);
40 CreateTable(context, tableInfo,
false);
44 CreateTable(context, tableInfo, onlyIfNotExists,
false);
48 if (tableInfo == null)
49 throw new ArgumentNullException(
"tableInfo");
53 if (!context.UserCanCreateTable(tableName))
56 if (context.TableExists(tableName)) {
58 throw new InvalidOperationException(
59 String.Format(
"The table {0} already exists and the IF NOT EXISTS clause was not specified.", tableName));
64 context.Session().CreateTable(tableInfo, temporary);
66 using (var systemContext = context.ForSystemUser()) {
72 if (tableInfo == null)
73 throw new ArgumentNullException(
"tableInfo");
77 if (!context.UserCanCreateTable(tableName))
80 context.Session().CreateTable(tableInfo,
false);
84 DropTables(context, tableNames,
false);
88 var tableNameList = tableNames.ToList();
89 foreach (var tableName
in tableNameList) {
90 if (!context.UserCanDropObject(
DbObjectType.Table, tableName))
95 foreach (var tableName
in tableNameList) {
96 var refs = context.GetTableImportedForeignKeys(tableName);
98 foreach (var reference
in refs) {
100 if (!tableNameList.Contains(reference.TableName)) {
102 String.Format(
"Constraint violation ({0}) dropping table '{1}' because of referential link from '{2}'",
103 reference.ConstraintName, tableName, reference.TableName));
112 foreach (var tableName
in tableNameList) {
114 if (!context.TableExists(tableName)) {
115 throw new InvalidOperationException(String.Format(
"The table '{0}' does not exist and cannot be dropped.",
121 foreach (var tname
in tableNameList) {
123 if (context.TableExists(tname)) {
128 context.RevokeAllGrantsOnTable(tname);
131 context.DropAllTableConstraints(tname);
137 DropTable(context, tableName,
false);
141 context.DropTables(
new[] {tableName}, onlyIfExists);
145 context.AlterObject(tableInfo);
151 table = context.Session().GetTable(tableName);
154 context.CacheTable(tableName.
FullName, table);
166 return context.ResolveService<
ICache>(
"TableCache");
170 var tableCache = context.TableCache();
171 if (tableCache == null)
175 if (!tableCache.TryGet(cacheKey, out obj))
182 var tableCache = context.TableCache();
183 if (tableCache == null)
186 tableCache.Set(cacheKey, table);
190 var tableCache = context.TableCache();
191 if (tableCache == null)
200 AddPrimaryKey(context, tableName, columnNames, null);
204 if (!context.UserCanAlterTable(tableName))
207 context.Session().AddPrimaryKey(tableName, columnNames,
ConstraintDeferrability.InitiallyImmediate, constraintName);
211 AddPrimaryKey(context, tableName, columnName, null);
215 context.AddPrimaryKey(tableName,
new[] { columnName }, constraintName);
220 String constraintName) {
221 AddForeignKey(context, table, columns, refTable, refColumns, deleteRule, updateRule,
ConstraintDeferrability.InitiallyImmediate, constraintName);
226 String constraintName) {
227 if (!context.UserCanAlterTable(table))
229 if (!context.UserCanReferenceTable(refTable))
232 context.Session().AddForeignKey(table, columns, refTable, refColumns, deleteRule, updateRule, deferred, constraintName);
236 AddUniqueKey(context, tableName, columns, null);
245 AddUniqueKey(context, tableName, columns, deferrability, null);
250 if (!context.UserCanAlterTable(tableName))
253 context.Session().AddUniqueKey(tableName, columns, deferrability, constraintName);
261 context.Session().AddCheck(tableName, expression, deferred, constraintName);
265 context.Session().DropAllTableConstraints(tableName);
269 return context.Session().DropTableConstraint(tableName, constraintName);
275 if (columnNames.Length > 1)
276 throw new ArgumentException();
278 context.AddPrimaryKey(tableName, columnNames[0], constraintInfo.
ConstraintName);
290 return context.Session().DropTablePrimaryKey(tableName, constraintName);
294 context.Session().CheckConstraintViolations(tableName);
298 return context.Session().QueryTableCheckExpressions(tableName);
302 return context.Session().QueryTableImportedForeignKeys(tableName);
306 return context.Session().QueryTableForeignKeys(tableName);
310 return context.Session().QueryTablePrimaryKey(tableName);
314 return context.Session().QueryTableUniqueKeys(tableName);
static void AddUniqueKey(this IQueryContext context, ObjectName tableName, string[] columns)
static ConstraintInfo[] GetTableForeignKeys(this IQueryContext context, 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 ObjectName Parse(string s)
Parses the given string into a ObjectName object.
A database exception that represents a constraint violation.
static void DropTables(this IQueryContext context, IEnumerable< ObjectName > tableNames)
static void CreateTable(this IQueryContext context, TableInfo tableInfo)
static void DropTable(this IQueryContext context, ObjectName tableName)
SqlExpression CheckExpression
static void CacheTable(this IQueryContext context, string cacheKey, ITable table)
Provides a context for executing queries, accessing the system resources and evaluation context...
Describes the name of an object within a database.
ConstraintDeferrability
The type of deferrance of a constraint.
static void DropAllTableConstraints(this IQueryContext context, ObjectName tableName)
static ObjectName ResolveTableName(this IQueryContext context, string name)
ObjectName TableName
Gets the fully qualified name of the table that is ensured to be unique within the system...
static void AddCheck(this IQueryContext context, ObjectName tableName, SqlExpression expression, string constraintName)
ForeignKeyAction
Enumerates the foreign key referential trigger actions.
static void CreateTable(this IQueryContext context, TableInfo tableInfo, bool onlyIfNotExists, bool temporary)
static void AddPrimaryKey(this IQueryContext context, ObjectName tableName, string[] columnNames)
static void AddCheck(this IQueryContext context, ObjectName tableName, SqlExpression expression, ConstraintDeferrability deferred, string constraintName)
ConstraintType
An enumeration of all the supported kinds of constraints within a table or a schema.
static void AddPrimaryKey(this IQueryContext context, ObjectName tableName, string[] columnNames, string constraintName)
static void AlterTable(this IQueryContext context, TableInfo tableInfo)
static void AddPrimaryKey(this IQueryContext context, ObjectName tableName, string columnName, string constraintName)
static void AddUniqueKey(this IQueryContext context, ObjectName tableName, string[] columns, ConstraintDeferrability deferrability)
ForeignKeyAction OnDelete
static ITable GetTable(this IQueryContext context, ObjectName tableName)
static ObjectName ResolveTableName(this IQueryContext context, ObjectName tableName)
static ICache TableCache(this IQueryContext context)
static void CreateTable(this IQueryContext context, TableInfo tableInfo, bool onlyIfNotExists)
string FullName
Gets the full reference name formatted.
static ConstraintInfo GetTablePrimaryKey(this IQueryContext context, ObjectName tableName)
static void AddForeignKey(this IQueryContext context, ObjectName table, string[] columns, ObjectName refTable, string[] refColumns, ForeignKeyAction deleteRule, ForeignKeyAction updateRule, String constraintName)
static ConstraintInfo[] GetTableCheckExpressions(this IQueryContext context, ObjectName tableName)
string[] ForeignColumnNames
static void DropTables(this IQueryContext context, IEnumerable< ObjectName > tableNames, bool onlyIfExists)
static void AddPrimaryKey(this IQueryContext context, ObjectName tableName, string columnName)
static void DropTable(this IQueryContext context, ObjectName tableName, bool onlyIfExists)
static ConstraintInfo[] GetTableImportedForeignKeys(this IQueryContext context, ObjectName tableName)
const int DropTableViolation
Tried to drop a table that is referenced by another source.
static ConstraintInfo[] GetTableUniqueKeys(this IQueryContext context, ObjectName tableName)
ForeignKeyAction OnUpdate
static void ClearCachedTables(this IQueryContext context)
static void AddConstraint(this IQueryContext context, ObjectName tableName, ConstraintInfo constraintInfo)
ConstraintType ConstraintType
Provides a contract to access a caching system.
static IMutableTable GetMutableTable(this IQueryContext context, ObjectName tableName)
static ITable GetCachedTable(this IQueryContext context, string cacheKey)
static bool TableExists(this IQueryContext context, ObjectName tableName)
static bool DropPrimaryKey(this IQueryContext context, ObjectName tableName, string constraintName)
Defines the base class for instances that represent SQL expression tree nodes.
static void AddUniqueKey(this IQueryContext context, ObjectName tableName, string[] columns, string constraintName)
DbObjectType
The kind of objects that can be handled by a database system and its managers
Defines the metadata properties of a table existing within a database.
static void CreateSystemTable(this IQueryContext context, TableInfo tableInfo)
static int DropConstraint(this IQueryContext context, ObjectName tableName, string constraintName)
An interface that defines contracts to alter the contents of a table.
static void AddUniqueKey(this IQueryContext context, ObjectName tableName, string[] columns, ConstraintDeferrability deferrability, string constraintName)
static void CheckConstraints(this IQueryContext context, ObjectName tableName)
static void AddForeignKey(this IQueryContext context, ObjectName table, string[] columns, ObjectName refTable, string[] refColumns, ForeignKeyAction deleteRule, ForeignKeyAction updateRule, ConstraintDeferrability deferred, String constraintName)