mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	- 增加 实体特性 [Column(ServerTime = DateTimeKind.Utc)] 使用数据库时间执行插入数据;
- 修复 ToList(a => new Dto { .. }) 在使用 GroupBy 之后报错的 bug;
- 修复 注释迁移到数据库,在 asp.net 4.7 无效的问题;
			
			
This commit is contained in:
		@@ -138,8 +138,7 @@ namespace FreeSql.Odbc.MySql
 | 
			
		||||
                            sb.Append("CREATE TABLE IF NOT EXISTS ").Append(createTableName).Append(" ( ");
 | 
			
		||||
                            foreach (var tbcol in tb.ColumnsByPosition)
 | 
			
		||||
                            {
 | 
			
		||||
                                sb.Append(" \r\n  ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
 | 
			
		||||
                                sb.Append(tbcol.Attribute.DbType);
 | 
			
		||||
                                sb.Append(" \r\n  ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType);
 | 
			
		||||
                                if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT");
 | 
			
		||||
                                if (string.IsNullOrEmpty(tbcol.Comment) == false) sb.Append(" COMMENT ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment));
 | 
			
		||||
                                sb.Append(",");
 | 
			
		||||
@@ -226,26 +225,31 @@ where a.table_schema in ({0}) and a.table_name in ({1})", tboldname ?? tbname);
 | 
			
		||||
                                tbcol.Attribute.IsIdentity != tbstructcol.is_identity ||
 | 
			
		||||
                                isCommentChanged)
 | 
			
		||||
                                {
 | 
			
		||||
                                    if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable && tbcol.Attribute.IsNullable == false && tbcol.DbDefaultValue != "NULL" && tbcol.Attribute.IsIdentity == false)
 | 
			
		||||
                                        sbalter.Append("UPDATE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" SET ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" = ").Append(tbcol.DbDefaultValue).Append(" WHERE ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" IS NULL;\r\n");
 | 
			
		||||
                                    sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(tbcol.Attribute.DbType);
 | 
			
		||||
                                    if (isCommentChanged) sbalter.Append(" COMMENT ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment ?? ""));
 | 
			
		||||
                                    if (isIdentityChanged) sbalter.Append(" AUTO_INCREMENT").Append(existsPrimary == null ? "" : ", DROP PRIMARY KEY").Append(", ADD PRIMARY KEY(").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(")");
 | 
			
		||||
                                    if (string.IsNullOrEmpty(tbcol.Comment) == false) sbalter.Append(" COMMENT ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment ?? ""));
 | 
			
		||||
                                    if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sbalter.Append(" AUTO_INCREMENT");
 | 
			
		||||
                                    if (tbcol.Attribute.IsIdentity == true) sbalter.Append(existsPrimary == null ? "" : ", DROP PRIMARY KEY").Append(", ADD PRIMARY KEY(").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(")");
 | 
			
		||||
                                    sbalter.Append(";\r\n");
 | 
			
		||||
                                }
 | 
			
		||||
                                if (string.Compare(tbstructcol.column, tbcol.Attribute.OldName, true) == 0)
 | 
			
		||||
                                {
 | 
			
		||||
                                    //修改列名
 | 
			
		||||
                                    sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" CHANGE COLUMN ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType);
 | 
			
		||||
                                    if (isCommentChanged) sbalter.Append(" COMMENT ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment ?? ""));
 | 
			
		||||
                                    if (isIdentityChanged) sbalter.Append(" AUTO_INCREMENT").Append(existsPrimary == null ? "" : ", DROP PRIMARY KEY").Append(", ADD PRIMARY KEY(").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(")");
 | 
			
		||||
                                    if (string.IsNullOrEmpty(tbcol.Comment) == false) sbalter.Append(" COMMENT ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment ?? ""));
 | 
			
		||||
                                    if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sbalter.Append(" AUTO_INCREMENT");
 | 
			
		||||
                                    if (tbcol.Attribute.IsIdentity == true) sbalter.Append(existsPrimary == null ? "" : ", DROP PRIMARY KEY").Append(", ADD PRIMARY KEY(").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(")");
 | 
			
		||||
                                    sbalter.Append(";\r\n");
 | 
			
		||||
                                }
 | 
			
		||||
                                continue;
 | 
			
		||||
                            }
 | 
			
		||||
                            //添加列
 | 
			
		||||
                            sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType);
 | 
			
		||||
                            if (tbcol.Attribute.IsNullable == false && tbcol.Attribute.IsIdentity == false) sbalter.Append(" DEFAULT ").Append(_commonUtils.FormatSql("{0}", tbcol.Attribute.DbDefautValue));
 | 
			
		||||
                            if (tbcol.Attribute.IsNullable == false && tbcol.DbDefaultValue != "NULL" && tbcol.Attribute.IsIdentity == false) sbalter.Append(" DEFAULT ").Append(tbcol.DbDefaultValue);
 | 
			
		||||
                            if (string.IsNullOrEmpty(tbcol.Comment) == false) sbalter.Append(" COMMENT ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment ?? ""));
 | 
			
		||||
                            if (isIdentityChanged) sbalter.Append(" AUTO_INCREMENT").Append(existsPrimary == null ? "" : ", DROP PRIMARY KEY").Append(", ADD PRIMARY KEY(").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(")");
 | 
			
		||||
                            if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sbalter.Append(" AUTO_INCREMENT");
 | 
			
		||||
                            if (tbcol.Attribute.IsIdentity == true) sbalter.Append(existsPrimary == null ? "" : ", DROP PRIMARY KEY").Append(", ADD PRIMARY KEY(").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(")");
 | 
			
		||||
                            sbalter.Append(";\r\n");
 | 
			
		||||
                        }
 | 
			
		||||
                        var dsuksql = _commonUtils.FormatSql(@"
 | 
			
		||||
@@ -290,8 +294,7 @@ where a.table_schema IN ({0}) and a.table_name IN ({1}) and a.index_name <> 'PRI
 | 
			
		||||
                    sb.Append("CREATE TABLE IF NOT EXISTS ").Append(tmptablename).Append(" ( ");
 | 
			
		||||
                    foreach (var tbcol in tb.ColumnsByPosition)
 | 
			
		||||
                    {
 | 
			
		||||
                        sb.Append(" \r\n  ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
 | 
			
		||||
                        sb.Append(tbcol.Attribute.DbType);
 | 
			
		||||
                        sb.Append(" \r\n  ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType);
 | 
			
		||||
                        if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT");
 | 
			
		||||
                        if (string.IsNullOrEmpty(tbcol.Comment) == false) sb.Append(" COMMENT ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment));
 | 
			
		||||
                        sb.Append(",");
 | 
			
		||||
@@ -320,10 +323,11 @@ where a.table_schema IN ({0}) and a.table_name IN ({1}) and a.index_name <> 'PRI
 | 
			
		||||
                                //insertvalue = $"cast({insertvalue} as {tbcol.Attribute.DbType.Split(' ').First()})";
 | 
			
		||||
                            }
 | 
			
		||||
                            if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable)
 | 
			
		||||
                                insertvalue = $"ifnull({insertvalue},{_commonUtils.FormatSql("{0}", tbcol.Attribute.DbDefautValue)})";
 | 
			
		||||
                                insertvalue = $"ifnull({insertvalue},{tbcol.DbDefaultValue})";
 | 
			
		||||
                        }
 | 
			
		||||
                        else if (tbcol.Attribute.IsNullable == false)
 | 
			
		||||
                            insertvalue = _commonUtils.FormatSql("{0}", tbcol.Attribute.DbDefautValue);
 | 
			
		||||
                            if (tbcol.DbDefaultValue != "NULL" && tbcol.Attribute.IsIdentity == false)
 | 
			
		||||
                                insertvalue = tbcol.DbDefaultValue;
 | 
			
		||||
                        sb.Append(insertvalue).Append(", ");
 | 
			
		||||
                    }
 | 
			
		||||
                    sb.Remove(sb.Length - 2, 2).Append(" FROM ").Append(tablename).Append(";\r\n");
 | 
			
		||||
 
 | 
			
		||||
@@ -175,8 +175,8 @@ namespace FreeSql.Odbc.MySql
 | 
			
		||||
            {
 | 
			
		||||
                switch (exp.Member.Name)
 | 
			
		||||
                {
 | 
			
		||||
                    case "Now": return "now()";
 | 
			
		||||
                    case "UtcNow": return "utc_timestamp()";
 | 
			
		||||
                    case "Now": return _common.Now;
 | 
			
		||||
                    case "UtcNow": return _common.NowUtc;
 | 
			
		||||
                    case "Today": return "curdate()";
 | 
			
		||||
                    case "MinValue": return "cast('0001/1/1 0:00:00' as datetime)";
 | 
			
		||||
                    case "MaxValue": return "cast('9999/12/31 23:59:59' as datetime)";
 | 
			
		||||
 
 | 
			
		||||
@@ -59,6 +59,8 @@ namespace FreeSql.Odbc.MySql
 | 
			
		||||
        public override string StringConcat(string[] objs, Type[] types) => $"concat({string.Join(", ", objs)})";
 | 
			
		||||
        public override string Mod(string left, string right, Type leftType, Type rightType) => $"{left} % {right}";
 | 
			
		||||
        public override string Div(string left, string right, Type leftType, Type rightType) => $"{left} div {right}";
 | 
			
		||||
        public override string Now => "now()";
 | 
			
		||||
        public override string NowUtc => "utc_timestamp()";
 | 
			
		||||
 | 
			
		||||
        public override string QuoteWriteParamter(Type type, string paramterName)
 | 
			
		||||
        {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user