Datei: Provider/MySqlConnectorProvider/NDO.MySqlConnector/Generator.cs
Last Commit (4b01a93)
| 1 | -- File didn't exist -- |
New Commit (e404055)
| 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.Text; |
| 25 | using System.Text.RegularExpressions; |
| 26 | using System.Data; |
| 27 | using NDOInterfaces; |
| 28 | |
| 29 | namespace MySqlConnectorProvider |
| 30 | { |
| 31 | ····public class MySqlGenerator : AbstractSQLGenerator |
| 32 | ····{ |
| 33 | |
| 34 | ········public MySqlGenerator() |
| 35 | ········{············ |
| 36 | ········} |
| 37 | |
| 38 | ········public override string ProviderName |
| 39 | ········{ |
| 40 | ············get { return "MySqlConnector"; } |
| 41 | ········} |
| 42 | |
| 43 | ········public override string DropTable(string tableName) |
| 44 | ········{ |
| 45 | ············return "DROP TABLE IF EXISTS " + tableName + ";"; |
| 46 | ········} |
| 47 | |
| 48 | ········public override string ConnectToDatabase(string connectionString) |
| 49 | ········{ |
| 50 | ············Regex regex = new Regex(@"Database\s*=\s*([^;]+)"); |
| 51 | ············Match match = regex.Match(connectionString); |
| 52 | ············if (match.Success) |
| 53 | ············{ |
| 54 | ················return("USE " + match.Groups[1] + ";"); |
| 55 | ············} |
| 56 | ············return string.Empty; |
| 57 | ········} |
| 58 | |
| 59 | |
| 60 | |
| 61 | ········public override bool LengthAllowed(Type t) |
| 62 | ········{ |
| 63 | ············return t == typeof(string) || t == typeof(decimal); |
| 64 | ········} |
| 65 | |
| 66 | ········public override bool LengthAllowed(string dbType) |
| 67 | ········{ |
| 68 | ············return (string.Compare(dbType, "varchar", true) == 0 || string.Compare(dbType, "decimal", true) == 0); |
| 69 | ········} |
| 70 | |
| 71 | ········public override string DbTypeFromType(Type t, int size) |
| 72 | ········{ |
| 73 | ············if (t == typeof( bool ) || t == typeof( Byte )) |
| 74 | ················return "Tinyint"; |
| 75 | ············else if (t == typeof( DateTime )) |
| 76 | ················return "Datetime"; |
| 77 | ············else if (t == typeof( decimal )) |
| 78 | ················return "Decimal"; |
| 79 | ············else if (t == typeof( double )) |
| 80 | ················return "Double"; |
| 81 | ············else if (t.IsEnum) |
| 82 | ················return "Int"; |
| 83 | ············else if (t == typeof( float )) |
| 84 | ················return "Float"; |
| 85 | ············else if (t == typeof( Guid )) |
| 86 | ················return "Varchar"; |
| 87 | ············else if (t == typeof( Int16 ) || t == typeof( UInt16 )) |
| 88 | ················return "Smallint"; |
| 89 | ············else if (t == typeof( Int32 ) || t == typeof( UInt32 )) |
| 90 | ················return "Int"; |
| 91 | ············else if (t == typeof( Int64 ) || t == typeof( UInt64 )) |
| 92 | ················return "BigInt"; |
| 93 | ············else if (t == typeof( string ) && size == -1) |
| 94 | ················return "TEXT"; |
| 95 | ············else if (t == typeof( string )) |
| 96 | ················return "Varchar"; |
| 97 | ············else if (t == typeof( byte[] )) |
| 98 | ················return "MediumBlob"; |
| 99 | ············throw new Exception("Can't resolve type " + t.FullName + " as storable."); |
| 100 | ········} |
| 101 | |
| 102 | ········public override string AutoIncrementColumn(string columnName, Type dataType, string columnType, string width) |
| 103 | ········{ |
| 104 | ············return columnName + " Int AUTO_INCREMENT"; |
| 105 | ········} |
| 106 | |
| 107 | ········public override bool HasSpecialAutoIncrementColumnFormat |
| 108 | ········{ |
| 109 | ············get { return true; } |
| 110 | ········} |
| 111 | |
| 112 | ········public string CreateConstraint(DataColumn[] primaryKeyColumns, string constraintName, string tableName) |
| 113 | ········{ |
| 114 | ············StringBuilder sb = new StringBuilder("CONSTRAINT "); |
| 115 | ············sb.Append(constraintName); |
| 116 | ············sb.Append(" PRIMARY KEY ("); |
| 117 | ············int lastIndex = primaryKeyColumns.Length - 1; |
| 118 | ············for (int i = 0; i < primaryKeyColumns.Length; i++) |
| 119 | ············{ |
| 120 | ················DataColumn dc = primaryKeyColumns[i]; |
| 121 | ················sb.Append(Provider.GetQuotedName(dc.ColumnName)); |
| 122 | ················if (i < lastIndex) |
| 123 | ····················sb.Append(","); |
| 124 | ············} |
| 125 | ············sb.Append(")"); |
| 126 | ············return sb.ToString(); |
| 127 | ········} |
| 128 | |
| 129 | |
| 130 | ········public override string RemoveColumn(string colName) |
| 131 | ········{ |
| 132 | ············return "DROP " + colName; |
| 133 | ········} |
| 134 | |
| 135 | ········//ALTER TABLE EMPLOYEE CHANGE old_col_name new_col_name char(20) (mysql) |
| 136 | ········public override string RenameColumn(string tableName, string oldName, string newName, string typeName) |
| 137 | ········{ |
| 138 | ············return "ALTER TABLE " + tableName + " CHANGE " + oldName + ' ' + newName + ' ' + typeName; |
| 139 | ········} |
| 140 | |
| 141 | ········public override string AlterColumnType() |
| 142 | ········{ |
| 143 | ············return "MODIFY"; |
| 144 | ········} |
| 145 | |
| 146 | |
| 147 | |
| 148 | ····} |
| 149 | |
| 150 | } |
| 151 |