public string CreateTable(Energy.Source.Structure.Table table, Energy.Query.Style configuration) { if (table == null || table.Columns.Count == 0) { return(""); } if (configuration == null) { configuration = Energy.Query.Style.Global; } List <string> script = new List <string>(); string identity = configuration.IdentityName; string tableName = "[" + table.Name + "]"; script.Add("IF ( OBJECT_ID('" + tableName + "') ) IS NULL"); script.Add(""); script.Add("CREATE TABLE " + tableName); script.Add("("); if (!String.IsNullOrEmpty(table.Identity)) { Energy.Source.Structure.Column column = table.Columns.Get(table.Identity) ?? new Energy.Source.Structure.Column(table.Identity); string type = column.Type; if (type == null || type.Length == 0) { type = configuration.IdentityType; } script.Add("\t[" + column.Name + "] " + type + " IDENTITY(1,1) NOT NULL ,"); identity = table.Identity; } else if (table.Columns.Get(identity) == default(Energy.Source.Structure.Column)) { script.Add("\t[" + identity + "] " + configuration.IdentityType + " IDENTITY(1,1) NOT NULL ,"); } foreach (Energy.Source.Structure.Column column in table.Columns) { if (!column.Ignore && !String.IsNullOrEmpty(column.Name) && column.Name != table.Identity) { script.Add("\t[" + column.Name + "] " + column.Type + " NOT NULL ,"); } } script.Add(""); script.Add("\tCONSTRAINT [PK_" + table.Name + "] PRIMARY KEY NONCLUSTERED"); script.Add("\t("); script.Add("\t\t[" + identity + "] ASC"); script.Add("\t)"); script.Add("\tWITH ( PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON )"); script.Add(")"); script.Add("ON [PRIMARY]"); script.Add(""); script.Add("GO"); return(String.Join(Energy.Base.Text.NL, script.ToArray())); }
public string CreateTable(Energy.Source.Structure.Table table, Energy.Query.Style configuration) { StringBuilder s = new StringBuilder(); s.Append("CREATE TABLE IF NOT EXISTS "); s.Append(Format.Object(table.Schema, table.Name)); s.Append(Energy.Base.Text.NL); s.Append("("); s.Append(Energy.Base.Text.NL); string identity = Energy.Base.Text.Select(table.Identity, table.Columns.GetPrimaryName()); List <string> list = new List <string>(); for (int i = 0; i < table.Columns.Count; i++) { Energy.Source.Structure.Column column = table.Columns[i]; if (column.Ignore || string.IsNullOrEmpty(column.Name)) { continue; } string line = string.Concat(Format.Object(column.Name), " ", column.Type, " ", (column.NotNull ? "NOT " : ""), "NULL"); if (column.Name == identity) { line += " PRIMARY KEY"; if (column.Increment > 0) { line += " AUTOINCREMENT"; } } list.Add(line); } string join = string.Concat(" ,", Energy.Base.Text.NL, "\t"); s.Append(string.Concat("\t", string.Join(join, list.ToArray()))); s.Append(Energy.Base.Text.NL); s.Append(")"); s.Append(Energy.Base.Text.NL); s.Append(Energy.Base.Text.NL); return(s.ToString()); }
public virtual string CreateTable(Energy.Source.Structure.Table table, Energy.Query.Style configuration) { throw new NotImplementedException(); }
/// <summary> /// Create table /// </summary> /// <param name="table"></param> /// <param name="configuration"></param> /// <returns></returns> public string CreateTable(Energy.Source.Structure.Table table, Energy.Query.Style configuration) { if (table == null || table.Columns.Count == 0) { return(""); } if (configuration == null) { configuration = Energy.Query.Style.Global; } StringBuilder script = new StringBuilder(); if (table == null || table.Columns.Count == 0) { return(""); } if (configuration == null) { configuration = Energy.Query.Style.Global; } string identity = configuration.IdentityName; string tableName = table.Name; script.Append("CREATE TABLE IF NOT EXISTS " + Format.Object(tableName)); script.Append(Energy.Base.Text.NL); script.Append("("); script.Append(Energy.Base.Text.NL); Energy.Source.Structure.Column primary = null; if (!string.IsNullOrEmpty(table.Identity)) { identity = table.Identity; } else { primary = table.Columns.GetPrimary(); if (primary != null) { identity = primary.Name; } } if (primary == null) { primary = table.Columns.Get(identity) ?? new Energy.Source.Structure.Column(table.Identity); } List <string> list = new List <string>(); string line; string type = primary.Type; if (type == null || type.Length == 0) { type = configuration.IdentityType; } line = string.Concat(Format.Object(primary.Name), " ", type, " NOT NULL"); if (Energy.Query.Type.IsNumeric(type)) { line += " AUTO_INCREMENT"; } list.Add(line); for (int i = 0; i < table.Columns.Count; i++) { Energy.Source.Structure.Column column = table.Columns[i]; if (column.Ignore || string.IsNullOrEmpty(column.Name) || column.IsName(identity)) { continue; } line = string.Concat(Format.Object(column.Name), " ", column.Type, " ", (column.NotNull ? "NOT " : ""), "NULL"); list.Add(line); } script.Append(string.Concat("\t", string.Join(" ," + Energy.Base.Text.NL + "\t", list.ToArray()), Energy.Base.Text.NL)); script.Append(")"); script.Append(Energy.Base.Text.NL); string engine = string.IsNullOrEmpty(table.Engine) ? DefaultEngine : table.Engine; if (!string.IsNullOrEmpty(engine)) { script.Append("ENGINE = " + engine); } script.Append(";"); script.Append(Energy.Base.Text.NL); return(script.ToString()); }