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 static Table Create(Type type) { Table table = new Table(); Energy.Attribute.Data.TableAttribute attributeTable = (Energy.Attribute.Data.TableAttribute) Energy.Base.Class.GetClassAttribute(type, typeof(Energy.Attribute.Data.TableAttribute)); if (attributeTable != null) { table.Name = attributeTable.Name; table.Description = attributeTable.Description; } string[] fields = Energy.Base.Class.GetFieldsAndProperties(type); for (int i = 0; i < fields.Length; i++) { Energy.Source.Structure.Column column = table.Columns.New(fields[i]); //typeof(Energy.Attribute.Data.PrimaryAttribute) Energy.Attribute.Data.PrimaryAttribute attributePrimary = (Energy.Attribute.Data.PrimaryAttribute) Energy.Base.Class.GetFieldOrPropertyAttribute(type, fields[i], typeof(Energy.Attribute.Data.PrimaryAttribute)); if (attributePrimary != null) { column.Primary = true; } Energy.Attribute.Data.ColumnAttribute attributeColumn = (Energy.Attribute.Data.ColumnAttribute) Energy.Base.Class.GetFieldOrPropertyAttribute(type, fields[i], typeof(Energy.Attribute.Data.ColumnAttribute)); if (attributeColumn != null) { column.Name = attributeColumn.Name; } Energy.Attribute.Data.TypeAttribute attributeType = (Energy.Attribute.Data.TypeAttribute) Energy.Base.Class.GetFieldOrPropertyAttribute(type, fields[i], typeof(Energy.Attribute.Data.TypeAttribute)); if (attributeType != null) { column.Type = attributeType.Name; } Energy.Attribute.Data.LabelAttribute attributeLabel = (Energy.Attribute.Data.LabelAttribute) Energy.Base.Class.GetFieldOrPropertyAttribute(type, fields[i], typeof(Energy.Attribute.Data.LabelAttribute)); if (attributeLabel != null) { column.Label = attributeLabel.Label; } Energy.Attribute.Data.DescriptionAttribute attributeDescription = (Energy.Attribute.Data.DescriptionAttribute) Energy.Base.Class.GetFieldOrPropertyAttribute(type, fields[i], typeof(Energy.Attribute.Data.DescriptionAttribute)); if (attributeDescription != null) { column.Description = attributeDescription.Description; } } return(table); }
public string CreateTable(Energy.Source.Structure.Table table) { 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 string DropColumn(Energy.Source.Structure.Column column) { 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()); }