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