コード例 #1
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);
                    }
                }
            }
        }
コード例 #2
0
        public static T ExecuteScalar <T>(this DbConnection connection, string commandText, object[] parameters = null,
                                          DbTransaction transaction = null, IExceptionProcessor exceptionProcessor = null)
        {
            var result = ExecuteScalar(connection, commandText, parameters, transaction, exceptionProcessor);

            return(ValueMapper.ProcessValue <T>(result));
        }
コード例 #3
0
 public QueryResult(DbConnection connection, DbCommand command, DbTransaction transaction = null, IExceptionProcessor exceptionProcessor = null)
 {
     Connection         = connection ?? throw new ArgumentNullException(nameof(connection));
     Command            = command ?? throw new ArgumentNullException(nameof(command));
     Transaction        = transaction;
     ExceptionProcessor = exceptionProcessor;
 }
コード例 #4
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;
                }
            }
        }
コード例 #5
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);
                    }
                }
            }
        }
コード例 #6
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();
            }
        }
コード例 #7
0
        public WebDiagnosticsInspector(IExceptionProcessor exceptionProcessor, IAgentBroker broker, IContextData<MessageContext> contextData, ILoggerFactory loggerFactory)
        {
            _exceptionProcessor = exceptionProcessor;
            _broker = broker;
            _contextData = contextData;
            _logger = loggerFactory.CreateLogger<WebDiagnosticsInspector>();

            _proxyAdapter = new ProxyAdapter();

            AspNetOnCreated();
            MvcOnCreated();
        }
コード例 #8
0
        public void CreateViews(DbConnection connection, DbTransaction transaction, IExceptionProcessor exceptionProcessor = null)
        {
            var views = VIEW_DEFINITIONS;

            foreach (var view in views)
            {
                Logger?.Log($"Creating View {view.ViewName}", LogCategory.DbBuilder, LogLevel.Info);

                var createViewCommand = view.CreateView;
                connection.ExecuteNonQuery(createViewCommand, transaction: transaction, exceptionProcessor: exceptionProcessor);
            }
        }
コード例 #9
0
        public WebDiagnosticsListener(IExceptionProcessor exceptionProcessor, IAgentBroker broker, IContextData <MessageContext> contextData, ILoggerFactory loggerFactory)
        {
            _exceptionProcessor = exceptionProcessor;
            _broker             = broker;
            _contextData        = contextData;
            _logger             = loggerFactory.CreateLogger <WebDiagnosticsListener>();

            _proxyAdapter = new ProxyAdapter();

            HostingOnCreated();
            MiddlewareOnCreated();
            MvcOnCreated();
        }
コード例 #10
0
        public static void Delete(this DbConnection connection, object data, string tableName = null,
                                  EntityDescription entityDescription = null, DbTransaction transaction              = null,
                                  ICommandBuilder commandBuilder      = null, IExceptionProcessor exceptionProcessor = null)
        {
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }

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

            Delete(connection, data, tableName, entityDescription.Fields, transaction, commandBuilder);
        }
コード例 #11
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();
                }
            }
        }
コード例 #12
0
 public DataProcessor
     (IUpdatingProgress progress,
     IConfiguration configuration,
     IUpdatedVesselFactory vesselUpdates,
     IStringParser stringParser,
     IDataAccessService dataService,
     IExceptionProcessor exceptionProcessor)
 {
     _progress           = progress;
     _configuration      = configuration;
     _vesselUpdates      = vesselUpdates;
     _stringParser       = stringParser;
     _dataService        = dataService;
     _exceptionProcessor = exceptionProcessor;
 }
コード例 #13
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);
                    }
                }
            }
        }
コード例 #14
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);
                    }
                }
            }
        }
コード例 #15
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);
                    }
                }
            }
        }
コード例 #16
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);
        }
コード例 #17
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));
        }
コード例 #18
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);
                }
            }
        }
コード例 #19
0
        public void CreateTables(DbConnection connection, DbTransaction transaction, IExceptionProcessor exceptionProcessor = null)
        {
            var tables = TABLE_DEFINITIONS;

            foreach (var table in tables)
            {
                Logger?.Log($"Creating Table {table.TableName}", LogCategory.DbBuilder, LogLevel.Info);

                var createCommand = table.CreateTable;
                connection.ExecuteNonQuery(createCommand, transaction: transaction, exceptionProcessor: exceptionProcessor);

                var createTombstone = table.CreateTombstoneTable;
                if (createTombstone != null)
                {
                    connection.ExecuteNonQuery(createTombstone, transaction: transaction, exceptionProcessor: exceptionProcessor);
                }

                var createIndexes = table.CreateIndexes;
                if (createIndexes != null)
                {
                    connection.ExecuteNonQuery(createIndexes, transaction: transaction, exceptionProcessor: exceptionProcessor);
                }

                var initialize = table.InitializeTable;
                if (initialize != null)
                {
                    connection.ExecuteNonQuery(initialize, transaction: transaction, exceptionProcessor: exceptionProcessor);
                }
            }

            var setDbVersion =
                $@"INSERT INTO Globals (Block, Key, Value) VALUES ('Database', 'Version', '{DATABASE_VERSION.ToString()}');
INSERT INTO Globals (Block, Key, Value) VALUES ('Database', 'CreateVersion', '{DATABASE_VERSION.ToString()}');";

            connection.ExecuteNonQuery(setDbVersion, transaction: transaction, exceptionProcessor: exceptionProcessor);
        }
コード例 #20
0
 public void BuildDatabase(DbConnection connection, DbTransaction transaction, IExceptionProcessor exceptionProcessor = null)
 {
     connection.ExecuteNonQuery(Schema.Schema.CREATE_TABLES, transaction: transaction, exceptionProcessor: exceptionProcessor);
     connection.ExecuteNonQuery(Schema.Schema.CREATE_TRIGGERS, transaction: transaction, exceptionProcessor: exceptionProcessor);
 }
コード例 #21
0
 public ChatService(IExceptionProcessor processor)
 {
     _processor = processor;
 }
コード例 #22
0
        public static void MigrateFromV2ToV3(DbConnection connection, string from = "v2", string deviceID = null, IExceptionProcessor exceptionProcessor = null, IEnumerable <IMigrator> migrators = null)
        {
            var to = "main";

            deviceID ??= GetDefaultDeviceID();

            var cruiseID = Guid.NewGuid().ToString();
            var saleID   = Guid.NewGuid().ToString();

            DbTransaction transaction = null;

            migrators ??= MIGRATORS;
            foreach (var migrator in migrators)
            {
                var command = migrator.MigrateToV3(to, from, cruiseID, saleID, deviceID);
                connection.ExecuteNonQuery(command, transaction: transaction, exceptionProcessor: exceptionProcessor);
            }
        }
コード例 #23
0
 public static IEnumerable <GenericEntity> QueryGeneric(this DbConnection connection, string commandText, DbTransaction transaction = null, IExceptionProcessor exceptionProcessor = null)
 {
     return(connection.QueryGeneric2(commandText, (object)null, transaction: transaction, exceptionProcessor: exceptionProcessor));
 }
コード例 #24
0
        public void CreateTriggers(DbConnection connection, DbTransaction transaction, IExceptionProcessor exceptionProcessor = null)
        {
            var tables = TABLE_DEFINITIONS;

            foreach (var table in tables)
            {
                Logger?.Log($"Creating Triggers For Table {table.TableName}", LogCategory.DbBuilder, LogLevel.Info);
                var triggers = table.CreateTriggers;
                if (triggers != null)
                {
                    foreach (var trigger in triggers)
                    {
                        connection.ExecuteNonQuery(trigger, transaction: transaction, exceptionProcessor: exceptionProcessor);
                    }
                }
            }
        }
コード例 #25
0
 public void BuildDatabase(DbConnection connection, DbTransaction transaction, IExceptionProcessor exceptionProcessor = null)
 {
     CreateTables(connection, transaction, exceptionProcessor);
     CreateTriggers(connection, transaction, exceptionProcessor);
     CreateViews(connection, transaction, exceptionProcessor);
 }
コード例 #26
0
 public void BuildDatabase(DbConnection connection, DbTransaction transaction, IExceptionProcessor exceptionProcessor = null)
 {
     connection.ExecuteNonQuery(CREATE_MULTIPROPTABLE, transaction: transaction, exceptionProcessor: exceptionProcessor);
     connection.ExecuteNonQuery(CREATE_AUTOINCREMENT_TABLE, transaction: transaction, exceptionProcessor: exceptionProcessor);
 }
コード例 #27
0
 public void AddProcessor(Type type, IExceptionProcessor processor)
 {
     _processors.Add(type, processor);
 }
コード例 #28
0
        public static IEnumerable <TResult> Query2 <TResult>(this DbConnection connection, string commandText, object paramaters = null, DbTransaction transaction = null, IExceptionProcessor exceptionProcessor = null) where TResult : new()
        {
            var discription = GlobalEntityDescriptionLookup.Instance.LookUpEntityByType(typeof(TResult));

            var command = connection.CreateCommand();

            command.CommandText = commandText;
            command.AddParams(paramaters);

            return(new QueryResult <TResult>(connection, command, transaction, exceptionProcessor));
        }
コード例 #29
0
 public QueryEnumerator(DbDataReader reader, IExceptionProcessor exceptionProcessor)
 {
     Reader      = reader ?? throw new ArgumentNullException(nameof(reader));
     Inflator    = InflatorLookup.Instance.GetEntityInflator(reader);
     Description = GlobalEntityDescriptionLookup.Instance.LookUpEntityByType(typeof(TResult));
 }
コード例 #30
0
        public static void MigrateFromV3ToV2(string cruiseID, DbConnection connection, string createdBy, string from = "v3", IExceptionProcessor exceptionProcessor = null, IEnumerable <IDownMigrator> migrators = null)
        {
            var to = "main";

            DbTransaction transaction = null;

            migrators ??= DOWN_MIGRATORS;
            foreach (var migrator in migrators)
            {
                var command = migrator.CreateCommand(to, from, cruiseID, createdBy);
                connection.ExecuteNonQuery(command, transaction: transaction, exceptionProcessor: exceptionProcessor);
            }
        }
コード例 #31
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);
                    }
                }
            }
        }