DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
MutableTableExtensions.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.Sql;
23 
24 namespace Deveel.Data.Sql.Tables {
25  public static class MutableTableExtensions {
26  public static bool RemoveRow(this IMutableTable table, int rowIndex) {
27  return table.RemoveRow(new RowId(table.TableInfo.Id, rowIndex));
28  }
29 
30  public static void DeleteRows(this IMutableTable table, IEnumerable<int> rows) {
31  foreach (var row in rows) {
32  table.RemoveRow(row);
33  }
34  }
35 
55  public static Row NewRow(this IMutableTable table) {
56  return new Row(table);
57  }
58 
59  public static int Delete(this IMutableTable table, ITable t) {
60  return Delete(table, t, -1);
61  }
62 
63  public static int Delete(this IMutableTable table, ITable other, int limit) {
64  List<int> rowSet = new List<int>(other.RowCount);
65  var e = other.GetEnumerator();
66  while (e.MoveNext()) {
67  rowSet.Add(e.Current.RowId.RowNumber);
68  }
69 
70  // HACKY: Find the first column of this table in the search table. This
71  // will allow us to generate a row set of only the rows in the search
72  // table.
73  int firstColumn = other.IndexOfColumn(table.GetResolvedColumnName(0));
74 
75  if (firstColumn == -1)
76  throw new DatabaseSystemException("Search table does not contain any reference to table being deleted from");
77 
78  // Generate a row set that is in this tables domain.
79  var rowsToDelete = other.ResolveRows(firstColumn, rowSet, table).ToList();
80 
81  // row_set may contain duplicate row indices, therefore we must sort so
82  // any duplicates are grouped and therefore easier to find.
83  rowSet.Sort();
84 
85  // If limit less than zero then limit is whole set.
86  if (limit < 0)
87  limit = Int32.MaxValue;
88 
89  // Remove each row in row set in turn. Make sure we don't remove the
90  // same row index twice.
91  int len = System.Math.Min(rowsToDelete.Count, limit);
92  int lastRemoved = -1;
93  int removeCount = 0;
94  for (int i = 0; i < len; ++i) {
95  int toRemove = rowsToDelete[i];
96  if (toRemove < lastRemoved)
97  throw new DatabaseSystemException("Internal error: row sorting error or row set not in the range > 0");
98 
99  if (toRemove != lastRemoved) {
100  table.RemoveRow(toRemove);
101  lastRemoved = toRemove;
102  ++removeCount;
103  }
104  }
105 
106  if (removeCount > 0)
107  // Perform a referential integrity check on any changes to the table.
108  table.AssertConstraints();
109 
110  return removeCount;
111  }
112 
113  public static bool Delete(this IMutableTable table, int columnOffset, DataObject value) {
114  var list = table.SelectRowsEqual(columnOffset, value).ToArray();
115  if (list.Length == 0)
116  return false;
117 
118  return table.RemoveRow(list[0]);
119  }
120 
121  public static int Update(this IMutableTable mutableTable, IQuery context, ITable table,
122  IEnumerable<SqlAssignExpression> assignList, int limit) {
123 
124  // Get the rows from the input table.
125  var rowSet = new List<int>();
126  var e = table.GetEnumerator();
127  while (e.MoveNext()) {
128  rowSet.Add(e.Current.RowId.RowNumber);
129  }
130 
131  // HACKY: Find the first column of this table in the search table. This
132  // will allow us to generate a row set of only the rows in the search
133  // table.
134  int firstColumn = table.FindColumn(mutableTable.GetResolvedColumnName(0));
135  if (firstColumn == -1)
136  throw new InvalidOperationException("Search table does not contain any " +
137  "reference to table being updated from");
138 
139  // Convert the row_set to this table's domain.
140  rowSet = table.ResolveRows(firstColumn, rowSet, mutableTable).ToList();
141 
142  // NOTE: Assume there's no duplicate rows.
143 
144  // If limit less than zero then limit is whole set.
145  if (limit < 0)
146  limit = Int32.MaxValue;
147 
148  // Update each row in row set in turn up to the limit.
149  int len = System.Math.Min(rowSet.Count, limit);
150 
151  int updateCount = 0;
152  for (int i = 0; i < len; ++i) {
153  int toUpdate = rowSet[i];
154 
155  // Make a row object from this row (plus keep the original intact
156  // in case we need to roll back to it).
157  var row = mutableTable.GetRow(toUpdate);
158  row.SetFromTable();
159 
160  // Run each assignment on the row.
161  foreach (var assignment in assignList) {
162  row.EvaluateAssignment(assignment, context);
163  }
164 
165  // Update the row
166  mutableTable.UpdateRow(row);
167 
168  ++updateCount;
169  }
170 
171  if (updateCount > 0)
172  // Perform a referential integrity check on any changes to the table.
173  mutableTable.AssertConstraints();
174 
175  return updateCount;
176  }
177  }
178 }
static bool RemoveRow(this IMutableTable table, int rowIndex)
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static int Delete(this IMutableTable table, ITable other, int limit)
static void DeleteRows(this IMutableTable table, IEnumerable< int > rows)
static int Update(this IMutableTable mutableTable, IQuery context, ITable table, IEnumerable< SqlAssignExpression > assignList, int limit)
int Id
Gets a unique identifier of the table in a database system.
Definition: TableInfo.cs:107
Exception thrown where various problems occur within the database.
A single row in a table of a database.
Definition: Row.cs:44
static bool Delete(this IMutableTable table, int columnOffset, DataObject value)
void UpdateRow(Row row)
Updates the values of a row into the table.
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
TableInfo TableInfo
Gets the metadata information of the table, used to resolve the column sources.
Definition: ITable.cs:47
int RowCount
Gets the total number of rows in the table.
Definition: ITable.cs:52
Defines the value of a ROWID object, that is a unique reference within a database system to a single ...
Definition: RowId.cs:24
static int Delete(this IMutableTable table, ITable t)
bool RemoveRow(RowId rowId)
Deletes row identified by the given coordinates from the table.
void AssertConstraints()
Performs all constraint integrity checks and actions to any modifications based on any changes that h...
static Row NewRow(this IMutableTable table)
Creates a new row that is compatible with the table context, ready to be populated and added...
An interface that defines contracts to alter the contents of a table.