/
NpgsqlServices.cs
118 lines (104 loc) · 3.72 KB
/
NpgsqlServices.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#if ENTITIES
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Xml;
#if ENTITIES6
using System.Data.Entity.Core.Common;
using System.Data.Entity.Core.Common.CommandTrees;
using System.Data.Entity.Core.Metadata.Edm;
#else
using System.Data.Common.CommandTrees;
using System.Data.Metadata.Edm;
#endif
using Npgsql.SqlGenerators;
namespace Npgsql
{
internal class NpgsqlServices : DbProviderServices
{
private static readonly NpgsqlServices _instance = new NpgsqlServices();
public static NpgsqlServices Instance
{
get { return _instance; }
}
protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
{
return CreateCommandDefinition(CreateDbCommand(commandTree));
}
private DbCommand CreateDbCommand(DbCommandTree commandTree)
{
if (commandTree == null)
throw new ArgumentNullException("commandTree");
DbCommand command = NpgsqlFactory.Instance.CreateCommand();
foreach (KeyValuePair<string, TypeUsage> parameter in commandTree.Parameters)
{
DbParameter dbParameter = command.CreateParameter();
dbParameter.ParameterName = parameter.Key;
command.Parameters.Add(dbParameter);
}
TranslateCommandTree(commandTree, command);
return command;
}
private void TranslateCommandTree(DbCommandTree commandTree, DbCommand command)
{
SqlBaseGenerator sqlGenerator = null;
DbQueryCommandTree select;
DbInsertCommandTree insert;
DbUpdateCommandTree update;
DbDeleteCommandTree delete;
if ((select = commandTree as DbQueryCommandTree) != null)
{
sqlGenerator = new SqlSelectGenerator(select);
}
else if ((insert = commandTree as DbInsertCommandTree) != null)
{
sqlGenerator = new SqlInsertGenerator(insert);
}
else if ((update = commandTree as DbUpdateCommandTree) != null)
{
sqlGenerator = new SqlUpdateGenerator(update);
}
else if ((delete = commandTree as DbDeleteCommandTree) != null)
{
sqlGenerator = new SqlDeleteGenerator(delete);
}
else
{
// TODO: get a message (unsupported DbCommandTree type)
throw new ArgumentException();
}
sqlGenerator.BuildCommand(command);
}
protected override string GetDbProviderManifestToken(DbConnection connection)
{
if (connection == null)
throw new ArgumentNullException("connection");
// TODO: used to use connection.ServerVersion
// that doesn't work with a closed connection.
bool openedConnection = false;
try
{
if (connection.State != System.Data.ConnectionState.Open)
{
connection.Open();
openedConnection = true;
}
return connection.ServerVersion;
}
finally
{
if (openedConnection)
{
connection.Close();
}
}
}
protected override DbProviderManifest GetDbProviderManifest(string versionHint)
{
if (versionHint == null)
throw new ArgumentNullException("versionHint");
return new NpgsqlProviderManifest(versionHint);
}
}
}
#endif