Ejemplo n.º 1
0
        private static object Insert(this DbConnection connection, object data, string tableName,
                                     IFieldInfoCollection fields, DbTransaction transaction = null,
                                     OnConflictOption option                = OnConflictOption.Default,
                                     ICommandBuilder commandBuilder         = null,
                                     IExceptionProcessor exceptionProcessor = null,
                                     object keyValue      = null,
                                     bool persistKeyvalue = true)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            commandBuilder = commandBuilder ?? DefaultCommandBuilder;

            using (var command = connection.CreateCommand())
            {
                commandBuilder.BuildInsert(command, data, tableName, fields,
                                           option: option,
                                           keyValue: keyValue,
                                           persistKeyvalue: persistKeyvalue);

                try
                {
                    var ptData = data as IPersistanceTracking;

                    ptData?.OnInserting();
                    var id = command.ExecuteScalar();
                    ptData?.OnInserted();

                    var pkField = fields.PrimaryKeyField;
                    if (pkField != null)
                    {
                        var value = ValueMapper.ProcessValue(pkField.RunTimeType, id);
                        pkField.SetFieldValue(data, value);

                        return(value);
                    }
                    else
                    {
                        return(id);
                    }
                }
                catch (Exception ex)
                {
                    if (exceptionProcessor is null)
                    {
                        throw;
                    }
                    else
                    {
                        throw exceptionProcessor.ProcessException(ex, connection, (string)null, transaction);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        internal static void Update(this DbConnection connection, object data, string tableName,
                                    string whereExpression                 = null,
                                    IFieldInfoCollection fields            = null, OnConflictOption option        = OnConflictOption.Default,
                                    DbTransaction transaction              = null, ICommandBuilder commandBuilder = null,
                                    IExceptionProcessor exceptionProcessor = null, object keyValue                = null)
        {
            if (data is null)
            {
                throw new ArgumentNullException("data");
            }

            commandBuilder = commandBuilder ?? DefaultCommandBuilder;

            if (data is IPersistanceTracking)
            {
                ((IPersistanceTracking)data).OnUpdating();
            }

            using (var command = connection.CreateCommand())
            {
                commandBuilder.BuildUpdate(command,
                                           data,
                                           tableName,
                                           fields,
                                           whereExpression: whereExpression,
                                           option: option,
                                           keyValue: keyValue);

                try
                {
                    var changes = ExecuteNonQuery(connection, command, transaction);
                    if (option != OnConflictOption.Ignore)
                    {
                        Debug.Assert(changes > 0, "update command resulted in no changes");
                    }
                }
                catch (Exception e)
                {
                    if (exceptionProcessor != null)
                    {
                        throw exceptionProcessor.ProcessException(e, connection, command.CommandText, transaction);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (data is IPersistanceTracking)
            {
                ((IPersistanceTracking)data).OnUpdated();
            }
        }
Ejemplo n.º 3
0
        public void Save(DataObject_Base data, OnConflictOption option, bool cache = true)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (data.IsChanged == false)
            {
                Logger.Log("save skipped because data has no changes", LogCategory.CRUD, LogLevel.Verbose);
                return;
            }

            if (!data.IsPersisted)
            {
                object primaryKey = Insert(data, option: option);

                var dal = data.DAL;
                if (dal != this)
                {
                    data.InternalSetDAL(this);
                }

                if (cache && primaryKey != null)
                {
                    EntityCache cacheStore = GetEntityCache(data.GetType());

                    Debug.Assert(cacheStore.ContainsKey(primaryKey) == false, "Cache already contains entity, existing entity will be replaced");
                    if (cacheStore.ContainsKey(primaryKey))
                    {
                        cacheStore[primaryKey] = data;
                    }
                    else
                    {
                        cacheStore.Add(primaryKey, data);
                    }
                }
            }
            else
            {
                Update(data, option: option);
            }
        }
Ejemplo n.º 4
0
        public static void Update(this DbConnection connection, object data, string tableName = null,
                                  string whereExpression = null,
                                  EntityDescription entityDescription    = null,
                                  OnConflictOption option                = OnConflictOption.Default, DbTransaction transaction = null,
                                  IExceptionProcessor exceptionProcessor = null, object keyValue = null)
        {
            if (data is null)
            {
                throw new ArgumentNullException("data");
            }

            entityDescription = entityDescription ?? DescriptionLookup.LookUpEntityByType(data.GetType());
            tableName         = tableName ?? entityDescription.SourceName;

            Update(connection, data, tableName,
                   whereExpression: whereExpression,
                   fields: entityDescription.Fields,
                   option: option,
                   transaction: transaction,
                   exceptionProcessor: exceptionProcessor,
                   keyValue: keyValue);
        }
Ejemplo n.º 5
0
        public static object Insert(this DbConnection connection, object data, string tableName = null,
                                    EntityDescription entityDescription = null, DbTransaction transaction = null,
                                    OnConflictOption option             = OnConflictOption.Default,
                                    ICommandBuilder commandBuilder      = null, IExceptionProcessor exceptionProcessor = null,
                                    object keyValue      = null,
                                    bool persistKeyvalue = true)
        {
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            entityDescription = entityDescription ?? DescriptionLookup.LookUpEntityByType(data.GetType());
            tableName         = tableName ?? entityDescription.SourceName;

            return(Insert(connection, data, tableName, entityDescription.Fields,
                          transaction: transaction,
                          option: option,
                          commandBuilder: commandBuilder,
                          exceptionProcessor: exceptionProcessor,
                          keyValue: keyValue,
                          persistKeyvalue: persistKeyvalue));
        }
 public override void Save(OnConflictOption option)
 {
     base.Save(option);
     this._hasEdits = false;
 }
 public virtual void Save(OnConflictOption option)
 {
     Debug.Assert(DAL != null);
     this.DAL.Save(this, option);
 }
 public virtual void Save(OnConflictOption option)
 {
     Debug.Assert(DAL != null);
     this.DAL.Save(this, option);
 }
Ejemplo n.º 9
0
        public void BuildInsert(IDbCommand command,
                                object data,
                                string tableName,
                                IFieldInfoCollection fields,
                                OnConflictOption option = OnConflictOption.Default,
                                object keyValue         = null,
                                bool persistKeyvalue    = true)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (fields is null)
            {
                throw new ArgumentNullException(nameof(fields));
            }
            if (tableName is null)
            {
                throw new ArgumentNullException(nameof(tableName));
            }

            var columnNames      = new List <string>();
            var valueExpressions = new List <string>();

            var pkField = fields.PrimaryKeyField;

            if (pkField != null &&
                (pkField.PersistanceFlags & PersistanceFlags.OnInsert) == PersistanceFlags.OnInsert &&
                keyValue == null)
            {
                keyValue = pkField.GetFieldValueOrDefault(data);
            }

            if (persistKeyvalue && keyValue != null)
            {
                if (pkField is null)
                {
                    throw new InvalidOperationException($"keyValue provided but [{data.GetType().Name}] has no property with a primary key attribute");
                }

                var param = MakeParameter(command, pkField, keyValue);
                command.Parameters.Add(param);

                columnNames.Add(pkField.Name);
                valueExpressions.Add(param.ParameterName);
            }

            foreach (var field in fields)
            {
                if ((field.PersistanceFlags & PersistanceFlags.OnInsert) == PersistanceFlags.OnInsert &&
                    field.IsKeyField == false)
                {
                    object value = field.GetFieldValueOrDefault(data);
                    if (value == null)
                    {
                        continue;
                    }

                    var param = MakeParameter(command, field, value);
                    command.Parameters.Add(param);

                    columnNames.Add(field.Name);
                    valueExpressions.Add(param.ParameterName);
                }
            }

            var builder = new SqlInsertCommand
            {
                TableName        = tableName,
                ConflictOption   = option,
                ColumnNames      = columnNames,
                ValueExpressions = valueExpressions
            };

            var keyFieldName = pkField?.Name ?? "RowID";

            command.CommandText = builder.ToString() + $"; SELECT {keyFieldName} FROM { tableName} WHERE RowID = last_insert_rowid();";
        }
Ejemplo n.º 10
0
        public void BuildUpdate(IDbCommand command,
                                object data,
                                string tableName,
                                IFieldInfoCollection fields,
                                string whereExpression  = null,
                                OnConflictOption option = OnConflictOption.Default,
                                object keyValue         = null,
                                object extraPrams       = null)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (fields is null)
            {
                throw new ArgumentNullException(nameof(fields));
            }
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("message", nameof(tableName));
            }

            var pkField = fields.PrimaryKeyField ?? throw new InvalidOperationException($"Primary Key field required for Update command");

            if (keyValue is null)
            {
                keyValue = pkField.GetFieldValue(data);
            }
            if (keyValue is null)
            {
                throw new InvalidOperationException($"{pkField.Property.Name} should not be null");
            }



            var columnNames       = new List <string>();
            var columnExpressions = new List <string>();

            foreach (var field in fields)
            {
                object value = field.GetFieldValueOrDefault(data) ?? DBNull.Value;
                var    param = MakeParameter(command, field, value);
                command.Parameters.Add(param);

                if ((field.PersistanceFlags & PersistanceFlags.OnUpdate) == PersistanceFlags.OnUpdate)
                {
                    columnNames.Add(field.Name);
                    columnExpressions.Add(param.ParameterName);
                }
            }

            if (extraPrams != null)
            {
                command.AddParams(extraPrams);
            }

            whereExpression = whereExpression ?? pkField.Name + " = " + GetParameterName(pkField);
            var where       = new WhereClause(whereExpression);
            var expression = new SqlUpdateCommand()
            {
                TableName        = tableName,
                ColumnNames      = columnNames,
                ValueExpressions = columnExpressions,
                ConflictOption   = option,
                Where            = where
            };

            command.CommandText = expression.ToString();
        }