Browse Source

Implement MySql Realization

pull/12/MERGE
Liubo 8 years ago
parent
commit
e3ebb4db9c
  1. 2
      OrmTest/OrmTest.csproj.user
  2. 4
      SqlSugar/Abstract/Core/SqlBuilder/DMLBuilder/QueryBuilder.cs
  3. 95
      SqlSugar/Realization/MySql/Core/DbBind/MySqlDbBind.cs
  4. 43
      SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlBuilder.cs
  5. 7
      SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlDeleteBuilder.cs
  6. 16
      SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlExpressionContext.cs
  7. 6
      SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlInsertBuilder.cs
  8. 282
      SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlQueryBuilder.cs
  9. 6
      SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlUpdateBuilder.cs
  10. 19
      SqlSugar/Realization/MySql/Queryable/MySqlQueryable.cs
  11. 8
      SqlSugar/Realization/SqlServer/DbType.cs
  12. 94
      SqlSugar/Realization/SqlServer/MySqlProvider.cs
  13. 17
      SqlSugar/SqlSugar.csproj
  14. 9
      SqlSugar/app.config
  15. 4
      SqlSugar/packages.config

2
OrmTest/OrmTest.csproj.user

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ShowAllFiles</ProjectView>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>

4
SqlSugar/Abstract/Core/SqlBuilder/DMLBuilder/QueryBuilder.cs

@ -17,10 +17,10 @@ namespace SqlSugar
}
#region Private Fileds
private List<JoinQueryInfo> _JoinQueryInfos;
protected List<JoinQueryInfo> _JoinQueryInfos;
private List<string> _WhereInfos;
private string _HavingInfos;
private string _TableNameString;
protected string _TableNameString;
#endregion
#region Service object

95
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;
}
}
}

43
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;
}
}
}

7
SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlDeleteBuilder.cs

@ -0,0 +1,7 @@
namespace SqlSugar
{
public class MySqlDeleteBuilder : DeleteBuilder
{
}
}

16
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
{
}
}

6
SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlInsertBuilder.cs

@ -0,0 +1,6 @@
namespace SqlSugar
{
public class MySqlInsertBuilder : InsertBuilder
{
}
}

282
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<SugarParameter>();
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
}
}

6
SqlSugar/Realization/MySql/Core/SqlBuilder/MySqlUpdateBuilder.cs

@ -0,0 +1,6 @@
namespace SqlSugar
{
public class MySqlUpdateBuilder : UpdateBuilder
{
}
}

19
SqlSugar/Realization/MySql/Queryable/MySqlQueryable.cs

@ -0,0 +1,19 @@
namespace SqlSugar
{
public class MySqlQueryable<T>:QueryableProvider<T>
{
}
public class MySqlQueryable<T,T2> : QueryableProvider<T,T2>
{
}
public class MySqlQueryable<T, T2,T3> : QueryableProvider<T, T2,T3>
{
}
public class MySqlQueryable<T, T2,T3,T4> : QueryableProvider<T, T2,T3,T4>
{
}
}

8
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";
}
}

94
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;
}
}
/// <summary>
/// Only SqlServer
/// </summary>
/// <param name="transactionName"></param>
public override void BeginTran(string transactionName)
{
throw new NotImplementedException();
}
/// <summary>
/// Only SqlServer
/// </summary>
/// <param name="iso"></param>
/// <param name="transactionName"></param>
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;
}
/// <summary>
/// if mysql return MySqlParameter[] pars
/// if sqlerver return SqlParameter[] pars ...
/// </summary>
/// <param name="pars"></param>
/// <returns></returns>
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;
}
}
}

17
SqlSugar/SqlSugar.csproj

@ -31,6 +31,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<HintPath>..\packages\MySql.Data.6.9.9\lib\net40\MySql.Data.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Lib\Newtonsoft.Json.dll</HintPath>
@ -59,6 +62,14 @@
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateableProvider.cs" />
<Compile Include="Interface\IConnectionConfig.cs" />
<Compile Include="Realization\MySql\Core\DbBind\MySqlDbBind.cs" />
<Compile Include="Realization\MySql\Core\SqlBuilder\MySqlBuilder.cs" />
<Compile Include="Realization\MySql\Core\SqlBuilder\MySqlDeleteBuilder.cs" />
<Compile Include="Realization\MySql\Core\SqlBuilder\MySqlExpressionContext.cs" />
<Compile Include="Realization\MySql\Core\SqlBuilder\MySqlInsertBuilder.cs" />
<Compile Include="Realization\MySql\Core\SqlBuilder\MySqlQueryBuilder.cs" />
<Compile Include="Realization\MySql\Core\SqlBuilder\MySqlUpdateBuilder.cs" />
<Compile Include="Realization\MySql\Queryable\MySqlQueryable.cs" />
<Compile Include="Realization\SqlServer\Core\SqlBuilder\SqlServerDeleteBuilder.cs" />
<Compile Include="Realization\SqlServer\Core\SqlBuilder\SqlServerInsertBuilder.cs" />
<Compile Include="Entities\EntityColumnInfo.cs" />
@ -92,6 +103,7 @@
<Compile Include="Realization\SqlServer\Core\SqlBuilder\SqlServerBuilder.cs" />
<Compile Include="Realization\SqlServer\DbMaintenance\SqlServerDbMaintenance.cs" />
<Compile Include="Realization\SqlServer\Core\SqlBuilder\SqlServerQueryBuilder.cs" />
<Compile Include="Realization\SqlServer\MySqlProvider.cs" />
<Compile Include="Realization\SqlServer\SqlServerProvider.cs" />
<Compile Include="Realization\SqlServer\Queryable\SqlServerQueryable.cs" />
<Compile Include="Entities\SugarDynamic.cs" />
@ -165,10 +177,13 @@
<Content Include="Lib\Newtonsoft.Json.dll" />
</ItemGroup>
<ItemGroup>
<Folder Include="Realization\MySql\" />
<Folder Include="Realization\Oracle\" />
<Folder Include="Realization\Sqlite\" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

9
SqlSugar/app.config

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
</configuration>

4
SqlSugar/packages.config

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MySql.Data" version="6.9.9" targetFramework="net40" />
</packages>
Loading…
Cancel
Save