/// <summary>
        /// 根据表名获取实体类的字符串
        /// </summary>
        /// <param name="db"></param>
        /// <param name="sql"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        public string TableNameToClass(SqlSugarClient db, string tableName)
        {
            var dt           = db.GetDataTable(string.Format("select * from {0} where rownum<=1", tableName.ToOracleTableName()));
            var tableColumns = GetTableColumns(db, tableName);
            var reval        = DataTableToClass(dt, tableName, null, tableColumns);

            return(reval);
        }
        /// <summary>
        /// 根据表名获取实体类的字符串
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public string TableNameToClass(SqlSugarClient db, string tableName)
        {
            var dt           = db.GetDataTable(string.Format(SqlSugarTool.GetSelectTopSql(), tableName.GetTranslationSqlName()));
            var tableColumns = GetTableColumns(db, tableName);
            var reval        = DataTableToClass(dt, tableName, null, tableColumns);

            return(reval);
        }
        /// <summary>
        ///  创建SQL实体文件,指定表名
        /// </summary>
        public void CreateClassFilesByTableNames(SqlSugarClient db, string fileDirectory, string nameSpace, params string[] tableNames)
        {
            string sql    = GetCreateClassSql(null);
            var    tables = db.GetDataTable(sql);

            if (tables != null && tables.Rows.Count > 0)
            {
                foreach (DataRow dr in tables.Rows)
                {
                    string tableName = dr["name"].ToString().ToLower();
                    if (tableNames.Any(it => it.ToLower() == tableName))
                    {
                        var currentTable = db.GetDataTable(string.Format("select  * from {0} WHERE rownum<=1", tableName.ToOracleTableName()));
                        var classCode    = DataTableToClass(currentTable, tableName, nameSpace);
                        FileSugar.WriteText(fileDirectory.TrimEnd('\\') + "\\" + tableName + ".cs", classCode);
                    }
                }
            }
        }
        /// <summary>
        /// 创建SQL实体文件
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableOrView">是生成视图文件还是表文件,null生成表和视图,true生成表,false生成视图(默认为:null)</param>
        /// <param name="callBack">回调函数</param>
        public void CreateClassFilesInterface(SqlSugarClient db, bool?tableOrView, Action <DataTable, string, string> callBack)
        {
            var isLog = db.IsEnableLogEvent;

            db.IsEnableLogEvent = false;
            string sql    = SqlSugarTool.GetCreateClassSql(tableOrView);
            var    tables = db.GetDataTable(sql);

            if (tables != null && tables.Rows.Count > 0)
            {
                foreach (DataRow dr in tables.Rows)
                {
                    string tableName    = dr["name"].ToString();
                    var    currentTable = db.GetDataTable(string.Format(SqlSugarTool.GetSelectTopSql(), GetTableNameWithSchema(db, tableName).GetTranslationSqlName()));
                    string className    = db.GetClassTypeByTableName(tableName);
                    callBack(tables, className, tableName);
                }
            }
            db.IsEnableLogEvent = isLog;
        }
Exemple #5
0
        /// <summary>
        /// 多线程请求所有数据库节点,同步汇总结果
        /// </summary>
        /// <typeparam name="T">支持DataTable、实体类和值类型</typeparam>
        /// <param name="sql"></param>
        /// <param name="connectionStringList">连接字符串数组</param>
        /// <param name="whereObj">参数 例如: new { id="1",name="张三"}</param>
        /// <returns></returns>
        public Taskable <T> Taskable <T>(string sql, List <string> connectionStringList, object whereObj = null)
        {
            Taskable <T> reval = new Taskable <T>();

            reval.Sql      = sql;
            reval.WhereObj = whereObj;
            var tasks = new Task <CloudSearchResult <T> > [connectionStringList.Count];

            for (int i = 0; i < tasks.Length; i++)
            {
                CloudPubMethod.TaskFactory <CloudSearchResult <T> >(ti =>
                {
                    string innerSql = sql;
                    var connString  = connectionStringList[ti];
                    var db          = new SqlSugarClient(connString);
                    SettingConnection(db);
                    CloudSearchResult <T> itemReval = new CloudSearchResult <T>();
                    var isDataTable = typeof(T) == typeof(DataTable);
                    var isClass     = typeof(T).IsClass;
                    if (innerSql.Contains("$:->connectionString<-:$"))
                    {
                        innerSql = innerSql.Replace("$:->connectionString<-:$", connString);
                    }
                    if (isDataTable)
                    {
                        itemReval.DataTable = db.GetDataTable(innerSql, whereObj);
                    }
                    else if (isClass)
                    {
                        itemReval.Entities = db.SqlQuery <T>(innerSql, whereObj);
                    }
                    else
                    {
                        var obj = db.GetScalar(sql, whereObj);
                        if (obj == DBNull.Value)
                        {
                            itemReval.Value = default(T);
                        }
                        else
                        {
                            obj             = Convert.ChangeType(obj, typeof(T));
                            itemReval.Value = (T)obj;
                        }
                    }
                    itemReval.ConnectionString = connString;
                    return(itemReval);
                }, tasks, i);
            }
            Task.WaitAll(tasks);
            reval.Tasks = tasks;
            return(reval);
        }
        /// <summary>
        /// 创建实体文件
        /// </summary>
        /// <param name="db"></param>
        /// <param name="fileDirectory"></param>
        /// <param name="nameSpace">命名空间(默认:system)</param>
        /// <param name="tableOrView">是生成视图文件还是表文件,null生成表和视图,true生成表,false生成视图(默认为:null)</param>
        /// <param name="callBack">生成文件后的处理,参数string为实体名</param>
        /// <param name="preAction">生成文件前的处理,参数string为表名</param>
        public void CreateClassFiles(SqlSugarClient db, string fileDirectory, string nameSpace = null, bool?tableOrView = null, Action <string> callBack = null, Action <string> preAction = null)
        {
            var isLog = db.IsEnableLogEvent;

            db.IsEnableLogEvent = false;
            string sql    = SqlSugarTool.GetCreateClassSql(tableOrView);
            var    tables = db.GetDataTable(sql);

            if (tables != null && tables.Rows.Count > 0)
            {
                foreach (DataRow dr in tables.Rows)
                {
                    string tableName = dr["name"].ToString();
                    if (preAction != null)
                    {
                        preAction(tableName);
                    }
                    var currentTable = db.GetDataTable(string.Format(SqlSugarTool.GetSelectTopSql(), GetTableNameWithSchema(db, tableName).GetTranslationSqlName()));
                    if (callBack != null)
                    {
                        var    tableColumns = GetTableColumns(db, tableName);
                        var    classCode    = DataTableToClass(currentTable, tableName, nameSpace, tableColumns);
                        string className    = db.GetClassTypeByTableName(tableName);
                        classCode = classCode.Replace("class " + tableName, "class " + className);
                        FileSugar.CreateFile(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode, Encoding.UTF8);
                        callBack(className);
                    }
                    else
                    {
                        var    tableColumns = GetTableColumns(db, tableName);
                        string className    = db.GetClassTypeByTableName(tableName);
                        var    classCode    = DataTableToClass(currentTable, className, nameSpace, tableColumns);
                        FileSugar.CreateFile(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode, Encoding.UTF8);
                    }
                }
            }
            db.IsEnableLogEvent = isLog;
        }
Exemple #7
0
        /// <summary>
        /// 根据表获取主键
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        internal static List <string> GetPrimaryKeyByTableNames(SqlSugarClient db, string tableName)
        {
            string key = "GetPrimaryKeyByTableName" + tableName;
            var    cm  = CacheManager <List <KeyValue> > .GetInstance();

            List <KeyValue> primaryInfo = null;

            //获取主键信息
            if (cm.ContainsKey(key))
            {
                primaryInfo = cm[key];
            }
            else
            {
                string sql   = @"               select cu.TABLE_NAME  ,cu.COLUMN_name KEYNAME  from user_cons_columns cu, user_constraints au 
   where cu.constraint_name = au.constraint_name
    and au.constraint_type = 'P' and au.table_name = '" + tableName.GetTranslationSqlName() + @"'";
                var    isLog = db.IsEnableLogEvent;
                db.IsEnableLogEvent = false;
                var dt = db.GetDataTable(sql);
                db.IsEnableLogEvent = isLog;
                primaryInfo         = new List <KeyValue>();
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        primaryInfo.Add(new KeyValue()
                        {
                            Key = dr["TABLE_NAME"].ToString(), Value = dr["KEYNAME"].ToString()
                        });
                    }
                }
                cm.Add(key, primaryInfo, cm.Day);
            }

            //反回主键
            if (!primaryInfo.Any(it => it.Key.ToLower() == tableName.ToLower()))
            {
                return(null);
            }
            return(primaryInfo.Where(it => it.Key.ToLower() == tableName.ToLower()).Select(it => it.Value).ToList());
        }
Exemple #8
0
        /// <summary>
        /// 多线程请求所有数据库节点,同步汇总结果
        /// </summary>
        /// <typeparam name="T">支持DataTable、实体类和值类型</typeparam>
        /// <param name="sqlSelect">sql from之前(例如: "select count(*)" )</param>
        /// <param name="sqlEnd">sql from之后(例如: "from table where id=1" </param>
        /// <param name="whereObj">参数 例如: new { id="1",name="张三"}</param>
        /// <returns></returns>
        public TaskableWithCount <T> TaskableWithCount <T>(string sqlSelect, string sqlEnd, object whereObj = null)
        {
            TaskableWithCount <T> reval = new TaskableWithCount <T>();

            reval.Sql      = sqlSelect + sqlEnd;
            reval.WhereObj = whereObj;
            var tasks = new Task <CloudSearchResult <T> > [configList.Count];

            for (int i = 0; i < tasks.Length; i++)
            {
                CloudPubMethod.TaskFactory <CloudSearchResult <T> >(ti =>
                {
                    var connString = configList[ti].ConnectionString;
                    var db         = new SqlSugarClient(connString);
                    SettingConnection(db);
                    CloudSearchResult <T> itemReval = new CloudSearchResult <T>();
                    var isDataTable = typeof(T) == typeof(DataTable);
                    var isClass     = typeof(T).IsClass;
                    if (isClass)
                    {
                        itemReval.Entities = db.SqlQuery <T>(reval.Sql, whereObj);
                    }
                    else if (isDataTable)
                    {
                        itemReval.DataTable = db.GetDataTable(reval.Sql, whereObj);
                    }
                    else
                    {
                        var obj         = db.GetScalar(reval.Sql, whereObj);
                        obj             = Convert.ChangeType(obj, typeof(T));
                        itemReval.Value = (T)obj;
                    }
                    itemReval.Count            = db.GetInt("SELECT COUNT(1)" + sqlEnd);;
                    itemReval.ConnectionString = connString;
                    return(itemReval);
                }, tasks, i);
            }
            Task.WaitAll(tasks);
            reval.Tasks = tasks;
            return(reval);
        }