Exemplo n.º 1
0
        protected BaseStorageWrite(IConnectionManager connectionManager, SimpleCRUD.ITableNameResolver tableNameResolver, DomainSqlConfig option)
            : base(connectionManager, tableNameResolver)
        {
            var key = typeof(T).Name;

            if (option.SqlMap != null && option.SqlMap.ContainsKey(key))
            {
                _domainSql = option.SqlMap[key];
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 2. 向表中填充默认数据
        /// </summary>
        /// <param name="command"></param>
        /// <param name="domainSql"></param>
        protected virtual void FillDefaultData(IDbCommand command, DomainSql domainSql)
        {
            if (domainSql.DefaultData == null || domainSql.DefaultData.Count <= 0)
            {
                _Logger.Debug("没有默认数据需要填充。");
                return;
            }

            var sql = domainSql.DefaultData[domainSql.CurrentDbType];

            command.CommandText = sql;
            var result = command.ExecuteScalar();

            _Logger.Info($"向表[{domainSql.TypeName}]中填充默认数据:{result}");
        }
Exemplo n.º 3
0
        /// <summary>
        /// 1. 检查数据表,如果表不存在,则创建表
        /// </summary>
        protected virtual void VerifyTableExists(IDbCommand command, DomainSql domainSql)
        {
            var tableName = domainSql.TypeName;

            switch (domainSql.CurrentDbType)
            {
            case DatabaseType.SqLite:
            {
                command.CommandText = $"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='{tableName}';";
                break;
            }

            case DatabaseType.MySql:
            {
                command.CommandText = $"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='{domainSql.DatabaseName}' AND TABLE_NAME='{tableName}';";
                break;
            }

            case DatabaseType.PostgreSql:
            {
                command.CommandText = $"SELECT COUNT(*) FROM pg_class WHERE relname='{tableName}';";
                break;
            }

            case DatabaseType.SqlServer:
            {
                command.CommandText = $"SELECT * FROM dbo.SysObjects WHERE ID=object_id(N'[{tableName}]') AND OBJECTPROPERTY(ID, 'IsTable')=1;";
                break;
            }
            }
            var result = (long)command.ExecuteScalar();

            if (1 != result)
            {
                _Logger.Info($"{domainSql.TypeName}在数据库中不存在,准备创建...");
                command.CommandText = domainSql.CreateTable[domainSql.CurrentDbType];
                command.ExecuteNonQuery();
                _Logger.Info($"执行建表{domainSql.TypeName}:\r\n{domainSql.CreateTable[domainSql.CurrentDbType]}");
                // 2. 向表中填充默认数据
                FillDefaultData(command, domainSql);
                _Logger.Info($"向表[{domainSql.TypeName}]中填充默认数据:\r\n{domainSql.CreateTable[domainSql.CurrentDbType]}");
                return;
            }
            _Logger.Debug($"数据表{domainSql.TypeName}正常。");
        }
Exemplo n.º 4
0
        /// <summary>
        /// 生成指定实体类型的数据库管理配置类。
        /// </summary>
        /// <param name="domain">指定实体类</param>
        /// <param name="dbType">数据库类型</param>
        /// <param name="defalutData">默认数据,该实体类的实例集合。默认为空</param>
        /// <param name="writeConn">写字符串,默认为空,当不指定时,采用通用的连接字符串</param>
        /// <param name="readConn">读字符串,默认为空,当不指定时,采用通用的连接字符串</param>
        /// <returns>指定实体类型的数据库管理配置类</returns>
        public static DomainSql Build(Type domain, DatabaseType dbType, object[] defalutData = null, string writeConn = "", string readConn = "")
        {
            var ds = new DomainSql
            {
                Type          = domain,
                TypeName      = domain.Name,
                CurrentDbType = dbType
            };

            if (!string.IsNullOrEmpty(writeConn))
            {
                ds.Write = writeConn;
            }
            else if (_CommonWriteConnMap.ContainsKey(dbType))
            {
                ds.Write = _CommonWriteConnMap[dbType];
            }
            if (!string.IsNullOrEmpty(readConn))
            {
                ds.Read = readConn;
            }
            else if (_CommonReadConnMap.ContainsKey(dbType))
            {
                ds.Read = _CommonReadConnMap[dbType];
            }

            var sql = GetCreateTableSql(dbType, domain);

            ds.CreateTable.Add(dbType, sql);
            sql = GetInsertTemplateSql(dbType, domain);
            ds.Insert.Add(dbType, sql);
            sql = GetUpdateTemplateSql(dbType, domain);
            ds.Update.Add(dbType, sql);

            if (null != defalutData && defalutData.Length > 0)
            {
                var data = GetInsertDefaultDataSql(dbType, defalutData);
                ds.DefaultData.Add(dbType, data);
            }

            return(ds);
        }
Exemplo n.º 5
0
 /// <summary>
 ///     关闭“读”数据库连接
 /// </summary>
 public virtual void CloseReadConnection(DomainSql domainSql, bool isWrite)
 {
     CloseDbConnection(_readConnectionMap, isWrite ? domainSql.Write : domainSql.Read);
 }
Exemplo n.º 6
0
 /// <summary>
 ///     打开“读”数据库连接,并返回该连接
 /// </summary>
 /// <returns>数据库连接</returns>
 public virtual IDbConnection OpenReadConnection(DomainSql domainSql, bool isWrite)
 {
     return(OpenDbConnection(_readConnectionMap, domainSql.CurrentDbType, isWrite ? domainSql.Write : domainSql.Read));
 }