public TableWithIncKeyUpdateLastRowReplicationStrategy(string sourceConnectionString, string targetConnectionString, ILog log, ISqlCommandFactory sqlCommandFactory)
 {
     _sourceConnectionString = sourceConnectionString;
     _targetConnectionString = targetConnectionString;
     _log = log;
     _sqlCommandFactory = sqlCommandFactory;
 }
 public TableSnapshotReplicationStrategy(string sourceConnectionString, string targetConnectionString, ILog log, IForeignKeysDropCreateScriptProvider foreignKeysDropCreateScriptProvider, ISqlCommandFactory sqlCommandFactory)
 {
     _sourceConnectionString = sourceConnectionString;
     _targetConnectionString = targetConnectionString;
     _log = log;
     _foreignKeysDropCreateScriptProvider = foreignKeysDropCreateScriptProvider;
     _sqlCommandFactory = sqlCommandFactory;
 }
 private static void InsertRow(Table sourceTable, Table targetTable, SqlDataReader reader, SqlConnection targetDatabaseConnection, SqlTransaction transaction, string identityInsertSetup, ISqlCommandFactory commandFactory)
 {
     SqlCommand insertCommand = commandFactory.CreateSqlCommand("", targetDatabaseConnection, transaction);
     string insertCommandText = $@"USE [{targetTable.Database}]
                                                               {identityInsertSetup}
                                                               INSERT INTO {targetTable.Schema}.{targetTable.Name} ( ";
     for (int i = 0; i < sourceTable.Columns.Length; i++)
         insertCommandText += $"[{sourceTable.Columns[i].Name}] {((i < sourceTable.Columns.Length - 1) ? "," : ") VALUES (")}";
     for (int i = 0; i < sourceTable.Columns.Length; i++)
     {
         string paramName = $"prm{i}";
         insertCommandText += $"@{paramName} {((i < sourceTable.Columns.Length - 1) ? "," : ")")}";
         insertCommand.Parameters.AddWithValue(paramName, reader[sourceTable.Columns[i].Name]);
     }
     insertCommand.CommandText = insertCommandText;
     if (insertCommand.ExecuteNonQuery() != 1)
         throw new ReplicationException("Replication error: Failed to insert row into target database");
 }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new Relational Store
        /// </summary>
        /// <param name="connectionString">Allows the connection string to be set after the store is built (but before it is used)</param>
        /// <param name="applicationName">Name of the application in the SQL string</param>
        /// <param name="sqlCommandFactory"></param>
        /// <param name="mappings"></param>
        /// <param name="jsonSettings"></param>
        /// <param name="relatedDocumentStore">If you don't have releated documents use the EmptyRelatedDocumentStore</param>
        /// <param name="keyBlockSize">Block size for the KeyAllocator</param>
        public RelationalStore(
            Func <string> connectionString,
            string applicationName,
            ISqlCommandFactory sqlCommandFactory,
            RelationalMappings mappings,
            JsonSerializerSettings jsonSettings,
            IRelatedDocumentStore relatedDocumentStore,
            int keyBlockSize = 20)
        {
            this.registry = new Lazy <RelationalTransactionRegistry>(
                () => SetConnectionStringOptions(connectionString(), applicationName)
                );
            this.sqlCommandFactory = sqlCommandFactory;
            this.mappings          = mappings;
            keyAllocator           = new KeyAllocator(this, keyBlockSize);

            this.jsonSettings         = jsonSettings;
            this.relatedDocumentStore = relatedDocumentStore;
        }
Exemplo n.º 5
0
 public RelationalStore(
     string connectionString,
     string applicationName,
     ISqlCommandFactory sqlCommandFactory,
     RelationalMappings mappings,
     JsonSerializerSettings jsonSettings,
     IRelatedDocumentStore relatedDocumentStore,
     int keyBlockSize = 20)
     : this(
         () => connectionString,
         applicationName,
         sqlCommandFactory,
         mappings,
         jsonSettings,
         relatedDocumentStore,
         keyBlockSize
         )
 {
 }
Exemplo n.º 6
0
        public RelationalTransaction(
            RelationalTransactionRegistry registry,
            RetriableOperation retriableOperation,
            IsolationLevel isolationLevel,
            ISqlCommandFactory sqlCommandFactory,
            JsonSerializerSettings jsonSerializerSettings,
            RelationalMappings mappings,
            IKeyAllocator keyAllocator,
            IRelatedDocumentStore relatedDocumentStore,
            string name = null,
            ObjectInitialisationOptions objectInitialisationOptions = ObjectInitialisationOptions.None
            )
        {
            this.registry               = registry;
            this.retriableOperation     = retriableOperation;
            this.sqlCommandFactory      = sqlCommandFactory;
            this.jsonSerializerSettings = jsonSerializerSettings;
            this.mappings               = mappings;
            this.keyAllocator           = keyAllocator;
            this.relatedDocumentStore   = relatedDocumentStore;
            this.name = name ?? Thread.CurrentThread.Name;
            this.objectInitialisationOptions = objectInitialisationOptions;
            if (string.IsNullOrEmpty(name))
            {
                this.name = "<unknown>";
            }

            dataModificationQueryBuilder = new DataModificationQueryBuilder(mappings, jsonSerializerSettings);

            try
            {
                registry.Add(this);
                connection = new SqlConnection(registry.ConnectionString);
                connection.OpenWithRetry();
                transaction = connection.BeginTransaction(isolationLevel);
            }
            catch
            {
                Dispose();
                throw;
            }
        }
Exemplo n.º 7
0
 public RelationalStore(
     string connectionString,
     string applicationName,
     ISqlCommandFactory sqlCommandFactory,
     RelationalMappings mappings,
     JsonSerializerSettings jsonSettings,
     IRelatedDocumentStore relatedDocumentStore,
     int keyBlockSize = 20,
     ObjectInitialisationOptions objectInitialisationOptions = ObjectInitialisationOptions.None)
     : this(
         () => connectionString,
         applicationName,
         sqlCommandFactory,
         mappings,
         jsonSettings,
         relatedDocumentStore,
         keyBlockSize,
         objectInitialisationOptions
         )
 {
 }
Exemplo n.º 8
0
 public SqlComparator(IDatabaseConnectionProvider connectionProviderProvider, ISqlCommandFactory commandFactory)
 {
     _commandFactory     = commandFactory;
     _connectionProvider = connectionProviderProvider;
 }
Exemplo n.º 9
0
 public UserRepository(ISession session, ISqlCommandFactory sqlCommandFactory)
 {
     _session = session;
     _sqlCommandFactory = sqlCommandFactory;
 }
 public TableValuesLoader(string connectionString, ILog log, ISqlCommandFactory  sqlCommandFactory)
 {
     _connectionString = connectionString;
     _log = log;
     _sqlCommandFactory = sqlCommandFactory;
 }
Exemplo n.º 11
0
        private SqlClientCrudAdapter(ISqlCommandFactory factory, string tableName, string keyName, bool isIdentity)
            : base
            (
                /* create */
                (e) =>
        {
            var t          = typeof(T);
            var parameters = (isIdentity ? _fieldListWithoutKey : _fieldList)
                             .Select(f => new { Key = f, Value = t.GetProperty(f).GetValue(e) }).ToDictionary(p => p.Key, p => p.Value);
            var ret = Execute(_insertCommand, parameters, isIdentity);
            if (isIdentity)
            {
                var prop = t.GetProperty(keyName);
                prop.SetValue(e, Convert.ChangeType(ret, prop.PropertyType));
            }
        },

                /* update */
                (e) =>
        {
            var t          = typeof(T);
            var parameters = _fieldList.Select(f => new { Key = f, Value = t.GetProperty(f).GetValue(e) }).ToDictionary(p => p.Key, p => p.Value);
            Execute(_updateCommand, parameters);
        },

                /* delete */
                (e) =>
        {
            var parameters = new Dictionary <string, object>()
            {
                { keyName, typeof(T).GetProperty(keyName).GetValue(e) }
            };
            Execute(_deleteCommand, parameters);
        },

                /* read */
                (predicate) =>
        {
            var wherePart = _whereBuilder.ToSql(predicate);
            var sql       = string.Format("{0} WHERE {1}", _selectCommand, wherePart.Sql);
            return(ExecuteQuery(sql, wherePart.Parameters));
        },

                /* read */
                (sql, parameters) =>
        {
            var paramInDictionary = parameters.Select((Value, i) => new { Key = i.ToString(), Value }).ToDictionary(p => p.Key, p => p.Value);
            return(ExecuteQuery(sql, paramInDictionary));
        }
            )
        {
            if (_fieldList == null)
            {
                _fieldList           = typeof(T).GetProperties().Where(p => p.PropertyType.IsSealed && p.GetAccessors().Any(a => !(a.IsVirtual && !a.IsFinal) && a.ReturnType == typeof(void))).Select(p => p.Name).ToList();
                _fieldListWithoutKey = _fieldList.Where(f => !string.Equals(f, keyName, StringComparison.OrdinalIgnoreCase)).ToList();

                _insertCommand = string.Format("INSERT INTO [{0}] ({1}) VALUES ({2}){3}", tableName,
                                               string.Join(", ", _fieldListWithoutKey.Select(f => string.Format("[{0}]", f))),
                                               string.Join(", ", _fieldListWithoutKey.Select(f => string.Format("@{0}", f))),
                                               isIdentity ? "; SELECT CAST(SCOPE_IDENTITY() AS INT);" : "");

                _updateCommand = string.Format("UPDATE [{0}] SET {1} WHERE {2}", tableName,
                                               string.Join(", ", _fieldListWithoutKey.Select(f => string.Format("[{0}] = @{0}", f))),
                                               string.Format("[{0}] = @{0}", keyName));

                _deleteCommand = string.Format("DELETE [{0}] WHERE {1}", tableName,
                                               string.Format("[{0}] = @{0}", keyName));

                _selectCommand = string.Format("SELECT {1} FROM [{0}]", tableName,
                                               string.Join(", ", _fieldList.Select(f => string.Format("[{0}]", f))));

                _factory = factory;
            }
        }
Exemplo n.º 12
0
 public SqlClientCrudAdapter(ISqlCommandFactory factory, string tableName)
     : this(factory, tableName, GetPropertyKey())
 {
 }
Exemplo n.º 13
0
 public SqlClientCrudAdapter(ISqlCommandFactory factory, string tableName, Expression <Func <T, object> > key, bool isIdentity)
     : this(factory, tableName, GetPropertyKeyName(key), isIdentity)
 {
 }
Exemplo n.º 14
0
 public ProductSqlRepository(IFactory <IProduct> productFactory, ISqlDbInstance db, ISqlCommandFactory commandFactory)
 {
     this.productFactory = productFactory;
     this.db             = db;
     this.commandFactory = commandFactory;
 }
Exemplo n.º 15
0
 public SqlClientCrudAdapter(ISqlCommandFactory factory)
     : this(factory, GetTableName())
 {
 }
Exemplo n.º 16
0
 public ChaosSqlCommandFactory(ISqlCommandFactory wrappedFactory, double chaosFactor = 0.2)
 {
     this.wrappedFactory = wrappedFactory;
     this.chaosFactor    = chaosFactor;
 }
Exemplo n.º 17
0
 public OracleDataContext(string connectString, ISqlCommandFactory commandFactory)
 {
     _connection     = new OracleConnection(connectString);
     _commandFactory = commandFactory;
 }
 private static int ExecuteNonQuerySqlCommand(string sqlCommandText, SqlConnection targetDatabaseConnection, SqlTransaction transaction, ISqlCommandFactory commandFactory)
 {
     SqlCommand deleteForeignKeysConstraintsCmd = commandFactory.CreateSqlCommand(sqlCommandText, targetDatabaseConnection);
     deleteForeignKeysConstraintsCmd.Transaction = transaction;
     return deleteForeignKeysConstraintsCmd.ExecuteNonQuery();
 }
Exemplo n.º 19
0
 public DataTableContext(ISqlConnection sqlconnection, ISqlCommandFactory commandFactory, ISqlAdapterFactory adapterFactory)
 {
     _sqlconnection  = sqlconnection;
     _commandFactory = commandFactory;
     _adapterFactory = adapterFactory;
 }
 public TableSchemaAnalyzer(string connectionString, ILog log, ISqlCommandFactory sqlCommandFactory)
 {
     _connectionString = connectionString;
     _log = log;
     _sqlCommandFactory = sqlCommandFactory;
 }
Exemplo n.º 21
0
 public UserRepository(ISession session, ISqlCommandFactory sqlCommandFactory)
 {
     _session           = session;
     _sqlCommandFactory = sqlCommandFactory;
 }
 public ForeignKeysDropCreateScriptProvider(string connectionString, ISqlCommandFactory sqlCommandFactory)
 {
     _connectionString = connectionString;
     _sqlCommandFactory = sqlCommandFactory;
 }