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

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