Datei: Provider/PostGresProvider/NDO.Postgre/Provider.cs
Last Commit (e34b4f0)
| 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 NDO.Postgre; |
| 24 | using NDOInterfaces; |
| 25 | using Npgsql; |
| 26 | using NpgsqlTypes; |
| 27 | using System; |
| 28 | using System.Collections; |
| 29 | using System.Collections.ObjectModel; |
| 30 | using System.Data; |
| 31 | using System.Data.Common; |
| 32 | using System.Text; |
| 33 | using System.Text.RegularExpressions; |
| 34 | |
| 35 | namespace NDO.PostGreProvider |
| 36 | { |
| 37 | ····/// <summary> |
| 38 | ····/// Sample adapter class to connect new ADO.NET Providers with NDO. |
| 39 | ····/// This Adapter is based on the Npgsql Npgsql Data Connector |
| 40 | ····/// </summary> |
| 41 | ····public class Provider : NDOAbstractProvider |
| 42 | ····{ |
| 43 | ········// The following methods provide objects of provider classes |
| 44 | ········// which implement common interfaces in .NET: |
| 45 | ········// IDbConnection, IDbCommand, DbDataAdapter and the Parameter objects |
| 46 | ········#region Provide specialized type objects |
| 47 | ········public override IDbConnection NewConnection(string connectionString) |
| 48 | ········{ |
| 49 | return new NdoNpgsqlConnection( new NpgsqlConnection( connectionString) ) ; |
| 50 | ········} |
| 51 | |
| 52 | ········public override IDbCommand NewSqlCommand(IDbConnection connection) |
| 53 | ········{ |
| 54 | ············NpgsqlCommand command = new NpgsqlCommand(); |
| 55 | command. Connection = ( NpgsqlConnection) ( ( INdoDbConnection) connection ) . InnerConnection; |
| 56 | ············return command; |
| 57 | ········} |
| 58 | |
| 59 | ········public override DbDataAdapter NewDataAdapter(IDbCommand select, IDbCommand update, IDbCommand insert, IDbCommand delete) |
| 60 | ········{ |
| 61 | ············NpgsqlDataAdapter da = new NpgsqlDataAdapter(); |
| 62 | ············da.SelectCommand = (NpgsqlCommand)select; |
| 63 | ············da.UpdateCommand = (NpgsqlCommand)update; |
| 64 | ············da.InsertCommand = (NpgsqlCommand)insert; |
| 65 | ············da.DeleteCommand = (NpgsqlCommand)delete; |
| 66 | ············return da; |
| 67 | ········} |
| 68 | |
| 69 | ········/// <summary> |
| 70 | ········/// See <see cref="IProvider"> IProvider interface </see> |
| 71 | ········/// </summary> |
| 72 | ········public override object NewCommandBuilder(DbDataAdapter dataAdapter) |
| 73 | ········{ |
| 74 | ············return new NpgsqlCommandBuilder((NpgsqlDataAdapter)dataAdapter); |
| 75 | ········} |
| 76 | |
| 77 | |
| 78 | ········public override IDataParameter AddParameter(IDbCommand command, string parameterName, object dbType, int size, string columnName) |
| 79 | ········{ |
| 80 | ············return ((NpgsqlCommand)command).Parameters.Add(new NpgsqlParameter(parameterName, (NpgsqlDbType)dbType, size, columnName));············ |
| 81 | ········} |
| 82 | |
| 83 | ········public override IDataParameter AddParameter(IDbCommand command, string parameterName, object dbType, int size, ParameterDirection dir, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value) |
| 84 | ········{ |
| 85 | ············return ((NpgsqlCommand)command).Parameters.Add(new NpgsqlParameter(parameterName, (NpgsqlDbType)dbType, size, srcColumn, dir, isNullable, precision, scale, srcVersion, value)); |
| 86 | ········} |
| 87 | ········#endregion |
| 88 | |
| 89 | ········// The following method convert System.Type objects to NpgsqlDbType-Members |
| 90 | ········// For your own adapter use members of the database type emumeration |
| 91 | ········// of your ADO.NET provider |
| 92 | ········#region Provide NpgsqlDbType members |
| 93 | ········public override object GetDbType(Type t) |
| 94 | ········{ |
| 95 | ············t = base.ConvertNullableType(t); |
| 96 | ············if (t == typeof(bool)) |
| 97 | ················return NpgsqlDbType.Boolean; |
| 98 | ············else if ( t == typeof(byte) ) |
| 99 | ················return NpgsqlDbType.Smallint; |
| 100 | ············else if ( t == typeof(sbyte) ) |
| 101 | ················return NpgsqlDbType.Smallint; |
| 102 | ············else if ( t == typeof(char) ) |
| 103 | ················return NpgsqlDbType.Smallint; |
| 104 | ············else if ( t == typeof(short)) |
| 105 | ················return NpgsqlDbType.Smallint; |
| 106 | ············else if ( t == typeof(ushort)) |
| 107 | ················return NpgsqlDbType.Smallint; |
| 108 | ············else if ( t == typeof(int)) |
| 109 | ················return NpgsqlDbType.Integer; |
| 110 | ············else if ( t == typeof(uint)) |
| 111 | ················return NpgsqlDbType.Integer; |
| 112 | ············else if ( t == typeof(long)) |
| 113 | ················return NpgsqlDbType.Bigint; |
| 114 | ············else if ( t == typeof(System.Guid)) |
| 115 | ················return NpgsqlDbType.Char; |
| 116 | ············else if ( t == typeof(ulong)) |
| 117 | ················return NpgsqlDbType.Bigint; |
| 118 | ············else if ( t == typeof(float)) |
| 119 | ················return NpgsqlDbType.Real; |
| 120 | ············else if ( t == typeof(double)) |
| 121 | ················return NpgsqlDbType.Double; |
| 122 | ············else if ( t == typeof(string)) |
| 123 | ················return NpgsqlDbType.Varchar; |
| 124 | ············else if ( t == typeof(byte[])) |
| 125 | ················return NpgsqlDbType.Bytea; |
| 126 | ············else if ( t == typeof(decimal)) |
| 127 | ················return NpgsqlDbType.Numeric; |
| 128 | ············else if ( t == typeof(System.DateTime)) |
| 129 | ················return NpgsqlDbType.Date; |
| 130 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
| 131 | ················return NpgsqlDbType.Integer; |
| 132 | ············else |
| 133 | ················throw new NDOException(27, "NDO.NpgsqlProvider.GetDbType: Typ " + t.Name + " kann nicht in NpgsqlDbType konvertiert werden"); |
| 134 | ········} |
| 135 | |
| 136 | ········// The following method converts string representations of NpgsqlDbType-Members |
| 137 | ········// into NpgsqlDbType. |
| 138 | ········// For your own adapter use members of the database type emumeration of your |
| 139 | ········// ADO.NET provider and convert it to the respective enumeration type········ |
| 140 | ········public override object GetDbType(string typeName) |
| 141 | ········{ |
| 142 | ············if (String.Compare(typeName, "bool", true) == 0) |
| 143 | ················return NpgsqlDbType.Boolean; |
| 144 | ············if (String.Compare(typeName, "int8", true) == 0) |
| 145 | ················return NpgsqlDbType.Bigint; |
| 146 | ············if (String.Compare(typeName, "bytea", true) == 0) |
| 147 | ················return NpgsqlDbType.Bytea; |
| 148 | ············if (String.Compare(typeName, "date", true) == 0) |
| 149 | ················return NpgsqlDbType.Date; |
| 150 | ············if (String.Compare(typeName, "float8", true) == 0) |
| 151 | ················return NpgsqlDbType.Double; |
| 152 | ············if (String.Compare(typeName, "int4", true) == 0) |
| 153 | ················return NpgsqlDbType.Integer; |
| 154 | ············if (String.Compare(typeName, "money", true) == 0) |
| 155 | ················return NpgsqlDbType.Money; |
| 156 | ············if (String.Compare(typeName, "numeric", true) == 0) |
| 157 | ················return NpgsqlDbType.Numeric; |
| 158 | ············if (String.Compare(typeName, "float4", true) == 0) |
| 159 | ················return NpgsqlDbType.Real; |
| 160 | ············if (String.Compare(typeName, "int2", true) == 0) |
| 161 | ················return NpgsqlDbType.Smallint; |
| 162 | ············if (String.Compare(typeName, "text", true) == 0) |
| 163 | ················return NpgsqlDbType.Text; |
| 164 | ············if (String.Compare(typeName, "time", true) == 0) |
| 165 | ················return NpgsqlDbType.Time; |
| 166 | ············if (String.Compare(typeName, "timestamp", true) == 0) |
| 167 | ················return NpgsqlDbType.Timestamp; |
| 168 | ············if (String.Compare(typeName, "timetz", true) == 0) |
| 169 | ················return NpgsqlDbType.Time; |
| 170 | ············if (String.Compare(typeName, "timestamptz", true) == 0) |
| 171 | ················return NpgsqlDbType.Timestamp; |
| 172 | ············if (String.Compare(typeName, "varchar", true) == 0) |
| 173 | ················return NpgsqlDbType.Varchar; |
| 174 | ············throw new NDOException(27, "NDONpgsql.Provider.GetDbType: Typname " + typeName + " kann nicht in NpgsqlDbType konvertiert werden"); |
| 175 | ········} |
| 176 | |
| 177 | |
| 178 | ········public override int GetDefaultLength(System.Type t) |
| 179 | ········{ |
| 180 | ············t = base.ConvertNullableType(t); |
| 181 | ············if (t == typeof(bool)) |
| 182 | ················return 1; |
| 183 | ············else if ( t == typeof(byte) ) |
| 184 | ················return 2; |
| 185 | ············else if ( t == typeof(sbyte) ) |
| 186 | ················return 2; |
| 187 | ············else if ( t == typeof(char) ) |
| 188 | ················return 2; |
| 189 | ············else if ( t == typeof(short)) |
| 190 | ················return 2; |
| 191 | ············else if ( t == typeof(ushort)) |
| 192 | ················return 2; |
| 193 | ············else if ( t == typeof(int)) |
| 194 | ················return 4; |
| 195 | ············else if ( t == typeof(uint)) |
| 196 | ················return 4; |
| 197 | ············else if ( t == typeof(long)) |
| 198 | ················return 8; |
| 199 | ············else if ( t == typeof(System.Guid)) |
| 200 | ················return 36; |
| 201 | ············else if ( t == typeof(ulong)) |
| 202 | ················return 8; |
| 203 | ············else if ( t == typeof(float)) |
| 204 | ················return 4; |
| 205 | ············else if ( t == typeof(double)) |
| 206 | ················return 8; |
| 207 | ············else if ( t == typeof(string)) |
| 208 | ················return 255; |
| 209 | ············else if ( t == typeof(byte[])) |
| 210 | ················return 255; |
| 211 | ············else if ( t == typeof(decimal)) |
| 212 | ················return 18; |
| 213 | ············else if ( t == typeof(System.DateTime)) |
| 214 | ················return 16; |
| 215 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
| 216 | ················return 4; |
| 217 | ············else |
| 218 | ················return 0;········ |
| 219 | ········} |
| 220 | |
| 221 | ········public override string GetDbTypeString( IDbDataParameter parameter ) |
| 222 | ········{ |
| 223 | ············return (((NpgsqlParameter)parameter).NpgsqlDbType).ToString(); |
| 224 | ········} |
| 225 | |
| 226 | ········public override string Wildcard |
| 227 | ········{ |
| 228 | ············get { return "%"; } |
| 229 | ········} |
| 230 | |
| 231 | ········public override bool UseNamedParams |
| 232 | ········{ |
| 233 | ············get { return true; } |
| 234 | ········} |
| 235 | |
| 236 | /* |
| 237 | ········public override bool UseStoredProcedure |
| 238 | ········{ |
| 239 | ············get { return false; } |
| 240 | ········} |
| 241 | */ |
| 242 | ········#endregion |
| 243 | ········ |
| 244 | ········//private Hashtable namedParameters = new Hashtable(); |
| 245 | ········public override string GetNamedParameter(string plainParameterName) |
| 246 | ········{ |
| 247 | ············return ":" + plainParameterName; |
| 248 | ········} |
| 249 | ···· |
| 250 | ········public override string GetQuotedName(string plainName) |
| 251 | ········{ |
| 252 | ············return "\"" + plainName + '"'; |
| 253 | ········} |
| 254 | |
| 255 | ········/// <summary> |
| 256 | ········/// Indicates whether the last automatically generated ID can be retrieved. |
| 257 | ········/// Returns true if a database provides automatically incremented IDs and its syntax has an expression |
| 258 | ········/// which retrieves the last generated ID; otherwise false. |
| 259 | ········/// </summary> |
| 260 | ········public override bool SupportsLastInsertedId => true; |
| 261 | |
| 262 | |
| 263 | ········/// <summary> |
| 264 | ········/// Gets an expression in the SQL dialect of the database, which retrieves the ID of the last |
| 265 | ········/// inserted row, if the ID is automatically generated by the database. |
| 266 | ········/// </summary> |
| 267 | ········public override string GetLastInsertedId(string tableName, string columnName) |
| 268 | ········{ |
| 269 | ············return $"(SELECT CURRVAL(pg_get_serial_sequence('{tableName}', '{columnName}')))"; |
| 270 | ········} |
| 271 | |
| 272 | ········/// <summary> |
| 273 | ········/// Determines whether a database supports bulk command strings. |
| 274 | ········/// </summary> |
| 275 | ········public override bool SupportsBulkCommands |
| 276 | ········{ |
| 277 | ············get { return true; } |
| 278 | ········} |
| 279 | ········ |
| 280 | |
| 281 | ········/// <summary> |
| 282 | ········/// Generate one big command string out of several partial commands to save roundtrips |
| 283 | ········/// to the server. |
| 284 | ········/// </summary> |
| 285 | ········/// <param name="commands"></param> |
| 286 | ········/// <returns></returns> |
| 287 | ········public override string GenerateBulkCommand(string[] commands) |
| 288 | ········{ |
| 289 | ············StringBuilder sb = new StringBuilder(commands.Length * 100); |
| 290 | ············foreach (string s in commands) |
| 291 | ············{ |
| 292 | ················sb.Append(s); |
| 293 | ················sb.Append(';'); |
| 294 | ············} |
| 295 | ············return sb.ToString(); |
| 296 | ········} |
| 297 | |
| 298 | ············ |
| 299 | ········public override string[] GetTableNames(IDbConnection conn, string owner) |
| 300 | ········{ |
| 301 | NpgsqlDataAdapter a = new NpgsqlDataAdapter( "Select * from pg_tables", ( NpgsqlConnection) ( ( INdoDbConnection) conn ) . InnerConnection ) ; |
| 302 | ············DataSet ds = new DataSet(); |
| 303 | ············a.Fill(ds); |
| 304 | ············DataTable dt = ds.Tables[0]; |
| 305 | ············ArrayList al = new ArrayList(); |
| 306 | ············bool hasOwner = !String.IsNullOrWhiteSpace(owner); |
| 307 | ············foreach (DataRow dr in dt.Rows) |
| 308 | ············{ |
| 309 | ················string sname = (string) dr["schemaname"]; |
| 310 | ················if (sname == "information_schema") |
| 311 | ····················continue; |
| 312 | ················if (sname == "pg_catalog") |
| 313 | ····················continue; |
| 314 | ················if (hasOwner && sname != owner) |
| 315 | ····················continue; |
| 316 | ···················· |
| 317 | ················al.Add(dr["tablename"]); |
| 318 | ············} |
| 319 | ············string[] strresult = new string[al.Count]; |
| 320 | ············for (int i = 0; i < al.Count; i++) |
| 321 | ················strresult[i] = (string) al[i]; |
| 322 | ············return strresult; |
| 323 | ········} |
| 324 | ···· |
| 325 | ········public override string[] TypeNames |
| 326 | ········{ |
| 327 | ············get |
| 328 | ············{················ |
| 329 | ················return Enum.GetNames(typeof(NpgsqlDbType)); |
| 330 | ············} |
| 331 | ········} |
| 332 | |
| 333 | ········public override string Name { get { return "Postgre"; }··} |
| 334 | |
| 335 | ········public override bool SupportsInsertBatch => false; |
| 336 | |
| 337 | ········public override bool SupportsNativeGuidType |
| 338 | ········{ |
| 339 | ············get { return false; } |
| 340 | ········} |
| 341 | |
| 342 | ········/// <summary> |
| 343 | ········/// Creates a Database |
| 344 | ········/// </summary> |
| 345 | ········/// <param name="databaseName">The name of the new database</param> |
| 346 | ········/// <param name="connectionString">The connection string used to create the database</param> |
| 347 | ········/// <param name="additionalData">Not used</param> |
| 348 | ········/// <returns></returns> |
| 349 | ········public override string CreateDatabase(string databaseName, string connectionString, object additionalData) |
| 350 | ········{ |
| 351 | ············base.CreateDatabase (databaseName, connectionString, additionalData); |
| 352 | |
| 353 | ············// We have to construct a connectionString |
| 354 | ············// containing the database name |
| 355 | |
| 356 | ············// If somebody has provided a connection string containing the name of an existing database |
| 357 | ············// replace it with the name of the new database. |
| 358 | ············Regex regex = new Regex(@"Database\s*=([^\;]*)"); |
| 359 | ············Match match = regex.Match(connectionString); |
| 360 | ············if (match.Success) |
| 361 | ············{ |
| 362 | ················return connectionString.Substring(0, match.Groups[1].Index) + databaseName + connectionString.Substring(match.Index + match.Length); |
| 363 | ············} |
| 364 | ············ |
| 365 | ············// Add the name of the new database to the connection string. |
| 366 | ············if (!connectionString.EndsWith(";")) |
| 367 | ················connectionString = connectionString + ";"; |
| 368 | ············ |
| 369 | ············return connectionString + "Database=" + databaseName; |
| 370 | ········} |
| 371 | ····} |
| 372 | } |
| 373 |
New Commit (14cacf2)
| 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 | using NDOInterfaces; |
| 23 | using Npgsql; |
| 24 | using NpgsqlTypes; |
| 25 | using System; |
| 26 | using System.Collections; |
| 27 | using System.Collections.ObjectModel; |
| 28 | using System.Data; |
| 29 | using System.Data.Common; |
| 30 | using System.Text; |
| 31 | using System.Text.RegularExpressions; |
| 32 | |
| 33 | namespace NDO.PostGreProvider |
| 34 | { |
| 35 | ····/// <summary> |
| 36 | ····/// Sample adapter class to connect new ADO.NET Providers with NDO. |
| 37 | ····/// This Adapter is based on the Npgsql Npgsql Data Connector |
| 38 | ····/// </summary> |
| 39 | ····public class Provider : NDOAbstractProvider |
| 40 | ····{ |
| 41 | ········// The following methods provide objects of provider classes |
| 42 | ········// which implement common interfaces in .NET: |
| 43 | ········// IDbConnection, IDbCommand, DbDataAdapter and the Parameter objects |
| 44 | ········#region Provide specialized type objects |
| 45 | ········public override IDbConnection NewConnection(string connectionString) |
| 46 | ········{ |
| 47 | return new NpgsqlConnection( connectionString) ; |
| 48 | ········} |
| 49 | |
| 50 | ········public override IDbCommand NewSqlCommand(IDbConnection connection) |
| 51 | ········{ |
| 52 | ············NpgsqlCommand command = new NpgsqlCommand(); |
| 53 | command. Connection = ( NpgsqlConnection) connection; |
| 54 | ············return command; |
| 55 | ········} |
| 56 | |
| 57 | ········public override object GetConnectionId( IDbConnection connection ) |
| 58 | ········{ |
| 59 | ············return ( (NpgsqlConnection) connection ).ProcessID; |
| 60 | ········} |
| 61 | |
| 62 | ········public override DbDataAdapter NewDataAdapter(IDbCommand select, IDbCommand update, IDbCommand insert, IDbCommand delete) |
| 63 | ········{ |
| 64 | ············NpgsqlDataAdapter da = new NpgsqlDataAdapter(); |
| 65 | ············da.SelectCommand = (NpgsqlCommand)select; |
| 66 | ············da.UpdateCommand = (NpgsqlCommand)update; |
| 67 | ············da.InsertCommand = (NpgsqlCommand)insert; |
| 68 | ············da.DeleteCommand = (NpgsqlCommand)delete; |
| 69 | ············return da; |
| 70 | ········} |
| 71 | |
| 72 | ········/// <summary> |
| 73 | ········/// See <see cref="IProvider"> IProvider interface </see> |
| 74 | ········/// </summary> |
| 75 | ········public override object NewCommandBuilder(DbDataAdapter dataAdapter) |
| 76 | ········{ |
| 77 | ············return new NpgsqlCommandBuilder((NpgsqlDataAdapter)dataAdapter); |
| 78 | ········} |
| 79 | |
| 80 | |
| 81 | ········public override IDataParameter AddParameter(IDbCommand command, string parameterName, object dbType, int size, string columnName) |
| 82 | ········{ |
| 83 | ············return ((NpgsqlCommand)command).Parameters.Add(new NpgsqlParameter(parameterName, (NpgsqlDbType)dbType, size, columnName));············ |
| 84 | ········} |
| 85 | |
| 86 | ········public override IDataParameter AddParameter(IDbCommand command, string parameterName, object dbType, int size, ParameterDirection dir, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value) |
| 87 | ········{ |
| 88 | ············return ((NpgsqlCommand)command).Parameters.Add(new NpgsqlParameter(parameterName, (NpgsqlDbType)dbType, size, srcColumn, dir, isNullable, precision, scale, srcVersion, value)); |
| 89 | ········} |
| 90 | ········#endregion |
| 91 | |
| 92 | ········// The following method convert System.Type objects to NpgsqlDbType-Members |
| 93 | ········// For your own adapter use members of the database type emumeration |
| 94 | ········// of your ADO.NET provider |
| 95 | ········#region Provide NpgsqlDbType members |
| 96 | ········public override object GetDbType(Type t) |
| 97 | ········{ |
| 98 | ············t = base.ConvertNullableType(t); |
| 99 | ············if (t == typeof(bool)) |
| 100 | ················return NpgsqlDbType.Boolean; |
| 101 | ············else if ( t == typeof(byte) ) |
| 102 | ················return NpgsqlDbType.Smallint; |
| 103 | ············else if ( t == typeof(sbyte) ) |
| 104 | ················return NpgsqlDbType.Smallint; |
| 105 | ············else if ( t == typeof(char) ) |
| 106 | ················return NpgsqlDbType.Smallint; |
| 107 | ············else if ( t == typeof(short)) |
| 108 | ················return NpgsqlDbType.Smallint; |
| 109 | ············else if ( t == typeof(ushort)) |
| 110 | ················return NpgsqlDbType.Smallint; |
| 111 | ············else if ( t == typeof(int)) |
| 112 | ················return NpgsqlDbType.Integer; |
| 113 | ············else if ( t == typeof(uint)) |
| 114 | ················return NpgsqlDbType.Integer; |
| 115 | ············else if ( t == typeof(long)) |
| 116 | ················return NpgsqlDbType.Bigint; |
| 117 | ············else if ( t == typeof(System.Guid)) |
| 118 | ················return NpgsqlDbType.Char; |
| 119 | ············else if ( t == typeof(ulong)) |
| 120 | ················return NpgsqlDbType.Bigint; |
| 121 | ············else if ( t == typeof(float)) |
| 122 | ················return NpgsqlDbType.Real; |
| 123 | ············else if ( t == typeof(double)) |
| 124 | ················return NpgsqlDbType.Double; |
| 125 | ············else if ( t == typeof(string)) |
| 126 | ················return NpgsqlDbType.Varchar; |
| 127 | ············else if ( t == typeof(byte[])) |
| 128 | ················return NpgsqlDbType.Bytea; |
| 129 | ············else if ( t == typeof(decimal)) |
| 130 | ················return NpgsqlDbType.Numeric; |
| 131 | ············else if ( t == typeof(System.DateTime)) |
| 132 | ················return NpgsqlDbType.Date; |
| 133 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
| 134 | ················return NpgsqlDbType.Integer; |
| 135 | ············else |
| 136 | ················throw new NDOException(27, "NDO.NpgsqlProvider.GetDbType: Typ " + t.Name + " kann nicht in NpgsqlDbType konvertiert werden"); |
| 137 | ········} |
| 138 | |
| 139 | ········// The following method converts string representations of NpgsqlDbType-Members |
| 140 | ········// into NpgsqlDbType. |
| 141 | ········// For your own adapter use members of the database type emumeration of your |
| 142 | ········// ADO.NET provider and convert it to the respective enumeration type········ |
| 143 | ········public override object GetDbType(string typeName) |
| 144 | ········{ |
| 145 | ············if (String.Compare(typeName, "bool", true) == 0) |
| 146 | ················return NpgsqlDbType.Boolean; |
| 147 | ············if (String.Compare(typeName, "int8", true) == 0) |
| 148 | ················return NpgsqlDbType.Bigint; |
| 149 | ············if (String.Compare(typeName, "bytea", true) == 0) |
| 150 | ················return NpgsqlDbType.Bytea; |
| 151 | ············if (String.Compare(typeName, "date", true) == 0) |
| 152 | ················return NpgsqlDbType.Date; |
| 153 | ············if (String.Compare(typeName, "float8", true) == 0) |
| 154 | ················return NpgsqlDbType.Double; |
| 155 | ············if (String.Compare(typeName, "int4", true) == 0) |
| 156 | ················return NpgsqlDbType.Integer; |
| 157 | ············if (String.Compare(typeName, "money", true) == 0) |
| 158 | ················return NpgsqlDbType.Money; |
| 159 | ············if (String.Compare(typeName, "numeric", true) == 0) |
| 160 | ················return NpgsqlDbType.Numeric; |
| 161 | ············if (String.Compare(typeName, "float4", true) == 0) |
| 162 | ················return NpgsqlDbType.Real; |
| 163 | ············if (String.Compare(typeName, "int2", true) == 0) |
| 164 | ················return NpgsqlDbType.Smallint; |
| 165 | ············if (String.Compare(typeName, "text", true) == 0) |
| 166 | ················return NpgsqlDbType.Text; |
| 167 | ············if (String.Compare(typeName, "time", true) == 0) |
| 168 | ················return NpgsqlDbType.Time; |
| 169 | ············if (String.Compare(typeName, "timestamp", true) == 0) |
| 170 | ················return NpgsqlDbType.Timestamp; |
| 171 | ············if (String.Compare(typeName, "timetz", true) == 0) |
| 172 | ················return NpgsqlDbType.Time; |
| 173 | ············if (String.Compare(typeName, "timestamptz", true) == 0) |
| 174 | ················return NpgsqlDbType.Timestamp; |
| 175 | ············if (String.Compare(typeName, "varchar", true) == 0) |
| 176 | ················return NpgsqlDbType.Varchar; |
| 177 | ············throw new NDOException(27, "NDONpgsql.Provider.GetDbType: Typname " + typeName + " kann nicht in NpgsqlDbType konvertiert werden"); |
| 178 | ········} |
| 179 | |
| 180 | |
| 181 | ········public override int GetDefaultLength(System.Type t) |
| 182 | ········{ |
| 183 | ············t = base.ConvertNullableType(t); |
| 184 | ············if (t == typeof(bool)) |
| 185 | ················return 1; |
| 186 | ············else if ( t == typeof(byte) ) |
| 187 | ················return 2; |
| 188 | ············else if ( t == typeof(sbyte) ) |
| 189 | ················return 2; |
| 190 | ············else if ( t == typeof(char) ) |
| 191 | ················return 2; |
| 192 | ············else if ( t == typeof(short)) |
| 193 | ················return 2; |
| 194 | ············else if ( t == typeof(ushort)) |
| 195 | ················return 2; |
| 196 | ············else if ( t == typeof(int)) |
| 197 | ················return 4; |
| 198 | ············else if ( t == typeof(uint)) |
| 199 | ················return 4; |
| 200 | ············else if ( t == typeof(long)) |
| 201 | ················return 8; |
| 202 | ············else if ( t == typeof(System.Guid)) |
| 203 | ················return 36; |
| 204 | ············else if ( t == typeof(ulong)) |
| 205 | ················return 8; |
| 206 | ············else if ( t == typeof(float)) |
| 207 | ················return 4; |
| 208 | ············else if ( t == typeof(double)) |
| 209 | ················return 8; |
| 210 | ············else if ( t == typeof(string)) |
| 211 | ················return 255; |
| 212 | ············else if ( t == typeof(byte[])) |
| 213 | ················return 255; |
| 214 | ············else if ( t == typeof(decimal)) |
| 215 | ················return 18; |
| 216 | ············else if ( t == typeof(System.DateTime)) |
| 217 | ················return 16; |
| 218 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
| 219 | ················return 4; |
| 220 | ············else |
| 221 | ················return 0;········ |
| 222 | ········} |
| 223 | |
| 224 | ········public override string GetDbTypeString( IDbDataParameter parameter ) |
| 225 | ········{ |
| 226 | ············return (((NpgsqlParameter)parameter).NpgsqlDbType).ToString(); |
| 227 | ········} |
| 228 | |
| 229 | ········public override string Wildcard |
| 230 | ········{ |
| 231 | ············get { return "%"; } |
| 232 | ········} |
| 233 | |
| 234 | ········public override bool UseNamedParams |
| 235 | ········{ |
| 236 | ············get { return true; } |
| 237 | ········} |
| 238 | |
| 239 | /* |
| 240 | ········public override bool UseStoredProcedure |
| 241 | ········{ |
| 242 | ············get { return false; } |
| 243 | ········} |
| 244 | */ |
| 245 | ········#endregion |
| 246 | ········ |
| 247 | ········//private Hashtable namedParameters = new Hashtable(); |
| 248 | ········public override string GetNamedParameter(string plainParameterName) |
| 249 | ········{ |
| 250 | ············return ":" + plainParameterName; |
| 251 | ········} |
| 252 | ···· |
| 253 | ········public override string GetQuotedName(string plainName) |
| 254 | ········{ |
| 255 | ············return "\"" + plainName + '"'; |
| 256 | ········} |
| 257 | |
| 258 | ········/// <summary> |
| 259 | ········/// Indicates whether the last automatically generated ID can be retrieved. |
| 260 | ········/// Returns true if a database provides automatically incremented IDs and its syntax has an expression |
| 261 | ········/// which retrieves the last generated ID; otherwise false. |
| 262 | ········/// </summary> |
| 263 | ········public override bool SupportsLastInsertedId => true; |
| 264 | |
| 265 | |
| 266 | ········/// <summary> |
| 267 | ········/// Gets an expression in the SQL dialect of the database, which retrieves the ID of the last |
| 268 | ········/// inserted row, if the ID is automatically generated by the database. |
| 269 | ········/// </summary> |
| 270 | ········public override string GetLastInsertedId(string tableName, string columnName) |
| 271 | ········{ |
| 272 | ············return $"(SELECT CURRVAL(pg_get_serial_sequence('{tableName}', '{columnName}')))"; |
| 273 | ········} |
| 274 | |
| 275 | ········/// <summary> |
| 276 | ········/// Determines whether a database supports bulk command strings. |
| 277 | ········/// </summary> |
| 278 | ········public override bool SupportsBulkCommands |
| 279 | ········{ |
| 280 | ············get { return true; } |
| 281 | ········} |
| 282 | ········ |
| 283 | |
| 284 | ········/// <summary> |
| 285 | ········/// Generate one big command string out of several partial commands to save roundtrips |
| 286 | ········/// to the server. |
| 287 | ········/// </summary> |
| 288 | ········/// <param name="commands"></param> |
| 289 | ········/// <returns></returns> |
| 290 | ········public override string GenerateBulkCommand(string[] commands) |
| 291 | ········{ |
| 292 | ············StringBuilder sb = new StringBuilder(commands.Length * 100); |
| 293 | ············foreach (string s in commands) |
| 294 | ············{ |
| 295 | ················sb.Append(s); |
| 296 | ················sb.Append(';'); |
| 297 | ············} |
| 298 | ············return sb.ToString(); |
| 299 | ········} |
| 300 | |
| 301 | ············ |
| 302 | ········public override string[] GetTableNames(IDbConnection conn, string owner) |
| 303 | ········{ |
| 304 | NpgsqlDataAdapter a = new NpgsqlDataAdapter( "Select * from pg_tables", ( NpgsqlConnection) conn ) ; |
| 305 | ············DataSet ds = new DataSet(); |
| 306 | ············a.Fill(ds); |
| 307 | ············DataTable dt = ds.Tables[0]; |
| 308 | ············ArrayList al = new ArrayList(); |
| 309 | ············bool hasOwner = !String.IsNullOrWhiteSpace(owner); |
| 310 | ············foreach (DataRow dr in dt.Rows) |
| 311 | ············{ |
| 312 | ················string sname = (string) dr["schemaname"]; |
| 313 | ················if (sname == "information_schema") |
| 314 | ····················continue; |
| 315 | ················if (sname == "pg_catalog") |
| 316 | ····················continue; |
| 317 | ················if (hasOwner && sname != owner) |
| 318 | ····················continue; |
| 319 | ···················· |
| 320 | ················al.Add(dr["tablename"]); |
| 321 | ············} |
| 322 | ············string[] strresult = new string[al.Count]; |
| 323 | ············for (int i = 0; i < al.Count; i++) |
| 324 | ················strresult[i] = (string) al[i]; |
| 325 | ············return strresult; |
| 326 | ········} |
| 327 | ···· |
| 328 | ········public override string[] TypeNames |
| 329 | ········{ |
| 330 | ············get |
| 331 | ············{················ |
| 332 | ················return Enum.GetNames(typeof(NpgsqlDbType)); |
| 333 | ············} |
| 334 | ········} |
| 335 | |
| 336 | ········public override string Name { get { return "Postgre"; }··} |
| 337 | |
| 338 | ········public override bool SupportsInsertBatch => false; |
| 339 | |
| 340 | ········public override bool SupportsNativeGuidType |
| 341 | ········{ |
| 342 | ············get { return false; } |
| 343 | ········} |
| 344 | |
| 345 | ········/// <summary> |
| 346 | ········/// Creates a Database |
| 347 | ········/// </summary> |
| 348 | ········/// <param name="databaseName">The name of the new database</param> |
| 349 | ········/// <param name="connectionString">The connection string used to create the database</param> |
| 350 | ········/// <param name="additionalData">Not used</param> |
| 351 | ········/// <returns></returns> |
| 352 | ········public override string CreateDatabase(string databaseName, string connectionString, object additionalData) |
| 353 | ········{ |
| 354 | ············base.CreateDatabase (databaseName, connectionString, additionalData); |
| 355 | |
| 356 | ············// We have to construct a connectionString |
| 357 | ············// containing the database name |
| 358 | |
| 359 | ············// If somebody has provided a connection string containing the name of an existing database |
| 360 | ············// replace it with the name of the new database. |
| 361 | ············Regex regex = new Regex(@"Database\s*=([^\;]*)"); |
| 362 | ············Match match = regex.Match(connectionString); |
| 363 | ············if (match.Success) |
| 364 | ············{ |
| 365 | ················return connectionString.Substring(0, match.Groups[1].Index) + databaseName + connectionString.Substring(match.Index + match.Length); |
| 366 | ············} |
| 367 | ············ |
| 368 | ············// Add the name of the new database to the connection string. |
| 369 | ············if (!connectionString.EndsWith(";")) |
| 370 | ················connectionString = connectionString + ";"; |
| 371 | ············ |
| 372 | ············return connectionString + "Database=" + databaseName; |
| 373 | ········} |
| 374 | ····} |
| 375 | } |
| 376 |