Skip to content

Commit 5a9f76a

Browse files
committed
NH-3466 - Register supported functions for MySQLDialect
1 parent 450679f commit 5a9f76a

File tree

1 file changed

+151
-84
lines changed

1 file changed

+151
-84
lines changed

src/NHibernate/Dialect/MySQLDialect.cs

Lines changed: 151 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -30,90 +30,157 @@ namespace NHibernate.Dialect
3030
/// </remarks>
3131
public class MySQLDialect : Dialect
3232
{
33-
private readonly TypeNames castTypeNames = new TypeNames();
34-
35-
public MySQLDialect()
36-
{
37-
//Reference 3-4.x
38-
//Numeric:
39-
//http://dev.mysql.com/doc/refman/4.1/en/numeric-type-overview.html
40-
//Date and time:
41-
//http://dev.mysql.com/doc/refman/4.1/en/date-and-time-type-overview.html
42-
//String:
43-
//http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html
44-
//default:
45-
//http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
46-
47-
48-
//string type
49-
RegisterColumnType(DbType.AnsiStringFixedLength, "CHAR(255)");
50-
RegisterColumnType(DbType.AnsiStringFixedLength, 255, "CHAR($l)");
51-
RegisterColumnType(DbType.AnsiStringFixedLength, 65535, "TEXT");
52-
RegisterColumnType(DbType.AnsiStringFixedLength, 16777215, "MEDIUMTEXT");
53-
RegisterColumnType(DbType.AnsiString, "VARCHAR(255)");
54-
RegisterColumnType(DbType.AnsiString, 255, "VARCHAR($l)");
55-
RegisterColumnType(DbType.AnsiString, 65535, "TEXT");
56-
RegisterColumnType(DbType.AnsiString, 16777215, "MEDIUMTEXT");
57-
RegisterColumnType(DbType.StringFixedLength, "CHAR(255)");
58-
RegisterColumnType(DbType.StringFixedLength, 255, "CHAR($l)");
59-
RegisterColumnType(DbType.StringFixedLength, 65535, "TEXT");
60-
RegisterColumnType(DbType.StringFixedLength, 16777215, "MEDIUMTEXT");
61-
RegisterColumnType(DbType.String, "VARCHAR(255)");
62-
RegisterColumnType(DbType.String, 255, "VARCHAR($l)");
63-
RegisterColumnType(DbType.String, 65535, "TEXT");
64-
RegisterColumnType(DbType.String, 16777215, "MEDIUMTEXT");
65-
//todo: future: add compatibility with decimal???
66-
//An unpacked fixed-point number. Behaves like a CHAR column;
67-
//“unpacked” means the number is stored as a string, using one character for each digit of the value.
68-
//M is the total number of digits and D is the number of digits after the decimal point
69-
//DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]
70-
71-
//binary type:
72-
RegisterColumnType(DbType.Binary, "LONGBLOB");
73-
RegisterColumnType(DbType.Binary, 127, "TINYBLOB");
74-
RegisterColumnType(DbType.Binary, 65535, "BLOB");
75-
RegisterColumnType(DbType.Binary, 16777215, "MEDIUMBLOB");
76-
77-
//Numeric type:
78-
RegisterColumnType(DbType.Boolean, "TINYINT(1)"); // SELECT IF(0, 'true', 'false');
79-
RegisterColumnType(DbType.Byte, "TINYINT UNSIGNED");
80-
RegisterColumnType(DbType.Currency, "NUMERIC(18,4)");
81-
RegisterColumnType(DbType.Decimal, "NUMERIC(19,5)");
82-
RegisterColumnType(DbType.Decimal, 19, "NUMERIC($p, $s)");
83-
RegisterColumnType(DbType.Double, "DOUBLE");
84-
//The signed range is -32768 to 32767. The unsigned range is 0 to 65535.
85-
RegisterColumnType(DbType.Int16, "SMALLINT");
86-
RegisterColumnType(DbType.Int32, "INTEGER"); //alias INT
87-
//As of MySQL 4.1, SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.
88-
RegisterColumnType(DbType.Int64, "BIGINT");
89-
//!!!
90-
//Using FLOAT might give you some unexpected problems because all calculations in MySQL are done with double precision
91-
RegisterColumnType(DbType.Single, "FLOAT");
92-
RegisterColumnType(DbType.Byte, 1, "BIT"); //Like TinyInt(i)
93-
RegisterColumnType(DbType.SByte, "TINYINT");
94-
95-
//UNSINGED Numeric type:
96-
RegisterColumnType(DbType.UInt16, "SMALLINT UNSIGNED");
97-
RegisterColumnType(DbType.UInt32, "INTEGER UNSIGNED");
98-
RegisterColumnType(DbType.UInt64, "BIGINT UNSIGNED");
99-
//there are no other DbType unsigned...but mysql support Float unsigned, double unsigned, etc..
100-
101-
//Date and time type:
102-
RegisterColumnType(DbType.Date, "DATE");
103-
RegisterColumnType(DbType.DateTime, "DATETIME");
104-
RegisterColumnType(DbType.Time, "TIME");
105-
106-
//special:
107-
RegisterColumnType(DbType.Guid, "VARCHAR(40)");
108-
109-
RegisterCastTypes();
110-
111-
//functions:
112-
RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "concat(", ",", ")"));
113-
114-
DefaultProperties[Environment.ConnectionDriver] = "NHibernate.Driver.MySqlDataDriver";
115-
}
116-
33+
private readonly TypeNames castTypeNames = new TypeNames();
34+
35+
public MySQLDialect()
36+
{
37+
//Reference 3-4.x
38+
//Numeric:
39+
//http://dev.mysql.com/doc/refman/4.1/en/numeric-type-overview.html
40+
//Date and time:
41+
//http://dev.mysql.com/doc/refman/4.1/en/date-and-time-type-overview.html
42+
//String:
43+
//http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html
44+
//default:
45+
//http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
46+
47+
48+
//string type
49+
RegisterColumnType(DbType.AnsiStringFixedLength, "CHAR(255)");
50+
RegisterColumnType(DbType.AnsiStringFixedLength, 255, "CHAR($l)");
51+
RegisterColumnType(DbType.AnsiStringFixedLength, 65535, "TEXT");
52+
RegisterColumnType(DbType.AnsiStringFixedLength, 16777215, "MEDIUMTEXT");
53+
RegisterColumnType(DbType.AnsiString, "VARCHAR(255)");
54+
RegisterColumnType(DbType.AnsiString, 255, "VARCHAR($l)");
55+
RegisterColumnType(DbType.AnsiString, 65535, "TEXT");
56+
RegisterColumnType(DbType.AnsiString, 16777215, "MEDIUMTEXT");
57+
RegisterColumnType(DbType.StringFixedLength, "CHAR(255)");
58+
RegisterColumnType(DbType.StringFixedLength, 255, "CHAR($l)");
59+
RegisterColumnType(DbType.StringFixedLength, 65535, "TEXT");
60+
RegisterColumnType(DbType.StringFixedLength, 16777215, "MEDIUMTEXT");
61+
RegisterColumnType(DbType.String, "VARCHAR(255)");
62+
RegisterColumnType(DbType.String, 255, "VARCHAR($l)");
63+
RegisterColumnType(DbType.String, 65535, "TEXT");
64+
RegisterColumnType(DbType.String, 16777215, "MEDIUMTEXT");
65+
//todo: future: add compatibility with decimal???
66+
//An unpacked fixed-point number. Behaves like a CHAR column;
67+
//“unpacked” means the number is stored as a string, using one character for each digit of the value.
68+
//M is the total number of digits and D is the number of digits after the decimal point
69+
//DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]
70+
71+
//binary type:
72+
RegisterColumnType(DbType.Binary, "LONGBLOB");
73+
RegisterColumnType(DbType.Binary, 127, "TINYBLOB");
74+
RegisterColumnType(DbType.Binary, 65535, "BLOB");
75+
RegisterColumnType(DbType.Binary, 16777215, "MEDIUMBLOB");
76+
77+
//Numeric type:
78+
RegisterColumnType(DbType.Boolean, "TINYINT(1)"); // SELECT IF(0, 'true', 'false');
79+
RegisterColumnType(DbType.Byte, "TINYINT UNSIGNED");
80+
RegisterColumnType(DbType.Currency, "NUMERIC(18,4)");
81+
RegisterColumnType(DbType.Decimal, "NUMERIC(19,5)");
82+
RegisterColumnType(DbType.Decimal, 19, "NUMERIC($p, $s)");
83+
RegisterColumnType(DbType.Double, "DOUBLE");
84+
//The signed range is -32768 to 32767. The unsigned range is 0 to 65535.
85+
RegisterColumnType(DbType.Int16, "SMALLINT");
86+
RegisterColumnType(DbType.Int32, "INTEGER"); //alias INT
87+
//As of MySQL 4.1, SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.
88+
RegisterColumnType(DbType.Int64, "BIGINT");
89+
//!!!
90+
//Using FLOAT might give you some unexpected problems because all calculations in MySQL are done with double precision
91+
RegisterColumnType(DbType.Single, "FLOAT");
92+
RegisterColumnType(DbType.Byte, 1, "BIT"); //Like TinyInt(i)
93+
RegisterColumnType(DbType.SByte, "TINYINT");
94+
95+
//UNSINGED Numeric type:
96+
RegisterColumnType(DbType.UInt16, "SMALLINT UNSIGNED");
97+
RegisterColumnType(DbType.UInt32, "INTEGER UNSIGNED");
98+
RegisterColumnType(DbType.UInt64, "BIGINT UNSIGNED");
99+
//there are no other DbType unsigned...but mysql support Float unsigned, double unsigned, etc..
100+
101+
//Date and time type:
102+
RegisterColumnType(DbType.Date, "DATE");
103+
RegisterColumnType(DbType.DateTime, "DATETIME");
104+
RegisterColumnType(DbType.Time, "TIME");
105+
106+
//special:
107+
RegisterColumnType(DbType.Guid, "VARCHAR(40)");
108+
109+
RegisterCastTypes();
110+
111+
//functions:
112+
RegisterFunctions();
113+
114+
DefaultProperties[Environment.ConnectionDriver] = "NHibernate.Driver.MySqlDataDriver";
115+
}
116+
117+
protected virtual void RegisterFunctions()
118+
{
119+
RegisterFunction("iif", new StandardSQLFunction("if"));
120+
121+
RegisterFunction("sign", new StandardSQLFunction("sign", NHibernateUtil.Int32));
122+
123+
RegisterFunction("acos", new StandardSQLFunction("acos", NHibernateUtil.Double));
124+
RegisterFunction("asin", new StandardSQLFunction("asin", NHibernateUtil.Double));
125+
RegisterFunction("atan", new StandardSQLFunction("atan", NHibernateUtil.Double));
126+
RegisterFunction("atan2", new StandardSQLFunction("atan2", NHibernateUtil.Double));
127+
RegisterFunction("cos", new StandardSQLFunction("cos", NHibernateUtil.Double));
128+
RegisterFunction("cot", new StandardSQLFunction("cot", NHibernateUtil.Double));
129+
RegisterFunction("sin", new StandardSQLFunction("sin", NHibernateUtil.Double));
130+
RegisterFunction("tan", new StandardSQLFunction("tan", NHibernateUtil.Double));
131+
RegisterFunction("log", new StandardSQLFunction("log", NHibernateUtil.Double));
132+
RegisterFunction("log10", new StandardSQLFunction("log10", NHibernateUtil.Double));
133+
RegisterFunction("ln", new StandardSQLFunction("ln", NHibernateUtil.Double));
134+
135+
RegisterFunction("ceil", new StandardSQLFunction("ceil"));
136+
RegisterFunction("ceiling", new StandardSQLFunction("ceiling"));
137+
RegisterFunction("floor", new StandardSQLFunction("floor"));
138+
RegisterFunction("round", new StandardSQLFunction("round"));
139+
RegisterFunction("truncate", new StandardSQLFunction("truncate"));
140+
141+
RegisterFunction("rand", new NoArgSQLFunction("rand", NHibernateUtil.Double));
142+
143+
RegisterFunction("power", new StandardSQLFunction("power", NHibernateUtil.Single));
144+
145+
RegisterFunction("stddev", new StandardSQLFunction("stddev", NHibernateUtil.Double));
146+
RegisterFunction("variance", new StandardSQLFunction("variance", NHibernateUtil.Double));
147+
148+
RegisterFunction("degrees", new StandardSQLFunction("degrees", NHibernateUtil.Double));
149+
RegisterFunction("radians", new StandardSQLFunction("radians", NHibernateUtil.Double));
150+
RegisterFunction("exp", new StandardSQLFunction("exp", NHibernateUtil.Double));
151+
152+
RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "concat(", ",", ")"));
153+
RegisterFunction("replace", new StandardSafeSQLFunction("replace", NHibernateUtil.String, 3));
154+
RegisterFunction("ltrim", new StandardSQLFunction("ltrim"));
155+
RegisterFunction("rtrim", new StandardSQLFunction("ltrim"));
156+
RegisterFunction("left", new StandardSQLFunction("left", NHibernateUtil.String));
157+
RegisterFunction("right", new StandardSQLFunction("right", NHibernateUtil.String));
158+
159+
RegisterFunction("ucase", new StandardSQLFunction("ucase"));
160+
RegisterFunction("lcase", new StandardSQLFunction("lcase"));
161+
162+
RegisterFunction("chr", new StandardSQLFunction("char", NHibernateUtil.Character));
163+
RegisterFunction("ascii", new StandardSQLFunction("ascii", NHibernateUtil.Int32));
164+
RegisterFunction("instr", new StandardSQLFunction("instr", NHibernateUtil.Int32));
165+
RegisterFunction("lpad", new StandardSQLFunction("lpad", NHibernateUtil.String));
166+
RegisterFunction("rpad", new StandardSQLFunction("rpad", NHibernateUtil.String));
167+
168+
RegisterFunction("hex", new StandardSQLFunction("hex", NHibernateUtil.String));
169+
RegisterFunction("soundex", new StandardSQLFunction("soundex", NHibernateUtil.String));
170+
171+
RegisterFunction("current_date", new NoArgSQLFunction("current_date", NHibernateUtil.Date, false));
172+
RegisterFunction("current_time", new NoArgSQLFunction("current_time", NHibernateUtil.Time, false));
173+
174+
RegisterFunction("second", new StandardSQLFunction("second", NHibernateUtil.Int32));
175+
RegisterFunction("minute", new StandardSQLFunction("minute", NHibernateUtil.Int32));
176+
RegisterFunction("hour", new StandardSQLFunction("hour", NHibernateUtil.Int32));
177+
RegisterFunction("day", new StandardSQLFunction("day", NHibernateUtil.Int32));
178+
RegisterFunction("month", new StandardSQLFunction("month", NHibernateUtil.Int32));
179+
RegisterFunction("year", new StandardSQLFunction("year", NHibernateUtil.Int32));
180+
RegisterFunction("date", new StandardSQLFunction("date", NHibernateUtil.Int32));
181+
RegisterFunction("last_day", new StandardSQLFunction("last_day", NHibernateUtil.Date));
182+
}
183+
117184
/// <summary></summary>
118185
public override string AddColumnString
119186
{

0 commit comments

Comments
 (0)