From e3ebb4db9c6b2ac30ee34d580a3a67f1bd8000d1 Mon Sep 17 00:00:00 2001 From: Liubo Date: Sat, 27 May 2017 16:44:48 +0800 Subject: [PATCH] Implement MySql Realization --- OrmTest/OrmTest.csproj.user | 2 +- .../SqlBuilder/DMLBuilder/QueryBuilder.cs | 4 +- .../MySql/Core/DbBind/MySqlDbBind.cs | 95 ++++++ .../MySql/Core/SqlBuilder/MySqlBuilder.cs | 43 +++ .../Core/SqlBuilder/MySqlDeleteBuilder.cs | 7 + .../Core/SqlBuilder/MySqlExpressionContext.cs | 16 + .../Core/SqlBuilder/MySqlInsertBuilder.cs | 6 + .../Core/SqlBuilder/MySqlQueryBuilder.cs | 282 ++++++++++++++++++ .../Core/SqlBuilder/MySqlUpdateBuilder.cs | 6 + .../MySql/Queryable/MySqlQueryable.cs | 19 ++ SqlSugar/Realization/SqlServer/DbType.cs | 8 +- .../Realization/SqlServer/MySqlProvider.cs | 94 ++++++ SqlSugar/SqlSugar.csproj | 17 +- SqlSugar/app.config | 9 + SqlSugar/packages.config | 4 + 15 files changed, 602 insertions(+), 10 deletions(-) create mode 100644 SqlSugar/Realization/MySql/Core/DbBind/MySqlDbBind.cs create mode 100644 SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlBuilder.cs create mode 100644 SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlDeleteBuilder.cs create mode 100644 SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlExpressionContext.cs create mode 100644 SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlInsertBuilder.cs create mode 100644 SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlQueryBuilder.cs create mode 100644 SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlUpdateBuilder.cs create mode 100644 SqlSugar/Realization/MySql/Queryable/MySqlQueryable.cs create mode 100644 SqlSugar/Realization/SqlServer/MySqlProvider.cs create mode 100644 SqlSugar/app.config create mode 100644 SqlSugar/packages.config diff --git a/OrmTest/OrmTest.csproj.user b/OrmTest/OrmTest.csproj.user index 5283ef199..ca1d04b08 100644 --- a/OrmTest/OrmTest.csproj.user +++ b/OrmTest/OrmTest.csproj.user @@ -1,6 +1,6 @@  - ShowAllFiles + ProjectFiles \ No newline at end of file diff --git a/SqlSugar/Abstract/Core/SqlBuilder/DMLBuilder/QueryBuilder.cs b/SqlSugar/Abstract/Core/SqlBuilder/DMLBuilder/QueryBuilder.cs index d5e433d07..d78f3229b 100644 --- a/SqlSugar/Abstract/Core/SqlBuilder/DMLBuilder/QueryBuilder.cs +++ b/SqlSugar/Abstract/Core/SqlBuilder/DMLBuilder/QueryBuilder.cs @@ -17,10 +17,10 @@ namespace SqlSugar } #region Private Fileds - private List _JoinQueryInfos; + protected List _JoinQueryInfos; private List _WhereInfos; private string _HavingInfos; - private string _TableNameString; + protected string _TableNameString; #endregion #region Service object diff --git a/SqlSugar/Realization/MySql/Core/DbBind/MySqlDbBind.cs b/SqlSugar/Realization/MySql/Core/DbBind/MySqlDbBind.cs new file mode 100644 index 000000000..eeba74c7b --- /dev/null +++ b/SqlSugar/Realization/MySql/Core/DbBind/MySqlDbBind.cs @@ -0,0 +1,95 @@ +namespace SqlSugar +{ + public class MySqlDbBind : DbBindProvider + { + public override string ChangeDBTypeToCSharpType(string typeName) + { + string reval; + switch (typeName.ToLower()) + { + case "int": + reval = "int"; + break; + case "text": + reval = "string"; + break; + case "bigint": + reval = "long"; + break; + case "binary": + reval = "object"; + break; + case "bit": + reval = "bool"; + break; + case "char": + reval = "string"; + break; + case "datetime": + case "date": + case "datetime2": + reval = "dateTime"; + break; + case "single": + case "decimal": + reval = "decimal"; + break; + case "float": + reval = "double"; + break; + case "image": + reval = "byte[]"; + break; + case "money": + reval = "decimal"; + break; + case "nchar": + reval = "string"; + break; + case "ntext": + reval = "string"; + break; + case "numeric": + reval = "decimal"; + break; + case "nvarchar": + reval = "string"; + break; + case "real": + reval = "float"; + break; + case "smalldatetime": + reval = "dateTime"; + break; + case "smallint": + reval = "short"; + break; + case "smallmoney": + reval = "decimal"; + break; + case "timestamp": + reval = "dateTime"; + break; + case "tinyint": + reval = "byte"; + break; + case "uniqueidentifier": + reval = "guid"; + break; + case "varbinary": + reval = "byte[]"; + break; + case "varchar": + reval = "string"; + break; + case "Variant": + reval = "object"; + break; + default: + reval = "other"; + break; + } + return reval; + } + } +} diff --git a/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlBuilder.cs b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlBuilder.cs new file mode 100644 index 000000000..ec16a9e66 --- /dev/null +++ b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlBuilder.cs @@ -0,0 +1,43 @@ +using System; +using System.Linq; +using System.Text.RegularExpressions; + +namespace SqlSugar +{ + public class MySqlBuilder : SqlBuilderProvider + { + public override string SqlParameterKeyWord { get { return "@"; } } + + public override string GetTranslationTableName(string name) + { + Check.ArgumentNullException(name, string.Format(ErrorMessage.ObjNotExist, "Table Name")); + var context = this.Context; + var mappingInfo = context + .MappingTables + .FirstOrDefault(it => it.EntityName.Equals(name, StringComparison.CurrentCultureIgnoreCase)); + return "`" + (mappingInfo == null ? name : mappingInfo.DbTableName) + "`"; + } + public override string GetTranslationColumnName(string entityName, string propertyName) + { + Check.ArgumentNullException(entityName, string.Format(ErrorMessage.ObjNotExist, "Table Name")); + Check.ArgumentNullException(propertyName, string.Format(ErrorMessage.ObjNotExist, "Column Name")); + var context = this.Context; + var mappingInfo = context + .MappingColumns + .FirstOrDefault(it => + it.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase) && + it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase)); + return (mappingInfo == null ? "`" + propertyName + "`" : "`" + mappingInfo.DbColumnName + "`"); + } + + public override string GetTranslationColumnName(string propertyName) + { + return "`" + propertyName + "`"; + } + + public override string GetNoTranslationColumnName(string name) + { + return name == null ? string.Empty : Regex.Match(name, @"\`(.*?)\`").Groups[1].Value; + } + } +} diff --git a/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlDeleteBuilder.cs b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlDeleteBuilder.cs new file mode 100644 index 000000000..97189bcab --- /dev/null +++ b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlDeleteBuilder.cs @@ -0,0 +1,7 @@ +namespace SqlSugar +{ + public class MySqlDeleteBuilder : DeleteBuilder + { + + } +} diff --git a/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlExpressionContext.cs b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlExpressionContext.cs new file mode 100644 index 000000000..db0004749 --- /dev/null +++ b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlExpressionContext.cs @@ -0,0 +1,16 @@ +namespace SqlSugar +{ + public class MySqlExpressionContext : ExpressionContext, ILambdaExpressions + { + public SqlSugarClient Context { get; set; } + public MySqlExpressionContext() + { + base.DbMehtods = new MySqlMethod(); + } + + } + public class MySqlMethod : DefaultDbMethod, IDbMethods + { + + } +} diff --git a/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlInsertBuilder.cs b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlInsertBuilder.cs new file mode 100644 index 000000000..aea9adaa9 --- /dev/null +++ b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlInsertBuilder.cs @@ -0,0 +1,6 @@ +namespace SqlSugar +{ + public class MySqlInsertBuilder : InsertBuilder + { + } +} diff --git a/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlQueryBuilder.cs b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlQueryBuilder.cs new file mode 100644 index 000000000..90a7a2b49 --- /dev/null +++ b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlQueryBuilder.cs @@ -0,0 +1,282 @@ +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Text.RegularExpressions; + +namespace SqlSugar +{ + public partial class MySqlQueryBuilder : QueryBuilder + { + #region Sql Template + public override string SqlTemplate + { + get + { + return "SELECT {0} FROM {1}{2}{3}{4} "; + } + } + public override string JoinTemplate + { + get + { + return "{0}JOIN {1}{2}ON {3} "; + } + } + public override string PageTempalte + { + get + { + /* + @"WITH PageTable AS( + {0} + ) + SELECT * FROM (SELECT *,ROW_NUMBER() OVER({1}) AS RowIndex FROM PageTable ) T WHERE RowIndex BETWEEN {2} AND {3}" + */ + var template = "SELECT {0} FROM {1} {2} {3} {4} LIMIT {5},{6}"; + return template; + } + } + public override string DefaultOrderByTemplate + { + get + { + return "ORDER BY NOW() "; + } + } + public override string OrderByTemplate + { + get + { + return "ORDER BY "; + } + } + public override string GroupByTemplate + { + get + { + return "GROUP BY "; + } + } + + public override string MaxTemplate + { + get + { + return "MAX({0})"; + } + } + public override string MinTemplate + { + get + { + return "MIN({0})"; + } + } + public override string SumTemplate + { + get + { + return "SUM({0})"; + } + } + public override string AvgTemplate + { + get + { + return "AVG({0})"; + } + } + public override string InTemplate + { + get + { + return "{0} IN ({1}) "; + } + } + #endregion + + #region Common Methods + public override bool IsSingle() + { + var isSingle = Builder.QueryBuilder.JoinQueryInfos.IsNullOrEmpty(); + return isSingle; + } + public override ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType) + { + ILambdaExpressions resolveExpress = this.LambdaExpressions; + this.LambdaExpressions.Clear(); + resolveExpress.JoinQueryInfos = Builder.QueryBuilder.JoinQueryInfos; + resolveExpress.MappingColumns = Context.MappingColumns; + resolveExpress.MappingTables = Context.MappingTables; + resolveExpress.IgnoreComumnList = Context.IgnoreColumns; + resolveExpress.Resolve(expression, resolveType); + this.Parameters = new List(); + this.Parameters.AddRange(resolveExpress.Parameters); + var reval = resolveExpress.Result; + return reval; + } + public override string ToSqlString() + { + sql = new StringBuilder(); + sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString); + if (IsCount) { return sql.ToString(); } + if (Skip != null && Take == null) + { + if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0]; + return string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString, Skip.ObjToInt() + 1, long.MaxValue); + } + else if (Skip == null && Take != null) + { + if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0]; + return string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, 1, Take.ObjToInt()); + } + else if (Skip != null && Take != null) + { + if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0]; + return string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() + 1 : 0, Take); + } + else + { + return sql.ToString(); + } + + } + public override string ToJoinString(JoinQueryInfo joinInfo) + { + return string.Format( + this.JoinTemplate, + joinInfo.JoinType.ToString() + PubConst.Space, + joinInfo.TableName + PubConst.Space, + joinInfo.ShortName + PubConst.Space + joinInfo.TableWithString, + joinInfo.JoinWhere); + } + public override void Clear() + { + this.Skip = 0; + this.Take = 0; + this.sql = null; + this.WhereIndex = 0; + this.Parameters = null; + this.GroupByValue = null; + this.WhereInfos = null; + this._TableNameString = null; + this.JoinQueryInfos = null; + } + public override bool IsComplexModel(string sql) + { + return Regex.IsMatch(sql, @"AS \[\w+\.\w+\]"); + } + + + #endregion + + #region Get SQL Partial + public override string GetSelectValue + { + get + { + if (this.IsCount) return "COUNT(1) AS `Count` "; + string reval = string.Empty; + if (this.SelectValue == null || this.SelectValue is string) + { + reval = GetSelectValueByString(); + } + else + { + reval = GetSelectValueByExpression(); + } + if (this.SelectType == ResolveExpressType.SelectMultiple) + { + this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this._JoinQueryInfos.Select(it => it.TableName)); + } + return reval; + } + } + public override string GetSelectValueByExpression() + { + var expression = this.SelectValue as Expression; + var reval = GetExpressionValue(expression, this.SelectType).GetResultString(); + this.SelectCacheKey = reval; + return reval; + } + public override string GetSelectValueByString() + { + string reval; + if (this.SelectValue.IsNullOrEmpty()) + { + string pre = null; + if (this.JoinQueryInfos.IsValuable() && this.JoinQueryInfos.Any(it => TableShortName.IsValuable())) + { + pre = Builder.GetTranslationColumnName(TableShortName) + "."; + } + reval = string.Join(",", this.Context.EntityProvider.GetEntityInfo(this.EntityType).Columns.Where(it => !it.IsIgnore).Select(it => pre + Builder.GetTranslationColumnName(it.EnitytName, it.PropertyName))); + } + else + { + reval = this.SelectValue.ObjToString(); + this.SelectCacheKey = reval; + } + + return reval; + } + public override string GetWhereValueString + { + get + { + if (this.WhereInfos == null) return null; + else + { + return string.Join(PubConst.Space, this.WhereInfos); + } + } + } + public override string GetJoinValueString + { + get + { + if (this.JoinQueryInfos.IsNullOrEmpty()) return null; + else + { + return string.Join(PubConst.Space, this.JoinQueryInfos.Select(it => this.ToJoinString(it))); + } + } + } + public override string GetTableNameString + { + get + { + var result = Builder.GetTranslationTableName(EntityName); + result += PubConst.Space; + if (this.TableWithString.IsValuable()) + { + result += TableWithString + PubConst.Space; + } + if (this.TableShortName.IsValuable()) + { + result += (TableShortName + PubConst.Space); + } + if (!this.IsSingle()) + { + result += GetJoinValueString + PubConst.Space; + } + return result; + } + } + public override string GetOrderByString + { + get + { + return this.OrderByValue; + } + } + public override string GetGroupByString + { + get + { + return this.GroupByValue; + } + } + #endregion + } +} diff --git a/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlUpdateBuilder.cs b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlUpdateBuilder.cs new file mode 100644 index 000000000..a886c55f9 --- /dev/null +++ b/SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlUpdateBuilder.cs @@ -0,0 +1,6 @@ +namespace SqlSugar +{ + public class MySqlUpdateBuilder : UpdateBuilder + { + } +} diff --git a/SqlSugar/Realization/MySql/Queryable/MySqlQueryable.cs b/SqlSugar/Realization/MySql/Queryable/MySqlQueryable.cs new file mode 100644 index 000000000..bf2b079f8 --- /dev/null +++ b/SqlSugar/Realization/MySql/Queryable/MySqlQueryable.cs @@ -0,0 +1,19 @@ +namespace SqlSugar +{ + public class MySqlQueryable:QueryableProvider + { + + } + public class MySqlQueryable : QueryableProvider + { + + } + public class MySqlQueryable : QueryableProvider + { + + } + public class MySqlQueryable : QueryableProvider + { + + } +} diff --git a/SqlSugar/Realization/SqlServer/DbType.cs b/SqlSugar/Realization/SqlServer/DbType.cs index c9e1b438d..25b9a6349 100644 --- a/SqlSugar/Realization/SqlServer/DbType.cs +++ b/SqlSugar/Realization/SqlServer/DbType.cs @@ -1,12 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace SqlSugar +namespace SqlSugar { public partial class DbType { public const string SqlServer = "SqlServer"; + public const string MySql = "MySql"; } } diff --git a/SqlSugar/Realization/SqlServer/MySqlProvider.cs b/SqlSugar/Realization/SqlServer/MySqlProvider.cs new file mode 100644 index 000000000..25f4e6182 --- /dev/null +++ b/SqlSugar/Realization/SqlServer/MySqlProvider.cs @@ -0,0 +1,94 @@ +using System; +using System.Data; +using System.Data.SqlClient; +using MySql.Data.MySqlClient; + +namespace SqlSugar +{ + public class MySqlProvider : AdoProvider + { + public MySqlProvider() {} + public override IDbConnection Connection + { + get + { + if (base._DbConnection == null) + { + base._DbConnection = new MySqlConnection(base.Context.CurrentConnectionConfig.ConnectionString); + } + return base._DbConnection; + } + set + { + base._DbConnection = value; + } + } + /// + /// Only SqlServer + /// + /// + public override void BeginTran(string transactionName) + { + throw new NotImplementedException(); + } + /// + /// Only SqlServer + /// + /// + /// + public override void BeginTran(IsolationLevel iso, string transactionName) + { + throw new NotImplementedException(); + } + public override IDataAdapter GetAdapter() + { + return new MySqlDataAdapter(); + } + public override IDbCommand GetCommand(string sql, SugarParameter[] pars) + { + MySqlCommand sqlCommand = new MySqlCommand(sql, (MySqlConnection)this.Connection); + sqlCommand.CommandType = this.CommandType; + sqlCommand.CommandTimeout = this.CommandTimeOut; + if (this.Transaction != null) + { + sqlCommand.Transaction = (MySqlTransaction)this.Transaction; + } + if (pars.IsValuable()) + { + IDataParameter[] ipars= ToIDbDataParameter(pars); + sqlCommand.Parameters.AddRange((SqlParameter[])ipars); + } + CheckConnection(); + return sqlCommand; + } + public override void SetCommandToAdapter(IDataAdapter dataAdapter, IDbCommand command) + { + ((MySqlDataAdapter)dataAdapter).SelectCommand = (MySqlCommand)command; + } + /// + /// if mysql return MySqlParameter[] pars + /// if sqlerver return SqlParameter[] pars ... + /// + /// + /// + public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] pars) + { + if (pars == null || pars.Length == 0) return null; + SqlParameter[] reval = new SqlParameter[pars.Length]; + int i = 0; + foreach (var par in pars) + { + if (par.Value == null) par.Value = DBNull.Value; + var p = new SqlParameter(); + p.ParameterName = par.ParameterName; + p.UdtTypeName = par.UdtTypeName; + p.Size = par.Size; + p.Value = par.Value; + p.DbType = par.DbType; + reval[i] =p; + ++i; + } + return reval; + } + } +} diff --git a/SqlSugar/SqlSugar.csproj b/SqlSugar/SqlSugar.csproj index a6c243cdb..9c37cc2a0 100644 --- a/SqlSugar/SqlSugar.csproj +++ b/SqlSugar/SqlSugar.csproj @@ -31,6 +31,9 @@ 4 + + ..\packages\MySql.Data.6.9.9\lib\net40\MySql.Data.dll + False Lib\Newtonsoft.Json.dll @@ -59,6 +62,14 @@ + + + + + + + + @@ -92,6 +103,7 @@ + @@ -165,10 +177,13 @@ - + + + +