Add float/double data types

This commit is contained in:
MarioE 2013-11-27 22:00:47 -05:00
parent 234e203eb3
commit 8b7e8772da

View file

@ -45,8 +45,8 @@ namespace TShockAPI.DB
table.Columns.Select( table.Columns.Select(
c => c =>
"'{0}' {1} {2} {3} {4} {5}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "", "'{0}' {1} {2} {3} {4} {5}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "",
c.AutoIncrement ? "AUTOINCREMENT" : "", c.NotNull ? "NOT NULL" : "", c.AutoIncrement ? "AUTOINCREMENT" : "", c.NotNull ? "NOT NULL" : "",
c.Unique ? "UNIQUE" : "")); c.Unique ? "UNIQUE" : ""));
return "CREATE TABLE {0} ({1})".SFormat(EscapeTableName(table.Name), string.Join(", ", columns)); return "CREATE TABLE {0} ({1})".SFormat(EscapeTableName(table.Name), string.Join(", ", columns));
} }
@ -57,14 +57,16 @@ namespace TShockAPI.DB
private static readonly Dictionary<MySqlDbType, string> TypesAsStrings = new Dictionary<MySqlDbType, string> private static readonly Dictionary<MySqlDbType, string> TypesAsStrings = new Dictionary<MySqlDbType, string>
{ {
{MySqlDbType.VarChar, "TEXT"}, { MySqlDbType.VarChar, "TEXT" },
{MySqlDbType.String, "TEXT"}, { MySqlDbType.String, "TEXT" },
{MySqlDbType.Text, "TEXT"}, { MySqlDbType.Text, "TEXT" },
{MySqlDbType.TinyText, "TEXT"}, { MySqlDbType.TinyText, "TEXT" },
{MySqlDbType.MediumText, "TEXT"}, { MySqlDbType.MediumText, "TEXT" },
{MySqlDbType.LongText, "TEXT"}, { MySqlDbType.LongText, "TEXT" },
{MySqlDbType.Int32, "INTEGER"}, { MySqlDbType.Float, "REAL" },
{MySqlDbType.Blob, "BLOB"}, { MySqlDbType.Double, "REAL" },
{ MySqlDbType.Int32, "INTEGER" },
{ MySqlDbType.Blob, "BLOB" },
}; };
public string DbTypeToString(MySqlDbType type, int? length) public string DbTypeToString(MySqlDbType type, int? length)
@ -72,7 +74,7 @@ namespace TShockAPI.DB
string ret; string ret;
if (TypesAsStrings.TryGetValue(type, out ret)) if (TypesAsStrings.TryGetValue(type, out ret))
return ret; return ret;
throw new NotImplementedException(Enum.GetName(typeof (MySqlDbType), type)); throw new NotImplementedException(Enum.GetName(typeof(MySqlDbType), type));
} }
protected override string EscapeTableName(string table) protected override string EscapeTableName(string table)
@ -89,12 +91,12 @@ namespace TShockAPI.DB
table.Columns.Select( table.Columns.Select(
c => c =>
"{0} {1} {2} {3} {4}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "", "{0} {1} {2} {3} {4}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "",
c.AutoIncrement ? "AUTO_INCREMENT" : "", c.NotNull ? "NOT NULL" : "")); c.AutoIncrement ? "AUTO_INCREMENT" : "", c.NotNull ? "NOT NULL" : ""));
var uniques = table.Columns.Where(c => c.Unique).Select(c => c.Name); var uniques = table.Columns.Where(c => c.Unique).Select(c => c.Name);
return "CREATE TABLE {0} ({1} {2})".SFormat(EscapeTableName(table.Name), string.Join(", ", columns), return "CREATE TABLE {0} ({1} {2})".SFormat(EscapeTableName(table.Name), string.Join(", ", columns),
uniques.Count() > 0 uniques.Count() > 0
? ", UNIQUE({0})".SFormat(string.Join(", ", uniques)) ? ", UNIQUE({0})".SFormat(string.Join(", ", uniques))
: ""); : "");
} }
public override string RenameTable(string from, string to) public override string RenameTable(string from, string to)
@ -104,21 +106,23 @@ namespace TShockAPI.DB
private static readonly Dictionary<MySqlDbType, string> TypesAsStrings = new Dictionary<MySqlDbType, string> private static readonly Dictionary<MySqlDbType, string> TypesAsStrings = new Dictionary<MySqlDbType, string>
{ {
{MySqlDbType.VarChar, "VARCHAR"}, { MySqlDbType.VarChar, "VARCHAR" },
{MySqlDbType.String, "CHAR"}, { MySqlDbType.String, "CHAR" },
{MySqlDbType.Text, "TEXT"}, { MySqlDbType.Text, "TEXT" },
{MySqlDbType.TinyText, "TINYTEXT"}, { MySqlDbType.TinyText, "TINYTEXT" },
{MySqlDbType.MediumText, "MEDIUMTEXT"}, { MySqlDbType.MediumText, "MEDIUMTEXT" },
{MySqlDbType.LongText, "LONGTEXT"}, { MySqlDbType.LongText, "LONGTEXT" },
{MySqlDbType.Int32, "INT"}, { MySqlDbType.Float, "FLOAT" },
{ MySqlDbType.Double, "DOUBLE" },
{ MySqlDbType.Int32, "INT" },
}; };
public string DbTypeToString(MySqlDbType type, int? length) public string DbTypeToString(MySqlDbType type, int? length)
{ {
string ret; string ret;
if (TypesAsStrings.TryGetValue(type, out ret)) if (TypesAsStrings.TryGetValue(type, out ret))
return ret + (length != null ? "({0})".SFormat((int) length) : ""); return ret + (length != null ? "({0})".SFormat((int)length) : "");
throw new NotImplementedException(Enum.GetName(typeof (MySqlDbType), type)); throw new NotImplementedException(Enum.GetName(typeof(MySqlDbType), type));
} }
protected override string EscapeTableName(string table) protected override string EscapeTableName(string table)