public override void ChangeColumn(string tableName, DataFieldInfo field) { string type = TypeToMysql(field); string defaultValue = String.Empty; string notNull = String.Empty; if (field.Type == typeof(string)) { if (field.Length == 0) { throw new Exception("String Length must not be zero thrown on " + field.Name); } } if (type.ToLower() == "text" || type.ToLower() == "mediumtext" || type.ToLower() == "longtext") { notNull = " NOT NULL"; } if (type.ToLower().Contains("int(")) { if ((field.Key & DataFieldKeys.Primary) == DataFieldKeys.Primary) { notNull = " NOT NULL"; if (Index.GetFields(field.PrimaryIndex.Key, GetFields(tableName)).Count == 1) { defaultValue = " DEFAULT NULL AUTO_INCREMENT"; } else { defaultValue = " DEFAULT 0"; } } else { defaultValue = " DEFAULT 0"; } } if (type.ToLower() == "float") { defaultValue = " DEFAULT 0"; } string query = string.Format(@"ALTER TABLE `{0}` MODIFY COLUMN `{1}` {2}{3}{4};", Mysql.Escape(tableName), Mysql.Escape(field.Name), type, notNull, defaultValue); try { UpdateQuery(query); } catch (Exception ex) { // DEBUG HERE } }
private string TypeToMysql(DataFieldInfo type) { if (type.Type == typeof(ulong)) { return "bigint(20) unsigned"; } else if (type.Type == typeof(long)) { return "bigint(20)"; } else if (type.Type == typeof(uint)) { return "int(11) unsigned"; } else if (type.Type == typeof(int)) { return "int(11)"; } else if (type.Type == typeof(ushort)) { return "smallint(5) unsigned"; } else if (type.Type == typeof(short)) { return "smallint(5)"; } else if (type.Type == typeof(byte)) { return "tinyint(3) unsigned"; } else if (type.Type == typeof(sbyte)) { return "tinyint(3)"; } else if (type.Type == typeof(bool)) { return "tinyint(1) unsigned"; } else if (type.Type == typeof(float)) { return "float"; } else if (type.Type == typeof(string)) { if (type.Length < 256) { return string.Format("varchar({0})", type.Length); } else if (type.Length < 65536) { return "text"; } else if (type.Length < 16777216) { return "mediumtext"; } else { return "longtext"; } } else if (type.Type == typeof(char[])) { return string.Format("char({0})", type.Length); } else { return "varchar(255)"; } }
public override void AddColumn(string tableName, DataFieldInfo field) { string type = TypeToMysql(field); string defaultValue = String.Empty; string notNull = String.Empty; if (type.ToLower() == "text" || type.ToLower() == "mediumtext" || type.ToLower() == "longtext") { notNull = " NOT NULL"; } if (type.ToLower().Contains("int(")) { if ((field.Key & DataFieldKeys.Primary) == DataFieldKeys.Primary) { notNull = " NOT NULL"; if (Index.GetFields(field.PrimaryIndex.Key, GetFields(tableName)).Count == 1) { defaultValue = " DEFAULT NULL AUTO_INCREMENT"; } else { defaultValue = " DEFAULT 0"; } } else { defaultValue = " DEFAULT 0"; } } if (type.ToLower() == "float") { defaultValue = " DEFAULT 0"; } string query = string.Format(@"ALTER TABLE `{0}` ADD COLUMN `{1}` {2}{3}{4};", Mysql.Escape(tableName), Mysql.Escape(field.Name), type, notNull, defaultValue); UpdateQuery(query); }