Datei: Provider/MySqlNdoProvider/NDO.MySql/Generator.cs
Last Commit (2a2e7ba)
1 | // |
2 | // Copyright (c) 2002-2016 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using System; |
24 | using System.Reflection; |
25 | using System.Text; |
26 | using System.Text.RegularExpressions; |
27 | using System.Collections; |
28 | using System.Data; |
29 | using System.IO; |
30 | using NDO; |
31 | using NDOInterfaces; |
32 | |
33 | namespace MySqlProvider |
34 | { |
35 | ····public class MySqlGenerator : AbstractSQLGenerator |
36 | ····{ |
37 | |
38 | ········public MySqlGenerator() |
39 | ········{············ |
40 | ········} |
41 | |
42 | ········public override string ProviderName |
43 | ········{ |
44 | ············get { return "MySql"; } |
45 | ········} |
46 | |
47 | ········public override string DropTable(string tableName) |
48 | ········{ |
49 | ············return "DROP TABLE IF EXISTS " + tableName + ";"; |
50 | ········} |
51 | |
52 | ········public override string ConnectToDatabase(string connectionString) |
53 | ········{ |
54 | ············Regex regex = new Regex(@"Database\s*=\s*([^;]+)"); |
55 | ············Match match = regex.Match(connectionString); |
56 | ············if (match.Success) |
57 | ············{ |
58 | ················return("USE " + match.Groups[1] + ";"); |
59 | ············} |
60 | ············return string.Empty; |
61 | ········} |
62 | |
63 | |
64 | |
65 | ········public override bool LengthAllowed(Type t) |
66 | ········{ |
67 | ············return t == typeof(string) || t == typeof(decimal); |
68 | ········} |
69 | |
70 | ········public override bool LengthAllowed(string dbType) |
71 | ········{ |
72 | ············return (string.Compare(dbType, "varchar", true) == 0 || string.Compare(dbType, "decimal", true) == 0); |
73 | ········} |
74 | |
75 | ········public override string DbTypeFromType(Type t, int size) |
76 | ········{ |
77 | ············if (t == typeof( bool ) || t == typeof( Byte )) |
78 | ················return "Tinyint"; |
79 | ············else if (t == typeof( DateTime )) |
80 | ················return "Datetime"; |
81 | ············else if (t == typeof( decimal )) |
82 | ················return "Decimal"; |
83 | ············else if (t == typeof( double )) |
84 | ················return "Double"; |
85 | ············else if (t.IsEnum) |
86 | ················return "Int"; |
87 | ············else if (t == typeof( float )) |
88 | ················return "Float"; |
89 | ············else if (t == typeof( Guid )) |
90 | ················return "Varchar"; |
91 | ············else if (t == typeof( Int16 ) || t == typeof( UInt16 )) |
92 | ················return "Smallint"; |
93 | ············else if (t == typeof( Int32 ) || t == typeof( UInt32 )) |
94 | ················return "Int"; |
95 | ············else if (t == typeof( Int64 ) || t == typeof( UInt64 )) |
96 | ················return "BigInt"; |
97 | ············else if (t == typeof( string ) && size == -1) |
98 | ················return "TEXT"; |
99 | ············else if (t == typeof( string )) |
100 | ················return "Varchar"; |
101 | ············else if (t == typeof( byte[] )) |
102 | ················return "MediumBlob"; |
103 | ············throw new Exception("Can't resolve type " + t.FullName + " as storable."); |
104 | ········} |
105 | |
106 | public override string AutoIncrementColumn( string columnName, Type dataType, string columnType, string width) |
107 | ········{ |
108 | return columnName + " Int AUTO_INCREMENT"; |
109 | ········} |
110 | |
111 | ········public override bool HasSpecialAutoIncrementColumnFormat |
112 | ········{ |
113 | ············get { return true; } |
114 | ········} |
115 | |
116 | ········public string CreateConstraint(DataColumn[] primaryKeyColumns, string constraintName, string tableName) |
117 | ········{ |
118 | ············StringBuilder sb = new StringBuilder("CONSTRAINT "); |
119 | ············sb.Append(constraintName); |
120 | ············sb.Append(" PRIMARY KEY ("); |
121 | ············int lastIndex = primaryKeyColumns.Length - 1; |
122 | ············for (int i = 0; i < primaryKeyColumns.Length; i++) |
123 | ············{ |
124 | ················DataColumn dc = primaryKeyColumns[i]; |
125 | ················sb.Append(Provider.GetQuotedName(dc.ColumnName)); |
126 | ················if (i < lastIndex) |
127 | ····················sb.Append(","); |
128 | ············} |
129 | ············sb.Append(")"); |
130 | ············return sb.ToString(); |
131 | ········} |
132 | |
133 | |
134 | ········public override string RemoveColumn(string colName) |
135 | ········{ |
136 | ············return "DROP " + colName; |
137 | ········} |
138 | |
139 | ········//ALTER TABLE EMPLOYEE CHANGE old_col_name new_col_name char(20) (mysql) |
140 | ········public override string RenameColumn(string tableName, string oldName, string newName, string typeName) |
141 | ········{ |
142 | ············return "ALTER TABLE " + tableName + " CHANGE " + oldName + ' ' + newName + ' ' + typeName; |
143 | ········} |
144 | |
145 | ········public override string AlterColumnType() |
146 | ········{ |
147 | ············return "MODIFY"; |
148 | ········} |
149 | |
150 | |
151 | |
152 | ····} |
153 | |
154 | } |
155 |
New Commit (1e68dc7)
1 | // |
2 | // Copyright (c) 2002-2016 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using System; |
24 | using System.Reflection; |
25 | using System.Text; |
26 | using System.Text.RegularExpressions; |
27 | using System.Collections; |
28 | using System.Data; |
29 | using System.IO; |
30 | using NDO; |
31 | using NDOInterfaces; |
32 | |
33 | namespace MySqlProvider |
34 | { |
35 | ····public class MySqlGenerator : AbstractSQLGenerator |
36 | ····{ |
37 | |
38 | ········public MySqlGenerator() |
39 | ········{············ |
40 | ········} |
41 | |
42 | ········public override string ProviderName |
43 | ········{ |
44 | ············get { return "MySql"; } |
45 | ········} |
46 | |
47 | ········public override string DropTable(string tableName) |
48 | ········{ |
49 | ············return "DROP TABLE IF EXISTS " + tableName + ";"; |
50 | ········} |
51 | |
52 | ········public override string ConnectToDatabase(string connectionString) |
53 | ········{ |
54 | ············Regex regex = new Regex(@"Database\s*=\s*([^;]+)"); |
55 | ············Match match = regex.Match(connectionString); |
56 | ············if (match.Success) |
57 | ············{ |
58 | ················return("USE " + match.Groups[1] + ";"); |
59 | ············} |
60 | ············return string.Empty; |
61 | ········} |
62 | |
63 | |
64 | |
65 | ········public override bool LengthAllowed(Type t) |
66 | ········{ |
67 | ············return t == typeof(string) || t == typeof(decimal); |
68 | ········} |
69 | |
70 | ········public override bool LengthAllowed(string dbType) |
71 | ········{ |
72 | ············return (string.Compare(dbType, "varchar", true) == 0 || string.Compare(dbType, "decimal", true) == 0); |
73 | ········} |
74 | |
75 | ········public override string DbTypeFromType(Type t, int size) |
76 | ········{ |
77 | ············if (t == typeof( bool ) || t == typeof( Byte )) |
78 | ················return "Tinyint"; |
79 | ············else if (t == typeof( DateTime )) |
80 | ················return "Datetime"; |
81 | ············else if (t == typeof( decimal )) |
82 | ················return "Decimal"; |
83 | ············else if (t == typeof( double )) |
84 | ················return "Double"; |
85 | ············else if (t.IsEnum) |
86 | ················return "Int"; |
87 | ············else if (t == typeof( float )) |
88 | ················return "Float"; |
89 | ············else if (t == typeof( Guid )) |
90 | ················return "Varchar"; |
91 | ············else if (t == typeof( Int16 ) || t == typeof( UInt16 )) |
92 | ················return "Smallint"; |
93 | ············else if (t == typeof( Int32 ) || t == typeof( UInt32 )) |
94 | ················return "Int"; |
95 | ············else if (t == typeof( Int64 ) || t == typeof( UInt64 )) |
96 | ················return "BigInt"; |
97 | ············else if (t == typeof( string ) && size == -1) |
98 | ················return "TEXT"; |
99 | ············else if (t == typeof( string )) |
100 | ················return "Varchar"; |
101 | ············else if (t == typeof( byte[] )) |
102 | ················return "MediumBlob"; |
103 | ············throw new Exception("Can't resolve type " + t.FullName + " as storable."); |
104 | ········} |
105 | |
106 | public override PrimaryConstraintPlacement PrimaryConstraintPlacement => PrimaryConstraintPlacement. InColumn; |
107 | |
108 | ········public override string AutoIncrementColumn(string columnName, Type dataType, string columnType, string width, bool isPrimary) |
109 | ········{ |
110 | string primary = isPrimary ? " PRIMARY KEY" : ""; |
111 | ············return $"{columnName} Int AUTO_INCREMENT{primary}"; |
112 | ········} |
113 | |
114 | ········public override bool HasSpecialAutoIncrementColumnFormat |
115 | ········{ |
116 | ············get { return true; } |
117 | ········} |
118 | |
119 | ········public override string RemoveColumn(string colName) |
120 | ········{ |
121 | ············return "DROP " + colName; |
122 | ········} |
123 | |
124 | ········//ALTER TABLE EMPLOYEE CHANGE old_col_name new_col_name char(20) (mysql) |
125 | ········public override string RenameColumn(string tableName, string oldName, string newName, string typeName) |
126 | ········{ |
127 | ············return "ALTER TABLE " + tableName + " CHANGE " + oldName + ' ' + newName + ' ' + typeName; |
128 | ········} |
129 | |
130 | ········public override string AlterColumnType() |
131 | ········{ |
132 | ············return "MODIFY"; |
133 | ········} |
134 | ····} |
135 | |
136 | } |
137 |