mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
- 优化 MySql/Sqlite InsertOrUpdate + IfExistsDoNothing 简化 SQL insert ignore into;#1601
This commit is contained in:
parent
c6443b4683
commit
dfb99d1822
@ -559,7 +559,7 @@ namespace base_entity
|
|||||||
//.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
|
//.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
|
||||||
//.UseQuoteSqlName(false)
|
//.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")
|
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
|
||||||
|
|
||||||
@ -602,8 +602,8 @@ namespace base_entity
|
|||||||
|
|
||||||
var dict = new List<Dictionary<string, object>>();
|
var dict = new List<Dictionary<string, object>>();
|
||||||
Dictionary<string, object> d = new Dictionary<string, object>();
|
Dictionary<string, object> d = new Dictionary<string, object>();
|
||||||
d.Add("id", 1);
|
d.Add("id", 2);
|
||||||
d.Add("name", "name1");
|
d.Add("name", "name2");
|
||||||
dict.Add(d);
|
dict.Add(d);
|
||||||
var testx01 = fsql.InsertOrUpdateDict(dict).AsTable("table222zzz").WherePrimary("id").IfExistsDoNothing().ToSql();
|
var testx01 = fsql.InsertOrUpdateDict(dict).AsTable("table222zzz").WherePrimary("id").IfExistsDoNothing().ToSql();
|
||||||
fsql.InsertOrUpdateDict(dict).AsTable("table222zzz").WherePrimary("id").IfExistsDoNothing().ExecuteAffrows();
|
fsql.InsertOrUpdateDict(dict).AsTable("table222zzz").WherePrimary("id").IfExistsDoNothing().ExecuteAffrows();
|
||||||
|
@ -87,19 +87,22 @@ namespace FreeSql.Custom.MySql
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + MySql ", _table.CsName));
|
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) =>
|
sql = insert.ToSqlValuesOrSelectUnionAll();
|
||||||
{
|
if (sql?.StartsWith("INSERT INTO ") == true)
|
||||||
sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
|
sql = $"INSERT IGNORE INTO {sql.Substring(12)}";
|
||||||
if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
|
//sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
|
||||||
sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
|
//{
|
||||||
else
|
// sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
|
||||||
sb.Append(
|
// if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
|
||||||
_orm.Select<T1>()
|
// sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
|
||||||
.AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
|
// else
|
||||||
.DisableGlobalFilter()
|
// sb.Append(
|
||||||
.WhereDynamic(rowd)
|
// _orm.Select<T1>()
|
||||||
.Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
|
// .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;
|
if (string.IsNullOrEmpty(sql)) return null;
|
||||||
|
@ -87,19 +87,22 @@ namespace FreeSql.MySql.Curd
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + MySql ", _table.CsName));
|
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) =>
|
sql = insert.ToSqlValuesOrSelectUnionAll();
|
||||||
{
|
if (sql?.StartsWith("INSERT INTO ") == true)
|
||||||
sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
|
sql = $"INSERT IGNORE INTO {sql.Substring(12)}";
|
||||||
if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
|
//sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
|
||||||
sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
|
//{
|
||||||
else
|
// sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
|
||||||
sb.Append(
|
// if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
|
||||||
_orm.Select<T1>()
|
// sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
|
||||||
.AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
|
// else
|
||||||
.DisableGlobalFilter()
|
// sb.Append(
|
||||||
.WhereDynamic(rowd)
|
// _orm.Select<T1>()
|
||||||
.Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
|
// .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;
|
if (string.IsNullOrEmpty(sql)) return null;
|
||||||
|
@ -87,19 +87,22 @@ namespace FreeSql.Odbc.MySql
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + MySql ", _table.CsName));
|
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) =>
|
sql = insert.ToSqlValuesOrSelectUnionAll();
|
||||||
{
|
if (sql?.StartsWith("INSERT INTO ") == true)
|
||||||
sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
|
sql = $"INSERT IGNORE INTO {sql.Substring(12)}";
|
||||||
if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
|
//sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) =>
|
||||||
sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
|
//{
|
||||||
else
|
// sb.Append(" \r\n FROM dual WHERE NOT EXISTS(");
|
||||||
sb.Append(
|
// if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
|
||||||
_orm.Select<T1>()
|
// sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
|
||||||
.AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
|
// else
|
||||||
.DisableGlobalFilter()
|
// sb.Append(
|
||||||
.WhereDynamic(rowd)
|
// _orm.Select<T1>()
|
||||||
.Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
|
// .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;
|
if (string.IsNullOrEmpty(sql)) return null;
|
||||||
|
@ -59,23 +59,26 @@ namespace FreeSql.Sqlite.Curd
|
|||||||
if (_updateIgnore.Any()) throw new Exception(CoreStrings.S_InsertOrUpdate_Unable_UpdateColumns);
|
if (_updateIgnore.Any()) throw new Exception(CoreStrings.S_InsertOrUpdate_Unable_UpdateColumns);
|
||||||
sql = insert.ToSql();
|
sql = insert.ToSql();
|
||||||
if (sql?.StartsWith("INSERT INTO ") == true)
|
if (sql?.StartsWith("INSERT INTO ") == true)
|
||||||
sql = $"REPLACE INTO {sql.Substring("INSERT INTO ".Length)}";
|
sql = $"INSERT OR REPLACE INTO {sql.Substring(12)}";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_tempPrimarys.Any() == false) throw new Exception(CoreStrings.Entity_Must_Primary_Key("fsql.InsertOrUpdate + IfExistsDoNothing + Sqlite ", _table.CsName));
|
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) => {
|
sql = insert.ToSql();
|
||||||
sb.Append(" \r\n WHERE NOT EXISTS(");
|
if (sql?.StartsWith("INSERT INTO ") == true)
|
||||||
if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
|
sql = $"INSERT OR IGNORE INTO {sql.Substring(12)}";
|
||||||
sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
|
//sql = insert.ToSqlValuesOrSelectUnionAllExtension101(false, (rowd, idx, sb) => {
|
||||||
else
|
// sb.Append(" \r\n WHERE NOT EXISTS(");
|
||||||
sb.Append(
|
// if (typeof(T1) == typeof(Dictionary<string, object>) && rowd is T1 dict)
|
||||||
_orm.Select<T1>()
|
// sb.Append($"SELECT 1 FROM {_commonUtils.QuoteSqlName(_tableRule(null))} WHERE {_commonUtils.WhereItems<T1>(_tempPrimarys, "", new T1[] { dict })})");
|
||||||
.AsTable((_, __) => _tableRule?.Invoke(__)).AsType(_table.Type)
|
// else
|
||||||
.DisableGlobalFilter()
|
// sb.Append(
|
||||||
.WhereDynamic(rowd)
|
// _orm.Select<T1>()
|
||||||
.Limit(1).ToSql("1").Replace(" \r\n", " \r\n ")).Append(")");
|
// .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;
|
if (string.IsNullOrEmpty(sql)) return null;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user