Skip to content

Commit

Permalink
🎁完善数据库兼容性
Browse files Browse the repository at this point in the history
  • Loading branch information
softwaiter committed Oct 15, 2023
1 parent 6da5ac4 commit 0e272b9
Show file tree
Hide file tree
Showing 19 changed files with 589 additions and 213 deletions.
12 changes: 10 additions & 2 deletions Source/Action/CommandUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CodeM.Common.DbHelper;
using CodeM.Common.Orm.SQL.Dialect;
using CodeM.Common.Tools;
using CodeM.Common.Tools.DynamicObject;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -190,11 +191,11 @@ public static DbType Type2DbType(Type type)
}
else if (type == typeof(UInt16))
{
return DbType.Int32;
return DbType.Int16;
}
else if (type == typeof(UInt32))
{
return DbType.Int64;
return DbType.Int32;
}
else if (type == typeof(UInt64))
{
Expand All @@ -209,6 +210,13 @@ public static DbType Type2DbType(Type type)
#endregion


public static string GetObjectIdentifier(params string[] items)
{
string origin = string.Join("-", items);
string result = Xmtool.Hash().MD5(origin);
return result.ToUpper();
}

/// <summary>
/// 将所有跨模型引用加入关联列表中,最终将转换为join语句
/// </summary>
Expand Down
187 changes: 123 additions & 64 deletions Source/Action/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,8 @@ public Model AscendingSort(string name)

if (mGetValues.Exists(item => item.FieldName == name))
{
mSorts.Add(string.Concat(quotes[0], name, quotes[1], " ASC"));
string sortSql = Features.GetAscSortSql(this, string.Concat(quotes[0], name, quotes[1]));
mSorts.Add(sortSql);
}
else
{
Expand All @@ -632,7 +633,9 @@ public Model AscendingSort(string name)
{
throw new Exception(string.Concat("未找到属性:", name));
}
mSorts.Add(string.Concat(quotes[0], p.Owner.Table, quotes[1], ".", quotes[0], p.Field, quotes[1], " ASC"));

string sortSql = Features.GetAscSortSql(this, string.Concat(quotes[0], p.Owner.Table, quotes[1], ".", quotes[0], p.Field, quotes[1]));
mSorts.Add(sortSql);
}
else
{
Expand All @@ -647,7 +650,8 @@ public Model AscendingSort(string name)
if (i == subNames.Length - 2)
{
Property lastProp = subM.GetProperty(subNames[i + 1]);
mSorts.Add(string.Concat(quotes[0], subM.Table, quotes[1], ".", quotes[0], lastProp.Field, quotes[1], " ASC"));
string sortSql = Features.GetAscSortSql(this, string.Concat(quotes[0], subM.Table, quotes[1], ".", quotes[0], lastProp.Field, quotes[1]));
mSorts.Add(sortSql);
break;
}
}
Expand All @@ -664,7 +668,8 @@ public Model DescendingSort(string name)

if (mGetValues.Exists(item => item.FieldName == name))
{
mSorts.Add(string.Concat(quotes[0], name, quotes[1], " DESC"));
string sortSql = Features.GetDescSortSql(this, string.Concat(quotes[0], name, quotes[1]));
mSorts.Add(sortSql);
}
else
{
Expand All @@ -676,7 +681,9 @@ public Model DescendingSort(string name)
{
throw new Exception(string.Concat("未找到属性:", name));
}
mSorts.Add(string.Concat(quotes[0], p.Owner.Table, quotes[1], ".", quotes[0], p.Field, quotes[1], " DESC"));

string sortSql = Features.GetDescSortSql(this, string.Concat(quotes[0], p.Owner.Table, quotes[1], ".", quotes[0], p.Field, quotes[1]));
mSorts.Add(sortSql);
}
else
{
Expand All @@ -691,7 +698,8 @@ public Model DescendingSort(string name)
if (i == subNames.Length - 2)
{
Property lastProp = subM.GetProperty(subNames[i + 1]);
mSorts.Add(string.Concat(quotes[0], subM.Table, quotes[1], ".", quotes[0], lastProp.Field, quotes[1], " DESC"));
string sortSql = Features.GetDescSortSql(this, string.Concat(quotes[0], subM.Table, quotes[1], ".", quotes[0], lastProp.Field, quotes[1]));
mSorts.Add(sortSql);
break;
}
}
Expand Down Expand Up @@ -805,40 +813,62 @@ public Model GroupBy(Function function)
#region ICommand
public void CreateTable(bool replace = false)
{
if (!replace && TableExists())
{
throw new Exception(String.Concat("", this.Table, " 已存在。"));
}

StringBuilder sb = new StringBuilder(ToString());
if (replace)
{
RemoveTable();
}

Derd.PrintSQL(sb.ToString());
CommandUtils.ExecuteNonQuery(this, Path.ToLower(), sb.ToString());
DbTransaction transaction = DbUtils.GetTransaction(Path.ToLower());

string tableIndexSQL = ToString(true);
if (!string.IsNullOrWhiteSpace(tableIndexSQL))
{
Derd.PrintSQL(tableIndexSQL);
CommandUtils.ExecuteNonQuery(this, Path.ToLower(), tableIndexSQL);
try
{
Derd.PrintSQL(sb.ToString());
CommandUtils.ExecuteNonQuery(this, transaction, sb.ToString());

string tableIndexSQL = ToString(true);
if (!string.IsNullOrWhiteSpace(tableIndexSQL))
{
Derd.PrintSQL(tableIndexSQL);
CommandUtils.ExecuteNonQuery(this, transaction, tableIndexSQL);
}

if (Features.IsSupportAutoIncrement(this))
{
for (int i = 0; i < PropertyCount; i++)
{
Property p = GetProperty(i);
if (p.AutoIncrement)
{
string identifierArgs = string.Concat(Table, ",", p.Field);
string objName = CommandUtils.GetObjectIdentifier(identifierArgs.Split(","));
if (objName.Length > 26)
{
objName = objName.Substring(0, 26);
}
string[] aiCmds = Features.GetAutoIncrementExtCommand(this, Table, p.Field, objName);
foreach (string cmd in aiCmds)
{
if (!string.IsNullOrEmpty(cmd))
{
Derd.PrintSQL(cmd);
DbUtils.ExecuteNonQuery(transaction, cmd);
}
}
}
}
}
}

if (Features.IsSupportAutoIncrement(this))
catch (Exception exp)
{
for (int i = 0; i < PropertyCount; i++)
{
Property p = GetProperty(i);
if (p.AutoIncrement)
{
string[] aiCmds = Features.GetAutoIncrementExtCommand(this, Table, p.Field);
foreach (string cmd in aiCmds)
{
if (!string.IsNullOrEmpty(cmd))
{
Derd.PrintSQL(cmd);
DbUtils.ExecuteNonQuery(Path.ToLower(), cmd);
}
}
}
}
DbUtils.RollbackTransaction(transaction);

throw exp;
}
}

Expand All @@ -858,53 +888,54 @@ public bool TryCreateTable(bool replace = false)

public void RemoveTable(bool throwError = false)
{
bool tableRemoved = false;
try
{
DbTransaction transaction = DbUtils.GetTransaction(Path.ToLower());
try
{
bool tableRemoved = false;
if (this.TableExists())
{
string[] quotes = Features.GetObjectQuotes(this);
string sql = string.Concat("DROP TABLE ", quotes[0], Table, quotes[1]);
Derd.PrintSQL(sql);
CommandUtils.ExecuteNonQuery(this, Path.ToLower(), sql);
Derd.PrintSQL(sql);
CommandUtils.ExecuteNonQuery(this, transaction, sql);
tableRemoved = true;
}
}
catch (Exception exp)
{
if (throwError)
{
throw exp;
}
}

if (tableRemoved)
{
try
}

if (tableRemoved)
{
for (int i = 0; i < PropertyCount; i++)
{
Property p = GetProperty(i);
if (p.AutoIncrement)
{
string[] aiCmds = Features.GetAutoIncrementGCExtCommand(this, Table, p.Field);
string identifierArgs = string.Concat(Table, ",", p.Field);
string objName = CommandUtils.GetObjectIdentifier(identifierArgs.Split(","));
if (objName.Length > 26)
{
objName = objName.Substring(0, 26);
}
string[] aiCmds = Features.GetAutoIncrementGCExtCommand(this, objName);
foreach (string cmd in aiCmds)
{
if (!string.IsNullOrEmpty(cmd))
{
Derd.PrintSQL(cmd);
DbUtils.ExecuteNonQuery(Path.ToLower(), cmd);
DbUtils.ExecuteNonQuery(transaction, cmd);
}
}
}
}
}
catch (Exception exp)
}

DbUtils.CommitTransaction(transaction);
}
catch (Exception exp)
{
DbUtils.RollbackTransaction(transaction);

if (throwError)
{
if (throwError)
{
throw exp;
}
throw exp;
}
}
}
Expand Down Expand Up @@ -1492,8 +1523,6 @@ public bool Delete(int? transCode, bool deleteAll = false)
" FROM (SELECT ", quotes[0], this.Table, quotes[1], ".", quotes[0], pp.Field, quotes[1],
" FROM ", quotes[0], this.Table, quotes[1], joinSql, " WHERE ", where.SQL, ") ", tempTable);

//string subSql = string.Concat("SELECT ", quotes[0], this.Table, quotes[1], ".", quotes[0], pp.Field, quotes[1], " FROM ", quotes[0], this.Table, quotes[1], joinSql, " WHERE ", where.SQL);

sql += String.Concat(" WHERE ", quotes[0], this.Table, quotes[1], ".", quotes[0], pp.Field, quotes[1], " IN (", subSql, ")");
}
else if (!string.IsNullOrWhiteSpace(where.SQL))
Expand Down Expand Up @@ -1629,11 +1658,32 @@ private void SetPropertyValueFromDB(dynamic obj, Property prop, string propName,
}
else if (prop.RealType == typeof(double))
{
obj.SetValueByPath(propName, dr.GetDouble(fieldName));
object value = dr.GetValue(fieldName);
if (value.GetType() == typeof(double))
{
obj.SetValueByPath(propName, value);
}
else
{
obj.SetValueByPath(propName, double.Parse("" + value));
}
}
else if (prop.RealType == typeof(bool))
{
obj.SetValueByPath(propName, dr.GetBoolean(fieldName));
object value = dr.GetValue(fieldName);
if (value.GetType() == typeof(bool))
{
obj.SetValueByPath(propName, value);
}
else
{
bool bValue;
if (!bool.TryParse("" + value, out bValue))
{
bValue = !(string.IsNullOrWhiteSpace("" + value) || "" + value == "0");
}
obj.SetValueByPath(propName, bValue);
}
}
else if (prop.RealType == typeof(DateTime))
{
Expand Down Expand Up @@ -1689,7 +1739,6 @@ public List<dynamic> Query(int? transCode = null)
List<dynamic> result = new List<dynamic>();

CommandSQL cmd = SQLBuilder.BuildQuerySQL(this);

Derd.PrintSQL(cmd.SQL, cmd.Params.ToArray());

dynamic mixedValues = MixActionValues(cmd.FilterProperties);
Expand Down Expand Up @@ -1742,8 +1791,18 @@ public List<dynamic> Query(int? transCode = null)
}
else
{
object processedValue = dr.GetValue(sfp.FieldName);
obj.SetValueByPath(sfp.OutputName, processedValue);
if (sfp.Function != null &&
(sfp.Function is COUNT ||
sfp.Function is LENGTH))
{
object processedValue = dr.GetInt32(sfp.FieldName);
obj.SetValueByPath(sfp.OutputName, processedValue);
}
else
{
object processedValue = dr.GetValue(sfp.FieldName);
obj.SetValueByPath(sfp.OutputName, processedValue);
}
}
}
}
Expand Down
Loading

0 comments on commit 0e272b9

Please sign in to comment.