comparison Source/Data/DataProvider/Interpreters/OracleDataProviderInterpreter.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.Linq;
5 using System.Text;
6 using BLToolkit.Data.Sql.SqlProvider;
7 using BLToolkit.DataAccess;
8 using BLToolkit.Mapping;
9
10 namespace BLToolkit.Data.DataProvider.Interpreters
11 {
12 public class OracleDataProviderInterpreter : DataProviderInterpreterBase
13 {
14 public override void SetParameterValue(IDbDataParameter parameter, object value)
15 {
16 if (value is TimeSpan)
17 {
18 parameter.Value = ((TimeSpan)value).ToString();
19 }
20 else
21 base.SetParameterValue(parameter, value);
22 }
23
24 public override List<string> GetInsertBatchSqlList<T>(
25 string insertText,
26 IEnumerable<T> collection,
27 MemberMapper[] members,
28 int maxBatchSize,
29 bool withIdentity)
30 {
31 //return GetInsertBatchSqlListWithInsertAll(insertText, collection, members, maxBatchSize);
32 return GetInsertBatchSqlListUnionAll(insertText, collection, members, maxBatchSize, withIdentity);
33 }
34
35 private List<string> GetInsertBatchSqlListUnionAll<T>(
36 string insertText,
37 IEnumerable<T> collection,
38 MemberMapper[] members,
39 int maxBatchSize,
40 bool withIdentity)
41 {
42 var sp = new OracleSqlProvider();
43 var n = 0;
44 var sqlList = new List<string>();
45
46 var indexValuesWord = insertText.IndexOf(" VALUES (", StringComparison.Ordinal);
47 var initQuery = insertText.Substring(0, indexValuesWord) + Environment.NewLine;
48 var valuesQuery = insertText.Substring(indexValuesWord + 9);
49 var indexEndValuesQuery = valuesQuery.IndexOf(")");
50 valuesQuery = valuesQuery.Substring(0, indexEndValuesQuery)
51 .Replace("\r", "")
52 .Replace("\n", "")
53 .Replace("\t", "");
54
55 // 1 = Number of primary keys generated by sequence
56 var valuesWihtoutSequence = withIdentity ? valuesQuery.Substring(valuesQuery.IndexOf(",") + 1) : valuesQuery;
57
58 var sb = new StringBuilder(initQuery);
59 sb.Append(" SELECT ");
60 sb.AppendFormat(valuesQuery, members.Select(m => m.Name).ToArray());
61 sb.AppendLine(" FROM (");
62
63 initQuery = sb.ToString();
64
65 sb = new StringBuilder(initQuery);
66 bool isFirstValues = true;
67
68 foreach (var item in collection)
69 {
70 if (!isFirstValues)
71 sb.AppendLine(" UNION ALL ");
72
73 sb.Append("SELECT ");
74
75 var values = new List<object>();
76 foreach (var member in members)
77 {
78 var sbItem = new StringBuilder();
79
80 var value = member.GetValue(item);
81
82 if (value is DateTime?)
83 value = ((DateTime?)value).Value;
84
85 sp.BuildValue(sbItem, value);
86
87 values.Add(sbItem + " " + member.Name);
88 }
89
90 sb.AppendFormat(valuesWihtoutSequence, values.ToArray());
91 sb.Append(" FROM DUAL");
92
93 isFirstValues = false;
94
95 n++;
96 if (n > maxBatchSize)
97 {
98 sb.Append(")");
99 sqlList.Add(sb.ToString());
100 sb = new StringBuilder(initQuery);
101 isFirstValues = true;
102 n = 0;
103 }
104 }
105
106 if (n > 0)
107 {
108 sb.Append(")");
109 sqlList.Add(sb.ToString());
110 }
111 return sqlList;
112 }
113
114 private List<string> GetInsertBatchSqlListWithInsertAll<T>(
115 string insertText,
116 IEnumerable<T> collection,
117 MemberMapper[] members,
118 int maxBatchSize)
119 {
120 var sb = new StringBuilder();
121 var sp = new OracleSqlProvider();
122 var n = 0;
123 var sqlList = new List<string>();
124
125 foreach (var item in collection)
126 {
127 if (sb.Length == 0)
128 sb.AppendLine("INSERT ALL");
129
130 string strItem = "\t" + insertText
131 .Replace("INSERT INTO", "INTO")
132 .Replace("\r", "")
133 .Replace("\n", "")
134 .Replace("\t", " ")
135 .Replace("( ", "(");
136
137 var values = new List<object>();
138 foreach (var member in members)
139 {
140 var sbItem = new StringBuilder();
141
142 var keyGenerator = member.MapMemberInfo.KeyGenerator as SequenceKeyGenerator;
143 if (keyGenerator != null)
144 {
145 values.Add(NextSequenceQuery(keyGenerator.Sequence));
146 }
147 else
148 {
149 var value = member.GetValue(item);
150
151 if (value is DateTime?)
152 value = ((DateTime?)value).Value;
153
154 sp.BuildValue(sbItem, value);
155
156 values.Add(sbItem.ToString());
157 }
158 }
159
160 sb.AppendFormat(strItem, values.ToArray());
161 sb.AppendLine();
162
163 n++;
164
165 if (n >= maxBatchSize)
166 {
167 sb.AppendLine("SELECT * FROM dual");
168
169 var sql = sb.ToString();
170 sqlList.Add(sql);
171
172 n = 0;
173 sb.Length = 0;
174 }
175 }
176
177 if (n > 0)
178 {
179 sb.AppendLine("SELECT * FROM dual");
180
181 var sql = sb.ToString();
182 sqlList.Add(sql);
183 }
184
185 return sqlList;
186 }
187
188 public override string GetSequenceQuery(string sequenceName)
189 {
190 return string.Format("SELECT {0}.NEXTVAL FROM DUAL", sequenceName);
191 }
192
193 public override string NextSequenceQuery(string sequenceName)
194 {
195 return string.Format("{0}.NEXTVAL", sequenceName);
196 }
197
198 public override string GetReturningInto(string columnName)
199 {
200 return string.Format("returning {0} into :IDENTITY_PARAMETER", columnName);
201 }
202 }
203 }