void ChangeTable(DataTable dt, ArrayList addedColumns, ArrayList removedColums, ArrayList changedColumns, StreamWriter sw) { if (addedColumns.Count == 0 && removedColums.Count == 0 && changedColumns.Count == 0) { return; } string tableName = QualifiedTableName.Get(dt.TableName, this.provider); string alterString = "ALTER TABLE " + tableName + ' '; //alter table mitarbeiter modify [column] position_x char(255); (mysql) //ALTER TABLE EMPLOYEE ALTER EMP_NUM TYPE CHAR(20); (Fb) foreach (DataColumn dc in addedColumns) { sw.Write(alterString); sw.Write(concreteGenerator.AddColumn()); sw.Write(' '); sw.Write(base.CreateColumn(dc, GetClassForTable(dt.TableName, this.mappings), this.provider, false)); sw.WriteLine(";"); } foreach (DataColumn dc in removedColums) { sw.Write(alterString); sw.Write(concreteGenerator.RemoveColumn(provider.GetQuotedName(dc.ColumnName))); sw.WriteLine(";"); } foreach (DataColumn dc in changedColumns) { sw.Write(alterString); sw.Write(concreteGenerator.AlterColumnType()); sw.Write(' '); sw.Write(base.CreateColumn(dc, GetClassForTable(dt.TableName, this.mappings), this.provider, false)); sw.WriteLine(";"); } if (removedColums.Count > 0) { sw.WriteLine("-- If you need to rename a column use the following syntax:"); string rename = RenameColumn(provider.GetQuotedName("YourTableName"), provider.GetQuotedName("OldColumnName"), provider.GetQuotedName("NewColumnName"), "NewColumnType [(Precision)] [" + concreteGenerator.NullExpression(true) + " | " + concreteGenerator.NullExpression(false) + "]"); string[] splittedRename = rename.Split('\n'); foreach (string s in splittedRename) { sw.WriteLine("-- " + s); } messages.WriteLine("Warning: Dropping one or more columns. See the comment in the in ndodiff.sql file how to rename columns, if required."); } }
private void CreateTable(DataTable dt, System.IO.StreamWriter stream, NDO.Mapping.NDOMapping mappings) { IProvider provider = new MySqlProvider(); string tableName = QualifiedTableName.Get(dt.TableName, provider); Class cl = FindClass(dt.TableName, mappings); //bool selfGeneratedOid = cl.Oid.FieldName != null; if (cl != null) { string conn = mappings.FindConnection(cl).Name; Regex regex = new Regex(@"Database\s*=\s*([^;]+)"); Match match = regex.Match(conn); if (match.Success) { stream.WriteLine("USE " + match.Groups[1] + ";"); } } else { stream.WriteLine("/* TODO: Insert correct USE statement */"); } stream.WriteLine("DROP TABLE IF EXISTS " + tableName + ";"); stream.WriteLine("CREATE TABLE " + tableName + "("); int vorletzterIndex = dt.Columns.Count - 1; DataColumn[] primaryKeyColumns = dt.PrimaryKey; bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0; for (int i = 0; i < dt.Columns.Count; i++) { System.Data.DataColumn dc = dt.Columns[i]; // bool isPrimary = false; // if (!selfGeneratedOid) // isPrimary = IsPrimary(primaryKeyColumns, dc); CreateColumn(dc, stream, cl, provider, IsPrimary(primaryKeyColumns, dc)); if (i < vorletzterIndex || hasPrimaryKeyColumns) { stream.WriteLine(","); } } if (!hasPrimaryKeyColumns) { stream.WriteLine(); } else { CreateConstraint(primaryKeyColumns, stream, dt, provider); } stream.WriteLine(");"); stream.WriteLine(); }
private void CreateTable(DataTable dt, System.IO.StreamWriter stream, NDO.Mapping.NDOMapping mappings) { /* * * DROP TABLE DataContainer; * CREATE TABLE [Table6] ( * Id COUNTER CONSTRAINT PrimaryKey PRIMARY KEY, * ... * ); */ IProvider provider = new NDOOleDbProvider(); string tableName = QualifiedTableName.Get(dt.TableName, provider); Class cl = FindClass(dt.TableName, mappings); bool selfGeneratedOid = false; if (cl != null) { selfGeneratedOid = cl.Oid.FieldName != null; } stream.WriteLine("DROP TABLE " + tableName + ";"); stream.WriteLine("CREATE TABLE " + tableName + "("); int vorletzterIndex = dt.Columns.Count - 1; DataColumn[] primaryKeyColumns = dt.PrimaryKey; bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0; for (int i = 0; i < dt.Columns.Count; i++) { System.Data.DataColumn dc = dt.Columns[i]; bool isPrimary = false; if (!selfGeneratedOid) { isPrimary = IsPrimary(primaryKeyColumns, dc); } CreateColumn(dc, stream, cl, provider, IsPrimary(primaryKeyColumns, dc)); if (i < vorletzterIndex) { stream.WriteLine(","); } } stream.WriteLine(");"); stream.WriteLine(); }
public void Generate(DataSet dsSchema, DataSet dsOld, System.IO.StreamWriter stream, TypeManager typeManager, bool generateConstraints) { IProvider provider = NDOProviderFactory.Instance[concreteGenerator.ProviderName]; if (dsOld != null) { foreach (DataTable dt in dsOld.Tables) { if (generateConstraints) { for (int i = 0; i < dt.ParentRelations.Count; i++) { DataRelation dr = dt.ParentRelations[i]; stream.Write("ALTER TABLE "); stream.Write(QualifiedTableName.Get(dt.TableName, provider)); stream.Write(" DROP CONSTRAINT "); stream.Write(provider.GetQuotedName(dr.RelationName)); stream.WriteLine(";"); } } } foreach (DataTable dt in dsOld.Tables) { stream.WriteLine(concreteGenerator.DropTable(QualifiedTableName.Get(dt.TableName, provider))); } if (dsOld.Tables.Count > 0) { stream.WriteLine(); } } foreach (DataTable dt in dsSchema.Tables) { stream.Write(CreateTable(dt)); } if (generateConstraints) { foreach (DataTable dt in dsSchema.Tables) { for (int i = 0; i < dt.ParentRelations.Count; i++) { DataRelation dr = dt.ParentRelations[i]; stream.Write("ALTER TABLE "); stream.Write(QualifiedTableName.Get(dt.TableName, provider)); stream.Write(" ADD "); stream.WriteLine(concreteGenerator.CreateForeignKeyConstraint(dr.ChildColumns, dr.ParentColumns, provider.GetQuotedName(dr.RelationName), QualifiedTableName.Get(dr.ParentTable.TableName, provider)) + ";"); } } } if (typeManager != null && typeManager.Entries.Length > 0) { DataTable typeTable = new DataTable("NDOTypeCodes"); DataColumn pkColumn = typeTable.Columns.Add("TypeCode", typeof(int)); typeTable.Columns.Add("TypeName", typeof(string)); typeTable.Columns.Add("AssemblyName", typeof(string)); typeTable.Columns.Add("TableName", typeof(string)); typeTable.PrimaryKey = new DataColumn[] { pkColumn }; stream.Write(CreateTable(typeTable)); Class[] descriptors = typeManager.Entries; int l = descriptors.Length; for (int i = 0; i < l; i++) { Class cl = descriptors[i]; stream.Write("INSERT INTO NDOTypeCodes (TypeCode, TypeName, AssemblyName, TableName) VALUES ("); stream.Write(cl.TypeCode); stream.Write(","); stream.Write(provider.GetSqlLiteral(cl.FullName)); stream.Write(","); stream.Write(provider.GetSqlLiteral(cl.AssemblyName)); stream.Write(","); stream.Write(provider.GetSqlLiteral(cl.TableName)); stream.WriteLine(");"); } } #if noindex foreach (DataTable dt in dsSchema.Tables) { for (int i = 0; i < dt.ParentRelations.Count; i++) { DataRelation dr = dt.ParentRelations[i]; string index = concreteGenerator.CreateIndex(provider.GetQuotedName("Ix" + dr.RelationName), QualifiedTableName.Get(dt.TableName, provider), dr.ChildColumns); if (index != string.Empty) { stream.Write(index); stream.WriteLine(";"); } } } foreach (DataTable dt in dsSchema.Tables) { if (GetClassForTable(dt.TableName, mappings) == null) // mappingTable { int i = 1; string tname = dt.TableName; tname = tname.Substring(tname.IndexOf('.') + 1); if (tname.StartsWith("rel")) { tname = tname.Substring(3); } tname = "Ix" + tname; foreach (DataColumn dc in dt.Columns) { string index = concreteGenerator.CreateIndex(provider.GetQuotedName(tname + i.ToString()), QualifiedTableName.Get(dt.TableName, provider), new DataColumn[] { dc }); if (index != string.Empty) { stream.Write(index); stream.WriteLine(";"); i++; } } } } #endif }
protected string CreateTable(DataTable dt) { StringBuilder sb = new StringBuilder(); IProvider provider = NDOProviderFactory.Instance[concreteGenerator.ProviderName]; if (provider == null) { throw new Exception("Can't find NDO provider '" + concreteGenerator.ProviderName + "'."); } string tableName = QualifiedTableName.Get(dt.TableName, provider); Class cl = FindClass(dt.TableName, mappings); if (cl != null) { Connection conn = mappings.FindConnection(cl); if (conn != null) { concreteGenerator.ConnectToDatabase(conn.Name); } } sb.Append(concreteGenerator.BeginnTable(tableName)); sb.Append('\n'); int vorletzterIndex = dt.Columns.Count - 1; DataColumn[] primaryKeyColumns = dt.PrimaryKey; bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0; for (int i = 0; i < dt.Columns.Count; i++) { System.Data.DataColumn dc = dt.Columns[i]; bool isPrimary = false; foreach (DataColumn pkc in primaryKeyColumns) { if (pkc.ColumnName == dc.ColumnName) { isPrimary = true; break; } } sb.Append(CreateColumn(dc, cl, provider, isPrimary)); if (i < vorletzterIndex) { sb.Append(","); sb.Append('\n'); } } // vorletzterIndex = dt.ParentRelations.Count - 1; // if (vorletzterIndex > -1) // sb.Append(","); // // for (int i = 0; i < dt.ParentRelations.Count; i++) // { // DataRelation dr = dt.ParentRelations[i]; // sb.Append(concreteGenerator.CreateForeignKeyConstraint(dr.ChildColumns, dr.ParentColumns, provider.GetQuotedName(dr.RelationName), dr.ParentTable.TableName)); // if (i < vorletzterIndex) // sb.Append(","); // } if (concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable && hasPrimaryKeyColumns) { sb.Append(","); sb.Append('\n'); } if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable) { sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider)); } else { sb.Append('\n'); } sb.Append(concreteGenerator.EndTable(tableName)); sb.Append('\n'); // CreateIndex(primaryKeyColumns, sb, dt, provider); if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.AfterTable) { sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider)); } sb.Append('\n'); return(sb.ToString()); }
protected string CreatePrimaryKeyConstraint(DataColumn[] primaryKeyColumns, DataTable dt, IProvider provider) { if (primaryKeyColumns.Length == 0) { return(string.Empty); } string[] strArr = dt.TableName.Split('.'); string constraintName = provider.GetQuotedName("PK_" + strArr[strArr.Length - 1]); return(concreteGenerator.CreatePrimaryKeyConstraint(primaryKeyColumns, constraintName, QualifiedTableName.Get(dt.TableName, provider)) + '\n'); }
private void CreateTable(DataTable dt, System.IO.StreamWriter stream, NDO.Mapping.NDOMapping mappings) { IProvider provider = NDOProviderFactory.Instance[concreteGenerator.ProviderName]; if (provider == null) { throw new NDOException("Can't find NDO provider '" + concreteGenerator.ProviderName + "'."); } string tableName = QualifiedTableName.Get(dt.TableName, provider); Class cl = FindClass(dt.TableName, mappings); if (cl != null) { Connection conn = mappings.FindConnection(cl); if (conn != null) { concreteGenerator.ConnectToDatabase(conn.Name); } } stream.WriteLine(concreteGenerator.DropTable(tableName)); stream.WriteLine(concreteGenerator.BeginnTable(tableName)); int vorletzterIndex = dt.Columns.Count - 1; DataColumn[] primaryKeyColumns = dt.PrimaryKey; bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0; for (int i = 0; i < dt.Columns.Count; i++) { System.Data.DataColumn dc = dt.Columns[i]; bool isPrimary = false; foreach (DataColumn pkc in primaryKeyColumns) { if (pkc.ColumnName == dc.ColumnName) { isPrimary = true; break; } } CreateColumn(dc, stream, cl, provider, isPrimary); if (i < vorletzterIndex || (concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable && hasPrimaryKeyColumns)) { stream.WriteLine(","); } } if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable) { CreateConstraint(primaryKeyColumns, stream, dt, provider); } else { stream.WriteLine(); } stream.WriteLine(concreteGenerator.EndTable(tableName)); // CreateIndex(primaryKeyColumns, stream, dt, provider); if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.AfterTable) { CreateConstraint(primaryKeyColumns, stream, dt, provider); } stream.WriteLine(); }
private void CreateConstraint(DataColumn[] primaryKeyColumns, System.IO.StreamWriter stream, DataTable dt, IProvider provider) { if (primaryKeyColumns.Length == 0) { return; } string[] strArr = dt.TableName.Split('.'); string constraintName = provider.GetQuotedName("PK_" + strArr[strArr.Length - 1]); stream.WriteLine(concreteGenerator.CreateConstraint(primaryKeyColumns, constraintName, QualifiedTableName.Get(dt.TableName, provider))); }