Esempio n. 1
0
            static Query <int> CreateQuery(
                IDataContext dataContext,
                string?tableName, string?serverName, string?databaseName, string?schemaName,
                Type type)
            {
                var sqlTable = new SqlTable(dataContext.MappingSchema, type);

                if (tableName != null)
                {
                    sqlTable.PhysicalName = tableName;
                }
                if (serverName != null)
                {
                    sqlTable.Server = serverName;
                }
                if (databaseName != null)
                {
                    sqlTable.Database = databaseName;
                }
                if (schemaName != null)
                {
                    sqlTable.Schema = schemaName;
                }

                var deleteStatement = new SqlDeleteStatement();

                deleteStatement.SelectQuery.From.Table(sqlTable);

                var ei = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    Statement = deleteStatement,
                                } }
                };

                var keys = sqlTable.GetKeys(true).Cast <SqlField>().ToList();

                if (keys.Count == 0)
                {
                    throw new LinqException($"Table '{sqlTable.Name}' does not have primary key.");
                }

                foreach (var field in keys)
                {
                    var param = GetParameter(type, dataContext, field);

                    ei.Queries[0].Parameters.Add(param);

                    deleteStatement.SelectQuery.Where.Field(field).Equal.Expr(param.SqlParameter);

                    if (field.CanBeNull)
                    {
                        deleteStatement.IsParameterDependent = true;
                    }
                }

                SetNonQueryQuery(ei);

                return(ei);
            }
Esempio n. 2
0
            static Query <int> CreateQuery(IDataContext dataContext)
            {
                var sqlTable = new SqlTable <T>(dataContext.MappingSchema);
                var sqlQuery = new SelectQuery {
                    QueryType = QueryType.Update
                };

                sqlQuery.From.Table(sqlTable);

                var ei = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    SelectQuery = sqlQuery,
                                } }
                };

                var keys   = sqlTable.GetKeys(true).Cast <SqlField>().ToList();
                var fields = sqlTable.Fields.Values.Where(f => f.IsUpdatable).Except(keys).ToList();

                if (fields.Count == 0)
                {
                    if (Configuration.Linq.IgnoreEmptyUpdate)
                    {
                        return(null);
                    }

                    throw new LinqException(
                              (keys.Count == sqlTable.Fields.Count ?
                               "There are no fields to update in the type '{0}'. No PK is defined or all fields are keys." :
                               "There are no fields to update in the type '{0}'.")
                              .Args(sqlTable.Name));
                }

                foreach (var field in fields)
                {
                    var param = GetParameter(typeof(T), dataContext, field);

                    ei.Queries[0].Parameters.Add(param);

                    sqlQuery.Update.Items.Add(new SelectQuery.SetExpression(field, param.SqlParameter));
                }

                foreach (var field in keys)
                {
                    var param = GetParameter(typeof(T), dataContext, field);

                    ei.Queries[0].Parameters.Add(param);

                    sqlQuery.Where.Field(field).Equal.Expr(param.SqlParameter);

                    if (field.CanBeNull)
                    {
                        sqlQuery.IsParameterDependent = true;
                    }
                }

                SetNonQueryQuery(ei);

                return(ei);
            }
Esempio n. 3
0
        public static int Delete(DbManager dataContext, T obj)
        {
            if (Equals(default(T), obj))
            {
                return(0);
            }

            ExpressionInfo <int> ei;

            var key = new { dataContext.MappingSchema, dataContext.DataProvider };

            if (!ObjectOperation <T> .Delete.TryGetValue(key, out ei))
            {
                lock (_sync)
                    if (!ObjectOperation <T> .Delete.TryGetValue(key, out ei))
                    {
                        var sqlTable = new SqlTable <T>(dataContext.MappingSchema);
                        var sqlQuery = new SqlQuery {
                            QueryType = QueryType.Delete
                        };

                        sqlQuery.From.Table(sqlTable);

                        ei = new ExpressionInfo <int>
                        {
                            MappingSchema = dataContext.MappingSchema,
                            DataProvider  = dataContext.DataProvider,
                            Queries       = { new ExpressionInfo <int> .QueryInfo {
                                                  SqlQuery = sqlQuery,
                                              } }
                        };

                        var keys = sqlTable.GetKeys(true).Cast <SqlField>().ToList();

                        if (keys.Count == 0)
                        {
                            throw new LinqException(
                                      string.Format("Table '{0}' does not have primary key.", sqlTable.Name));
                        }

                        foreach (var field in keys)
                        {
                            var param = GetParameter <int>(dataContext, field);

                            ei.Queries[0].Parameters.Add(param);

                            sqlQuery.Where.Field(field).Equal.Expr(param.SqlParameter);
                        }

                        ei.SetNonQueryQuery();

                        ObjectOperation <T> .Delete.Add(key, ei);
                    }
            }

            return((int)ei.GetElement(null, dataContext, Expression.Constant(obj), null));
        }
Esempio n. 4
0
            static Query <int> CreateQuery(IDataContext dataContext)
            {
                var sqlTable = new SqlTable <T>(dataContext.MappingSchema);
                var sqlQuery = new SelectQuery {
                    QueryType = QueryType.Delete
                };

                sqlQuery.From.Table(sqlTable);

                var ei = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    SelectQuery = sqlQuery,
                                } }
                };

                var keys = sqlTable.GetKeys(true).Cast <SqlField>().ToList();

                if (keys.Count == 0)
                {
                    throw new LinqException("Table '{0}' does not have primary key.".Args(sqlTable.Name));
                }

                foreach (var field in keys)
                {
                    var param = GetParameter(typeof(T), dataContext, field);

                    ei.Queries[0].Parameters.Add(param);

                    sqlQuery.Where.Field(field).Equal.Expr(param.SqlParameter);

                    if (field.CanBeNull)
                    {
                        sqlQuery.IsParameterDependent = true;
                    }
                }

                SetNonQueryQuery(ei);

                return(ei);
            }
            static Query <int> CreateQuery(IDataContext dataContext, EntityDescriptor descriptor, T obj, string tableName, string databaseName, string schemaName, Type type)
            {
                var fieldDic = new Dictionary <SqlField, ParameterAccessor>();
                var sqlTable = new SqlTable(dataContext.MappingSchema, type);

                if (tableName != null)
                {
                    sqlTable.PhysicalName = tableName;
                }
                if (databaseName != null)
                {
                    sqlTable.Database = databaseName;
                }
                if (schemaName != null)
                {
                    sqlTable.Schema = schemaName;
                }

                var sqlQuery = new SelectQuery();

                ParameterAccessor param = null;

                var insertOrUpdateStatement = new SqlInsertOrUpdateStatement(sqlQuery);

                insertOrUpdateStatement.Insert.Into  = sqlTable;
                insertOrUpdateStatement.Update.Table = sqlTable;

                sqlQuery.From.Table(sqlTable);

                var ei = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    Statement = insertOrUpdateStatement,
                                } }
                };

                var supported = ei.SqlProviderFlags.IsInsertOrUpdateSupported && ei.SqlProviderFlags.CanCombineParameters;

                // Insert.
                //
                foreach (var field in sqlTable.Fields.Select(f => f.Value))
                {
                    if (field.IsInsertable && !field.ColumnDescriptor.ShouldSkip(obj, descriptor, SkipModification.Insert))
                    {
                        if (!supported || !fieldDic.TryGetValue(field, out param))
                        {
                            param = GetParameter(type, dataContext, field);
                            ei.Queries[0].Parameters.Add(param);

                            if (supported)
                            {
                                fieldDic.Add(field, param);
                            }
                        }

                        insertOrUpdateStatement.Insert.Items.Add(new SqlSetExpression(field, param.SqlParameter));
                    }
                    else if (field.IsIdentity)
                    {
                        throw new LinqException("InsertOrReplace method does not support identity field '{0}.{1}'.", sqlTable.Name, field.Name);
                    }
                }

                // Update.
                //
                var keys   = sqlTable.GetKeys(true).Cast <SqlField>().ToList();
                var fields = sqlTable.Fields.Values.Where(f => f.IsUpdatable && !f.ColumnDescriptor.ShouldSkip(obj, descriptor, SkipModification.Update))
                             .Except(keys).ToList();

                if (keys.Count == 0)
                {
                    throw new LinqException("InsertOrReplace method requires the '{0}' table to have a primary key.", sqlTable.Name);
                }

                var q =
                    (
                        from k in keys
                        join i in insertOrUpdateStatement.Insert.Items on k equals i.Column
                        select new { k, i }
                    ).ToList();

                var missedKey = keys.Except(q.Select(i => i.k)).FirstOrDefault();

                if (missedKey != null)
                {
                    throw new LinqException("InsertOrReplace method requires the '{0}.{1}' field to be included in the insert setter.",
                                            sqlTable.Name,
                                            missedKey.Name);
                }

                if (fields.Count == 0)
                {
                    throw new LinqException("There are no fields to update in the type '{0}'.", sqlTable.Name);
                }

                foreach (var field in fields)
                {
                    if (!supported || !fieldDic.TryGetValue(field, out param))
                    {
                        param = GetParameter(type, dataContext, field);
                        ei.Queries[0].Parameters.Add(param);

                        if (supported)
                        {
                            fieldDic.Add(field, param = GetParameter(typeof(T), dataContext, field));
                        }
                    }

                    insertOrUpdateStatement.Update.Items.Add(new SqlSetExpression(field, param.SqlParameter));
                }

                insertOrUpdateStatement.Update.Keys.AddRange(q.Select(i => i.i));

                // Set the query.
                //
                if (ei.SqlProviderFlags.IsInsertOrUpdateSupported)
                {
                    SetNonQueryQuery(ei);
                }
                else
                {
                    MakeAlternativeInsertOrUpdate(ei);
                }

                return(ei);
            }
Esempio n. 6
0
            static Query <int>?CreateQuery(
                IDataContext dataContext,
                EntityDescriptor descriptor,
                T obj,
                UpdateColumnFilter <T>?columnFilter,
                string?tableName,
                string?serverName,
                string?databaseName,
                string?schemaName,
                TableOptions tableOptions,
                Type type)
            {
                var sqlTable = new SqlTable <T>(dataContext.MappingSchema);

                if (tableName != null)
                {
                    sqlTable.PhysicalName = tableName;
                }
                if (serverName != null)
                {
                    sqlTable.Server = serverName;
                }
                if (databaseName != null)
                {
                    sqlTable.Database = databaseName;
                }
                if (schemaName != null)
                {
                    sqlTable.Schema = schemaName;
                }
                if (tableOptions.IsSet())
                {
                    sqlTable.TableOptions = tableOptions;
                }

                var sqlQuery        = new SelectQuery();
                var updateStatement = new SqlUpdateStatement(sqlQuery);

                sqlQuery.From.Table(sqlTable);

                var ei = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    Statement = updateStatement,
                                } }
                };

                var keys   = sqlTable.GetKeys(true).Cast <SqlField>().ToList();
                var fields = sqlTable.Fields
                             .Where(f => f.IsUpdatable && !f.ColumnDescriptor.ShouldSkip(obj !, descriptor, SkipModification.Update) && (columnFilter == null || columnFilter(obj, f.ColumnDescriptor)))
                             .Except(keys);

                var fieldCount = 0;

                foreach (var field in fields)
                {
                    var param = GetParameter(type, dataContext, field);

                    ei.Queries[0].Parameters.Add(param);

                    updateStatement.Update.Items.Add(new SqlSetExpression(field, param.SqlParameter));

                    fieldCount++;
                }

                if (fieldCount == 0)
                {
                    if (Configuration.Linq.IgnoreEmptyUpdate)
                    {
                        return(null);
                    }

                    throw new LinqException(
                              keys.Count == sqlTable.Fields.Count ?
                              $"There are no fields to update in the type '{sqlTable.Name}'. No PK is defined or all fields are keys." :
                              $"There are no fields to update in the type '{sqlTable.Name}'.");
                }

                foreach (var field in keys)
                {
                    var param = GetParameter(type, dataContext, field);

                    ei.Queries[0].Parameters.Add(param);

                    sqlQuery.Where.Field(field).Equal.Expr(param.SqlParameter);

                    if (field.CanBeNull)
                    {
                        sqlQuery.IsParameterDependent = true;
                    }
                }

                SetNonQueryQuery(ei);

                return(ei);
            }
Esempio n. 7
0
            static Query <int> CreateQuery(IDataContext dataContext, string tableName, string databaseName, string schemaName, Type type)
            {
                var sqlTable = new SqlTable <T>(dataContext.MappingSchema);

                if (tableName != null)
                {
                    sqlTable.PhysicalName = tableName;
                }
                if (databaseName != null)
                {
                    sqlTable.Database = databaseName;
                }
                if (schemaName != null)
                {
                    sqlTable.Schema = schemaName;
                }

                var sqlQuery        = new SelectQuery();
                var updateStatement = new SqlUpdateStatement(sqlQuery);

                sqlQuery.From.Table(sqlTable);

                var ei = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    Statement = updateStatement,
                                } }
                };

                var keys   = sqlTable.GetKeys(true).Cast <SqlField>().ToList();
                var fields = sqlTable.Fields.Values.Where(f => f.IsUpdatable).Except(keys).ToList();

                if (fields.Count == 0)
                {
                    if (Configuration.Linq.IgnoreEmptyUpdate)
                    {
                        return(null);
                    }

                    throw new LinqException(
                              keys.Count == sqlTable.Fields.Count ?
                              $"There are no fields to update in the type '{sqlTable.Name}'. No PK is defined or all fields are keys." :
                              $"There are no fields to update in the type '{sqlTable.Name}'.");
                }

                foreach (var field in fields)
                {
                    var param = GetParameter(type, dataContext, field);

                    ei.Queries[0].Parameters.Add(param);

                    updateStatement.Update.Items.Add(new SqlSetExpression(field, param.SqlParameter));
                }

                foreach (var field in keys)
                {
                    var param = GetParameter(type, dataContext, field);

                    ei.Queries[0].Parameters.Add(param);

                    sqlQuery.Where.Field(field).Equal.Expr(param.SqlParameter);

                    if (field.CanBeNull)
                    {
                        sqlQuery.IsParameterDependent = true;
                    }
                }

                SetNonQueryQuery(ei);

                return(ei);
            }