2 // PostgresTypes.cs - holding methods to convert
\r
3 // between PostgreSQL types and .NET types
\r
6 // Daniel Morgan <danmorg@sc.rr.com>
\r
8 // (c)copyright 2002 Daniel Morgan
\r
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 // Note: this might become PostgresType and PostgresTypeCollection
\r
33 // also, the PostgresTypes that exist as an inner internal class
\r
34 // within PgSqlConnection maybe moved here in the future
\r
37 using System.Collections;
\r
39 using System.Data.Common;
\r
42 namespace Mono.Data.PostgreSqlClient {
\r
44 internal struct PostgresType {
\r
46 public string typname;
\r
47 public DbType dbType;
\r
50 sealed internal class PostgresHelper {
52 // translates the PostgreSQL typname to System.Data.DbType
53 public static DbType TypnameToSqlDbType(string typname) {
\r
56 // FIXME: use hashtable here?
\r
61 sqlType = DbType.Int32;
\r
65 sqlType = DbType.String;
\r
69 sqlType = DbType.String;
\r
73 sqlType = DbType.Boolean;
\r
77 sqlType = DbType.String;
\r
81 sqlType = DbType.String;
\r
85 sqlType = DbType.String;
\r
89 sqlType = DbType.String;
\r
93 sqlType = DbType.String;
\r
97 sqlType = DbType.String;
\r
101 sqlType = DbType.Date;
\r
105 sqlType = DbType.Single;
\r
109 sqlType = DbType.Double;
\r
113 sqlType = DbType.String;
\r
117 sqlType = DbType.Int16;
\r
121 sqlType = DbType.Int32;
\r
125 sqlType = DbType.Int64;
\r
129 sqlType = DbType.String;
\r
133 sqlType = DbType.String;
\r
137 sqlType = DbType.String;
\r
141 sqlType = DbType.String;
\r
145 sqlType = DbType.Decimal;
\r
149 sqlType = DbType.String;
\r
153 sqlType = DbType.Decimal;
\r
157 sqlType = DbType.Int32;
\r
161 sqlType = DbType.String;
\r
165 sqlType = DbType.String;
\r
169 sqlType = DbType.String;
\r
173 sqlType = DbType.String;
\r
177 sqlType = DbType.String;
\r
181 sqlType = DbType.String;
\r
185 sqlType = DbType.Time;
\r
189 sqlType = DbType.DateTime;
\r
192 case "timestamptz":
\r
193 sqlType = DbType.DateTime;
\r
197 sqlType = DbType.DateTime;
\r
201 sqlType = DbType.String;
\r
205 sqlType = DbType.String;
\r
209 sqlType = DbType.String;
\r
213 sqlType = DbType.String;
\r
219 // Converts data value from database to .NET System type.
\r
220 public static object ConvertDbTypeToSystem (DbType typ, String value) {
223 // FIXME: more types need
225 // from PostgreSQL oid type
226 // to .NET System.<type>
228 // FIXME: need to handle a NULL for each type
229 // maybe setting obj to System.DBNull.Value ?
233 //Console.WriteLine("ConvertDbTypeToSystemDbType typ: " +
234 // typ + " value is null");
237 else if(value.Equals("")) {
238 //Console.WriteLine("ConvertDbTypeToSystemDbType typ: " +
239 // typ + " value is string empty");
243 //Console.WriteLine("ConvertDbTypeToSystemDbType typ: " +
244 // typ + " value: " + value);
246 // Date, Time, and DateTime
247 // are parsed based on ISO format
248 // "YYYY-MM-DD hh:mi:ss.ms"
252 obj = String.Copy(value);
255 obj = value.Equals("t");
258 obj = Int16.Parse(value);
\r
261 obj = Int32.Parse(value);
\r
264 obj = Int64.Parse(value);
\r
266 case DbType.Decimal:
\r
267 obj = Decimal.Parse(value);
\r
269 case DbType.Single:
\r
270 obj = Single.Parse(value);
\r
272 case DbType.Double:
\r
273 obj = Double.Parse(value);
\r
276 String[] sd = value.Split(new Char[] {'-'});
\r
277 obj = new DateTime(
\r
278 Int32.Parse(sd[0]), Int32.Parse(sd[1]), Int32.Parse(sd[2]),
\r
282 String[] st = value.Split(new Char[] {':'});
\r
283 obj = new DateTime(0001,01,01,
\r
284 Int32.Parse(st[0]),Int32.Parse(st[1]),Int32.Parse(st[2]));
\r
286 case DbType.DateTime:
\r
287 Int32 YYYY,MM,DD,hh,mi,ss,ms;
\r
288 YYYY = Int32.Parse(value.Substring(0,4));
\r
289 MM = Int32.Parse(value.Substring(5,2));
\r
290 DD = Int32.Parse(value.Substring(8,2));
\r
291 hh = Int32.Parse(value.Substring(11,2));
\r
292 mi = Int32.Parse(value.Substring(14,2));
\r
293 ss = Int32.Parse(value.Substring(17,2));
\r
294 ms = Int32.Parse(value.Substring(20,2));
\r
295 obj = new DateTime(YYYY,MM,DD,hh,mi,ss,ms);
\r
298 obj = String.Copy(value);
\r
305 // Translates System.Data.DbType to System.Type
306 public static Type DbTypeToSystemType (DbType dType) {
307 // FIXME: more types need
309 // from PostgreSQL oid type
310 // to .NET System.<type>
316 typ = typeof(String);
319 typ = typeof(Boolean);
321 case DbType.Int16:
\r
322 typ = typeof(Int16);
\r
325 typ = typeof(Int32);
\r
328 typ = typeof(Int64);
\r
330 case DbType.Decimal:
\r
331 typ = typeof(Decimal);
\r
333 case DbType.Single:
\r
334 typ = typeof(Single);
\r
336 case DbType.Double:
\r
337 typ = typeof(Double);
\r
341 case DbType.DateTime:
\r
342 typ = typeof(DateTime);
\r
345 typ = typeof(String);
\r
351 // Find DbType for oid
352 // which requires a look up of PostgresTypes
353 // DbType <-> typname <-> oid
354 public static string OidToTypname (int oid, ArrayList pgTypes) {
355 // FIXME: more types need
357 // from PostgreSQL oid type
358 // to .NET System.<type>
360 string typname = "text"; // default
362 for(i = 0; i < pgTypes.Count; i++) {
363 PostgresType pt = (PostgresType) pgTypes[i];
365 typname = pt.typname;
373 // Convert a .NET System value type (Int32, String, Boolean, etc)
374 // to a string that can be included within a SQL statement.
375 // This is to methods provides the parameters support
376 // for the PostgreSQL .NET Data provider
377 public static string ObjectToString(DbType dbtype, object obj) {
\r
379 // TODO: how do we handle a NULL?
\r
380 //if(isNull == true)
\r
385 // Date, Time, and DateTime are expressed in ISO format
\r
386 // which is "YYYY-MM-DD hh:mm:ss.ms";
\r
390 const string zero = "0";
\r
397 if((bool)obj == true)
403 s = obj.ToString();
\r
406 s = obj.ToString();
\r
409 s = obj.ToString();
\r
411 case DbType.Decimal:
\r
412 s = obj.ToString();
\r
414 case DbType.Single:
\r
415 s = obj.ToString();
\r
417 case DbType.Double:
\r
418 s = obj.ToString();
\r
421 dt = (DateTime) obj;
\r
422 sb = new StringBuilder();
\r
426 sb.Append("000" + dt.Year);
\r
427 else if(dt.Year < 100)
\r
428 sb.Append("00" + dt.Year);
\r
429 else if(dt.Year < 1000)
\r
430 sb.Append("0" + dt.Year);
\r
432 sb.Append(dt.Year);
\r
436 sb.Append(zero + dt.Month);
\r
438 sb.Append(dt.Month);
\r
442 sb.Append(zero + dt.Day);
\r
449 dt = (DateTime) obj;
\r
450 sb = new StringBuilder();
\r
454 sb.Append(zero + dt.Hour);
\r
456 sb.Append(dt.Hour);
\r
460 sb.Append(zero + dt.Minute);
\r
462 sb.Append(dt.Minute);
\r
466 sb.Append(zero + dt.Second);
\r
468 sb.Append(dt.Second);
\r
472 case DbType.DateTime:
\r
473 dt = (DateTime) obj;
\r
474 sb = new StringBuilder();
\r
478 sb.Append("000" + dt.Year);
\r
479 else if(dt.Year < 100)
\r
480 sb.Append("00" + dt.Year);
\r
481 else if(dt.Year < 1000)
\r
482 sb.Append("0" + dt.Year);
\r
484 sb.Append(dt.Year);
\r
488 sb.Append(zero + dt.Month);
\r
490 sb.Append(dt.Month);
\r
494 sb.Append(zero + dt.Day);
\r
500 sb.Append(zero + dt.Hour);
\r
502 sb.Append(dt.Hour);
\r
506 sb.Append(zero + dt.Minute);
\r
508 sb.Append(dt.Minute);
\r
512 sb.Append(zero + dt.Second);
\r
514 sb.Append(dt.Second);
\r
517 if(dt.Millisecond < 10)
\r
518 sb.Append(zero + dt.Millisecond);
\r
520 sb.Append(dt.Millisecond);
\r
525 // default to DbType.String
\r
526 s = "'" + obj + "'";
\r