mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
Update MySqlConnector
This commit is contained in:
parent
57375c5b14
commit
a37a945950
@ -25,6 +25,10 @@ namespace SqlSugar.MySqlConnector
|
||||
DbColumnInfo dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item);
|
||||
columns.Add(dbColumnInfo);
|
||||
}
|
||||
if (entityInfo.IsCreateTableFiledSort)
|
||||
{
|
||||
columns = columns.OrderBy(c => c.CreateTableFieldSort).ToList();
|
||||
}
|
||||
}
|
||||
this.Context.DbMaintenance.CreateTable(tableName, columns,true);
|
||||
}
|
||||
@ -42,7 +46,8 @@ namespace SqlSugar.MySqlConnector
|
||||
DefaultValue = item.DefaultValue,
|
||||
ColumnDescription = item.ColumnDescription,
|
||||
Length = item.Length,
|
||||
DecimalDigits=item.DecimalDigits
|
||||
DecimalDigits=item.DecimalDigits,
|
||||
CreateTableFieldSort = item.CreateTableFieldSort
|
||||
};
|
||||
GetDbType(item, propertyType, result);
|
||||
if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0)
|
||||
@ -76,5 +81,33 @@ namespace SqlSugar.MySqlConnector
|
||||
{
|
||||
return EntityColumnToDbColumn(entity,dbTableName,item);
|
||||
}
|
||||
|
||||
protected override void GetDbType(EntityColumnInfo item, Type propertyType, DbColumnInfo result)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(item.DataType))
|
||||
{
|
||||
result.DataType = item.DataType;
|
||||
}
|
||||
else if (propertyType.IsEnum())
|
||||
{
|
||||
result.DataType = this.Context.Ado.DbBind.GetDbTypeName(item.Length > 9 ? UtilConstants.LongType.Name : UtilConstants.IntType.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
var name = GetType(propertyType.Name);
|
||||
if (name == "Boolean")
|
||||
{
|
||||
result.DataType = "tinyint";
|
||||
result.Length = 1;
|
||||
result.Scale = 0;
|
||||
result.DecimalDigits = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.DataType = this.Context.Ado.DbBind.GetDbTypeName(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -153,6 +153,13 @@ namespace SqlSugar.MySqlConnector
|
||||
return "alter table {0} change column {1} {2}";
|
||||
}
|
||||
}
|
||||
protected override string IsAnyProcedureSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "select count(*) from information_schema.Routines where ROUTINE_NAME='{0}' and ROUTINE_TYPE='PROCEDURE'";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Check
|
||||
@ -269,16 +276,35 @@ namespace SqlSugar.MySqlConnector
|
||||
{
|
||||
get
|
||||
{
|
||||
return "SELECT count(*) FROM information_schema.statistics WHERE index_name = '{0}'";
|
||||
return "SELECT count(*) FROM information_schema.statistics WHERE index_name = '{0}' and index_schema = '{1}'";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
public override bool IsAnyTable(string tableName, bool isCache = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
return base.IsAnyTable(tableName, isCache);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (SugarCompatible.IsFramework && ex.Message == "Invalid attempt to Read when reader is closed.")
|
||||
{
|
||||
Check.ExceptionEasy($"To upgrade the MySql.Data. Error:{ex.Message}", $" 请先升级MySql.Data 。 详细错误:{ex.Message}");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
public override bool IsAnyColumnRemark(string columnName, string tableName)
|
||||
{
|
||||
var isAny=this.Context.DbMaintenance.GetColumnInfosByTableName(tableName, false)
|
||||
.Any(it => it.ColumnDescription.HasValue() && it.DbColumnName.ToLower()==columnName.ToLower());
|
||||
.Any(it => it.ColumnDescription.HasValue() && it.DbColumnName.EqualCase(columnName));
|
||||
return isAny;
|
||||
}
|
||||
public override bool AddColumnRemark(string columnName, string tableName, string description)
|
||||
@ -299,6 +325,12 @@ namespace SqlSugar.MySqlConnector
|
||||
/// <returns></returns>
|
||||
public override bool CreateDatabase(string databaseName, string databaseDirectory = null)
|
||||
{
|
||||
|
||||
if (this.Context.Ado.IsValidConnection())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (databaseDirectory != null)
|
||||
{
|
||||
if (!FileHelper.IsExistDirectory(databaseDirectory))
|
||||
@ -308,7 +340,9 @@ namespace SqlSugar.MySqlConnector
|
||||
}
|
||||
var oldDatabaseName = this.Context.Ado.Connection.Database;
|
||||
var connection = this.Context.CurrentConnectionConfig.ConnectionString;
|
||||
Check.Exception(Regex.Split(connection,oldDatabaseName).Length > 2, "The user name and password cannot be the same as the database name ");
|
||||
Check.ExceptionEasy(Regex.Split(connection,oldDatabaseName).Length > 2
|
||||
, "The user name and password cannot be the same as the database name ",
|
||||
" 创建数据库失败, 请换一个库名,库名不能 password 或者 username 有重叠 ");
|
||||
connection = connection.Replace(oldDatabaseName, "mysql");
|
||||
var newDb = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
@ -384,6 +418,10 @@ namespace SqlSugar.MySqlConnector
|
||||
string primaryKey = null;
|
||||
string identity = item.IsIdentity ? this.CreateTableIdentity : null;
|
||||
string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity);
|
||||
if (!string.IsNullOrEmpty(item.ColumnDescription))
|
||||
{
|
||||
addItem += " COMMENT '"+item.ColumnDescription.ToSqlFilter()+"' ";
|
||||
}
|
||||
columnArray.Add(addItem);
|
||||
}
|
||||
string tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray));
|
||||
@ -461,9 +499,9 @@ namespace SqlSugar.MySqlConnector
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
else if (defaultValue=="0"|| defaultValue == "1")
|
||||
else if (defaultValue == "0" || defaultValue == "1")
|
||||
{
|
||||
string sql = string.Format(AddDefaultValueSql.Replace("'",""), tableName, columnName, defaultValue);
|
||||
string sql = string.Format(AddDefaultValueSql.Replace("'", ""), tableName, columnName, defaultValue);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
|
@ -131,7 +131,7 @@ namespace SqlSugar.MySqlConnector
|
||||
{
|
||||
if (this.Chara == null)
|
||||
{
|
||||
return "UTF8";
|
||||
return "utf8mb4";
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -170,12 +170,23 @@ namespace SqlSugar.MySqlConnector
|
||||
}
|
||||
else if (colum.DataType == typeof(bool))
|
||||
{
|
||||
sb.Append(row[colum].ObjToBool() ? 1 : 0);
|
||||
if (row[colum] == DBNull.Value)
|
||||
{
|
||||
sb.Append("NULL");
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(row[colum].ObjToBool() ? 1 : 0);
|
||||
}
|
||||
}
|
||||
else if (colum.DataType == UtilConstants.DateType&& row[colum] != null && row[colum] != DBNull.Value)
|
||||
{
|
||||
sb.Append(row[colum].ObjToDate().ToString("yyyy-MM-dd HH:mm:ss.fff"));
|
||||
}
|
||||
else if (row[colum] == null || row[colum] == DBNull.Value)
|
||||
{
|
||||
sb.Append("NULL");
|
||||
}
|
||||
else sb.Append(row[colum].ToString());
|
||||
}
|
||||
sb.AppendLine();
|
||||
|
@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar.MySqlConnector
|
||||
{
|
||||
public class MySqlExpressionContext : ExpressionContext, ILambdaExpressions
|
||||
@ -66,12 +69,16 @@ namespace SqlSugar.MySqlConnector
|
||||
var parameter2 = model.Args[1];
|
||||
return string.Format(" (TIMESTAMPDIFF(day,date({0}),date({1}))=0) ", parameter.MemberName, parameter2.MemberName); ;
|
||||
}
|
||||
|
||||
|
||||
public override string DateIsSameByType(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var parameter2 = model.Args[1];
|
||||
var parameter3 = model.Args[2];
|
||||
if (parameter3.MemberValue.ObjToString() == DateType.Weekday.ObjToString())
|
||||
{
|
||||
parameter3.MemberValue = "Week";
|
||||
}
|
||||
return string.Format(" (TIMESTAMPDIFF({2},{0},{1})=0) ", parameter.MemberName, parameter2.MemberName, parameter3.MemberValue);
|
||||
}
|
||||
|
||||
@ -150,7 +157,14 @@ namespace SqlSugar.MySqlConnector
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var parameter1 = model.Args[1];
|
||||
return string.Format("IFNULL({0},{1})", parameter.MemberName, parameter1.MemberName);
|
||||
if (parameter1.MemberValue is bool)
|
||||
{
|
||||
return string.Format("IFNULL(CAST({0} as SIGNED),{1})", parameter.MemberName, parameter1.MemberName);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format("IFNULL({0},{1})", parameter.MemberName, parameter1.MemberName);
|
||||
}
|
||||
}
|
||||
public override string GetDate()
|
||||
{
|
||||
@ -162,9 +176,69 @@ namespace SqlSugar.MySqlConnector
|
||||
return "rand()";
|
||||
}
|
||||
|
||||
public override string Collate(MethodCallExpressionModel model)
|
||||
{
|
||||
var name = model.Args[0].MemberName;
|
||||
return $" binary {name} ";
|
||||
}
|
||||
|
||||
public override string CharIndex(MethodCallExpressionModel model)
|
||||
{
|
||||
return string.Format("instr ({0},{1})", model.Args[0].MemberName, model.Args[1].MemberName);
|
||||
}
|
||||
|
||||
public override string JsonField(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var parameter1 = model.Args[1];
|
||||
//var parameter2 = model.Args[2];
|
||||
//var parameter3= model.Args[3];
|
||||
var result = GetJson(parameter.MemberName, parameter1.MemberValue, model.Args.Count() == 2);
|
||||
if (model.Args.Count > 2)
|
||||
{
|
||||
result = GetJson(result, model.Args[2].MemberValue, model.Args.Count() == 3);
|
||||
}
|
||||
if (model.Args.Count > 3)
|
||||
{
|
||||
result = GetJson(result, model.Args[3].MemberValue, model.Args.Count() == 4);
|
||||
}
|
||||
if (model.Args.Count > 4)
|
||||
{
|
||||
result = GetJson(result, model.Args[4].MemberValue, model.Args.Count() == 5);
|
||||
}
|
||||
if (model.Args.Count > 5)
|
||||
{
|
||||
result = GetJson(result, model.Args[5].MemberValue, model.Args.Count() == 6);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private string GetJson(object memberName1, object memberName2, bool isLast)
|
||||
{
|
||||
return $"{memberName1}->\"$.{memberName2}\"";
|
||||
}
|
||||
|
||||
public override string JsonArrayAny(MethodCallExpressionModel model)
|
||||
{
|
||||
if (SqlSugar.UtilMethods.IsNumber(model.Args[1].MemberValue.GetType().Name))
|
||||
{
|
||||
return $"JSON_CONTAINS({model.Args[0].MemberName}, '{model.Args[1].MemberValue}')";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"JSON_CONTAINS({model.Args[0].MemberName}, '\"{model.Args[1].MemberValue.ObjToStringNoTrim().ToSqlFilter()}\"')";
|
||||
}
|
||||
}
|
||||
public override string JsonListObjectAny(MethodCallExpressionModel model)
|
||||
{
|
||||
if (SqlSugar.UtilMethods.IsNumber(model.Args[2].MemberValue.GetType().Name))
|
||||
{
|
||||
return $"JSON_CONTAINS({model.Args[0].MemberName},'{{\"{model.Args[1].MemberValue}\":{model.Args[2].MemberValue}}}')";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"JSON_CONTAINS({model.Args[0].MemberName},'{{\"{model.Args[1].MemberValue}\":\"{model.Args[2].MemberValue.ObjToStringNoTrim().ToSqlFilter()}\"}}')";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ namespace SqlSugar.MySqlConnector
|
||||
public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
|
||||
{
|
||||
|
||||
var dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "failFiles");
|
||||
var dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bulkcopyfiles");
|
||||
DirectoryInfo dir = new DirectoryInfo(dllPath);
|
||||
if (!dir.Exists)
|
||||
{
|
||||
@ -34,7 +34,7 @@ namespace SqlSugar.MySqlConnector
|
||||
// IsolationLevel.Parse
|
||||
MySqlBulkLoader bulk = new MySqlBulkLoader(conn)
|
||||
{
|
||||
CharacterSet = "UTF8",
|
||||
CharacterSet = "utf8mb4",
|
||||
FieldTerminator = ",",
|
||||
FieldQuotationCharacter = '"',
|
||||
EscapeCharacter = '"',
|
||||
@ -62,9 +62,14 @@ namespace SqlSugar.MySqlConnector
|
||||
{
|
||||
Check.ExceptionEasy("connection string add : AllowLoadLocalInfile=true", "BulkCopy MySql连接字符串需要添加 AllowLoadLocalInfile=true; 添加后如果还不行Mysql数据库执行一下 SET GLOBAL local_infile=1 ");
|
||||
}
|
||||
else
|
||||
else if (ex.Message == "Loading local data is disabled; this must be enabled on both the client and server sides")
|
||||
{
|
||||
throw ex;
|
||||
this.Context.Ado.ExecuteCommand("SET GLOBAL local_infile=1");
|
||||
Check.ExceptionEasy(ex.Message, " 检测到你没有开启文件,已自动执行 SET GLOBAL local_infile=1 在试一次");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
@ -44,12 +44,11 @@ namespace SqlSugar.MySqlConnector
|
||||
var type = UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
|
||||
{
|
||||
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
|
||||
}
|
||||
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
|
||||
return GetDateTimeString(value);
|
||||
}
|
||||
else if (value is DateTimeOffset)
|
||||
{
|
||||
return GetDateTimeOffsetString(value);
|
||||
}
|
||||
else if (type == UtilConstants.ByteArrayType)
|
||||
{
|
||||
@ -74,7 +73,7 @@ namespace SqlSugar.MySqlConnector
|
||||
else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
|
||||
{
|
||||
++i;
|
||||
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
|
||||
var parameterName = this.Builder.SqlParameterKeyWord + name +"_"+ i;
|
||||
this.Parameters.Add(new SugarParameter(parameterName, value));
|
||||
return parameterName;
|
||||
}
|
||||
@ -84,6 +83,27 @@ namespace SqlSugar.MySqlConnector
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private object GetDateTimeOffsetString(object value)
|
||||
{
|
||||
var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
|
||||
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
|
||||
{
|
||||
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
|
||||
}
|
||||
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
|
||||
}
|
||||
|
||||
private object GetDateTimeString(object value)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
|
||||
{
|
||||
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
|
||||
}
|
||||
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
|
||||
}
|
||||
|
||||
private string GetString(object value)
|
||||
{
|
||||
var result = value.ToString();
|
||||
|
@ -53,6 +53,10 @@ namespace SqlSugar.MySqlConnector
|
||||
}
|
||||
else if (Skip != null && Take != null)
|
||||
{
|
||||
if (Skip == 0 && Take == 1 && this.OrderByValue == "ORDER BY NOW() ")
|
||||
{
|
||||
this.OrderByValue = null;
|
||||
}
|
||||
if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
|
||||
result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() : 0, Take);
|
||||
}
|
||||
@ -62,6 +66,14 @@ namespace SqlSugar.MySqlConnector
|
||||
}
|
||||
this.OrderByValue = oldOrderValue;
|
||||
result = GetSqlQuerySql(result);
|
||||
if (result.IndexOf("-- No table") > 0)
|
||||
{
|
||||
return "-- No table";
|
||||
}
|
||||
if (TranLock != null)
|
||||
{
|
||||
result = result + TranLock;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private string ToCountSqlString()
|
||||
@ -103,7 +115,7 @@ namespace SqlSugar.MySqlConnector
|
||||
}
|
||||
public override string ToCountSql(string sql)
|
||||
{
|
||||
if (this.GroupByValue.HasValue())
|
||||
if (this.GroupByValue.HasValue()||this.IsDistinct)
|
||||
{
|
||||
return base.ToCountSql(sql);
|
||||
}
|
||||
@ -136,6 +148,10 @@ namespace SqlSugar.MySqlConnector
|
||||
{
|
||||
result = " DISTINCT " + result;
|
||||
}
|
||||
if (this.SubToListParameters != null && this.SubToListParameters.Any())
|
||||
{
|
||||
result = SubToListMethod(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -106,12 +106,11 @@ namespace SqlSugar.MySqlConnector
|
||||
var type = UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
|
||||
{
|
||||
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
|
||||
}
|
||||
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
|
||||
return GetDateTimeString(value);
|
||||
}
|
||||
else if (value is DateTimeOffset)
|
||||
{
|
||||
return GetDateTimeOffsetString(value);
|
||||
}
|
||||
else if (type == UtilConstants.ByteArrayType)
|
||||
{
|
||||
@ -144,7 +143,7 @@ namespace SqlSugar.MySqlConnector
|
||||
else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
|
||||
{
|
||||
++i;
|
||||
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
|
||||
var parameterName = this.Builder.SqlParameterKeyWord + name +"_"+ i;
|
||||
this.Parameters.Add(new SugarParameter(parameterName, value));
|
||||
return parameterName;
|
||||
}
|
||||
@ -154,6 +153,27 @@ namespace SqlSugar.MySqlConnector
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private object GetDateTimeString(object value)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
|
||||
{
|
||||
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
|
||||
}
|
||||
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
|
||||
}
|
||||
|
||||
private object GetDateTimeOffsetString(object value)
|
||||
{
|
||||
var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
|
||||
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
|
||||
{
|
||||
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
|
||||
}
|
||||
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
|
||||
}
|
||||
|
||||
private string GetString(object value)
|
||||
{
|
||||
var result = value.ToString();
|
||||
|
@ -9,6 +9,22 @@ namespace SqlSugar.MySqlConnector
|
||||
/// </summary>
|
||||
public static class UtilExtensions
|
||||
{
|
||||
public static bool EqualCase(this string thisValue, string equalValue)
|
||||
{
|
||||
if (thisValue != null && equalValue != null)
|
||||
{
|
||||
return thisValue.ToLower() == equalValue.ToLower();
|
||||
}
|
||||
else
|
||||
{
|
||||
return thisValue == equalValue;
|
||||
}
|
||||
}
|
||||
public static string ObjToStringNoTrim(this object thisValue)
|
||||
{
|
||||
if (thisValue != null) return thisValue.ToString();
|
||||
return "";
|
||||
}
|
||||
public static int ObjToInt(this object thisValue)
|
||||
{
|
||||
int reval = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user