Пример #1
0
        public static long GetLastInsertRowID(this DbConnection connection, DbTransaction transaction = null,
                                              IExceptionProcessor exceptionProcessor = null)
        {
            if (connection is null)
            {
                throw new ArgumentNullException(nameof(connection));
            }

            var cmdText = "SELECT last_insert_rowid();";

            try
            {
                return(connection.ExecuteScalar <long>(cmdText, (object[])null, transaction));
            }
            catch (Exception e)
            {
                if (exceptionProcessor != null)
                {
                    throw exceptionProcessor.ProcessException(e, connection, cmdText, transaction);
                }
                else
                {
                    throw;
                }
            }
        }
Пример #2
0
        public static int ExecuteNonQuery2(this DbConnection connection, string commandText, object parameterData = null,
                                           DbTransaction transaction = null,
                                           IExceptionProcessor exceptionProcessor = null)
        {
            if (string.IsNullOrEmpty(commandText))
            {
                throw new ArgumentException("command can't be null or empty", "command");
            }
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            using (var command = connection.CreateCommand())
            {
                command.CommandText = commandText;
                command.AddParams(parameterData);

                try
                {
                    return(ConnectionExtentions.ExecuteNonQuery(connection, command, transaction));
                }
                catch (Exception e)
                {
                    if (exceptionProcessor is null)
                    {
                        throw;
                    }
                    else
                    {
                        throw exceptionProcessor.ProcessException(e, connection, commandText, transaction);
                    }
                }
            }
        }
Пример #3
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);
                    }
                }
            }
        }
Пример #4
0
        public static IEnumerable <GenericEntity> QueryGeneric2(this DbConnection connection, string commandText, object paramaters, DbTransaction transaction = null, IExceptionProcessor exceptionProcessor = null)
        {
            using (var command = connection.CreateCommand())
            {
                command.CommandText = commandText;
                command.AddParams(paramaters);

                using (var reader = connection.ExecuteReader(command, transaction: transaction, exceptionProcessor: exceptionProcessor))
                {
                    while (reader.Read())
                    {
                        var fieldCount = reader.FieldCount;
                        var fields     = new string[fieldCount];
                        for (int i = 0; i < fieldCount; i++)
                        {
                            fields[i] = reader.GetName(i);
                        }

                        var data = new GenericEntity(fieldCount);

                        try
                        {
                            foreach (var x in fields.Select((field, i) => new { field, i }))
                            {
                                try
                                {
                                    var value = reader.GetValue(x.i);
                                    data.Add(x.field, value);
                                }
                                catch (FormatException)
                                {
                                    // if a value is saved as a string
                                    // but the column type of different

                                    var value = reader.GetString(x.i);
                                    data.Add(x.field, value);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            if (exceptionProcessor is null)
                            {
                                throw;
                            }
                            else
                            {
                                throw exceptionProcessor.ProcessException(e, connection, commandText, transaction);
                            }
                        }

                        yield return(data);
                    }
                }
            }
        }
Пример #5
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();
            }
        }
Пример #6
0
        internal static void Delete(this DbConnection connection, object data, string tableName,
                                    IFieldInfoCollection fields, DbTransaction transaction = null,
                                    ICommandBuilder commandBuilder = null, IExceptionProcessor exceptionProcessor = null)
        {
            if (connection is null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (data is null)
            {
                throw new ArgumentNullException("data");
            }

            commandBuilder = commandBuilder ?? DefaultCommandBuilder;

            lock (data)
            {
                if (data is IPersistanceTracking)
                {
                    ((IPersistanceTracking)data).OnDeleting();
                }

                using (var command = connection.CreateCommand())
                {
                    commandBuilder.BuildDelete(command, data, tableName, fields);

                    try
                    {
                        ExecuteNonQuery(connection, command, transaction);
                    }
                    catch (Exception e)
                    {
                        if (exceptionProcessor is null)
                        {
                            throw;
                        }
                        else
                        {
                            throw exceptionProcessor.ProcessException(e, connection, command.CommandText, transaction);
                        }
                    }
                }

                if (data is IPersistanceTracking)
                {
                    ((IPersistanceTracking)data).OnDeleted();
                }
            }
        }
Пример #7
0
        public static object ExecuteScalar2(this DbConnection connection, string commandText,
                                            object parameterData = null, DbTransaction transaction = null,
                                            IExceptionProcessor exceptionProcessor = null)
        {
            if (connection is null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (string.IsNullOrEmpty(commandText))
            {
                throw new ArgumentException("commandText can not be null or empty", nameof(commandText));
            }

            using (var command = connection.CreateCommand())
            {
                command.CommandText = commandText;
                command.AddParams(parameterData);

                if (transaction != null)
                {
                    command.Transaction = transaction;
                }

                try
                {
                    return(command.ExecuteScalar());
                }
                catch (Exception e)
                {
                    if (exceptionProcessor is null)
                    {
                        throw;
                    }
                    else
                    {
                        throw exceptionProcessor.ProcessException(e, connection, commandText, transaction);
                    }
                }
            }
        }
Пример #8
0
        public static object ExecuteScalar(this DbConnection connection, string commandText, object[] parameters = null,
                                           DbTransaction transaction = null,
                                           IExceptionProcessor exceptionProcessor = null)
        {
            if (connection is null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (commandText is null)
            {
                throw new ArgumentNullException(nameof(commandText));
            }

            using (var command = connection.CreateCommand())
            {
                command.CommandText = commandText;
                command.SetParams(parameters);

                if (transaction != null)
                {
                    command.Transaction = transaction;
                }

                try
                {
                    return(command.ExecuteScalar());
                }
                catch (Exception e)
                {
                    if (exceptionProcessor is null)
                    {
                        throw;
                    }
                    else
                    {
                        throw exceptionProcessor.ProcessException(e, connection, commandText, transaction);
                    }
                }
            }
        }
Пример #9
0
        public static IEnumerable <TResult> QueryScalar2 <TResult>(this DbConnection connection, string commandText,
                                                                   object paramaters         = null,
                                                                   DbTransaction transaction = null,
                                                                   IExceptionProcessor exceptionProcessor = null)
        {
            var targetType = typeof(TResult);

            using (var command = connection.CreateCommand())
            {
                command.CommandText = commandText;
                command.AddParams(paramaters);

                using (var reader = connection.ExecuteReader(command, transaction: transaction, exceptionProcessor: exceptionProcessor))
                {
                    while (reader.Read())
                    {
                        TResult value = default(TResult);
                        try
                        {
                            var dbValue = reader.GetValue(0);
                            value = (TResult)ValueMapper.ProcessValue(targetType, dbValue);
                        }
                        catch (Exception e)
                        {
                            if (exceptionProcessor != null)
                            {
                                throw exceptionProcessor.ProcessException(e, connection, commandText, transaction);
                            }
                            else
                            {
                                throw;
                            }
                        }

                        yield return(value);
                    }
                }
            }
        }
Пример #10
0
        public static DbDataReader ExecuteReader(this DbConnection connection, DbCommand command,
                                                 DbTransaction transaction = null,
                                                 IExceptionProcessor exceptionProcessor = null)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            command.Connection = connection;
            if (transaction != null)
            {
                command.Transaction = transaction;
            }

            Logger.LogCommand(command);

            try
            {
                return(command.ExecuteReader());
            }
            catch (Exception e)
            {
                if (exceptionProcessor is null)
                {
                    throw;
                }
                else
                {
                    throw exceptionProcessor.ProcessException(e, connection, command.CommandText, transaction);
                }
            }
        }