public UlTable(UlDb OwnerDB, UlTableAttribute attribute, List <UlTableColumnAttribute> ColumnAttributes) { tableAttribute = attribute; columnAttributes = ColumnAttributes; ownerDb = OwnerDB; command = new SqlCommand() { Connection = ownerDb.connection, }; table = new UlTableList <T>(); }
/// <summary> /// Створює чи привязуються до таблиці. Всі дані беруться з атрибутів класу T /// </summary> /// <typeparam name="T">Клас-шаблон для таблиці</typeparam> /// <returns>Таблиця, яка відповідатиме класу T</returns> public UlTable <T> CreateOrLinkToTable <T>() where T : class, new() { string tableName; Type type = typeof(T); UlTable <T> rez; List <UlTableColumnAttribute> columnAttributes = new List <UlTableColumnAttribute>(); UlTableAttribute ulTableAttribute = (UlTableAttribute)type.GetCustomAttributes(false).FirstOrDefault((a) => a is UlTableAttribute); if (ulTableAttribute?.name?.Equals("") ?? true) { tableName = type.Name; rez = new UlTable <T>(this, new UlTableAttribute(tableName), columnAttributes); } else { tableName = ulTableAttribute.name; rez = new UlTable <T>(this, ulTableAttribute, columnAttributes); } command.CommandType = CommandType.Text; command.CommandText = $"IF NOT EXISTS(select * from sys.tables where name='{tableName}') " + $"CREATE TABLE {tableName} ( "; foreach (var prop in type.GetProperties()) { if (prop.GetGetMethod().IsPrivate || prop.GetSetMethod().IsPrivate) { continue; } if (prop.GetCustomAttributes(false).FirstOrDefault((a) => a is UlTableColumnAttribute) is UlTableColumnAttribute columnAttribute) { if (columnAttribute.name?.Equals("") ?? true) { columnAttribute.name = prop.Name; } command.CommandText += columnAttribute.name + ' '; //for(int i = 0; i < table.Columns.Count; i++) { // sqlsc += ","; //} //return sqlsc.Substring(0, sqlsc.Length - 1) + "\n)"; if (columnAttribute.dbType?.Equals("") ?? true) { switch (prop.PropertyType.FullName) { case "System.Byte": columnAttribute.dbType = "tinyint"; break; case "System.Int16": columnAttribute.dbType = "smallint"; break; case "System.Int32": columnAttribute.dbType = "int"; break; case "System.Int64": columnAttribute.dbType = "bigint"; break; case "System.Float": columnAttribute.dbType = "real"; break; case "System.Double": columnAttribute.dbType = "float"; break; case "System.Decimal": columnAttribute.dbType = "decimal"; break; case "System.DateTime": columnAttribute.dbType = "datetime"; break; case "System.String": default: columnAttribute.dbType = "nvarchar(100)"; break; } } command.CommandText += columnAttribute.dbType + ' '; if (columnAttribute.isPrimaryKey) { command.CommandText += "IDENTITY PRIMARY KEY "; } if (columnAttribute.notNull && !columnAttribute.isPrimaryKey) { command.CommandText += "NOT NULL "; } command.CommandText += ", "; columnAttributes.Add(columnAttribute); } } command.CommandText = command.CommandText.Substring(0, command.CommandText.Length - 2); command.CommandText += ")"; connection.Open(); command.ExecuteNonQuery(); connection.Close(); return(rez); }