Datei: Provider/PostGresProvider/NDO.Postgre/Provider.cs

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