comparison DataProviders/DevartOraclePro/DevartOracleDataProvider.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Data.Common;
5 using System.Text;
6
7 using BLToolkit.Data.Sql.SqlProvider;
8 using BLToolkit.Mapping;
9
10 using Devart.Data.Oracle;
11
12 namespace BLToolkit.Data.DataProvider
13 {
14 public class DevartOracleDataProvider : DataProviderBase
15 {
16 /// <summary>
17 /// Data provider name string.
18 /// </summary>
19 public const string NameString = "DevartOracle";
20
21 public override Type ConnectionType
22 {
23 get { return typeof(OracleConnection); }
24 }
25
26 public override string Name
27 {
28 get { return NameString; }
29 }
30
31 /// <summary>
32 /// Gets or sets the database activity monitor.
33 /// </summary>
34 private static Devart.Common.DbMonitor DbMonitor { get; set; }
35
36 /// <summary>
37 /// Gets or sets value indicating whether the database activity monitor is enabled.
38 /// </summary>
39 /// <remarks>
40 /// This feature requires Standard or Pro edition of Devart dotConnect for Oracle provider.
41 /// </remarks>
42 public static bool DbMonitorActive
43 {
44 get { return DbMonitor == null ? false : DbMonitor.IsActive; }
45 set
46 {
47 // setting this property has no effect in Express edition
48 #if DEVART_PRO
49 if (DbMonitorActive != value)
50 {
51 DbMonitor = DbMonitor ?? new OracleMonitor();
52 DbMonitor.IsActive = value;
53 }
54 #endif
55 }
56 }
57
58 public override IDbConnection CreateConnectionObject()
59 {
60 return new OracleConnection();
61 }
62
63 public override DbDataAdapter CreateDataAdapterObject()
64 {
65 return new OracleDataAdapter();
66 }
67
68 public override bool DeriveParameters(IDbCommand command)
69 {
70 var oraCommand = command as OracleCommand;
71
72 if (null != oraCommand)
73 {
74 try
75 {
76 OracleCommandBuilder.DeriveParameters(oraCommand);
77 }
78 catch (Exception ex)
79 {
80 // Make Oracle less laconic.
81 //
82 throw new DataException(string.Format("{0}\nCommandText: {1}", ex.Message, oraCommand.CommandText), ex);
83 }
84
85 return true;
86 }
87
88 return false;
89 }
90
91 public override void AttachParameter(IDbCommand command, IDbDataParameter parameter)
92 {
93 var value = parameter.Value;
94
95 if (value is DateTime)
96 {
97 parameter.Value = new OracleTimeStamp((DateTime)value);
98 }
99 else if (value is Guid)
100 {
101 parameter.Value = ((Guid)value).ToByteArray();
102 parameter.DbType = DbType.Binary;
103 ((OracleParameter)parameter).OracleDbType = OracleDbType.Raw;
104 }
105 // else if (value is string)
106 // {
107 // ((OracleParameter)parameter).OracleDbType = OracleDbType.NVarChar;
108 // }
109
110 base.AttachParameter(command, parameter);
111 }
112
113 public override int ExecuteArray(IDbCommand command, int iterations)
114 {
115 var cmd = (OracleCommand)command;
116 return cmd.ExecuteArray(iterations);
117 }
118
119 public override ISqlProvider CreateSqlProvider()
120 {
121 return new OracleSqlProvider();
122 }
123
124
125 #region InsertBatch
126
127 public override int InsertBatch<T>(
128 DbManager db,
129 string insertText,
130 IEnumerable<T> collection,
131 MemberMapper[] members,
132 int maxBatchSize,
133 DbManager.ParameterProvider<T> getParameters)
134 {
135 var sb = new StringBuilder();
136 var sp = new OracleSqlProvider();
137 var n = 0;
138 var cnt = 0;
139 var str = "\t" + insertText
140 .Substring(0, insertText.IndexOf(") VALUES ("))
141 .Substring(7)
142 .Replace("\r", "")
143 .Replace("\n", "")
144 .Replace("\t", " ")
145 .Replace("( ", "(")
146 //.Replace(" ", " ")
147 + ") VALUES (";
148
149 foreach (var item in collection)
150 {
151 if (sb.Length == 0)
152 sb.AppendLine("INSERT ALL");
153
154 sb.Append(str);
155
156 foreach (var member in members)
157 {
158 var value = member.GetValue(item);
159
160 if (value is Nullable<DateTime>)
161 value = ((DateTime?)value).Value;
162
163 if (value is DateTime)
164 {
165 var dt = (DateTime)value;
166 sb.Append(string.Format("to_timestamp('{0:dd.MM.yyyy HH:mm:ss.ffffff}', 'DD.MM.YYYY HH24:MI:SS.FF6')", dt));
167 }
168 else
169 sp.BuildValue(sb, value);
170
171 sb.Append(", ");
172 }
173
174 sb.Length -= 2;
175 sb.AppendLine(")");
176
177 n++;
178
179 if (n >= maxBatchSize)
180 {
181 sb.AppendLine("SELECT * FROM dual");
182
183 var sql = sb.ToString();
184
185 if (DbManager.TraceSwitch.TraceInfo)
186 DbManager.WriteTraceLine("\n" + sql.Replace("\r", ""), DbManager.TraceSwitch.DisplayName);
187
188 cnt += db.SetCommand(sql).ExecuteNonQuery();
189
190 n = 0;
191 sb.Length = 0;
192 }
193 }
194
195 if (n > 0)
196 {
197 sb.AppendLine("SELECT * FROM dual");
198
199 var sql = sb.ToString();
200
201 if (DbManager.TraceSwitch.TraceInfo)
202 DbManager.WriteTraceLine("\n" + sql.Replace("\r", ""), DbManager.TraceSwitch.DisplayName);
203
204 cnt += db.SetCommand(sql).ExecuteNonQuery();
205 }
206
207 return cnt;
208 }
209
210 #endregion
211 }
212 }