Datei: Provider/PostGresProvider/NDO.Postgre/Generator.cs
Last Commit (fbaa1b0)
| 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 NDO.PostGreProvider |
| 34 | { |
| 35 | ····public class PostGreGenerator : AbstractSQLGenerator |
| 36 | ····{ |
| 37 | |
| 38 | ········public PostGreGenerator() |
| 39 | ········{············ |
| 40 | ········} |
| 41 | |
| 42 | ········public override string ProviderName |
| 43 | ········{ |
| 44 | ············get { return "Postgre"; } |
| 45 | ········} |
| 46 | |
| 47 | ········public override string DropTable(string tableName) |
| 48 | ········{ |
| 49 | ············return "DROP TABLE " + tableName + ";"; |
| 50 | ········} |
| 51 | |
| 52 | |
| 53 | ········public override bool LengthAllowed(Type t) |
| 54 | ········{ |
| 55 | ············return t == typeof(string) || t == typeof(decimal) || t == typeof(Guid); |
| 56 | ········} |
| 57 | |
| 58 | ········public override bool LengthAllowed(string dbType) |
| 59 | ········{ |
| 60 | ············return (string.Compare(dbType, "char", true) == 0 || string.Compare(dbType, "varchar", true) == 0 || string.Compare(dbType, "numeric", true) == 0); |
| 61 | ········} |
| 62 | |
| 63 | ········public override string DbTypeFromType(Type t, int size) |
| 64 | ········{ |
| 65 | ············if (t == typeof(bool)) |
| 66 | ················return("bool"); |
| 67 | ············else if (t == typeof(Byte)) |
| 68 | ················return "int2"; |
| 69 | ············else if (t == typeof(Byte[])) |
| 70 | ················return "bytea"; |
| 71 | ············else if (t == typeof(DateTime)) |
| 72 | ················return "date"; |
| 73 | ············else if (t == typeof(decimal)) |
| 74 | ················return "numeric"; |
| 75 | ············else if (t == typeof(double)) |
| 76 | ················return "float8"; |
| 77 | ············else if (t.IsEnum) |
| 78 | ················return "int4"; |
| 79 | ············else if (t == typeof(float)) |
| 80 | ················return "real"; |
| 81 | ············else if (t == typeof(Guid)) |
| 82 | ················return "char"; |
| 83 | ············else if (t == typeof(Int16) || t == typeof(UInt16)) |
| 84 | ················return "int2"; |
| 85 | ············else if (t == typeof(Int32) || t == typeof(UInt32)) |
| 86 | ················return "int4"; |
| 87 | ············else if (t == typeof(Int64) || t == typeof(UInt64)) |
| 88 | ················return "int8"; |
| 89 | ············else if (t == typeof( string ) && size == -1) |
| 90 | ················return "text"; |
| 91 | ············else if (t == typeof(string)) |
| 92 | ················return "varchar"; |
| 93 | ············throw new Exception("Can't resolve type " + t.FullName + " as storable."); |
| 94 | ········} |
| 95 | |
| 96 | ········public override string AutoIncrementColumn(string columnName, Type dataType, string columnType, string width, bool isPrimary) |
| 97 | ········{ |
| 98 | var primary = isPrimary ? " PRIMARY KEY" : String. Empty; |
| 99 | return columnName + " SERIAL" + primary; |
| 100 | ········} |
| 101 | |
| 102 | ········public override bool HasSpecialAutoIncrementColumnFormat => true; |
| 103 | |
| 104 | ········public override PrimaryConstraintPlacement PrimaryConstraintPlacement |
| 105 | ········{ |
| 106 | ············get { return PrimaryConstraintPlacement.InColumn; } |
| 107 | ········} |
| 108 | |
| 109 | ········public override string PrimaryKeyColumn(string columnName, Type dataType, string columnType, string width) |
| 110 | ········{ |
| 111 | ············string coldef = columnName + " " + columnType; |
| 112 | ············if (dataType == typeof(string) || dataType == typeof(Guid)) |
| 113 | ················coldef += '(' + width + ')'; |
| 114 | ············return coldef + " PRIMARY KEY"; |
| 115 | ········} |
| 116 | |
| 117 | ····} |
| 118 | |
| 119 | } |
| 120 |
New Commit (f4c9ea2)
| 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 NDO.PostGreProvider |
| 34 | { |
| 35 | ····public class PostGreGenerator : AbstractSQLGenerator |
| 36 | ····{ |
| 37 | |
| 38 | ········public PostGreGenerator() |
| 39 | ········{············ |
| 40 | ········} |
| 41 | |
| 42 | ········public override string ProviderName |
| 43 | ········{ |
| 44 | ············get { return "Postgre"; } |
| 45 | ········} |
| 46 | |
| 47 | ········public override string DropTable(string tableName) |
| 48 | ········{ |
| 49 | ············return "DROP TABLE " + tableName + ";"; |
| 50 | ········} |
| 51 | |
| 52 | |
| 53 | ········public override bool LengthAllowed(Type t) |
| 54 | ········{ |
| 55 | ············return t == typeof(string) || t == typeof(decimal) || t == typeof(Guid); |
| 56 | ········} |
| 57 | |
| 58 | ········public override bool LengthAllowed(string dbType) |
| 59 | ········{ |
| 60 | ············return (string.Compare(dbType, "char", true) == 0 || string.Compare(dbType, "varchar", true) == 0 || string.Compare(dbType, "numeric", true) == 0); |
| 61 | ········} |
| 62 | |
| 63 | ········public override string DbTypeFromType(Type t, int size) |
| 64 | ········{ |
| 65 | ············if (t == typeof(bool)) |
| 66 | ················return("bool"); |
| 67 | ············else if (t == typeof(Byte)) |
| 68 | ················return "int2"; |
| 69 | ············else if (t == typeof(Byte[])) |
| 70 | ················return "bytea"; |
| 71 | ············else if (t == typeof(DateTime)) |
| 72 | ················return "date"; |
| 73 | ············else if (t == typeof(decimal)) |
| 74 | ················return "numeric"; |
| 75 | ············else if (t == typeof(double)) |
| 76 | ················return "float8"; |
| 77 | ············else if (t.IsEnum) |
| 78 | ················return "int4"; |
| 79 | ············else if (t == typeof(float)) |
| 80 | ················return "real"; |
| 81 | ············else if (t == typeof(Guid)) |
| 82 | ················return "char"; |
| 83 | ············else if (t == typeof(Int16) || t == typeof(UInt16)) |
| 84 | ················return "int2"; |
| 85 | ············else if (t == typeof(Int32) || t == typeof(UInt32)) |
| 86 | ················return "int4"; |
| 87 | ············else if (t == typeof(Int64) || t == typeof(UInt64)) |
| 88 | ················return "int8"; |
| 89 | ············else if (t == typeof( string ) && size == -1) |
| 90 | ················return "text"; |
| 91 | ············else if (t == typeof(string)) |
| 92 | ················return "varchar"; |
| 93 | ············throw new Exception("Can't resolve type " + t.FullName + " as storable."); |
| 94 | ········} |
| 95 | |
| 96 | ········public override string AutoIncrementColumn(string columnName, Type dataType, string columnType, string width, bool isPrimary) |
| 97 | ········{ |
| 98 | // This always results in a primary key column. |
| 99 | return $"{ columnName} { columnType} GENERATED ALWAYS AS IDENTITY"; |
| 100 | ········} |
| 101 | |
| 102 | ········public override bool HasSpecialAutoIncrementColumnFormat => true; |
| 103 | |
| 104 | ········public override PrimaryConstraintPlacement PrimaryConstraintPlacement |
| 105 | ········{ |
| 106 | ············get { return PrimaryConstraintPlacement.InColumn; } |
| 107 | ········} |
| 108 | |
| 109 | ········public override string PrimaryKeyColumn(string columnName, Type dataType, string columnType, string width) |
| 110 | ········{ |
| 111 | ············string coldef = columnName + " " + columnType; |
| 112 | ············if (dataType == typeof(string) || dataType == typeof(Guid)) |
| 113 | ················coldef += '(' + width + ')'; |
| 114 | ············return coldef + " PRIMARY KEY"; |
| 115 | ········} |
| 116 | |
| 117 | ····} |
| 118 | |
| 119 | } |
| 120 |