2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Npgsql / Npgsql / NpgsqlServices.cs
blobc6cf226bc14223a209b7c4bb107fa583e2c8d06a
1 #if ENTITIES
2 using System;
3 using System.Collections.Generic;
4 using System.Data.Common;
5 using System.Xml;
6 using System.Data.Common.CommandTrees;
7 using System.Data.Metadata.Edm;
8 using Npgsql.SqlGenerators;
10 namespace Npgsql
12 internal class NpgsqlServices : DbProviderServices
14 private static readonly NpgsqlServices _instance = new NpgsqlServices();
16 public static NpgsqlServices Instance
18 get { return _instance; }
21 protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
23 return CreateCommandDefinition(CreateDbCommand(commandTree));
26 private DbCommand CreateDbCommand(DbCommandTree commandTree)
28 if (commandTree == null)
29 throw new ArgumentNullException("commandTree");
31 DbCommand command = NpgsqlFactory.Instance.CreateCommand();
33 foreach (KeyValuePair<string, TypeUsage> parameter in commandTree.Parameters)
35 DbParameter dbParameter = command.CreateParameter();
36 dbParameter.ParameterName = parameter.Key;
37 command.Parameters.Add(dbParameter);
40 TranslateCommandTree(commandTree, command);
42 return command;
45 private void TranslateCommandTree(DbCommandTree commandTree, DbCommand command)
47 SqlBaseGenerator sqlGenerator = null;
49 DbQueryCommandTree select;
50 DbInsertCommandTree insert;
51 DbUpdateCommandTree update;
52 DbDeleteCommandTree delete;
53 if ((select = commandTree as DbQueryCommandTree) != null)
55 sqlGenerator = new SqlSelectGenerator(select);
57 else if ((insert = commandTree as DbInsertCommandTree) != null)
59 sqlGenerator = new SqlInsertGenerator(insert);
61 else if ((update = commandTree as DbUpdateCommandTree) != null)
63 sqlGenerator = new SqlUpdateGenerator(update);
65 else if ((delete = commandTree as DbDeleteCommandTree) != null)
67 sqlGenerator = new SqlDeleteGenerator(delete);
69 else
71 // TODO: get a message (unsupported DbCommandTree type)
72 throw new ArgumentException();
75 sqlGenerator.BuildCommand(command);
78 protected override string GetDbProviderManifestToken(DbConnection connection)
80 if (connection == null)
81 throw new ArgumentNullException("connection");
82 // TODO: used to use connection.ServerVersion
83 // that doesn't work with a closed connection.
84 bool openedConnection = false;
85 try
87 if (connection.State != System.Data.ConnectionState.Open)
89 connection.Open();
90 openedConnection = true;
92 return connection.ServerVersion;
94 finally
96 if (openedConnection)
98 connection.Close();
103 protected override DbProviderManifest GetDbProviderManifest(string versionHint)
105 if (versionHint == null)
106 throw new ArgumentNullException("versionHint");
107 return new NpgsqlProviderManifest(versionHint);
112 #endif