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

Last 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
New Commit (2df8096)
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. Timestamp;
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