mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-30 04:45:54 +08:00
Update scar
This commit is contained in:
parent
e5fd408f47
commit
8b97a92b67
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
@ -19,33 +20,18 @@ namespace SqlSugar
|
||||
{
|
||||
get
|
||||
{
|
||||
string sql = @"select cast (pclass.oid as int4) as TableId,cast(ptables.tablename as varchar) as TableName,
|
||||
pcolumn.column_name as DbColumnName,pcolumn.udt_name as DataType,
|
||||
CASE WHEN pcolumn.numeric_scale >0 THEN pcolumn.numeric_precision ELSE pcolumn.character_maximum_length END as Length,
|
||||
pcolumn.column_default as DefaultValue,
|
||||
pcolumn.numeric_scale as DecimalDigits,
|
||||
pcolumn.numeric_scale as Scale,
|
||||
col_description(pclass.oid, pcolumn.ordinal_position) as ColumnDescription,
|
||||
case when pkey.colname = pcolumn.column_name
|
||||
then true else false end as IsPrimaryKey,
|
||||
case when pcolumn.column_default like 'nextval%'
|
||||
string sql = @"SELECT
|
||||
COLUMN_NAME AS DbColumnName,
|
||||
TABLE_NAME AS TableName,
|
||||
DATA_TYPE AS DataType,
|
||||
case when DATA_DEFAULT like 'NEXTVAL%'
|
||||
then true else false end as IsIdentity,
|
||||
case when pcolumn.is_nullable = 'YES'
|
||||
then true else false end as IsNullable
|
||||
from (select * from sys_tables where tablename = '{0}' and schemaname='public') ptables inner join sys_class pclass
|
||||
on ptables.tablename = pclass.relname inner join (SELECT *
|
||||
FROM INFO_SCHEM.ALL_TAB_COLUMNS
|
||||
) pcolumn on pcolumn.table_name = ptables.tablename
|
||||
left join (
|
||||
select sys_class.relname,sys_attribute.attname as colname from
|
||||
sys_constraint inner join sys_class
|
||||
on sys_constraint.conrelid = sys_class.oid
|
||||
inner join sys_attribute on sys_attribute.attrelid = sys_class.oid
|
||||
and sys_attribute.attnum = sys_constraint.conkey[1]
|
||||
inner join sys_type on sys_type.oid = sys_attribute.atttypid
|
||||
where sys_constraint.contype='p'
|
||||
) pkey on pcolumn.table_name = pkey.relname
|
||||
order by ptables.tablename";
|
||||
case when NULLABLE = 'Y'
|
||||
then true else false end as IsNullable
|
||||
FROM
|
||||
INFO_SCHEM.ALL_TAB_COLUMNS WHERE upper(TABLE_NAME)=upper('{0}')
|
||||
|
||||
";
|
||||
return sql;
|
||||
}
|
||||
}
|
||||
@ -69,10 +55,17 @@ namespace SqlSugar
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"select cast(relname as varchar) as Name,cast(Description as varchar) from sys_description
|
||||
join sys_class on sys_description.objoid = sys_class.oid
|
||||
where objsubid = 0 and relname in (SELECT viewname from sys_views
|
||||
WHERE schemaname ='public')";
|
||||
return @"select cast(relname as varchar(500)) as Name ,
|
||||
'' AS DESCRIPTION
|
||||
from sys_class c
|
||||
where relkind = 'v'
|
||||
and relname not like 'SYS_%'
|
||||
and relname not like 'V_SYS_%'
|
||||
and relname not like 'sql_%'
|
||||
and relname not like 'AQ$%'
|
||||
AND relvbase=1
|
||||
AND relname NOT IN('LOGIN_FORBIDDEN_RULE','DBMS_LOCK_ALLOCATED','DUAL','_OBJ_BINLOG_SHOW_EVENTS_','USER_LOGIN_HISTORY','LOGIN_ALLOW_IPLIST')
|
||||
order by relname";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
@ -377,10 +370,20 @@ namespace SqlSugar
|
||||
|
||||
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
|
||||
{
|
||||
var result= base.GetColumnInfosByTableName(tableName.TrimEnd('"').TrimStart('"').ToLower(), isCache);
|
||||
if (result == null || result.Count() == 0)
|
||||
var result= base.GetColumnInfosByTableName(tableName.TrimStart('"').TrimEnd('"'), isCache);
|
||||
string sql = "select * from " + SqlBuilder.GetTranslationTableName(tableName) + " WHERE 1=2 ";
|
||||
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
|
||||
this.Context.Ado.IsEnableLogEvent = false;
|
||||
using (DbDataReader reader = (DbDataReader)this.Context.Ado.GetDataReader(sql))
|
||||
{
|
||||
result = base.GetColumnInfosByTableName(tableName, isCache);
|
||||
this.Context.Ado.IsEnableLogEvent = oldIsEnableLog;
|
||||
var schemaTable = reader.GetSchemaTable();
|
||||
foreach (System.Data.DataRow row in schemaTable.Rows)
|
||||
{
|
||||
var name = row["columnname"] + "";
|
||||
var data = result.First(it => it.DbColumnName.Equals(name, StringComparison.OrdinalIgnoreCase));
|
||||
data.IsPrimarykey= row["iskey"].ToString() =="True"? true : false;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -78,107 +78,30 @@ namespace SqlSugar
|
||||
|
||||
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
|
||||
{
|
||||
Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key");
|
||||
int pageSize = 200;
|
||||
int pageIndex = 1;
|
||||
int totalRecord = groupList.Count;
|
||||
int pageCount = (totalRecord + pageSize - 1) / pageSize;
|
||||
StringBuilder batchUpdateSql = new StringBuilder();
|
||||
while (pageCount >= pageIndex)
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int i = 0;
|
||||
sb.AppendLine(string.Join("\r\n", groupList.Select(t =>
|
||||
{
|
||||
StringBuilder updateTable = new StringBuilder();
|
||||
string setValues = string.Join(",", groupList.First().Where(it => it.IsPrimarykey == false && (it.IsIdentity == false || (IsOffIdentity && it.IsIdentity))).Select(it =>
|
||||
var updateTable = string.Format("UPDATE {0} SET", base.GetTableNameStringNoWith);
|
||||
var setValues = string.Join(",", t.Where(s => !s.IsPrimarykey).Select(m => GetOracleUpdateColums(i, m)).ToArray());
|
||||
var pkList = t.Where(s => s.IsPrimarykey).ToList();
|
||||
List<string> whereList = new List<string>();
|
||||
foreach (var item in pkList)
|
||||
{
|
||||
if (SetValues.IsValuable())
|
||||
{
|
||||
var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName));
|
||||
if (setValue != null && setValue.Any())
|
||||
{
|
||||
return setValue.First().Value;
|
||||
}
|
||||
}
|
||||
var result = string.Format("{0}=T.{0}", Builder.GetTranslationColumnName(it.DbColumnName));
|
||||
return result;
|
||||
}));
|
||||
string tempColumnValue = string.Join(",", groupList.First().Select(it =>
|
||||
{
|
||||
if (SetValues.IsValuable())
|
||||
{
|
||||
var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName));
|
||||
if (setValue != null && setValue.Any())
|
||||
{
|
||||
return setValue.First().Value;
|
||||
}
|
||||
}
|
||||
var result = Builder.GetTranslationColumnName(it.DbColumnName);
|
||||
return result;
|
||||
}));
|
||||
batchUpdateSql.AppendFormat(SqlTemplateBatch.ToString(), setValues, GetTableNameStringNoWith, TableWithString);
|
||||
int i = 0;
|
||||
var tableColumnList = this.Context.DbMaintenance.GetColumnInfosByTableName(GetTableNameStringNoWith);
|
||||
|
||||
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
|
||||
{
|
||||
var isFirst = i == 0;
|
||||
if (!isFirst)
|
||||
{
|
||||
updateTable.Append(SqlTemplateBatchUnion);
|
||||
}
|
||||
updateTable.Append("\r\n (" + string.Join(",", columns.Select(it =>
|
||||
{
|
||||
var columnInfo = tableColumnList.FirstOrDefault(x => x.DbColumnName.Equals(it.DbColumnName, StringComparison.OrdinalIgnoreCase));
|
||||
var dbType = columnInfo?.DataType;
|
||||
if (dbType == null) {
|
||||
var typeName = it.PropertyType.Name.ToLower();
|
||||
if (typeName == "int32")
|
||||
typeName = "int";
|
||||
if (typeName == "int64")
|
||||
typeName = "long";
|
||||
if (typeName == "int16")
|
||||
typeName = "short";
|
||||
if (typeName == "boolean")
|
||||
typeName = "bool";
|
||||
|
||||
var isAnyType = OscarDbBind.MappingTypesConst.Where(x => x.Value.ToString().ToLower() == typeName).Any();
|
||||
if (isAnyType)
|
||||
{
|
||||
dbType = OscarDbBind.MappingTypesConst.Where(x => x.Value.ToString().ToLower() == typeName).FirstOrDefault().Key;
|
||||
}
|
||||
else {
|
||||
dbType = "varchar";
|
||||
}
|
||||
}
|
||||
return string.Format("CAST({0} AS {1})", FormatValue(it.Value), dbType);
|
||||
|
||||
})) + ")");
|
||||
++i;
|
||||
var isFirst = pkList.First() == item;
|
||||
var whereString = "";
|
||||
whereString += GetOracleUpdateColums(i, item);
|
||||
whereList.Add(whereString);
|
||||
}
|
||||
pageIndex++;
|
||||
updateTable.Append("\r\n");
|
||||
string whereString = null;
|
||||
if (this.WhereValues.HasValue())
|
||||
{
|
||||
foreach (var item in WhereValues)
|
||||
{
|
||||
var isFirst = whereString == null;
|
||||
whereString += (isFirst ? null : " AND ");
|
||||
whereString += item;
|
||||
}
|
||||
}
|
||||
else if (PrimaryKeys.HasValue())
|
||||
{
|
||||
foreach (var item in PrimaryKeys)
|
||||
{
|
||||
var isFirst = whereString == null;
|
||||
whereString += (isFirst ? null : " AND ");
|
||||
whereString += string.Format("{0}.{1}=T.{1}", GetTableNameStringNoWith, Builder.GetTranslationColumnName(item));
|
||||
}
|
||||
}
|
||||
var format = string.Format(SqlTemplateJoin, updateTable, whereString, tempColumnValue);
|
||||
batchUpdateSql.Replace("${0}", format);
|
||||
batchUpdateSql.Append(";");
|
||||
}
|
||||
return batchUpdateSql.ToString();
|
||||
i++;
|
||||
return string.Format("{0} {1} WHERE {2};", updateTable, setValues, string.Join("AND", whereList));
|
||||
}).ToArray()));
|
||||
return sb.ToString();
|
||||
}
|
||||
private string GetOracleUpdateColums(int i, DbColumnInfo m)
|
||||
{
|
||||
return string.Format("\"{0}\"={1}", m.DbColumnName.ToUpper(), FormatValue(m.Value));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user