DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DropTableStatement.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 
22 using Deveel.Data.Sql.Tables;
23 
24 namespace Deveel.Data.Sql.Statements {
26  public DropTableStatement(string[] tableNames)
27  : this(tableNames, false) {
28  }
29 
30  public DropTableStatement(string[] tableNames, bool ifExists) {
31  if (tableNames == null)
32  throw new ArgumentNullException("tableNames");
33  if (tableNames.Length == 0)
34  throw new ArgumentException("The table name list cannot be empty", "tableNames");
35 
36  if (tableNames.Any(String.IsNullOrEmpty))
37  throw new ArgumentException("One of the specified table names is null.");
38 
39  TableNames = tableNames;
40  IfExists = ifExists;
41  }
42 
43  public DropTableStatement(string tableName)
44  : this(tableName, false) {
45  }
46 
47  public DropTableStatement(string tableName, bool ifExists)
48  : this(new[] {tableName}, ifExists) {
49  }
50 
51  public string[] TableNames { get; private set; }
52 
53  public bool IfExists { get; set; }
54 
56  var tableNameList = TableNames.ToList();
57  var dropTables = new List<string>();
58 
59  foreach (var tableName in tableNameList) {
60  if (dropTables.Contains(tableName, StringComparer.OrdinalIgnoreCase))
61  throw new StatementPrepareException(String.Format("Duplicated table name '{0}' in the list of tables to drop.",
62  tableName));
63 
64  dropTables.Add(tableName);
65  }
66 
67  var resolvedTableNames = dropTables.Select(context.Query.ResolveTableName);
68 
69  return new Prepared(resolvedTableNames.ToArray(), IfExists);
70  }
71 
72  #region Prepared
73 
74  [Serializable]
76  public Prepared(ObjectName[] tableNames, bool ifExists) {
77  TableNames = tableNames;
78  IfExists = ifExists;
79  }
80 
81  private Prepared(ObjectData data) {
82  TableNames = data.GetValue<ObjectName[]>("TableNames");
83  IfExists = data.GetBoolean("IfExists");
84  }
85 
86  public ObjectName[] TableNames { get; private set; }
87 
88  public bool IfExists { get; private set; }
89 
90  protected override void GetData(SerializeData data) {
91  data.SetValue("TableNames", TableNames);
92  data.SetValue("IfExists", IfExists);
93  }
94 
95  protected override void ExecuteStatement(ExecutionContext context) {
96  context.Request.Query.DropTables(TableNames, IfExists);
97  }
98  }
99 
100  #endregion
101  }
102 }
An exception that happens during the SqlStatement.Prepare(IExpressionPreparer, IQueryContext).
DropTableStatement(string tableName, bool ifExists)
void SetValue(string key, Type type, object value)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
Represents the foundation class of SQL statements to be executed.
Definition: SqlStatement.cs:32
override void ExecuteStatement(ExecutionContext context)
Prepared(ObjectName[] tableNames, bool ifExists)
DropTableStatement(string[] tableNames, bool ifExists)