Ejemplo n.º 1
0
        void CheckTable(ISqler sqler, DbProvider provider, CSV.Log log, Object key, object value)
        {
            var tabName = key.GetType().Name;

            log.Info("检测表", tabName);

            var keys = CBO.GetProperty(key);
            IDictionary <string, object> textKeys = new Dictionary <string, object>();

            if (value != null)
            {
                if (value.GetType().IsArray == false)
                {
                    textKeys = CBO.GetProperty(value);
                }
            }
            var builder = provider.Builder;


            tabName = GetName(provider, tabName);
            if (builder.Check(tabName.Replace(provider.QuotePrefix, "").Replace(provider.QuoteSuffix, ""), sqler) == false)
            {
                CreateTable(sqler, provider, log, key, value);
            }
            else
            {
                var ps = key.GetType().GetProperties();
                foreach (var property in ps)
                {
                    var filed  = String.Format("{0}{2}{1} ", provider.QuotePrefix, provider.QuoteSuffix, property.Name);
                    var cfiled = builder.Column(filed);
                    if (builder.Check(tabName.Replace(provider.QuotePrefix, "").Replace(provider.QuoteSuffix, "").Trim(), cfiled.Replace(provider.QuotePrefix, "").Replace(provider.QuoteSuffix, "").Trim(), sqler) == false)
                    {
                        var sb   = new StringBuilder();
                        var type = property.PropertyType;
                        if (type.IsGenericType)
                        {
                            type = type.GetGenericArguments()[0];
                        }
                        switch (type.FullName)
                        {
                        case "System.SByte":
                        case "System.Byte":
                        case "System.Int16":
                        case "System.UInt16":
                        case "System.Int32":
                        case "System.UInt32":
                            sb.Append(builder.AddColumn(tabName, cfiled, builder.Integer()));
                            break;

                        case "System.Double":
                        case "System.Single":
                            sb.Append(builder.AddColumn(tabName, cfiled, builder.Float()));
                            break;

                        case "System.Int64":
                        case "System.UInt64":
                        case "System.Decimal":
                            sb.Append(builder.AddColumn(tabName, cfiled, builder.Number()));
                            break;

                        case "System.Boolean":
                            sb.Append(builder.AddColumn(tabName, cfiled, builder.Boolean()));
                            break;

                        case "System.DateTime":
                            sb.Append(builder.AddColumn(tabName, cfiled, builder.Date()));
                            break;

                        case "System.Guid":
                            sb.Append(builder.AddColumn(tabName, cfiled, builder.Guid()));
                            break;

                        default:
                            if (property.PropertyType.IsEnum)
                            {
                                sb.Append(builder.AddColumn(tabName, cfiled, builder.Integer()));
                            }
                            else
                            {
                                if (textKeys.ContainsKey(property.Name))
                                {
                                    sb.Append(builder.AddColumn(tabName, cfiled, builder.Text()));
                                }
                                else
                                {
                                    sb.Append(builder.AddColumn(tabName, cfiled, builder.String()));
                                }
                            }
                            break;
                        }
                        if (sb.Length > 0)
                        {
                            log.Info("追加表字段", tabName + '.' + cfiled);
                            ExecuteNonQuery(log, sqler, sb.ToString());
                        }
                    }
                }
                if (value != null)
                {
                    if (value.GetType().IsArray)
                    {
                        var fs = (string[])value;
                        foreach (var f in fs)
                        {
                            var filed = builder.Column(String.Format("{0}{2}{1} ", provider.QuotePrefix, provider.QuoteSuffix, f));
                            if (builder.Check(tabName.Replace(provider.QuotePrefix, "").Replace(provider.QuoteSuffix, "").Trim(), filed.Replace(provider.QuotePrefix, "").Replace(provider.QuoteSuffix, "").Trim(), sqler) == false)
                            {
                                log.Info("追加表字段", tabName + '.' + filed);
                                ExecuteNonQuery(log, sqler, builder.AddColumn(tabName, filed, builder.String()));
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        void CreateTable(ISqler sqler, DbProvider provider, CSV.Log log, Object key, object value)
        {
            var tabName = key.GetType().Name;

            log.Info("创建表", tabName);

            var keys = CBO.GetProperty(key);
            IDictionary <string, object> textKeys = new Dictionary <string, object>();

            if (value != null)
            {
                if (value.GetType().IsArray == false)
                {
                    textKeys = CBO.GetProperty(value);
                }
            }
            var Delimiter = provider.Delimiter;
            //var provider = sqler.DbProvider;
            var builder = provider.Builder;

            if (String.IsNullOrEmpty(Delimiter))
            {
                tabName = String.Format("{0}{3}{2}", provider.QuotePrefix, provider.Prefixion, provider.QuoteSuffix, tabName);
            }
            else
            {
                if (String.IsNullOrEmpty(provider.Prefixion))
                {
                    tabName = String.Format("{0}{1}{2}", provider.QuotePrefix, tabName, provider.QuoteSuffix);
                }
                else
                {
                    switch (Delimiter)
                    {
                    case ".":
                        tabName = String.Format("{0}{1}{2}.{0}{3}{2}", provider.QuotePrefix, provider.Prefixion, provider.QuoteSuffix, tabName);
                        break;

                    default:
                        tabName = String.Format("{0}{1}{4}{3}{2}", provider.QuotePrefix, provider.Prefixion, provider.QuoteSuffix, tabName, Delimiter);
                        break;
                    }
                }
            }
            var sb = new StringBuilder();

            sb.Append("CREATE TABLE ");
            sb.Append(tabName);
            sb.Append("(");
            var ps = key.GetType().GetProperties();

            foreach (var property in ps)
            {
                var filed = String.Format("{0}{2}{1} ", provider.QuotePrefix, provider.QuoteSuffix, property.Name);

                sb.Append(builder.Column(filed));
                var type = property.PropertyType;
                if (type.IsGenericType)
                {
                    type = type.GetGenericArguments()[0];
                }
                switch (type.FullName)
                {
                case "System.SByte":
                case "System.Byte":
                case "System.Int16":
                case "System.UInt16":
                case "System.Int32":
                case "System.UInt32":
                    sb.Append(builder.Integer());
                    break;

                case "System.Double":
                case "System.Single":
                    sb.Append(builder.Float());
                    break;

                case "System.Int64":
                case "System.UInt64":
                case "System.Decimal":
                    sb.Append(builder.Number());
                    break;

                case "System.Boolean":
                    sb.Append(builder.Boolean());
                    break;

                case "System.DateTime":
                    sb.Append(builder.Date());
                    break;

                case "System.Guid":
                    sb.Append(builder.Guid());
                    break;

                default:
                    if (property.PropertyType.IsEnum)
                    {
                        sb.Append(builder.Integer());
                    }
                    else
                    {
                        if (textKeys.ContainsKey(property.Name))
                        {
                            sb.Append(builder.Text());
                        }
                        else
                        {
                            sb.Append(builder.String());
                        }
                    }
                    break;
                }
                if (keys.ContainsKey(property.Name))
                {
                    sb.Append(" NOT NULL");
                }
                sb.Append(",");
            }
            if (value != null)
            {
                if (value.GetType().IsArray)
                {
                    var fs = (string[])value;
                    foreach (var f in fs)
                    {
                        var filed = String.Format("{0}{2}{1} ", provider.QuotePrefix, provider.QuoteSuffix, f);
                        sb.Append(builder.Column(filed));
                        sb.Append(builder.String());
                        sb.Append(",");
                    }
                }
            }
            sb.Remove(sb.Length - 1, 1);

            sb.Append(")");
            try
            {
                sqler.ExecuteNonQuery(sb.ToString());
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            if (keys.Count > 0)
            {
                var ids = new List <String>();
                var m   = keys.GetEnumerator();
                while (m.MoveNext())
                {
                    var filed = String.Format("{0}{1}{2}", provider.QuotePrefix, m.Current.Key, provider.QuoteSuffix);
                    ids.Add(filed);
                }
                var sql = builder.PrimaryKey(tabName, ids.ToArray());
                if (String.IsNullOrEmpty(sql) == false)
                {
                    try
                    {
                        sqler.ExecuteNonQuery(sql);
                    }
                    catch (Exception ex)
                    {
                        log.Error("创建主键" + String.Join(",", ids.ToArray()), ex.Message);
                    }
                }
            }
        }
Ejemplo n.º 3
0
 public static string Format(DbProvider provider, bool autoPfx, DbCommand cmd, String sql, object[] paramers)
 {
     return(Format(new SqlParamer(), provider, autoPfx, cmd, sql, paramers));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 备份
        /// </summary>
        public void BackUp(CSV.Log log, DbProvider provider, string file)
        {
            DbFactory        dsource = new DbFactory(provider);
            SQLiteDbProvider sqlite  = new SQLiteDbProvider();

            sqlite.Provider = Provider.Create(provider.Provider.Name, sqlite.GetType().FullName);
            sqlite.Provider.Attributes["db"] = file;
            var       em          = dictionary.GetEnumerator();
            var       Delimiter   = provider.Delimiter;
            DbFactory target      = new DbFactory(sqlite);
            var       targetSqler = target.Sqler();
            var       sourceSqler = dsource.Sqler();

            while (em.MoveNext())
            {
                var value = em.Current.Value;
                CreateTable(target.Sqler(), sqlite, log, em.Current.Key, em.Current.Value);
                var tabName = em.Current.Key.GetType().Name;
                //log.Info("备份表", tabName);
                var fields    = new List <String>();;
                var insertSql = new StringBuilder();
                insertSql.Append("INSERT INTO ");
                insertSql.Append(tabName);
                insertSql.Append("(");
                foreach (var f in em.Current.Key.GetType().GetProperties())
                {
                    insertSql.AppendFormat("{0}{1}{2},", sqlite.QuotePrefix, f.Name, sqlite.QuoteSuffix);
                    fields.Add(f.Name);
                }
                if (value != null)
                {
                    if (value.GetType().IsArray)
                    {
                        var fs = (string[])value;
                        foreach (var f in fs)
                        {
                            insertSql.AppendFormat("{0}{1}{2},", sqlite.QuotePrefix, f, sqlite.QuoteSuffix);
                            fields.Add(f);
                        }
                    }
                }
                insertSql.Remove(insertSql.Length - 1, 1);
                insertSql.Append(")VALUES(");
                for (var i = 0; i < fields.Count; i++)
                {
                    insertSql.Append("{");
                    insertSql.Append(i);
                    insertSql.Append("},");
                }
                insertSql.Remove(insertSql.Length - 1, 1);
                insertSql.Append(")");

                sourceSqler.Execute(String.Format("SELECT *FROM {0}", tabName), dr =>
                {
                    System.Data.Common.DbTransaction transaction = null;
                    targetSqler.Execute(s =>
                    {
                        var valueStrSQL = insertSql.ToString();
                        while (dr.Read())
                        {
                            var values = new List <Object>();

                            for (int d = 0; d < fields.Count; d++)
                            {
                                values.Add(dr[fields[d]]);
                            }
                            s.Reset(valueStrSQL, values.ToArray());
                            return(true);
                        }
                        if (transaction != null)
                        {
                            transaction.Commit();
                        }
                        return(false);
                    }, cmd =>
                    {
                        if (transaction == null)
                        {
                            transaction = cmd.Connection.BeginTransaction();
                        }
                        cmd.ExecuteNonQuery();
                    });
                });
            }
            target.Close();
            dsource.Close();
        }
Ejemplo n.º 5
0
 public Inserter(Sqler sqler)
 {
     this.dbProvider = sqler.DbProvider;
     this.sqler      = sqler;
 }
Ejemplo n.º 6
0
 public EntityHelper(DbProvider dbProvider, Type type)
     : this(dbProvider, type, String.Empty)
 {
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 用数据连接字符串初始化访问工厂
 /// </summary>
 /// <param name="dbProvider">访问管理器</param>
 public DbFactory(DbProvider dbProvider)
 {
     this.Provider = dbProvider;
     this.Progress = this.ConnectionProgress;
 }
Ejemplo n.º 8
0
 public Sqler(DbProvider dbProvider, CommandProgress progress, bool autoPfx)
 {
     this.dbProvider = dbProvider;
     this.Command    = progress;
     this.isAutoPfx  = autoPfx;
 }
Ejemplo n.º 9
0
 public Sqler(DbProvider dbProvider, CommandProgress progress)
 {
     this.dbProvider = dbProvider;
     this.Command    = progress;
     this.isAutoPfx  = true;
 }