File: QueryBuilder.Upsert.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (342 lines) | stat: -rw-r--r-- 17,489 bytes parent folder | download | duplicates (2)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#region MIT license
// 
// MIT license
//
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// 
#endregion

using System;
using System.Collections.Generic;
using System.Data.Linq.Mapping;
using System.Linq.Expressions;
using System.Reflection;

using DbLinq.Data.Linq.Sql;
using DbLinq.Data.Linq.Sugar.Expressions;
using DbLinq.Util;

namespace DbLinq.Data.Linq.Sugar.Implementation
{
    partial class QueryBuilder
    {
        protected class UpsertParameters
        {
            public MetaTable Table;
            public readonly IList<ObjectInputParameterExpression> InputParameters = new List<ObjectInputParameterExpression>();
            public readonly IList<ObjectOutputParameterExpression> OutputParameters = new List<ObjectOutputParameterExpression>();
            public readonly IList<ObjectInputParameterExpression> PKParameters = new List<ObjectInputParameterExpression>();
            public readonly IList<SqlStatement> InputColumns = new List<SqlStatement>();
            public readonly IList<SqlStatement> InputValues = new List<SqlStatement>();
            public readonly IList<SqlStatement> OutputColumns = new List<SqlStatement>();
            public readonly IList<SqlStatement> OutputValues = new List<SqlStatement>();
            public readonly IList<SqlStatement> OutputExpressions = new List<SqlStatement>();
            public readonly IList<SqlStatement> AutoPKColumns = new List<SqlStatement>();
            public readonly IList<SqlStatement> InputPKColumns = new List<SqlStatement>();
            public readonly IList<SqlStatement> InputPKValues = new List<SqlStatement>();
            public readonly IList<SqlStatement> PKColumns = new List<SqlStatement>();
            public readonly IList<SqlStatement> PKValues = new List<SqlStatement>();
        }

        // SQLite:
        // IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, Expression = null
        // INSERT INTO main.Products (CategoryID, Discontinued, ProductName, QuantityPerUnit) 
        //                  VALUES (@P1, @P2, @P3, @P4) ;SELECT last_insert_rowid()
        //
        // Ingres:
        // IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, 
        //                       Expression = "next value for \"linquser\".\"products_seq\"")]
        // INSERT INTO linquser.products (categoryid, discontinued, productid, productname, quantityperunit) 
        //                  VALUES ($param_000001_param$, $param_000002_param$, 
        //                          next value for "linquser"."products_seq", $param_000004_param$, $param_000005_param$) 
        //
        // Oracle:
        // IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, Expression = null
        // BEGIN 
        // INSERT INTO NORTHWIND."Products" ("CategoryID", "Discontinued", "ProductID", "ProductName", "QuantityPerUnit") 
        //                  VALUES (:P1, :P2, NORTHWIND."Products_SEQ".NextVal, :P4, :P5)
        //               ;SELECT NORTHWIND."Products_SEQ".CurrVal INTO :P3 FROM DUAL; END;
        //
        // PostgreSQL:
        // IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, Expression = "nextval('\"Products_ProductID_seq\"')"
        // INSERT INTO public."Products" ("CategoryID", "Discontinued", "ProductName", "QuantityPerUnit") 
        //                  VALUES (:P1, :P2, :P3, :P4) 
        //               ;SELECT currval('"Products_ProductID_seq"')
        //
        // SQL Server (bogus):
        // IsPrimaryKey = true, IsDbGenerated = true
        // INSERT INTO [dbo].[Products] (, , , ) VALUES (@P1, @P2, @P3, @P4) 
        //                  ; SELECT @@IDENTITY
        //
        // Column:               default --> use value
        //          PK: Expression !null --> use parameter (Oracle is wrong here)
        //              Expression  null --> ignore
        // SQL: wrap clause with PK information


        /// <summary>
        /// Creates a query for insertion
        /// </summary>
        /// <param name="objectToInsert"></param>
        /// <param name="queryContext"></param>
        /// <returns></returns>
        public UpsertQuery GetInsertQuery(object objectToInsert, QueryContext queryContext)
        {
            // TODO: cache
            var upsertParameters = GetUpsertParameters(objectToInsert, false, null, queryContext);
            var sqlProvider = queryContext.DataContext.Vendor.SqlProvider;
            var insertSql = sqlProvider.GetInsert(
                sqlProvider.GetTable(upsertParameters.Table.TableName),
                upsertParameters.InputColumns,
                upsertParameters.InputValues);
            var insertIdSql = sqlProvider.GetInsertIds(
                sqlProvider.GetTable(upsertParameters.Table.TableName),
                upsertParameters.AutoPKColumns,
                upsertParameters.PKColumns,
                upsertParameters.PKValues,
                upsertParameters.OutputColumns,
                upsertParameters.OutputValues,
                upsertParameters.OutputExpressions);
            return new UpsertQuery(queryContext.DataContext, insertSql, insertIdSql, upsertParameters.InputParameters, upsertParameters.OutputParameters, upsertParameters.PKParameters);
        }

        protected enum ParameterType
        {
            Input,
            InputPK,
            Output,
            AutoSync
        }

        /// <summary>
        /// Gets values for insert/update
        /// </summary>
        /// <param name="objectToUpsert"></param>
        /// <param name="queryContext"></param>
        /// <param name="update"></param>
        /// <param name="modifiedMembers"></param>
        /// <returns></returns>
        protected virtual UpsertParameters GetUpsertParameters(object objectToUpsert, bool update, IList<MemberInfo> modifiedMembers, QueryContext queryContext)
        {
            var rowType = objectToUpsert.GetType();
            var sqlProvider = queryContext.DataContext.Vendor.SqlProvider;
            var upsertParameters = new UpsertParameters
                                       {
                                           Table = queryContext.DataContext.Mapping.GetTable(rowType)
                                       };
            foreach (var dataMember in upsertParameters.Table.RowType.PersistentDataMembers)
            {
                var column = sqlProvider.GetColumn(dataMember.MappedName);
                ParameterType type = GetParameterType(objectToUpsert, dataMember, update, update ? AutoSync.OnUpdate : AutoSync.OnInsert);
                var memberInfo = dataMember.Member;
                // if the column is generated AND not specified, we may have:
                // - an explicit generation (Expression property is not null, so we add the column)
                // - an implicit generation (Expression property is null
                // in all cases, we want to get the value back

                var getter = (Expression<Func<object, object>>)(o => memberInfo.GetMemberValue(o));
                var inputParameter = new ObjectInputParameterExpression(
                    getter,
                    memberInfo.GetMemberType(), dataMember.Name);
                if (dataMember.IsPrimaryKey && (! dataMember.IsDbGenerated))
                {
                    upsertParameters.PKColumns.Add(column);
                    upsertParameters.PKParameters.Add(inputParameter);
                    upsertParameters.PKValues.Add(sqlProvider.GetParameterName(inputParameter.Alias));
                }

                if (type == ParameterType.Output)
                {
                    if (dataMember.Expression != null)
                    {
                        upsertParameters.InputColumns.Add(column);
                        upsertParameters.InputValues.Add(dataMember.Expression);
                    }
                    var setter = (Expression<Action<object, object>>)((o, v) => memberInfo.SetMemberValue(o, v));
                    var outputParameter = new ObjectOutputParameterExpression(setter,
                                                                              memberInfo.GetMemberType(),
                                                                              dataMember.Name);

                    if ((dataMember.IsPrimaryKey) && (dataMember.IsDbGenerated))
                        upsertParameters.AutoPKColumns.Add(column);

                    upsertParameters.OutputColumns.Add(column);
                    upsertParameters.OutputParameters.Add(outputParameter);
                    upsertParameters.OutputValues.Add(sqlProvider.GetParameterName(outputParameter.Alias));
                    upsertParameters.OutputExpressions.Add(dataMember.Expression);
                }
                else // standard column
                {
                    if (type == ParameterType.InputPK)
                    {
                        upsertParameters.InputPKColumns.Add(column);
                        upsertParameters.InputPKValues.Add(sqlProvider.GetParameterName(inputParameter.Alias));
                        upsertParameters.InputParameters.Add(inputParameter);
                    }
                    // for a standard column, we keep it only if modifiedMembers contains the specified memberInfo
                    // caution: this makes the cache harder to maintain
                    else if (modifiedMembers == null || modifiedMembers.Contains(memberInfo))
                    {
                        upsertParameters.InputColumns.Add(column);
                        upsertParameters.InputValues.Add(sqlProvider.GetParameterName(inputParameter.Alias));
                        upsertParameters.InputParameters.Add(inputParameter);
                    }

                    if (type == ParameterType.AutoSync)
                    {
                        var setter = (Expression<Action<object, object>>)((o, v) => memberInfo.SetMemberValue(o, v));
                        var outputParameter = new ObjectOutputParameterExpression(setter,
                                                                                  memberInfo.GetMemberType(),
                                                                                  dataMember.Name);
                        upsertParameters.OutputColumns.Add(column);
                        upsertParameters.OutputParameters.Add(outputParameter);
                        upsertParameters.OutputValues.Add(sqlProvider.GetParameterName(outputParameter.Alias));
                        upsertParameters.OutputExpressions.Add(dataMember.Expression);
                    }
                }
            }
            return upsertParameters;
        }

        /// <summary>
        /// Provides the parameter type for a given data member
        /// </summary>
        /// <param name="objectToUpsert"></param>
        /// <param name="dataMember"></param>
        /// <param name="update"></param>
        /// <returns></returns>
        protected virtual ParameterType GetParameterType(object objectToUpsert, MetaDataMember dataMember, bool update, AutoSync autoSync)
        {
            var memberInfo = dataMember.Member;
            // the deal with columns is:
            // PK only:  criterion for INSERT, criterion for UPDATE
            // PK+GEN:   implicit/criterion for INSERT, criterion for UPDATE
            // GEN only: implicit for both
            // -:        explicit for both
            //
            // explicit is input,
            // implicit is output, 
            // criterion is input PK
            ParameterType type;
            if (dataMember.IsPrimaryKey)
            {
                if (update)
                    type = ParameterType.InputPK;
                else
                {
                    if (dataMember.IsDbGenerated)
                    {
                        if (IsSpecified(objectToUpsert, memberInfo))
                            type = ParameterType.Input;
                        else
                            type = ParameterType.Output;
                    }
                    else
                        type = ParameterType.Input;
                }
            }
            else
            {
                if (dataMember.IsDbGenerated)
                    type = ParameterType.Output;
				else if ((dataMember.AutoSync == AutoSync.Always) || (dataMember.AutoSync == autoSync))
					type = ParameterType.AutoSync;
                else
					type = ParameterType.Input;
            }
            return type;
        }

        /// <summary>
        /// Determines if a property is different from its default value
        /// </summary>
        /// <param name="target"></param>
        /// <param name="memberInfo"></param>
        /// <returns></returns>
        protected virtual bool IsSpecified(object target, MemberInfo memberInfo)
        {
            object value = memberInfo.GetMemberValue(target);
            if (value == null)
                return false;
            if (Equals(value, TypeConvert.GetDefault(memberInfo.GetMemberType())))
                return false;
            return true;
        }

        /// <summary>
        /// Creates or gets an UPDATE query
        /// </summary>
        /// <param name="objectToUpdate"></param>
        /// <param name="modifiedMembers">List of modified members, or NULL</param>
        /// <param name="queryContext"></param>
        /// <returns></returns>
        public UpsertQuery GetUpdateQuery(object objectToUpdate, IList<MemberInfo> modifiedMembers, QueryContext queryContext)
        {
            var upsertParameters = GetUpsertParameters(objectToUpdate, true, modifiedMembers, queryContext);
            var sqlProvider = queryContext.DataContext.Vendor.SqlProvider;
            var updateSql = sqlProvider.GetUpdate(sqlProvider.GetTable(upsertParameters.Table.TableName),
                upsertParameters.InputColumns, upsertParameters.InputValues,
                upsertParameters.OutputValues, upsertParameters.OutputExpressions,
                upsertParameters.InputPKColumns, upsertParameters.InputPKValues
                );
            var insertIdSql = (upsertParameters.OutputValues.Count == 0) ? "" :
                    sqlProvider.GetInsertIds(
                sqlProvider.GetTable(upsertParameters.Table.TableName),
                upsertParameters.AutoPKColumns,
                upsertParameters.PKColumns,
                upsertParameters.PKValues,
                upsertParameters.OutputColumns,
                upsertParameters.OutputValues,
                upsertParameters.OutputExpressions);
            return new UpsertQuery(queryContext.DataContext, updateSql, insertIdSql, upsertParameters.InputParameters, upsertParameters.OutputParameters, upsertParameters.PKParameters);
        }

        /// <summary>
        /// Creates or gets a DELETE query
        /// </summary>
        /// <param name="objectToDelete"></param>
        /// <param name="queryContext"></param>
        /// <returns></returns>
        public DeleteQuery GetDeleteQuery(object objectToDelete, QueryContext queryContext)
        {
            var sqlProvider = queryContext.DataContext.Vendor.SqlProvider;
            var rowType = objectToDelete.GetType();
            var table = queryContext.DataContext.Mapping.GetTable(rowType);
            var deleteParameters = new List<ObjectInputParameterExpression>();
            var pkColumns = new List<SqlStatement>();
            var pkValues = new List<SqlStatement>();
            foreach (var pkMember in table.RowType.IdentityMembers)
            {
                var memberInfo = pkMember.Member;
                var getter = (Expression<Func<object, object>>)(o => memberInfo.GetMemberValue(o));
                var inputParameter = new ObjectInputParameterExpression(
                    getter,
                    memberInfo.GetMemberType(), pkMember.Name);
                var column = sqlProvider.GetColumn(pkMember.MappedName);
                pkColumns.Add(column);
                pkValues.Add(sqlProvider.GetParameterName(inputParameter.Alias));
                deleteParameters.Add(inputParameter);
            }
            var deleteSql = sqlProvider.GetDelete(sqlProvider.GetTable(table.TableName), pkColumns, pkValues);
            return new DeleteQuery(queryContext.DataContext, deleteSql, deleteParameters);
        }
    }
}