internal void GenerateSql(CSourceWriter sw) { sw.WriteLn("create table dbo." + this.Name + "("); bool primero = true; sw.AddTab(); foreach (CFieldM f in this.lFieldAll) { f.GeneraSql(sw, ref primero); } if (this.lFieldPk.Count > 0) { sw.WriteLn(",CONSTRAINT PK_" + this.Name + " PRIMARY KEY ("); primero = true; sw.AddTab(); foreach (CFieldM f in this.lFieldPk) { sw.Write(""); sw.WriteComma(ref primero); sw.WriteLn(f.Name); } sw.DelTab(); sw.WriteLn(")"); } foreach (CUniqueConstraintM u in this.LUniqueConstraint) { u.GeneraSql(sw); } sw.DelTab(); sw.WriteLn(")"); sw.EndLine(); sw.WriteLn("GO"); //Genera las unique constraints }
private void GenerateUsings(CSourceWriter sw) { bool escribioUsings = false; LUsing l = new LUsing(); foreach (var item in this.lElement) { l.AddLUsing(item.lUsing); } foreach (CUsing u in l) { u.Generate(sw); escribioUsings = true; } if (escribioUsings) { sw.EndLine(); } }
public string GenerateSql(bool withDrops) { MemoryStream ms = new MemoryStream(); CSourceWriter sw = new CSourceWriter(ms); if (withDrops) { //Primero se borran todas las relaciones para evitar errores de eliminaciĆ³n de tablas con otras relacionadas.(Si existen) foreach (CTableM t in this.lTable) { foreach (CRelationM r in t.lRelation) { sw.WriteLn(string.Format("IF OBJECT_ID('dbo.{0}', 'F') IS NOT NULL ALTER TABLE dbo.{1} DROP CONSTRAINT {0};", r.Name, r.tableSource.Name)); sw.WriteLn("GO"); } } //Se elinan las tablas si existen foreach (CTableM t in this.lTable) { sw.WriteLn(string.Format("IF OBJECT_ID('dbo.{0}', 'U') IS NOT NULL DROP TABLE dbo.{0};", t.Name)); sw.WriteLn("GO"); } } foreach (CTableM t in this.lTable) { t.GenerateSql(sw); sw.EndLine(); } foreach (CTableM t in this.lTable) { foreach (CRelationM r in t.lRelation) { r.GeneraSql(sw); } } sw.Close(); return(Encoding.UTF8.GetString(ms.ToArray())); }
public void GeneraSql(CSourceWriter sw, ref bool primero) { sw.WriteComma(ref primero); sw.Write(this.Name); sw.Write(" "); sw.Write(this.CadenaTDSqlServer); if ((this.Td == SqlDbType.VarChar) || (this.Td == SqlDbType.VarBinary)) { sw.Write("("); sw.Write(this.Lenght.ToString()); sw.Write(")"); } if (this.IsIdentity) { sw.Write(" identity "); } if (!this.IsNullable) { sw.Write(" not null "); } sw.EndLine(); }