protected void CreateTable(InDbTableDef tableDef) { StringCollection stringCollection = new StringCollection(); foreach (InDbFieldDef fieldDef in tableDef.FieldDefs) { stringCollection.Add(string.Format("[{0}] {1}", (object)fieldDef.Name, (object)InDbSqlDatabase.InDbFieldDefToSqlDataType(fieldDef))); } string str = string.Format("CREATE TABLE {0}.[{1}] ({2}", (object)this.GetSchemaName(), (object)tableDef.Name, (object)StrUtils.Join((IList)stringCollection, "{0}", ",")); if (tableDef.PrimaryKey != null) { str = str + ", PRIMARY KEY (" + StrUtils.Join((IList)tableDef.PrimaryKey.FieldDefs.GetFieldNames(), "[{0}]", ",") + ")"; } this.Execute(str + ")"); try { for (int index = 0; index < tableDef.IndexDefs.Count; ++index) { InDbIndexDef indexDef = tableDef.IndexDefs[index]; if (indexDef != tableDef.PrimaryKey) { indexDef.FName = "Index" + (object)index; this.CreateIndex(tableDef.Name, indexDef); } } } catch (Exception ex) { this.DeleteTable(tableDef.Name); throw; } }
private void LoadIndexDefs(InDbTableDef tableDef) { SqlCommand command = this.Connection.CreateCommand(); command.CommandText = string.Format("EXEC sp_indexes_rowset @table_name = '{0}', @table_schema = '{1}'", (object)tableDef.Name, (object)this.GetSchemaName()); Hashtable hashtable = new Hashtable(); string str1 = string.Empty; using (IDataReader dataReader = (IDataReader)command.ExecuteReader()) { while (dataReader.Read()) { string str2 = dataReader.GetString(dataReader.GetOrdinal("INDEX_NAME")); string str3 = dataReader.GetString(dataReader.GetOrdinal("COLUMN_NAME")); int index = dataReader.GetInt32(dataReader.GetOrdinal("ORDINAL_POSITION")) - 1; if (Convert.ToInt32(dataReader.GetValue(dataReader.GetOrdinal("PRIMARY_KEY"))) != 0) { str1 = str2; } StringCollection stringCollection = (StringCollection)hashtable[(object)str2]; if (stringCollection == null) { stringCollection = new StringCollection(); hashtable.Add((object)str2, (object)stringCollection); } while (index > stringCollection.Count - 1) { stringCollection.Add(string.Empty); } if (stringCollection[index] == str3) { throw new InDbException(string.Format("Столбец {0} входит более одного раза в индекс {1}.", (object)str3, (object)str2)); } stringCollection[index] = str3; } } foreach (string key in (IEnumerable)hashtable.Keys) { StringCollection stringCollection = (StringCollection)hashtable[(object)key]; string[] array = new string[stringCollection.Count]; stringCollection.CopyTo(array, 0); InDbFieldDefs fieldDefs = new InDbFieldDefs((InDbDatabase)this); foreach (string name in stringCollection) { fieldDefs.Add(tableDef.FieldDefs[name]); } InDbIndexDef indexDef = new InDbIndexDef(key, fieldDefs); tableDef.IndexDefs.Add(indexDef); if (key == str1) { tableDef.OriginalPrimaryKey = indexDef; tableDef.PrimaryKey = indexDef; } } }
public void DeleteIndexDef(int index) { InDbIndexDef indexDef = this.IndexDefs[index]; if (!StrUtils.IsNullOrEmpty(indexDef.Name)) { this.FIndexDefsToDelete.Add(indexDef); } this.IndexDefs.RemoveAt(index); if (this.PrimaryKey != indexDef) { return; } this.PrimaryKey = (InDbIndexDef)null; }
public void ApplyChanges() { if (!this.Modified) { return; } this.FDb.ApplyTableStructure(this); this.OriginalName = this.Name; this.OriginalPrimaryKey = this.PrimaryKey; this.FFieldDefsToDelete.Clear(); this.FIndexDefsToDelete.Clear(); this.FModifiedIndexDefs.Clear(); foreach (InDbFieldDef fieldDef in this.FieldDefs) { fieldDef.FOriginalName = fieldDef.Name; fieldDef.FOriginalDataType = fieldDef.DataType; fieldDef.FOriginalSize = fieldDef.Size; } }
public void DeleteFieldDef(string name, bool autoCorrectIndexes) { try { InDbFieldDef fieldDef = this.FieldDefs[name]; if (!StrUtils.IsNullOrEmpty(fieldDef.OriginalName)) { this.FFieldDefsToDelete.Add(fieldDef); } this.FieldDefs.RemoveAt(this.FieldDefs.IndexOf(name)); if (!autoCorrectIndexes) { return; } for (int index1 = this.IndexDefs.Count - 1; index1 >= 0; --index1) { InDbIndexDef indexDef = this.IndexDefs[index1]; for (int index2 = indexDef.FieldDefs.Count - 1; index2 >= 0; --index2) { if (indexDef.FieldDefs[index2] == fieldDef) { indexDef.FieldDefs.RemoveAt(index2); if (this.FModifiedIndexDefs.IndexOf(indexDef) == -1) { this.FModifiedIndexDefs.Add(indexDef); } } } if (indexDef.FieldDefs.Count == 0) { this.DeleteIndexDef(index1); } } } catch (Exception ex) { throw new InDbException(string.Format("Ошибка удаления поля '{0}' таблицы '{1}'.", (object)name, (object)this.Name), ex); } }
private void CreateIndex(string tableName, InDbIndexDef indexDef) { string str = StrUtils.Join((IList)indexDef.FieldDefs.GetFieldNames(), "[{0}]", ","); this.Execute(string.Format("CREATE INDEX [{0}] ON {1}.[{2}] ({3})", (object)indexDef.Name, (object)this.GetSchemaName(), (object)tableName, (object)str)); }