private static void DumpConstraints(StreamWriter ASw, TTable ATable, eInclude onlyForeign, Boolean AAdd) { foreach (TConstraint constr in ATable.grpConstraint) { WriteConstraint(ASw, ATable, constr, onlyForeign, AAdd); } }
private static void DumpIndexes(eDatabaseType ATargetDatabase, StreamWriter ASw, TTable ATable, eInclude includeIndexes, Boolean AAdd) { for (System.Int32 implicit_ = 0; implicit_ <= 1; implicit_ += 1) { countGeneratedIndex = 0; foreach (TIndex index in ATable.grpIndex) { // first the automatically generated Indexes if (index.bImplicit == (implicit_ != 1)) { string indexName = index.strName; if ((indexName.IndexOf("_fkcr_key") != 0) || (indexName.IndexOf("_fkmd_key") != 0) || (indexName.IndexOf("inx_s_group_gift") != 0)) { indexName += countGeneratedIndex++; } if (AAdd) { if (includeIndexes == eInclude.eInCreateTable) { ASw.WriteLine(","); } else { ASw.Write("CREATE "); } if (index.bUnique) { ASw.Write("UNIQUE "); } if (includeIndexes == eInclude.eInCreateTable) { ASw.WriteLine("KEY {0} ", indexName); } else { ASw.WriteLine("INDEX {0} ", indexName); ASw.WriteLine(" ON {0}", ATable.strName); } string fields = ""; foreach (TIndexField indfield in index.grpIndexField) { if (fields.Length > 0) { fields += ","; } fields += indfield.strName; foreach (TTableField field in ATable.grpTableField) { if ((ATargetDatabase == eDatabaseType.MySQL) && (field.strName == indfield.strName && field.iLength >= 1000)) { // avoid keys that are too long (Mysql on 32 bit) // ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytes fields += "(50)"; } } } if (includeIndexes == eInclude.eInCreateTable) { ASw.Write(" ({0})", fields); } else { ASw.WriteLine(" ({0});", fields); } } else { ASw.WriteLine("DROP INDEX IF EXISTS {0} CASCADE;", indexName); } } } } }
private static void WriteConstraint(StreamWriter ASw, TTable ATable, TConstraint constr, eInclude onlyForeign, Boolean AAdd) { if (onlyForeign != eInclude.eOnlyForeign && (constr.strType == "primarykey")) { ASw.WriteLine(","); ASw.WriteLine(" CONSTRAINT {0}", constr.strName); ASw.Write(" PRIMARY KEY ({0})", StringHelper.StrMerge(constr.strThisFields, ',')); } if (onlyForeign != eInclude.eOnlyForeign && (constr.strType == "uniquekey")) { ASw.WriteLine(","); ASw.WriteLine(" CONSTRAINT {0}", constr.strName); ASw.Write(" UNIQUE ({0})", StringHelper.StrMerge(constr.strThisFields, ',')); } if (onlyForeign == eInclude.eOnlyForeign && (constr.strType == "foreignkey")) { ASw.WriteLine("ALTER TABLE {0}", ATable.strName); if (AAdd) { ASw.WriteLine(" ADD CONSTRAINT {0}", constr.strName); ASw.WriteLine(" FOREIGN KEY ({0})", StringHelper.StrMerge(constr.strThisFields, ',')); ASw.WriteLine(" REFERENCES {0}({1});", constr.strOtherTable, StringHelper.StrMerge(constr.strOtherFields, ',')); } else { ASw.WriteLine(" DROP CONSTRAINT IF EXISTS {0};", constr.strName); } } else if (onlyForeign == eInclude.eInCreateTable && (constr.strType == "foreignkey")) { ASw.WriteLine(","); ASw.WriteLine(" CONSTRAINT {0}", constr.strName); ASw.WriteLine(" FOREIGN KEY ({0})", StringHelper.StrMerge(constr.strThisFields, ',')); ASw.Write(" REFERENCES {0}({1})", constr.strOtherTable, StringHelper.StrMerge(constr.strOtherFields, ',')); } }
private static void DumpIndexes(StreamWriter ASw, TTable ATable, eInclude includeIndexes, Boolean AAdd) { for (System.Int32 implicit_ = 0; implicit_ <= 1; implicit_ += 1) { countGeneratedIndex = 0; foreach (TIndex index in ATable.grpIndex) { // first the automatically generated Indexes if (index.bImplicit == (implicit_ != 1)) { string indexName = index.strName; if ((indexName.IndexOf("_fkcr_key") != 0) || (indexName.IndexOf("_fkmd_key") != 0) || (indexName.IndexOf("inx_s_group_gift") != 0)) { indexName += countGeneratedIndex++; } if (AAdd) { if (includeIndexes == eInclude.eInCreateTable) { ASw.WriteLine(","); } else { ASw.Write("CREATE "); } if (index.bUnique) { ASw.Write("UNIQUE "); } if (includeIndexes == eInclude.eInCreateTable) { ASw.WriteLine("KEY {0} ", indexName); } else { ASw.WriteLine("INDEX {0} ", indexName); ASw.WriteLine(" ON {0}", ATable.strName); } string fields = ""; foreach (TIndexField indfield in index.grpIndexField) { if (fields.Length > 0) { fields += ","; } fields += indfield.strName; } if (includeIndexes == eInclude.eInCreateTable) { ASw.Write(" ({0})", fields); } else { ASw.WriteLine(" ({0});", fields); } } else { ASw.WriteLine("DROP INDEX IF EXISTS {0} CASCADE;", indexName); } } } } }