Пример #1
0
 private void BuildSequences()
 {
     if (!_driver.Supports(DbFeatures.Sequences))
     {
         return;
     }
     foreach (var m in _entityModel.App.Modules)
     {
         foreach (var seq in m.Sequences)
         {
             var dbSeqInfo = new DbSequenceInfo(this._dbModel, seq);
             _dbModel.Sequences.Add(dbSeqInfo);
         }
     }
 }//method
Пример #2
0
        public bool CanProcessMany(DbUpdateTableGroup group)
        {
            if (group.Records.Count <= 1)
            {
                return(false);
            }
            switch (group.Operation)
            {
            case LinqOperation.Delete:
                return(group.Table.PrimaryKey.KeyColumns.Count == 1);

            case LinqOperation.Insert:
                if (!_driver.Supports(DbFeatures.InsertMany))
                {
                    return(false);
                }
                if (group.Table.Entity.Flags.IsSet(EntityFlags.HasIdentity | EntityFlags.HasRowVersion))
                {
                    return(false);
                }
                return(true);

            default:
                return(false);
            }
        }
Пример #3
0
        public DbModelConfig(DbDriver driver, DbOptions options = DbOptions.Default,
                             DbNamingPolicy namingPolicy        = null, IDictionary <string, string> schemaMappings = null)
        {
            Util.Check(driver != null, "Driver parameter may not be null.");
            Driver       = driver;
            Options      = options;
            NamingPolicy = namingPolicy ?? new DbNamingPolicy();
            //import schema mappings
            if (schemaMappings != null)
            {
                foreach (var de in schemaMappings)
                {
                    SchemaMappings[de.Key] = de.Value;
                }
            }

            //Verify options
            if (!Driver.Supports(DbFeatures.StoredProcedures))
            {
                Options &= ~DbOptions.UseStoredProcs;
            }
            //Batch mode is not available without stored procedures
            if (!Options.IsSet(DbOptions.UseStoredProcs))
            {
                Options &= ~DbOptions.UseBatchMode;
            }
        }
Пример #4
0
        private void BuildCrudCommands()
        {
            bool useSPs = _driver.Supports(DbFeatures.StoredProcedures) && _config.Options.IsSet(DbOptions.UseStoredProcs);

            foreach (var entityCommand in _entityModel.GetCrudCommands())
            {
                if (entityCommand.TargetEntityInfo.Kind == EntityKind.View && !_dbModel.Driver.Supports(DbFeatures.Views))
                {
                    continue;
                }
                // We try to match commands by descrTag; it sometimes happens that two entity commands are identical and have the same underlying db command
                var descrTag = _dbSqlBuilder.GetDbCommandDescriptiveTag(entityCommand);
                var cmd      = _dbModel.GetCommandByTag(descrTag);
                if (cmd != null) //if it exists already, register it with other entity command
                {
                    _dbModel.RegisterDbObject(entityCommand, cmd);
                    continue;
                }
                cmd = BuildCommand(_dbSqlBuilder, entityCommand);
                if (cmd == null)
                {
                    continue;
                }

                if (useSPs && !cmd.IsTemplatedSql)
                {
                    _dbSqlBuilder.ConvertToStoredProc(cmd);
                    cmd.CommandType = CommandType.StoredProcedure;
                }
            }
        }
Пример #5
0
        //Prepares for full run with a specified server
        public static void Reset(DbServerType serverType)
        {
            DeleteLocalLogFiles();
            InitAppConfig();
            ServerType = serverType;
            if (ServerType == DbServerType.SQLite)
            {
                DeleteSqliteDbFile(); //it will be created on connect, creat-option in conn string
            }
            Driver = DataUtility.CreateDriver(ServerType);

            //Load connection string
            var connStringName = ServerType.ToString() + "ConnectionString";

            // For SQLite we can use either provider
            var useMsSqlite = AppConfig["UseMsSqliteProvider"] == "true";

            if (ServerType == DbServerType.SQLite && useMsSqlite)
            {
                var sqliteDriver = (Data.SQLite.SQLiteDbDriver)Driver;
                sqliteDriver.ConnectionFactory = (s) => new Microsoft.Data.Sqlite.SqliteConnection(s);
                sqliteDriver.CommandFactory    = () => new Microsoft.Data.Sqlite.SqliteCommand();
                connStringName += "_MS";
            }


            var connString = AppConfig[connStringName];

            Util.Check(!string.IsNullOrEmpty(connString), "Connection string not found for key: {0}.", connStringName);
            if (connString.Contains("{bin}"))
            {
                var asmPath   = Assembly.GetExecutingAssembly().Location;
                var binFolder = Path.GetDirectoryName(asmPath);
                connString = connString.Replace("{bin}", binFolder);
            }
            ConnectionString = connString;


            DbOptions = Driver.GetDefaultOptions();
            //enable batch
            var useBatch = AppConfig["useBatchMode"] == "true";

            if (useBatch && Driver.Supports(DbFeatures.BatchedUpdates))
            {
                DbOptions |= DbOptions.UseBatchMode;
            }
            else
            {
                DbOptions &= ~DbOptions.UseBatchMode;
            }
            //check connection
            if (!DataUtility.TestConnection(Driver, ConnectionString, out var error))
            {
                Util.Throw("Failed to connect to the database: {0} \r\n  Connection string: {1}", error, ConnectionString);
            }
        }
Пример #6
0
        //Prepares for full run with a specified server
        public static void Reset(DbServerType serverType)
        {
            ServerType = serverType;
            //Load connection string
            var connStringName = ServerType.ToString() + "ConnectionString";
            var connString     = ConfigurationManager.AppSettings[connStringName];

            Util.Check(!string.IsNullOrEmpty(connString), "Connection string not found for key: {0}.", connStringName);
            if (connString.Contains("{bin}"))
            {
                var asmPath   = Assembly.GetExecutingAssembly().Location;
                var binFolder = Path.GetDirectoryName(asmPath);
                connString = connString.Replace("{bin}", binFolder);
            }
            ConnectionString = connString;
            Driver           = ToolHelper.CreateDriver(ServerType);
            DbOptions        = ToolHelper.GetDefaultOptions(ServerType);
            //enable stored procs
            DbOptions &= ~DbOptions.UseStoredProcs; //it is on by default
            var useSp = ConfigurationManager.AppSettings["useStoredProcs"] == "true";

            if (useSp && Driver.Supports(DbFeatures.StoredProcedures))
            {
                DbOptions |= Data.DbOptions.UseStoredProcs;
            }
            //enable batch
            var useBatch = ConfigurationManager.AppSettings["useBatchMode"] == "true";

            if (useBatch && Driver.Supports(DbFeatures.BatchedUpdates))
            {
                DbOptions |= DbOptions.UseBatchMode;
            }
            //check connection
            string error;

            if (!ToolHelper.TestConnection(Driver, ConnectionString, out error))
            {
                Util.Throw("Failed to connection to the database: {0} \r\n  Connection string: {1}", error, ConnectionString);
            }
        }
Пример #7
0
        private string FormatListPlaceHolder(SqlListParamPlaceHolder ph, object arg)
        {
            // read value from locals (linq); for DeleteMany: read list of IDs from list of records
            var list = ph.ListValueReader((object[])arg);
            // always use parameter, unless option is not available
            var useLiteral = !_driver.Supports(DbFeatures.ArrayParameters) || _genMode == SqlGenMode.NoParameters;

            if (useLiteral)
            {
                return(ph.FormatLiteral(list));
            }
            else
            {
                var prm = _sqlDialect.AddDbParameter(_dbCommand, ph, list);
                var fmt = ph.FormatParameter;
                return(fmt == null ? prm.ParameterName : fmt(prm));
            }
        }