Exemplo n.º 1
0
        private static void SaveLog(string message)
        {
            int aux = 0;

TryAgain:
            try
            {
                if (aux < 2)
                {
                    js_Log log = new js_Log();
                    log.CreateOnDate = DateTime.Now;
                    log.Message      = message;
                    log.Module       = string.Empty;

                    using (var context = new Model.Model())
                    {
                        context.js_Log.Add(log);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                CreateTableSql.CreateTable(ex);
                aux++;
                goto TryAgain;
            }
        }
Exemplo n.º 2
0
        public static string GetTableSql(IEnumerable <string> filePaths, string tableName, char csvDelimiter = ',', int headerOffsetRows = 1)
        {
            var readers = new List <IDataReader>();

            var readerFacs = new List <Func <DataReaderInfo> >();

            foreach (var filePath in filePaths)
            {
                var readerFac = new Func <DataReaderInfo>(() =>
                {
                    var r = new DataReaderInfo
                    {
                        DataReader = DataReaderFactories.Default(filePath, true, csvDelimiter),
                        FilePath   = filePath
                    };

                    readers.Add(r.DataReader);

                    return(r);
                });
                readerFacs.Add(readerFac);
            }

            var rr = CreateTableSql.FromDataReader_Smart(tableName, readerFacs);

            foreach (var dataReader in readers)
            {
                dataReader.Dispose();
            }

            return(rr);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Fits the data in the enumerable to a create table statement.
        /// </summary>
        /// <param name="enumerable"></param>
        /// <param name="outputTableName"></param>
        /// <param name="numberOfRowsToExamine"></param>
        /// <param name="ignoreNonStringReferenceTypes"></param>
        /// <returns></returns>
        public static string FitToCreateTableSql <T>(this IEnumerable <T> enumerable, string outputTableName,
                                                     int?numberOfRowsToExamine          = null,
                                                     bool ignoreNonStringReferenceTypes = true)
        {
            var r = CreateTableSql.FromDataReader_Smart(outputTableName, enumerable.ToDataReader(ignoreNonStringReferenceTypes),
                                                        numberOfRowsToExamine);

            return(r);
        }
Exemplo n.º 4
0
        public static string GetTableSql(string filePath, string destinationTableName)
        {
            var readerFacs = new List <Func <DataReaderInfo> >();

            var readerFac = new Func <DataReaderInfo>(
                () => new DataReaderInfo
            {
                DataReader = DataReaderFactories.Default(filePath),
                FilePath   = filePath
            });

            readerFacs.Add(readerFac);

            var outputText = CreateTableSql.FromDataReader_Smart(destinationTableName, readerFacs);

            return(outputText);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Builds the create table.
        /// </summary>
        /// <param name="createTableSql">The create table sql.</param>
        /// <returns>An ISqlResult.</returns>
        protected override ISqlResult BuildCreateTable(CreateTableSql createTableSql)
        {
            var r         = new ParmsSqlResult();
            var type      = createTableSql.TableType;
            var typeInfo  = type.GetTypeInfo();
            var props     = typeInfo.GetRuntimeProperties();
            var columSqls = new List <string>();
            var tableName = TableUtlis.GetTableName(type);
            var keys      = new List <string>();

            foreach (var item in props)
            {
                var pType      = item.PropertyType.Name;
                var isNullType = false;
                if (item.PropertyType.Name == typeof(Nullable <>).Name)
                {
                    isNullType = true;
                    pType      = item.PropertyType.GenericTypeArguments[0].Name;
                }

                var aType    = _typeMap.GetSqlType(pType);
                var columSql = $"{item.Name} {aType}";
                if (item.Name == "SequenceId")
                {
                    columSql = $"SequenceId int AUTO_INCREMENT";
                    var indexSql = "UNIQUE KEY(SequenceId)";
                    columSqls.Add(columSql);
                    columSqls.Add(indexSql);
                    continue;
                }
                else if (item.Name == "Id")
                {
                    columSql = $"Id nvarchar(36)";
                    columSqls.Add(columSql);
                    continue;
                }
                var igAttribute = item.GetCustomAttribute(typeof(IngoreAttribute), true);
                if (igAttribute != null)
                {
                    continue;
                }
                if (item.GetCustomAttribute(typeof(ColumnAttribute), true) is ColumnAttribute columnAttribute)
                {
                    if (string.IsNullOrWhiteSpace(columnAttribute.Name))
                    {
                        columSql = columSql.Replace(item.Name, columnAttribute.Name);
                    }
                    if (pType == nameof(TypeCode.Double) || pType == nameof(TypeCode.Single))
                    {
                        var length = columnAttribute.Length == -1 ? 15 : columnAttribute.Length;
                        columSql = $"{columSql}({length},6)";
                    }
                    else if (pType == nameof(TypeCode.DateTime) || pType == nameof(TypeCode.Byte))
                    {
                        var length = columnAttribute.Length < 36 ? 0 : columnAttribute.Length;
                        columSql = $"{columSql}({length})";
                    }
                    else if (pType == typeof(Guid).Name)
                    {
                        columSql = $"{columSql}";
                    }
                    else
                    {
                        var length = columnAttribute.Length == -1 ? 255 : columnAttribute.Length;
                        columSql = $"{columSql}({length})";
                    }
                }
                else
                {
                    if (pType == nameof(TypeCode.Double) || pType == nameof(TypeCode.Single))
                    {
                        columSql = $"{columSql}(15,6)";
                    }
                    else if (pType == nameof(TypeCode.DateTime) || pType == nameof(TypeCode.Byte) || pType == nameof(TypeCode.Int32) || pType == nameof(TypeCode.Int64) ||
                             pType == nameof(TypeCode.Decimal))
                    {
                        columSql = $"{columSql}";
                    }
                    else if (pType == typeof(Guid).Name)
                    {
                        columSql = $"{columSql}(36)";
                    }
                    else
                    {
                        columSql = $"{columSql}(255)";
                    }
                }
                if (item.GetCustomAttribute(typeof(KeyAttribute), true) is KeyAttribute _)
                {
                    keys.Add(item.Name);
                }
                var isNotNullAttribute = item.GetCustomAttribute(typeof(NotNullAttribute), true);
                if (item.PropertyType == typeof(string) && isNotNullAttribute == null)
                {
                    isNullType = true;
                }
                else if (isNotNullAttribute != null && isNullType)
                {
                    throw new NotSupportedException("可空类型不能被NotNull标记");
                }
                else if (!isNullType)
                {
                    columSql = $"{columSql} not null";
                }
                columSqls.Add(columSql);
            }
            if (keys.Count == 0)
            {
                columSqls.Add($"PRIMARY KEY ( Id )");
            }
            else if (keys.Count == 1)
            {
                columSqls.Add($"PRIMARY KEY ( {keys[0]} )");
            }
            else
            {
                throw new NotSupportedException("暂时不支持联合主键,请使用联合唯一索引代替");
            }
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new Exception("当前TabelName的值为null 无法创建表");
            }
            var sql = $@"create table if not exists {tableName}({string.Join(",", columSqls)})";

            r.SQL = sql;
            return(r);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Generates a CREATE TABLE statement from a type definition, where the
 /// </summary>
 /// <param name="t"></param>
 /// <param name="outputTableName">Output table name, default will be class name.</param>
 /// <returns></returns>
 public static string GenerateCreateTableScript(this Type t, string outputTableName = null)
 {
     return(CreateTableSql.GenerateCreateTableScriptFromType(t, outputTableName));
 }