using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MJTop.Data { /// /// 拼接Sql脚本类 /// public class Script { #region SqlIn/SqlLike /// /// 拼接in sql语句 /// /// 元素类型 /// 列名 /// 元素值 /// not in 或 in /// and开头的 in sql语句 public static string SqlIn(string columnName, T[] values, bool isNotIn = false) { string result = string.Empty; if (values == null || values.Length <= 0) { return string.Empty; } bool isValueType = TypeInfo.IsValueType; List lst = new List(); foreach (T obj in values) { if (obj != null) { string val = obj.ToString(); if (val.StartsWith("'") && val.EndsWith("'")) { val = val.Replace("'", "'''"); lst.Add(val); continue; } if (!isValueType) { val = "'" + val + "'"; } lst.Add(val); } } if (lst.Count > 0) { result = " and " + columnName + " " + (isNotIn ? "not" : "") + " in (" + string.Join(",", lst) + ") "; } return result; } /// /// 拼接in sql语句 /// /// 元素类型 /// 列名 /// 元素值 /// not in 或 in /// and开头的 in sql语句 public static string SqlInByDBType(ColumnInfo columnInfo, object[] values, DBType dBType = DBType.SqlServer, bool isNotIn = false) { string result = string.Empty; if (values == null || values.Length <= 0) { return string.Empty; } List lst = new List(); foreach (object obj in values) { if (obj != null) { string val = obj.ToString(); if (val.StartsWith("'") && val.EndsWith("'")) { val = val.Replace("'", "'''"); lst.Add(val); continue; } if (columnInfo.LikeType != LikeType.Number) { val = "'" + val + "'"; } lst.Add(val); } } if (lst.Count > 0) { result = " and " + columnInfo.ColumnName + " " + (isNotIn ? "not" : "") + " in (" + string.Join(",", lst) + ") "; } return result; } /// /// 拼接多条 like 语句 /// /// 实体类型 /// 列名 /// 元素值 /// or like 或 and like /// and开头的 多条 like 语句 public static string SqlLike(string columnName, T[] values, bool isOrLike = true) { string result = string.Empty; if (values == null || values.Length <= 0) { return string.Empty; } List lst = new List(); foreach (T obj in values) { if (obj != null) { string like_sql = columnName + " like '%{0}%' "; string temp_sql = string.Empty; string val = obj.ToString(); if (val.StartsWith("'") && val.EndsWith("'")) { val = val.Replace("'", "''"); temp_sql = string.Format(like_sql, val); lst.Add(temp_sql); continue; } temp_sql = string.Format(like_sql, val); lst.Add(temp_sql); } } if (lst.Count > 0) { result = " and (" + (string.Join((isOrLike ? " or" : " and "), lst)) + ") "; } return result; } #endregion /// /// 插入后查询自增列的值 /// /// 数据库类型 /// 表名 /// 序列名称 /// 自增列的列名 /// 查询自增Id的脚本 public static string IdentitySql(DBType type, string tableName = null, string sequenceName = null, string identityColName = null) { switch (type) { case DBType.SqlServer: if (string.IsNullOrWhiteSpace(tableName)) { throw new ArgumentException(type + ",查询自增Id的值,不能为空!", "tableName"); } return string.Format("select Ident_Current('{0}')", tableName); case DBType.MySql: return string.Format("select @@Identity"); case DBType.Oracle: case DBType.OracleDDTek: if (string.IsNullOrWhiteSpace(sequenceName)) { throw new ArgumentException(type + ",查询自增Id的值,不能为空!", "sequenceName"); } return string.Format("select {0}.currval from dual", sequenceName); case DBType.PostgreSql: if (string.IsNullOrWhiteSpace(tableName)) { throw new ArgumentException(type + ",查询自增Id的值,不能为空!", "tableName"); } if (string.IsNullOrWhiteSpace(identityColName)) { throw new ArgumentException(type + ",查询自增Id的值,不能为空!", "identityColName"); } //只支持表定义时定义的:smallserial,serial,bigserial //不支持创建 序列+触发器 时的实现。 return string.Format("select currval(pg_get_serial_sequence('{0}', '{1}'));", tableName.ToLower(), identityColName.ToLower()); case DBType.SQLite: return string.Format("select last_insert_rowid()"); default: throw new ArgumentException("不支持的数据库类型!"); } } } }