0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Data;
|
|
4 using System.Data.Common;
|
|
5 using System.Transactions;
|
|
6 using BLToolkit.Data.DataProvider.Interpreters;
|
|
7 using BLToolkit.Data.Sql.SqlProvider;
|
|
8
|
|
9 namespace BLToolkit.Data.DataProvider
|
|
10 {
|
|
11 /// <summary>
|
|
12 /// Creates an instance of a db provider for a specified provider name.
|
|
13 /// </summary>
|
|
14 public sealed class GenericDataProvider : DataProviderBase
|
|
15 {
|
|
16 private readonly string _providerName;
|
|
17 private readonly DbProviderFactory _factory;
|
|
18 private readonly DataProviderInterpreterBase _dataProviderInterpreter;
|
|
19
|
|
20 public GenericDataProvider(string providerName)
|
|
21 {
|
|
22 _providerName = providerName;
|
|
23 using (new TransactionScope())
|
|
24 {
|
|
25 _factory = DbProviderFactories.GetFactory(providerName);
|
|
26 }
|
|
27
|
|
28 switch (Name)
|
|
29 {
|
|
30 case ProviderFullName.Oracle:
|
|
31 case ProviderFullName.OracleNet:
|
|
32 _dataProviderInterpreter = new OracleDataProviderInterpreter();
|
|
33 break;
|
|
34 case ProviderFullName.SQLite:
|
|
35 _dataProviderInterpreter = new SqliteDataProviderInterpreter();
|
|
36 break;
|
|
37 default:
|
|
38 throw new Exception(string.Format("The sql provider {0} isnt supported in the DataProviderInterpreterBase", Name));
|
|
39 }
|
|
40 }
|
|
41
|
|
42 #region Overrides of DataProviderBase
|
|
43
|
|
44 public override Type ConnectionType
|
|
45 {
|
|
46 get
|
|
47 {
|
|
48 var dbConnection = _factory.CreateConnection();
|
|
49 if (dbConnection != null)
|
|
50 return dbConnection.GetType();
|
|
51 return null;
|
|
52 }
|
|
53 }
|
|
54
|
|
55 public override string Name
|
|
56 {
|
|
57 get { return _providerName; }
|
|
58 }
|
|
59
|
|
60 public override IDbConnection CreateConnectionObject()
|
|
61 {
|
|
62 return _factory.CreateConnection();
|
|
63 }
|
|
64
|
|
65 public override DbDataAdapter CreateDataAdapterObject()
|
|
66 {
|
|
67 return _factory.CreateDataAdapter();
|
|
68 }
|
|
69
|
|
70 /// <summary>
|
|
71 /// Populates the specified IDbCommand object's Parameters collection with
|
|
72 /// parameter information for the stored procedure specified in the IDbCommand.
|
|
73 /// </summary>
|
|
74 public override bool DeriveParameters(IDbCommand command)
|
|
75 {
|
|
76 return false;
|
|
77 }
|
|
78
|
|
79 public override ISqlProvider CreateSqlProvider()
|
|
80 {
|
|
81 switch (Name)
|
|
82 {
|
|
83 case ProviderFullName.OracleNet:
|
|
84 return new OracleSqlProvider();
|
|
85 case ProviderFullName.SQLite:
|
|
86 return new SQLiteSqlProvider();
|
|
87 case ProviderFullName.Oracle:
|
|
88 return new OracleSqlProvider();
|
|
89 default:
|
|
90 throw new Exception(string.Format("The sql provider {0} isnt supported in the GenericDataProvider", Name));
|
|
91 }
|
|
92 }
|
|
93
|
|
94 public override void SetParameterValue(IDbDataParameter parameter, object value)
|
|
95 {
|
|
96 _dataProviderInterpreter.SetParameterValue(parameter, value);
|
|
97 }
|
|
98
|
|
99 public override string GetSequenceQuery(string sequenceName)
|
|
100 {
|
|
101 return _dataProviderInterpreter.GetSequenceQuery(sequenceName);
|
|
102 }
|
|
103
|
|
104 public override string NextSequenceQuery(string sequenceName)
|
|
105 {
|
|
106 return _dataProviderInterpreter.NextSequenceQuery(sequenceName);
|
|
107 }
|
|
108
|
|
109 public override string GetReturningInto(string columnName)
|
|
110 {
|
|
111 return _dataProviderInterpreter.GetReturningInto(columnName);
|
|
112 }
|
|
113
|
|
114 public override object Convert(object value, ConvertType convertType)
|
|
115 {
|
|
116 if (Name == ProviderFullName.Oracle)
|
|
117 {
|
|
118 switch (convertType)
|
|
119 {
|
|
120 case ConvertType.NameToQueryParameter:
|
|
121 var qname = (string) value;
|
|
122
|
|
123 //
|
|
124 // Avoid "ORA-00972: identifier is too long" error
|
|
125 // Cause error : You tried to reference a table, cluster, view, index, synonym, tablespace, or username with a value that was longer than 30 characters.
|
|
126 // Resolution : Names for tables, clusters, views, indexes, synonyms, tablespaces, and usernames must be 30 characters or less.
|
|
127 // You must shorten the name to no more than 30 characters for these objects.
|
|
128 //
|
|
129 if (qname.Length > 30)
|
|
130 {
|
|
131 qname = qname.Substring(0, 30);
|
|
132 return SqlProvider.Convert(qname, convertType);
|
|
133 }
|
|
134 return SqlProvider.Convert(value, convertType);
|
|
135 }
|
|
136 }
|
|
137
|
|
138 return base.Convert(value, convertType);
|
|
139 }
|
|
140
|
|
141 public override int InsertBatchWithIdentity<T>(
|
|
142 DbManager db,
|
|
143 string insertText,
|
|
144 IEnumerable<T> collection,
|
|
145 Mapping.MemberMapper[] members,
|
|
146 int maxBatchSize,
|
|
147 DbManager.ParameterProvider<T> getParameters)
|
|
148 {
|
|
149 if (db.UseQueryText && Name == ProviderFullName.Oracle)
|
|
150 {
|
|
151 List<string> sqlList = _dataProviderInterpreter.GetInsertBatchSqlList(insertText, collection, members, maxBatchSize, true);
|
|
152 return ExecuteSqlList(db, sqlList);
|
|
153 }
|
|
154 return base.InsertBatchWithIdentity(db, insertText, collection, members, maxBatchSize, getParameters);
|
|
155 }
|
|
156
|
|
157 public override int InsertBatch<T>(
|
|
158 DbManager db,
|
|
159 string insertText,
|
|
160 IEnumerable<T> collection,
|
|
161 Mapping.MemberMapper[] members,
|
|
162 int maxBatchSize,
|
|
163 DbManager.ParameterProvider<T> getParameters)
|
|
164 {
|
|
165 if (Name == ProviderFullName.Oracle)
|
|
166 {
|
|
167 if (db.UseQueryText)
|
|
168 {
|
|
169 List<string> sqlList = _dataProviderInterpreter.GetInsertBatchSqlList(insertText, collection, members, maxBatchSize, false);
|
|
170 return ExecuteSqlList(db, sqlList);
|
|
171 }
|
|
172 throw new NotSupportedException("Set UseQueryText = true on the current generic data provider!");
|
|
173 }
|
|
174 return base.InsertBatch(db, insertText, collection, members, maxBatchSize, getParameters);
|
|
175 }
|
|
176
|
|
177 #endregion
|
|
178 }
|
|
179 }
|