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

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