Пример #1
0
 /// <summary>
 /// Builds the sql result.
 /// </summary>
 /// <param name="sql">The sql.</param>
 /// <returns>An ISqlResult.</returns>
 public ISqlResult BuildSqlResult(ISql sql)
 {
     return(sql switch
     {
         IQuery query => BuildQuery(query),
         IfSql ifSql => BuilderIfSql(ifSql),
         StringSql stringSql => BuilderStringSql(stringSql),
         ExistSql existSql => BuilderExist(existSql),
         CountDatabaseSql countDatabaseSql => BuildCountForDatabase(countDatabaseSql),
         DeleteDatabaseSql deleteDatabaseSql => BuildDeleteDatabase(deleteDatabaseSql),
         AllTableNamesSql allTableNamesSql => BuildAllTableName(allTableNamesSql),
         CreateDatabaseSql createDatabaseSql => BuildCreateDatabase(createDatabaseSql),
         AllDatabaseSql allDatabaseSql => BuildAllDatabase(allDatabaseSql),
         CreateTableSql createTableSql => BuildCreateTable(createTableSql),
         CountTableSql countTableSql => BuildTableCount(countTableSql),
         UpdateTableNameSql updateTableNameSql => BuildUpdateTableName(updateTableNameSql),
         DeleteTableSql deleteTableSql => BuildDeleteTable(deleteTableSql),
         ColumsNamesSql columsNamesSql => BuildColumnNames(columsNamesSql),
         CountColumsSql countColumsSql => BuildColumnsCount(countColumsSql),
         RenameColumnSql renameColumnSql => BuildRenameColumnsName(renameColumnSql),
         UpdateColumnsSql updateColumnsSql => BuildUpdateColumns(updateColumnsSql),
         AddColumnsSql addColumnsSql => BuildAddColumns(addColumnsSql),
         DeleteColumsSql deleteColumsSql => BuildDeleteColumn(deleteColumsSql),
         CreateIndexSql createIndexSql => BuildCreateIndex(createIndexSql),
         DeleteIndexSql deleteIndexSql => BuildDeleteIndex(deleteIndexSql),
         CountIndexSql countIndexSql => BuildIndexCount(countIndexSql),
         RenameIndexSql renameIndexSql => RenameIndexSql(renameIndexSql),
         DeleteSql deleteSql => BuildDeleteSql(deleteSql),
         InsertSql insertSql => BuildInsertSql(insertSql),
         UpdateSql updateSql => BuildUpdateSql(updateSql),
         _ => new ParmsSqlResult()
     });
Пример #2
0
        /// <summary>
        /// Creates the table.
        /// </summary>
        /// <param name="createTableSql">The create table sql.</param>
        /// <returns>An ISqlResult.</returns>
        protected virtual ISqlResult BuildCreateTable(CreateTableSql createTableSql)
        {
            var type      = createTableSql.TableType;
            var r         = new ParmsSqlResult();
            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}";
                var isSequence = item.GetCustomAttribute(typeof(SequenceAttribute), true) != null;
                if (isSequence)
                {
                    columSql = $"SequenceId int AUTO_INCREMENT";
                    columSqls.Add(columSql);
                    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))
                    {
                        columSql = $"{columSql}";
                    }
                    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))
                    {
                        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)//如果是string类型默认是可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 {tableName}({string.Join(",", columSqls)})";

            r.SQL = sql;
            return(r);
        }