Esempio n. 1
0
 private void InitializeSqlStatements(IHiLoConfiguration config)
 {
     _sqlStatementToGetLatestNextHiValue = PrepareSqlStatement("SELECT {1} FROM {0} WHERE {2} = {3}", config);
     _sqlStatementToUpdateNextHiValue    = PrepareSqlStatement("UPDATE {0} SET {1} = {1} + 1 WHERE {2} = {3}", config);
     _sqlStatementToCreateRepository     = PrepareSqlStatement("CREATE TABLE IF NOT EXISTS `{0}` ( `{2}` varchar(100) NOT NULL, `{1}` BIGINT NOT NULL, PRIMARY KEY (`{2}`));", config);
     _sqlStatementToInitializeEntity     = PrepareSqlStatement("INSERT IGNORE INTO `{0}` SET {2} = {3}, {1} = 1;", config);
 }
Esempio n. 2
0
 private void InitializeSqlStatements(IHiLoConfiguration config)
 {
     _sqlStatementToSelectAndUpdateNextHiValue = PrepareSqlStatement("SELECT {1} FROM {0} WHERE {2} = {3};UPDATE {0} SET {1} = {1} + 1 WHERE {2} = {3};", config);
     _sqlStatementToCreateRepository           = PrepareSqlStatement(SQLSERVERCREATESTRUCTURE, config);
     _sqlStatementToInitializeEntity           = PrepareSqlStatement(SQLSERVERINITIALIZEENTITY, config);
     _sqlStatementToCheckSelectPermission      = PrepareSqlStatement("SELECT * FROM [dbo].[{0}]", config);
 }
Esempio n. 3
0
        //  internal Func<string, DbProviderFactory> DbFactoryCreator { private get; set; } // for testability

        public AgnosticHiLoRepository(IDatabase db, string entityName, IHiLoConfiguration config)
        {
            database    = db;
            _entityName = entityName;
            _config     = config;
            //DbFactoryCreator = (providerName) => DbProviderFactories.GetFactory(providerName);
        }
        private void InitializeSqlStatements(IHiLoConfiguration config)
        {
            _sqlStatementToSelectAndUpdateNextHiValue = PrepareSqlStatement("SELECT {1} FROM {0} WHERE {2} = {3};UPDATE {0} SET {1} = {1} + 1 WHERE {2} = {3};", config);
            _sqlStatementToCreateRepository           = PrepareSqlStatement(
                @"IF NOT EXISTS(SELECT 1 FROM SYS.OBJECTS WHERE NAME = '{0}' AND TYPE = 'U')
BEGIN
    CREATE TABLE [dbo].[{0}](
    [{2}] [nvarchar](100) NOT NULL,
    [{1}] [bigint] NOT NULL,
        CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED 
        (
            [{2}] ASC
        )
    );
    SELECT 1;
END
ELSE
    SELECT 0;", config);
            _sqlStatementToInitializeEntity = PrepareSqlStatement(
                @"SET NOCOUNT ON;
IF NOT EXISTS(SELECT 1 FROM {0} WHERE {2} = {3})
BEGIN
    INSERT INTO {0} VALUES ({3}, 1);
END", config);
        }
Esempio n. 5
0
 public SqlServerSequenceHiLoRepository(IHiLoConfiguration config)
     : base(config, Microsoft.Data.SqlClient.SqlClientFactory.Instance)
 {
     if (!string.IsNullOrEmpty(config.ObjectPrefix))
     {
         _objectPrefix = config.ObjectPrefix;
     }
 }
Esempio n. 6
0
 private static IHiLoRepository GetSqlServerRepository(IHiLoConfiguration config)
 {
     if (config.StorageType == Common.Config.HiLoStorageType.Sequence)
     {
         return(new SqlServerSequenceHiLoRepository(config));
     }
     return(new SqlServerHiLoRepository(config));
 }
Esempio n. 7
0
        private void InitializeSqlStatements(IHiLoConfiguration config)
        {
            _sqlStatementToGetLatestNextHiValue = PrepareSqlStatement("SELECT {1} FROM {0} WHERE {2} = {3}", config);
            _sqlStatementToUpdateNextHiValue    = PrepareSqlStatement("UPDATE {0} SET {1} = {1} + 1 WHERE {2} = {3}", config);

            _sqlStatementToCreateRepository = PrepareSqlStatement("CREATE TABLE IF NOT EXISTS {0} ( {2} VARCHAR(100) NOT NULL PRIMARY KEY, {1} BIGINT NOT NULL );", config);
            _sqlStatementToInitializeEntity = PrepareSqlStatement("INSERT INTO {0}({2}, {1}) SELECT {3}, 1 FROM {0} WHERE NOT EXISTS(SELECT 1 FROM {0} WHERE {2} = {3});", config);
        }
        // private Regex _entityNameValidator = new Regex(@"^[a-zA-Z]+[a-zA-Z0-9_]*$");

        public SqlServerSequenceHiLoRepository(IDatabase db, string entityName, IHiLoConfiguration config)
            : base(db, entityName, config)
        {
            if (!string.IsNullOrEmpty(config.ObjectPrefix))
            {
                _objectPrefix = config.ObjectPrefix;
            }
        }
Esempio n. 9
0
        public IHiLoRepository GetRepository(string entityName, IHiLoConfiguration config)
        {
            IHiLoRepository repository = null;

            repository = _factoryFunction(entityName, config);
            repository.PrepareRepository();
            return(repository);
        }
 private void InitializeSqlStatements(IHiLoConfiguration config)
 {
     _sqlStatementToUpdateNextHiValue            = PrepareSqlStatement("UPDATE {0} SET {1} = {1} + 1 WHERE {2} = {3}", config);
     _sqlStatementToGetLatestNextHiValue         = PrepareSqlStatement("SELECT {1} FROM {0} WHERE {2} = {3}", config);
     _sqlStatementToCheckIfNHilosTableExists     = PrepareSqlStatement("SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{0}' AND TABLE_TYPE = 'TABLE'", config);
     _sqlStatementToCreateNHiloTable             = PrepareSqlStatement("CREATE TABLE {0} ({2} NVARCHAR(100) PRIMARY KEY NOT NULL, {1} BIGINT NOT NULL)", config);
     _sqlStatementToCheckIfEntityExists          = PrepareSqlStatement("SELECT 1 FROM {0} WHERE {2} = {3}", config);
     _sqlStatementToInsertNewEntityToNHilosTable = PrepareSqlStatement("INSERT INTO {0} ({2}, {1}) VALUES ({3}, 1)", config);
 }
Esempio n. 11
0
        //private Regex _entityNameValidator = new Regex(@"^[a-zA-Z]+[a-zA-Z0-9]*$");

        public HiLoGeneratorFactory(IDatabase database, Action <IHiLoConfiguration> config)
        {
            _config = new HiLoConfiguration();
            if (config != null)
            {
                config(_config);
            }
            _repositoryFactory = new HiLoRepositoryFactory(database);
        }
Esempio n. 12
0
 private HiLoGeneratorFactory(IHiLoRepositoryFactory repositoryFactory, IConfiguration configuration)
 {
     if (configuration == null)
     {
         var builder = new ConfigurationBuilder()
                       .Add(new NETConfigConfigurationProvider())
                       .AddEnvironmentVariables();
         configuration = builder.Build();
     }
     _config            = new HiLoConfigurationBuilder(new ConfigurationManagerWrapper(configuration)).Build();
     _repositoryFactory = repositoryFactory ?? new HiLoRepositoryFactory();
 }
Esempio n. 13
0
        public IHiLoRepository GetRepository(string entityName, IHiLoConfiguration config)
        {
            string provider = config.ProviderName;

            if (string.IsNullOrWhiteSpace(provider))
            {
                throw new NHiLoException(ErrorCodes.NoProviderName);
            }
            if (!_factoryFunctions.ContainsKey(provider))
            {
                throw new NHiLoException(ErrorCodes.ProviderNotImplemented).WithInfo("Provider Name", provider);
            }
            var repository = new ExceptionWrapperRepository(() => _factoryFunctions[provider](config));

            repository.PrepareRepository(entityName);
            return(repository);
        }
Esempio n. 14
0
 private void InitializeSqlStatements(IHiLoConfiguration config)
 {
     _sqlStatementToGetLatestNextHiValue = PrepareSqlStatement("SELECT {1} FROM {0} WHERE {2} = {3}", config);
     _sqlStatementToUpdateNextHiValue    = PrepareSqlStatement("UPDATE {0} SET {1} = {1} + 1 WHERE {2} = {3}", config);
     _sqlStatementToCreateRepository     = PrepareSqlStatement("DECLARE vCOUNT NUMBER; " +
                                                               "BEGIN " +
                                                               "SELECT COUNT(*) INTO vCOUNT FROM USER_TABLES WHERE TABLE_NAME = '{0}'; " +
                                                               "IF vCOUNT = 0 THEN " +
                                                               "  EXECUTE IMMEDIATE 'CREATE TABLE {0} ({2} VARCHAR2(100) NOT NULL, {1} NUMBER(19) NOT NULL, CONSTRAINT PK_{0} PRIMARY KEY ({2}))'; " +
                                                               "END IF; " +
                                                               "END;", config);
     _sqlStatementToInitializeEntity = PrepareSqlStatement("DECLARE vCOUNT NUMBER; " +
                                                           "BEGIN " +
                                                           "SELECT COUNT(*) INTO vCOUNT FROM {0} WHERE {2} = {3}; " +
                                                           "IF vCOUNT = 0 THEN " +
                                                           "  INSERT INTO {0} ({2}, {1}) VALUES ({3}, 1); " +
                                                           "END IF; " +
                                                           "END;", config);
 }
Esempio n. 15
0
 public AgnosticHiLoRepository(IHiLoConfiguration config, DbProviderFactory provider)
 {
     _config          = config;
     DbFactoryCreator = (providerName) => provider;
 }
Esempio n. 16
0
        private ConnectionStringSettings FindConnectionStringToBeUsedByHiLoConfiguration(IHiLoConfiguration hiloConfig)
        {
            ConnectionStringSettings connectionStringSettings;

            if (!string.IsNullOrEmpty(hiloConfig.ConnectionStringId))
            {
                connectionStringSettings = _connectionStringsConfig.ConnectionStrings[hiloConfig.ConnectionStringId];
                if (connectionStringSettings == null)
                {
                    throw new NHiLoException(ErrorCodes.NoSpecifiedConnectionStringWasFound).WithInfo("ConnectionStringId", hiloConfig.ConnectionStringId);
                }
            }
            else
            {
                connectionStringSettings = _connectionStringsConfig.ConnectionStrings[_connectionStringsConfig.ConnectionStrings.Count - 1];   // or get the last connection string
            }
            CheckConsistenceOfConnectionStringsSettings(connectionStringSettings);
            return(connectionStringSettings);
        }
Esempio n. 17
0
 private IHiLoConfiguration PrepareHiLoConfigurationWithConnectionString(IHiLoConfiguration hiloConfig, ConnectionStringSettings connectionStringSettings)
 {
     hiloConfig.ConnectionString = connectionStringSettings.ConnectionString;
     hiloConfig.ProviderName     = connectionStringSettings.ProviderName;
     return(hiloConfig);
 }
Esempio n. 18
0
 public MySqlHiLoRepository(IHiLoConfiguration config)
     : base(config, MySql.Data.MySqlClient.MySqlClientFactory.Instance)
 {
     InitializeSqlStatements(config);
 }
Esempio n. 19
0
 /// <summary>
 /// Prepare the SQL statement provided these information:
 /// {0} - table name
 /// {1} - nexthi column name
 /// {2} - entity column name
 /// {3} - parameter name
 /// </summary>
 /// <param name="rawStatement">The SQL statement which will be filled with custom information.</param>
 /// <param name="config">Object that holds the database information.</param>
 /// <returns></returns>
 protected string PrepareSqlStatement(string rawStatement, IHiLoConfiguration config)
 {
     return(string.Format(rawStatement, config.TableName, config.NextHiColumnName, config.EntityColumnName, EntityParameterName));
 }
Esempio n. 20
0
 public PostgreSQLHiLoRepository(IDatabase db, string entityName, IHiLoConfiguration config)
     : base(db, entityName, config)
 {
     InitializeSqlStatements(config);
 }
Esempio n. 21
0
 public OracleHiLoRepository(IHiLoConfiguration config)
     : base(config, Oracle.ManagedDataAccess.Client.OracleClientFactory.Instance)
 {
     InitializeSqlStatements(config);
 }
Esempio n. 22
0
 public SqlServerHiLoRepository(IHiLoConfiguration config)
     : base(config, Microsoft.Data.SqlClient.SqlClientFactory.Instance)
 {
     InitializeSqlStatements(config);
 }
 public TestableHiLoRepository(IHiLoConfiguration config, DbProviderFactory provider)
     : base(config, provider)
 {
 }
Esempio n. 24
0
 internal HiLoGeneratorFactory(IHiLoRepositoryFactory repositoryFactory, IDatabase database, IHiLoConfiguration config)
 {
     _config            = config ?? new HiLoConfiguration();
     _repositoryFactory = repositoryFactory ?? new HiLoRepositoryFactory(database);
 }