Ejemplo n.º 1
0
        public IDecorationCommandImplementation Get(IBatchCommand command, IDbConnection connection)
        {
            if (command is IBatchRunCommand)
                return new BatchRunCommand(command as IBatchRunCommand, connection as SqlConnection);

            throw new ArgumentException();
        }
Ejemplo n.º 2
0
        public IDecorationCommandImplementation Get(IBatchCommand command)
        {
            var connectionFactory = new ConnectionFactory();
            var connection        = connectionFactory.Get(command.ConnectionString);

            var directory = AssemblyDirectory;
            var filename  = string.Format("NBi.Core.{0}.dll", command.Version);
            var filepath  = string.Format("{0}\\{1}", directory, filename);

            if (!File.Exists(filepath))
            {
                throw new InvalidOperationException(string.Format("Can't find the dll for version '{0}' in '{1}'. NBi was expecting to find a dll named '{2}'.", "2014", directory, filename));
            }

            var assembly = Assembly.LoadFrom(filepath);
            var types    = assembly.GetTypes()
                           .Where(m => m.IsClass && m.GetInterface("IBatchRunnerFatory") != null);

            if (types.Count() == 0)
            {
                throw new InvalidOperationException(string.Format("Can't find a class implementing 'IBatchRunnerFatory' in '{0}'.", assembly.FullName));
            }
            if (types.Count() > 1)
            {
                throw new InvalidOperationException(string.Format("Found more than one class implementing 'IBatchRunnerFatory' in '{0}'.", assembly.FullName));
            }

            var batchFactory = Activator.CreateInstance(types.ElementAt(0)) as IBatchRunnerFatory;

            var batchRunner = batchFactory.Get(command, connection);

            return(batchRunner);
        }
Ejemplo n.º 3
0
        protected void Cycle(IBatchCommand <T> batchCommand)
        {
            try
            {
                batchCommand.PreRun();

                bool continueProcessing;
                do
                {
                    continueProcessing = false;
                    foreach (var queueConfig in this.queuesConfiguration)
                    {
                        var messages = queueConfig.Queue.GetMessages(queueConfig.BatchSize);
                        GenericQueueHandler <T> .ProcessMessages(queueConfig.Queue, messages, batchCommand.Run);

                        continueProcessing |= messages.Count() >= queueConfig.BatchSize;
                    }
                }while (continueProcessing);

                batchCommand.PostRun();

                this.Sleep(this.interval);
            }
            catch (TimeoutException ex)
            {
                TraceHelper.TraceWarning(ex.TraceInformation());
            }
        }
Ejemplo n.º 4
0
        protected void Cycle(IBatchCommand <T> batchCommand)
        {
            try
            {
                batchCommand.PreRun();

                int  batches = 0;
                bool continueProcessing;
                do
                {
                    continueProcessing = false;
                    foreach (var queueConfig in this.queuesConfiguration)
                    {
                        var messages = queueConfig.Queue.GetMessages(queueConfig.BatchSize);
                        GenericQueueHandler <T> .ProcessMessages(queueConfig.Queue, messages, batchCommand.Run);

                        continueProcessing |= messages.Count() >= queueConfig.BatchSize;
                    }
                    batches++;
                }while (continueProcessing && batches < this.maxBatchesPerCycle);

                batchCommand.PostRun();

                this.Sleep(this.interval);
            }
            catch (TimeoutException ex)
            {
                TraceHelper.TraceWarning(ex.TraceInformation());
            }
            catch (Exception ex)
            {
                // no exception should get here - we don't want the handler to stop (we log it as ERROR)
                TraceHelper.TraceError(ex.TraceInformation());
            }
        }
Ejemplo n.º 5
0
        public IDecorationCommandImplementation Get(IBatchCommand command, IDbConnection connection)
        {
            if (command is IBatchRunCommand)
            {
                return(new BatchRunCommand(command as IBatchRunCommand, connection as SqlConnection));
            }

            throw new ArgumentException();
        }
        public void AddParameters(IBatchCommand batch)
        {
            // Parameterized queries won't work here: https://github.com/npgsql/npgsql/issues/331
            // In order to parameterize, sproc would need to be created            
            _sql = $@"DO $$ BEGIN IF
  (SELECT max(events.version)
   FROM {tableName} AS events
   WHERE events.stream_id = '{stream}') <> {expectedVersion} THEN 
RAISE EXCEPTION '{EventContracts.UnexpectedMaxEventIdForStream.Value}'; END IF; END; $$;";
        }
Ejemplo n.º 7
0
 public virtual void Do(IBatchCommand <T> batchCommand)
 {
     Task.Factory.StartNew(async() =>
     {
         while (true)
         {
             await this.CycleAsync(batchCommand);
         }
     }, TaskCreationOptions.LongRunning);
 }
Ejemplo n.º 8
0
        public void AddParameters(IBatchCommand batch)
        {
            // Parameterized queries won't work here: https://github.com/npgsql/npgsql/issues/331
            // In order to parameterize, sproc would need to be created
            _sql = $@"DO $$ BEGIN IF
  (SELECT max(events.version)
   FROM {TableName} AS events
   WHERE events.stream_id = '{Stream}') <> {ExpectedVersion} THEN 
RAISE EXCEPTION '{EventContracts.UnexpectedMaxEventIdForStream.Value}'; END IF; END; $$;";
        }
 public virtual void Do(IBatchCommand <T> batchCommand, CancellationToken cancellationToken)
 {
     Task.Factory.StartNew(async() =>
     {
         while (true)
         {
             cancellationToken.ThrowIfCancellationRequested();
             await this.CycleAsync(batchCommand);
         }
     }, TaskCreationOptions.LongRunning);
 }
Ejemplo n.º 10
0
        public void AddParameters(IBatchCommand batch)
        {
            var patchJson    = batch.Serializer.ToCleanJson(_patch);
            var patchParam   = batch.AddParameter(patchJson, NpgsqlDbType.Jsonb);
            var versionParam = batch.AddParameter(CombGuidIdGeneration.NewGuid(), NpgsqlDbType.Uuid);

            var where = _fragment.ToSql(batch.Command);

            _sql = $@"
update {_document.Table.QualifiedName} as d 
set data = {_transform.Function.QualifiedName}(data, :{patchParam.ParameterName}), {DocumentMapping.LastModifiedColumn} = (now() at time zone 'utc'), {DocumentMapping.VersionColumn} = :{versionParam.ParameterName}
where {where}";
        }
Ejemplo n.º 11
0
        public void AddParameters(IBatchCommand batch)
        {
            var patchJson = batch.Serializer.ToCleanJson(_patch);
            var patchParam = batch.AddParameter(patchJson, NpgsqlDbType.Jsonb);
            var versionParam = batch.AddParameter(CombGuidIdGeneration.NewGuid(), NpgsqlDbType.Uuid);

            var where = _fragment.ToSql(batch.Command);

            _sql = $@"
update {_document.Table.QualifiedName} as d 
set data = {_transform.Function.QualifiedName}(data, :{patchParam.ParameterName}), {DocumentMapping.LastModifiedColumn} = (now() at time zone 'utc'), {DocumentMapping.VersionColumn} = :{versionParam.ParameterName}
where {where}";

        }
Ejemplo n.º 12
0
        public void LargeBatchInsert()
        {
            Guid TempGuid = Guid.NewGuid();

            Utilities.SQL.MicroORM.Command TempCommand = new Utilities.SQL.MicroORM.Command("insert into TestTable(StringValue1,StringValue2,BigIntValue,BitValue,DecimalValue,FloatValue,DateTimeValue,GUIDValue) VALUES (@0,@1,@2,@3,@4,@5,@6,@7)", CommandType.Text, "@", "Test String", "Test String", 12345L, true, 1234.5678m, 12345.6534f, new DateTime(1999, 12, 31), TempGuid);
            using (Utilities.SQL.SQLHelper Helper = new Utilities.SQL.SQLHelper(TempCommand, "Data Source=localhost;Initial Catalog=TestDatabase;Integrated Security=SSPI;Pooling=false"))
            {
                IBatchCommand Batch = Helper.Batch();
                for (int x = 0; x < 1000; ++x)
                {
                    Batch.AddCommands(new Utilities.SQL.MicroORM.Command("insert into TestTable(StringValue1,StringValue2,BigIntValue,BitValue,DecimalValue,FloatValue,DateTimeValue,GUIDValue) VALUES (@0,@1,@2,@3,@4,@5,@6,@7)", CommandType.Text, "@", "Test String", "Test String", 12345L, true, 1234.5678m, 12345.6534f, new DateTime(1999, 12, 31), TempGuid));
                }
                Assert.Throws <SqlException>(() => Helper.ExecuteNonQuery());
            }
        }
Ejemplo n.º 13
0
        public IDecorationCommandImplementation Get(IBatchCommand command)
        {
            var          connectionFactory = new ConnectionFactory();
            var          connection        = connectionFactory.Get(command.ConnectionString);
            IBatchFatory factory           = null;

            if (connection is SqlConnection)
            {
                factory = new SqlServerBatchFactory();
            }

            if (factory != null)
            {
                return(factory.Get(command, connection));
            }

            throw new ArgumentException();
        }
Ejemplo n.º 14
0
        private void CommandExecute(IBatchCommand batchCommand, T item)
        {
            if (batchOperation == null)
            {
                batchOperation = new TableBatchOperation();
            }

            U azureEntity = converter.Write(item);

            if (partitionKeyChanged(azureEntity.PartitionKey))
            {
                ExecuteBatchAndCreateNew();
            }

            batchCommand.Execute(batchOperation, azureEntity);

            if (batchOperation.Count == 100)
            {
                ExecuteBatchAndCreateNew();
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Creates a database out of the structure it is given
        /// </summary>
        /// <param name="Database">Database structure</param>
        /// <param name="ConnectionString">The connection string to the database's location</param>
        public static void CreateDatabase(Database Database, string ConnectionString)
        {
            string Command = BuildCommands(Database);

            string[] Splitter = { "\n" };
            string[] Commands = Command.Split(Splitter, StringSplitOptions.RemoveEmptyEntries);
            ConnectionString = Regex.Replace(ConnectionString, "Pooling=(.*?;)", "", RegexOptions.IgnoreCase) + ";Pooling=false;";
            string DatabaseConnectionString = Regex.Replace(ConnectionString, "Initial Catalog=(.*?;)", "", RegexOptions.IgnoreCase);

            using (SQLHelper Helper = new SQLHelper(Commands[0], CommandType.Text, DatabaseConnectionString))
            {
                Helper.ExecuteNonQuery();
            }
            using (SQLHelper Helper = new SQLHelper("", CommandType.Text, ConnectionString))
            {
                IBatchCommand Batcher = Helper.Batch();
                for (int x = 1; x < Commands.Length; ++x)
                {
                    if (Commands[x].Contains("CREATE TRIGGER") || Commands[x].Contains("CREATE FUNCTION"))
                    {
                        if (Batcher.CommandCount > 0)
                        {
                            Helper.ExecuteNonQuery();
                            Batcher = Helper.Batch();
                        }
                        Batcher.AddCommand(Commands[x], CommandType.Text);
                        if (x < Commands.Length - 1)
                        {
                            Helper.ExecuteNonQuery();
                            Batcher = Helper.Batch();
                        }
                    }
                    else
                    {
                        Batcher.AddCommand(Commands[x], CommandType.Text);
                    }
                }
                Helper.ExecuteNonQuery();
            }
        }
Ejemplo n.º 16
0
 void IStorageOperation.AddParameters(IBatchCommand batch)
 {
     _param = batch.AddParameter(Id, _storage.IdType);
 }
Ejemplo n.º 17
0
 public void AddParameters(IBatchCommand batch)
 {
     _nameArg = batch.AddParameter(_key, NpgsqlDbType.Varchar);
     _numberArg = batch.AddParameter(_number, NpgsqlDbType.Bigint);
 }
Ejemplo n.º 18
0
 public override void Do(IBatchCommand <MessageStub> batchCommand)
 {
     this.Cycle(batchCommand);
 }
Ejemplo n.º 19
0
 public void AddParameters(IBatchCommand batch)
 {
 }
Ejemplo n.º 20
0
        void IStorageOperation.AddParameters(IBatchCommand batch)
        {
            var whereClause = _where.ToSql(batch.Command);

            Sql = Sql.Replace("?", whereClause);
        }
Ejemplo n.º 21
0
 void IStorageOperation.AddParameters(IBatchCommand batch)
 {
     _param = batch.AddParameter(Id, _storage.IdType);
 }
Ejemplo n.º 22
0
 public override void Do(IBatchCommand <MessageStub> batchCommand, CancellationToken cancellationToken)
 {
     this.CycleAsync(batchCommand).Wait();
 }
Ejemplo n.º 23
0
        void IStorageOperation.AddParameters(IBatchCommand batch)
        {
            var whereClause = _where.ToSql(batch.Command);

            Sql = Sql.Replace("?", whereClause);
        }
Ejemplo n.º 24
0
 public void AddParameters(IBatchCommand batch)
 {
     // nothing here
 }
Ejemplo n.º 25
0
 public override void Do(IBatchCommand <MessageStub> batchCommand)
 {
     this.CycleAsync(batchCommand).Wait();
 }