1 module hunt.entity.dialect.MySQLDialect; 2 3 // import hunt.database; 4 import hunt.entity.dialect.Dialect; 5 import hunt.database.query.Common; 6 import hunt.Exceptions; 7 8 import std.conv; 9 import std.format; 10 import std.variant; 11 12 class MySQLDialect : Dialect 13 { 14 // Database _db; 15 16 this() 17 { 18 // _db = db; Database db 19 } 20 21 Variant fromSqlValue(DlangDataType type,Variant value) 22 { 23 if(typeid(type) == typeid(dBoolType)){ 24 if(*value.peek!string == "1") 25 return Variant(true); 26 else 27 return Variant(false); 28 }else if(typeid(type) == typeid(dFloatType)) 29 return Variant(safeConvert!(string,float)(*value.peek!string)); 30 else if(typeid(type) == typeid(dDoubleType)) 31 return Variant(safeConvert!(string,double)(*value.peek!string)); 32 else if(typeid(type) == typeid(dIntType)) 33 return Variant(safeConvert!(string,int)(*value.peek!string)); 34 else if(typeid(type) == typeid(dLongType)) 35 return Variant(safeConvert!(string,long)(*value.peek!string)); 36 else 37 return value; 38 } 39 string toSqlValueImpl(DlangDataType type,Variant value) 40 { 41 if(typeid(type) == typeid(dBoolType)) 42 return value.get!(bool) ? "1" : "0"; 43 else if(typeid(type) == typeid(dFloatType)) 44 //return isNaN(*value.peek!float) ? "0" : *value.peek!string; 45 return (*value.peek!float).to!string; 46 else if(typeid(type) == typeid(dDoubleType)){ 47 return (*value.peek!double).to!string; 48 //return isNaN(*value.peek!double) ? "0" : *value.peek!string; 49 } 50 else if(typeid(type) == typeid(dIntType)) 51 return value.toString; 52 else { 53 implementationMissing(false); 54 return value.toString(); 55 // FIXME: Needing refactor or cleanup -@zxp at 9/18/2019, 1:49:02 PM 56 // 57 // return _db.escapeLiteral( value.toString); 58 } 59 } 60 string getColumnDefinition(ColumnDefinitionInfo info) { 61 if (!(info.dType in DTypeToPropertyType)) 62 throw new Exception("unsupport type %d of %s".format(info.dType,info.name)); 63 SqlSingleTypeInfo sqlInfo = DTypeToSqlInfo[DTypeToPropertyType[info.dType]]; 64 string unsigned = sqlInfo.unsigned ? " UNSIGNED" : ""; 65 string nullable = info.isNullable ? "" : " NOT NULL"; 66 string primaryKey = info.isId ? " PRIMARY KEY" : ""; 67 string autoIncrease = info.isAuto ? " AUTO_INCREMENT" : ""; 68 int len = info.len == 0 ? sqlInfo.len : info.len; 69 string modifiers = unsigned ~ nullable ~ primaryKey ~ autoIncrease; 70 string lenmodifiers = "("~to!string(len > 0 ? len : 255)~")"~modifiers; 71 switch (sqlInfo.sqlType) { 72 case SqlType.BIGINT: 73 return "BIGINT" ~ modifiers; 74 case SqlType.BIT: 75 return "TINYINT" ~ modifiers; 76 ///sometimes referred to as a type code, that identifies the generic SQL type BLOB. 77 case SqlType.BLOB: 78 return "BLOB"; 79 ///somtimes referred to as a type code, that identifies the generic SQL type BOOLEAN. 80 case SqlType.BOOLEAN: 81 return "BIT" ~ modifiers; 82 ///sometimes referred to as a type code, that identifies the generic SQL type CHAR. 83 case SqlType.CHAR: 84 return "CHAR" ~ lenmodifiers; 85 ///sometimes referred to as a type code, that identifies the generic SQL type CLOB. 86 case SqlType.CLOB: 87 return "MEDIUMTEXT"; 88 //somtimes referred to as a type code, that identifies the generic SQL type DATALINK. 89 //DATALINK, 90 ///sometimes referred to as a type code, that identifies the generic SQL type DATE. 91 case SqlType.DATE: 92 return "DATE" ~ modifiers; 93 ///sometimes referred to as a type code, that identifies the generic SQL type DATETIME. 94 case SqlType.DATETIME: 95 return "DATETIME" ~ modifiers; 96 ///sometimes referred to as a type code, that identifies the generic SQL type DECIMAL. 97 case SqlType.DECIMAL: 98 return "DOUBLE" ~ modifiers; 99 //sometimes referred to as a type code, that identifies the generic SQL type DISTINCT. 100 //DISTINCT, 101 ///sometimes referred to as a type code, that identifies the generic SQL type DOUBLE. 102 case SqlType.DOUBLE: 103 return "DOUBLE" ~ modifiers; 104 ///sometimes referred to as a type code, that identifies the generic SQL type FLOAT. 105 case SqlType.FLOAT: 106 return "FLOAT" ~ modifiers; 107 ///sometimes referred to as a type code, that identifies the generic SQL type INTEGER. 108 case SqlType.INTEGER: 109 return "INT" ~ modifiers; 110 //sometimes referred to as a type code, that identifies the generic SQL type JAVA_OBJECT. 111 //JAVA_OBJECT, 112 ///sometimes referred to as a type code, that identifies the generic SQL type LONGNVARCHAR. 113 case SqlType.LONGNVARCHAR: 114 return "VARCHAR" ~ lenmodifiers; 115 ///sometimes referred to as a type code, that identifies the generic SQL type LONGVARBINARY. 116 case SqlType.LONGVARBINARY: 117 return "VARCHAR" ~ lenmodifiers; 118 ///sometimes referred to as a type code, that identifies the generic SQL type LONGVARCHAR. 119 case SqlType.LONGVARCHAR: 120 return "VARCHAR" ~ lenmodifiers; 121 ///sometimes referred to as a type code, that identifies the generic SQL type NCHAR 122 case SqlType.NCHAR: 123 return "NCHAR" ~ lenmodifiers; 124 ///sometimes referred to as a type code, that identifies the generic SQL type NCLOB. 125 case SqlType.NCLOB: 126 return "MEDIUMTEXT"; 127 ///sometimes referred to as a type code, that identifies the generic SQL type NUMERIC. 128 case SqlType.NUMERIC: 129 return "DOUBLE" ~ modifiers; 130 ///sometimes referred to as a type code, that identifies the generic SQL type NVARCHAR. 131 case SqlType.NVARCHAR: 132 return "NVARCHAR" ~ lenmodifiers; 133 ///sometimes referred to as a type code, that identifies the generic SQL type SMALLINT. 134 case SqlType.SMALLINT: 135 return "SMALLINT" ~ modifiers; 136 //sometimes referred to as a type code, that identifies the generic SQL type XML. 137 //SQLXML, 138 //sometimes referred to as a type code, that identifies the generic SQL type STRUCT. 139 //STRUCT, 140 ///sometimes referred to as a type code, that identifies the generic SQL type TIME. 141 case SqlType.TIME: 142 return "TIME" ~ modifiers; 143 //sometimes referred to as a type code, that identifies the generic SQL type TIMESTAMP. 144 //TIMESTAMP, 145 ///sometimes referred to as a type code, that identifies the generic SQL type TINYINT. 146 case SqlType.TINYINT: 147 return "TINYINT" ~ modifiers; 148 ///sometimes referred to as a type code, that identifies the generic SQL type VARBINARY. 149 case SqlType.VARBINARY: 150 return "VARCHAR" ~ lenmodifiers; 151 ///sometimes referred to as a type code, that identifies the generic SQL type VARCHAR. 152 case SqlType.VARCHAR: 153 return "VARCHAR" ~ lenmodifiers; 154 default: 155 return "VARCHAR(255)"; 156 } 157 } 158 } 159 160 161 162 string[] MYSQL_RESERVED_WORDS = 163 [ 164 "ACCESSIBLE", "ADD", "ALL", 165 "ALTER", "ANALYZE", "AND", 166 "AS", "ASC", "ASENSITIVE", 167 "BEFORE", "BETWEEN", "BIGINT", 168 "BINARY", "BLOB", "BOTH", 169 "BY", "CALL", "CASCADE", 170 "CASE", "CHANGE", "CHAR", 171 "CHARACTER", "CHECK", "COLLATE", 172 "COLUMN", "CONDITION", "CONSTRAINT", 173 "CONTINUE", "CONVERT", "CREATE", 174 "CROSS", "CURRENT_DATE", "CURRENT_TIME", 175 "CURRENT_TIMESTAMP", "CURRENT_USER", "CURSOR", 176 "DATABASE", "DATABASES", "DAY_HOUR", 177 "DAY_MICROSECOND", "DAY_MINUTE", "DAY_SECOND", 178 "DEC", "DECIMAL", "DECLARE", 179 "DEFAULT", "DELAYED", "DELETE", 180 "DESC", "DESCRIBE", "DETERMINISTIC", 181 "DISTINCT", "DISTINCTROW", "DIV", 182 "DOUBLE", "DROP", "DUAL", 183 "EACH", "ELSE", "ELSEIF", 184 "ENCLOSED", "ESCAPED", "EXISTS", 185 "EXIT", "EXPLAIN", "FALSE", 186 "FETCH", "FLOAT", "FLOAT4", 187 "FLOAT8", "FOR", "FORCE", 188 "FOREIGN", "FROM", "FULLTEXT", 189 "GET", "GRANT", "GROUP", 190 "HAVING", "HIGH_PRIORITY", "HOUR_MICROSECOND", 191 "HOUR_MINUTE", "HOUR_SECOND", "IF", 192 "IGNORE", "IN", "INDEX", 193 "INFILE", "INNER", "INOUT", 194 "INSENSITIVE", "INSERT", "INT", 195 "INT1", "INT2", "INT3", 196 "INT4", "INT8", "INTEGER", 197 "INTERVAL", "INTO", "IO_AFTER_GTIDS", 198 "IO_BEFORE_GTIDS", "IS", "ITERATE", 199 "JOIN", "KEY", "KEYS", 200 "KILL", "LEADING", "LEAVE", 201 "LEFT", "LIKE", "LIMIT", 202 "LINEAR", "LINES", "LOAD", 203 "LOCALTIME", "LOCALTIMESTAMP", "LOCK", 204 "LONG", "LONGBLOB", "LONGTEXT", 205 "LOOP", "LOW_PRIORITY", "MASTER_BIND", 206 "MASTER_SSL_VERIFY_SERVER_CERT", "MATCH", "MAXVALUE", 207 "MEDIUMBLOB", "MEDIUMINT", "MEDIUMTEXT", 208 "MIDDLEINT", "MINUTE_MICROSECOND", "MINUTE_SECOND", 209 "MOD", "MODIFIES", "NATURAL", 210 "NOT", "NO_WRITE_TO_BINLOG", "NULL", 211 "NUMERIC", "ON", "OPTIMIZE", 212 "OPTION", "OPTIONALLY", "OR", 213 "ORDER", "OUT", "OUTER", 214 "OUTFILE", "PARTITION", "PRECISION", 215 "PRIMARY", "PROCEDURE", "PURGE", 216 "RANGE", "READ", "READS", 217 "READ_WRITE", "REAL", "REFERENCES", 218 "REGEXP", "RELEASE", "RENAME", 219 "REPEAT", "REPLACE", "REQUIRE", 220 "RESIGNAL", "RESTRICT", "RETURN", 221 "REVOKE", "RIGHT", "RLIKE", 222 "SCHEMA", "SCHEMAS", "SECOND_MICROSECOND", 223 "SELECT", "SENSITIVE", "SEPARATOR", 224 "SET", "SHOW", "SIGNAL", 225 "SMALLINT", "SPATIAL", "SPECIFIC", 226 "SQL", "SQLEXCEPTION", "SQLSTATE", 227 "SQLWARNING", "SQL_BIG_RESULT", "SQL_CALC_FOUND_ROWS", 228 "SQL_SMALL_RESULT", "SSL", "STARTING", 229 "STRAIGHT_JOIN", "TABLE", "TERMINATED", 230 "THEN", "TINYBLOB", "TINYINT", 231 "TINYTEXT", "TO", "TRAILING", 232 "TRIGGER", "TRUE", "UNDO", 233 "UNION", "UNIQUE", "UNLOCK", 234 "UNSIGNED", "UPDATE", "USAGE", 235 "USE", "USING", "UTC_DATE", 236 "UTC_TIME", "UTC_TIMESTAMP", "VALUES", 237 "VARBINARY", "VARCHAR", "VARCHARACTER", 238 "VARYING", "WHEN", "WHERE", 239 "WHILE", "WITH", "WRITE", 240 "XOR", "YEAR_MONTH", "ZEROFILL", 241 "GET", "IO_AFTER_GTIDS", "IO_BEFORE_GTIDS", 242 "MASTER_BIND", "ONE_SHOT", "PARTITION", 243 "SQL_AFTER_GTIDS", "SQL_BEFORE_GTIDS", 244 ]; 245