Skip to content

Commit

Permalink
- 优化 MySql/Sqlite InsertOrUpdate + IfExistsDoNothing 简化 SQL insert ig…
Browse files Browse the repository at this point in the history
…nore into;#1601
  • Loading branch information
2881099 committed Aug 24, 2023
1 parent c6443b4 commit dfb99d1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 55 deletions.
6 changes: 3 additions & 3 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ static void Main(string[] args)
//.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
//.UseQuoteSqlName(false)

//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")

//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")

Expand Down Expand Up @@ -602,8 +602,8 @@ static void Main(string[] args)

var dict = new List<Dictionary<string, object>>();
Dictionary<string, object> d = new Dictionary<string, object>();
d.Add("id", 1);
d.Add("name", "name1");
d.Add("id", 2);
d.Add("name", "name2");
dict.Add(d);
var testx01 = fsql.InsertOrUpdateDict(dict).AsTable("table222zzz").WherePrimary("id").IfExistsDoNothing().ToSql();
fsql.InsertOrUpdateDict(dict).AsTable("table222zzz").WherePrimary("id").IfExistsDoNothing().ExecuteAffrows();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,22 @@ string getInsertSql(List<T1> data, bool flagInsert, bool noneParameter)
else
{
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + MySql ", _table.CsName));
sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
{
sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
else
sb.Append(
_orm.Select<T1>()
.AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
.DisableGlobalFilter()
.WhereDynamic(rowd)
.Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
});
sql = insert.ToSqlValuesOrSelectUnionAll();
if (sql?.StartsWith("INSERT INTO ") == true)
sql = $"INSERT IGNORE INTO {sql.Substring(12)}";
//sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
//{
// sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
// if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
// sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
// else
// sb.Append(
// _orm.Select<T1>()
// .AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
// .DisableGlobalFilter()
// .WhereDynamic(rowd)
// .Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
//});
}
}
if (string.IsNullOrEmpty(sql)) return null;
Expand Down
29 changes: 16 additions & 13 deletions Providers/FreeSql.Provider.MySql/Curd/MySqlInsertOrUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,22 @@ string getInsertSql(List<T1> data, bool flagInsert, bool noneParameter)
else
{
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + MySql ", _table.CsName));
sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
{
sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
else
sb.Append(
_orm.Select<T1>()
.AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
.DisableGlobalFilter()
.WhereDynamic(rowd)
.Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
});
sql = insert.ToSqlValuesOrSelectUnionAll();
if (sql?.StartsWith("INSERT INTO ") == true)
sql = $"INSERT IGNORE INTO {sql.Substring(12)}";
//sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
//{
// sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
// if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
// sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
// else
// sb.Append(
// _orm.Select<T1>()
// .AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
// .DisableGlobalFilter()
// .WhereDynamic(rowd)
// .Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
//});
}
}
if (string.IsNullOrEmpty(sql)) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,22 @@ string getInsertSql(List<T1> data, bool flagInsert, bool noneParameter)
else
{
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + MySql ", _table.CsName));
sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
{
sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
else
sb.Append(
_orm.Select<T1>()
.AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
.DisableGlobalFilter()
.WhereDynamic(rowd)
.Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
});
sql = insert.ToSqlValuesOrSelectUnionAll();
if (sql?.StartsWith("INSERT INTO ") == true)
sql = $"INSERT IGNORE INTO {sql.Substring(12)}";
//sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
//{
// sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
// if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
// sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
// else
// sb.Append(
// _orm.Select<T1>()
// .AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
// .DisableGlobalFilter()
// .WhereDynamic(rowd)
// .Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
//});
}
}
if (string.IsNullOrEmpty(sql)) return null;
Expand Down
29 changes: 16 additions & 13 deletions Providers/FreeSql.Provider.Sqlite/Curd/SqliteInsertOrUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,26 @@ string getInsertSql(List<T1> data, bool flagInsert)
if (_updateIgnore.Any()) throw new Exception(CoreStrings.S_InsertOrUpdate_Unable_UpdateColumns);
sql = insert.ToSql();
if (sql?.StartsWith("INSERT INTO ") == true)
sql = $"REPLACE INTO {sql.Substring("INSERT INTO ".Length)}";
sql = $"INSERT OR REPLACE INTO {sql.Substring(12)}";
}
else
{
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + Sqlite ", _table.CsName));
sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) => {
sb.Append(" \r\n WHERE NOT EXISTS(");
if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
else
sb.Append(
_orm.Select<T1>()
.AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
.DisableGlobalFilter()
.WhereDynamic(rowd)
.Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
});
sql = insert.ToSql();
if (sql?.StartsWith("INSERT INTO ") == true)
sql = $"INSERT OR IGNORE INTO {sql.Substring(12)}";
//sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) => {
// sb.Append(" \r\n WHERE NOT EXISTS(");
// if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
// sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
// else
// sb.Append(
// _orm.Select<T1>()
// .AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
// .DisableGlobalFilter()
// .WhereDynamic(rowd)
// .Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
//});
}
}
if (string.IsNullOrEmpty(sql)) return null;
Expand Down

0 comments on commit dfb99d1

Please sign in to comment.