diff --git a/Tiobon.Core.Common/DB/DbSql/DbInsert.cs b/Tiobon.Core.Common/DB/DbSql/DbInsert.cs new file mode 100644 index 00000000..fe2bb0b0 --- /dev/null +++ b/Tiobon.Core.Common/DB/DbSql/DbInsert.cs @@ -0,0 +1,291 @@ +using System; +using System.Collections.Generic; +using System.Text; +using EU.Core.UserManager; +using EU.Core.Utilities; +using Microsoft.AspNetCore.Mvc.Core; + +namespace EU.Core +{ + /// + /// Sql插入类 + /// + public class DbInsert + { + #region 变量定义 + private string sql; + private string createProgram; + private string rowId; + /// + /// 如果使用线程,此处可以指定插入用户代码,否则CREATED_BY为空 + /// + public string CreatedBy { get; set; } + public string RowId + { + get { return rowId; } + set { rowId = value; } + } + private bool isInitDefaultValue = true; + /// + /// 是否初始化默认字段,像:ROW_ID,CREATED_BY,CREATED_DATE,CREATED_PROGRAM,TAG,ACTIVE_FLAG等,默认会初始化 + /// + public bool IsInitDefaultValue + { + get + { + return isInitDefaultValue; + } + set + { + isInitDefaultValue = value; + } + } + private bool isInitRowId = true; + /// + /// 是否初始化ROW_ID字段(默认初始化),只有IsInitDefaultValue为true时才有效! + /// + public bool IsInitRowId + { + get + { + return isInitRowId; + } + set + { + isInitRowId = value; + } + } + #endregion + + #region 构造函数 + public DbInsert() + { + } + + public DbInsert(string tableName) + { + SetTableName(tableName.ToUpper()); + } + + public DbInsert(string tableName, string createProgram) + { + SetTableName(tableName.ToUpper()); + this.createProgram = createProgram; + } + + public void SetTableName(string tableName) + { + sql = "INSERT INTO {0} () VALUES ()"; + sql = string.Format(sql, tableName.ToUpper()); + } + #endregion + + #region Values + public void Values(string fieldName, string value) + { + if (string.IsNullOrEmpty(value)) return; + value = value.Replace("'", "''"); + value = value.Trim(); + string s = "N'{0}'"; + s = string.Format(s, value); + + FormatValue(fieldName.ToUpper(), s); + } + public void Values(string fieldName, Guid value) + { + string value1 = value.ToString(); + if (string.IsNullOrEmpty(value1)) return; + value1 = value1.Replace("'", "''"); + value1 = value1.Trim(); + string s = "N'{0}'"; + s = string.Format(s, value1); + + FormatValue(fieldName.ToUpper(), s); + } + + public void Values(string fieldName, Guid? value) + { + string value1 = value.ToString(); + if (string.IsNullOrEmpty(value1)) return; + value1 = value1.Replace("'", "''"); + value1 = value1.Trim(); + string s = "N'{0}'"; + s = string.Format(s, value1); + + FormatValue(fieldName.ToUpper(), s); + } + + public void Values(string fieldName, int value) + { + FormatValue(fieldName.ToUpper(), Convert.ToString(value)); + } + + public void Values(string fieldName, double value) + { + FormatValue(fieldName.ToUpper(), Convert.ToString(value)); + } + + public void Values(string fieldName, decimal value) + { + FormatValue(fieldName.ToUpper(), Convert.ToString(value)); + } + + public void Values(string fieldName, decimal? value) + { + FormatValue(fieldName.ToUpper(), Convert.ToString(value)); + } + + public void Values(string fieldName, DateTime value) + { + if (value == DateTime.MinValue) return; + string valTemp = string.Empty; + valTemp = Convert.ToString(value); + //valTemp = valTemp.Replace("'", "''"); + string s = string.Empty; + s = "CAST('{0}' AS DATETIME)"; + s = string.Format(s, valTemp); + + FormatValue(fieldName.ToUpper(), s); + } + public void Values(string fieldName, DateTime? value) + { + if (value == null) return; + string valTemp = string.Empty; + valTemp = Convert.ToString(value); + //valTemp = valTemp.Replace("'", "''"); + string s = string.Empty; + s = "CAST('{0}' AS DATETIME)"; + s = string.Format(s, valTemp); + + FormatValue(fieldName.ToUpper(), s); + } + #endregion + + #region 加密插入 + /// + /// 以加密方式插入 + /// + /// + /// + public void ValuesAsSecurity(string fieldName, string value) + { + if (string.IsNullOrEmpty(value)) return; + value = value.Replace("'", "''"); + string s = "encryptbykey(key_guid('fookey'),N'{0}')"; + s = string.Format(s, value); + + FormatValue(fieldName.ToUpper(), s); + } + /// + /// 加密保存 + /// + /// + /// + public void ValuesAsSecurity(string fieldName, int value) + { + ValuesAsSecurity(fieldName.ToUpper(), Convert.ToString(value)); + } + /// + /// 加密保存 + /// + /// + /// + public void ValuesAsSecurity(string fieldName, double value) + { + ValuesAsSecurity(fieldName.ToUpper(), Convert.ToString(value)); + } + /// + /// 加密保存 + /// + /// + /// + public void ValuesAsSecurity(string fieldName, decimal value) + { + ValuesAsSecurity(fieldName.ToUpper(), Convert.ToString(value)); + } + #endregion + + #region Public Method + public string GetSql() + { + if (isInitRowId == true) + { + rowId = StringHelper.Id; + Values("ID", rowId); + } + if (isInitDefaultValue == true) + InitDefaultValues(); + + return sql; + } + #endregion + + #region Private Method + private void FormatValue(string fieldName, string value) + { + if (value != null) + { + value = value.Trim(); + } + if (DBHelper.MySql) + { + fieldName = fieldName.Replace("[", "`"); + fieldName = fieldName.Replace("]", "`"); + } + sql = sql.Replace("{", "{{"); + sql = sql.Replace("}", "}}"); + int n = sql.IndexOf("() VALUES"); + + if (n == -1) + { + n = sql.IndexOf(") VALUES"); + sql = sql.Insert(n, ", {0} "); + n = sql.LastIndexOf(")"); + sql = sql.Insert(n, ", {1}"); + } + else + { + sql = sql.Insert(++n, " {0} "); + n = sql.LastIndexOf(")"); + sql = sql.Insert(n, " {1}"); + } + sql = string.Format(sql, fieldName.ToUpper(), value); + } + + /// + /// 初始化Insert语句默认需要插入的值 + /// + private void InitDefaultValues() + { + //DatabaseType dbType = DbAccess.GetDatabaseType(); + //row_id + //if (DbAccess.IsOracle()) + //{ + // rowId = DbAccess.ExecuteScalar("SELECT F_GET_SYSID FROM DUAL") as string; + //} + //else + //{ + // rowId = DbAccess.ExecuteScalar("SELECT REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR, GETDATE(), 120 ),'-',''),' ',''),':','')") as string; + //} + + //create_by + Guid? createBy = UserContext.Current.User_Id; + if (createBy != Guid.Empty) + Values("CreatedBy", createBy); + + //create_date + DateTime createDate = DateTime.Now; + Values("CreatedTime", createDate); + Values("GroupId", UserContext.Current.GroupId); + Values("CompanyId", UserContext.Current.CompanyId); + + //create_program + //Values("CREATED_PROGRAM", createProgram); + //tag + Values("Tag", 1); + //active_flag + //Values("DELETE_FLAG", "N"); + } + #endregion + } +} diff --git a/Tiobon.Core.Common/DB/DbSql/DbSelect.cs b/Tiobon.Core.Common/DB/DbSql/DbSelect.cs new file mode 100644 index 00000000..d69efb34 --- /dev/null +++ b/Tiobon.Core.Common/DB/DbSql/DbSelect.cs @@ -0,0 +1,527 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EU.Core +{ + public class DbSelect + { + #region 属性 + private string tableNames; + private string primaryTableAlias; + private string companyId; + private string selectItems; + private string whereCondition; + private string orderBy; + private string groupBy; + private bool isInitDefaultValue = true; + /// + /// 是否初始化默认查询条件,如COMPANY_ID等 + /// + public bool IsInitDefaultValue + { + get + { + return isInitDefaultValue; + } + set { isInitDefaultValue = value; } + } + #endregion + + #region 构造函数 + public DbSelect(string tableNames, string primaryTableAlias, string companyId) + { + this.tableNames = tableNames; + this.primaryTableAlias = primaryTableAlias; + this.companyId = companyId; + } + + public DbSelect(string tableNames, string primaryTableAlias) + { + this.tableNames = tableNames; + this.primaryTableAlias = primaryTableAlias; + this.companyId = null; + } + #endregion + + #region Select + /// + /// 解密查询 + /// + /// + public void SelectSecurity(string selectItem) + { + string aliasName = selectItem.Substring(selectItem.IndexOf(".") + 1); + this.selectItems += "cast(decryptbykey(" + selectItem + ") AS NVARCHAR(256))" + " AS " + aliasName + ","; + } + /// + /// 解密查询 + /// + /// + /// + public void SelectSecurity(string selectItem, string aliasName) + { + this.selectItems += "cast(decryptbykey(" + selectItem + ") AS NVARCHAR(256))" + " AS " + aliasName + ","; + } + /// + /// 对加密字段求和 + /// + /// + public void SelectSecuritySum(string selectItem) + { + string aliasName = selectItem.Substring(selectItem.IndexOf(".") + 1); + this.selectItems += "isnull(sum(cast (cast(decryptbykey(" + selectItem + ") AS NVARCHAR(256)) as decimal(20,6))),0)" + " AS " + aliasName + ","; + } + /// + /// 对加密字段求和 + /// + /// + /// + public void SelectSecuritySum(string selectItem, string aliasName) + { + this.selectItems += "isnull(sum(cast (cast(decryptbykey(" + selectItem + ") AS NVARCHAR(256)) as decimal(20,6))),0)" + " AS " + aliasName + ","; + } + + public void Select(string selectItems) + { + this.selectItems += selectItems + ","; + } + public void Select(string selectItem, string aliasName) + { + this.selectItems += selectItem + " AS " + aliasName + ","; + } + public void Select(string selectItem, string aliasName, DateFormat dateFormat) + { + string tempValue = string.Empty; + switch (dateFormat) + { + case DateFormat.Month: + { + tempValue = "DBO.TO_CHAR(" + selectItem + ",'YYYY/MM')" + " AS " + aliasName + ","; + break; + } + case DateFormat.Day: + { + tempValue = "DBO.TO_CHAR(" + selectItem + ",'YYYY/MM/DD')" + " AS " + aliasName + ","; + break; + } + case DateFormat.Hour: + { + tempValue = "DBO.TO_CHAR(" + selectItem + ",'YYYY/MM/DD HH24')" + " AS " + aliasName + ","; + break; + } + case DateFormat.Minute: + { + tempValue = "DBO.TO_CHAR(" + selectItem + ",'YYYY/MM/DD HH24:MI')" + " AS " + aliasName + ","; + break; + } + case DateFormat.Second: + { + tempValue = "DBO.TO_CHAR(" + selectItem + ",'YYYY/MM/DD HH24:MI:SS')" + " AS " + aliasName + ","; + break; + } + } + this.selectItems += tempValue; + } + private string GetInternalSelect() + { + string result; + if (string.IsNullOrEmpty(this.selectItems)) + { + result = "*"; + } + else + { + result = this.selectItems.Substring(0, this.selectItems.Length - 1); + } + return result; + } + /// + /// 返回SQL的SELECT部分,如:SELECT * + /// + /// + public string GetSelect() + { + string result; + if (string.IsNullOrEmpty(this.selectItems)) + { + result = "*"; + } + else + { + result = this.selectItems.Substring(0, this.selectItems.Length - 1); + } + return "SELECT " + result; + } + #endregion + + #region Where + public void Where(string whereCondition) + { + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, params string[] args) + { + this.whereCondition += string.Format(whereCondition, args) + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue) + { + string tempFieldValue = Convert.ToString(fieldValue); + tempFieldValue = tempFieldValue.Replace("'", "''"); + //if (IsOracle) + //{ + // whereCondition = string.Format(whereCondition, "TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')"); + //} + //else + //{ + // //whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')"); + // whereCondition = string.Format(whereCondition, "'{0}'"); + //} + whereCondition = string.Format(whereCondition, "'{0}'"); + whereCondition = string.Format(whereCondition, tempFieldValue); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue1, DateTime fieldValue2) + { + string tempFieldValue1 = Convert.ToString(fieldValue1); + tempFieldValue1 = tempFieldValue1.Replace("'", "''"); + string tempFieldValue2 = Convert.ToString(fieldValue2); + tempFieldValue2 = tempFieldValue2.Replace("'", "''"); + //if (IsOracle) + //{ + // whereCondition = string.Format(whereCondition, "TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')"); + //} + //else + //{ + // //whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')"); + // whereCondition = string.Format(whereCondition, "'{0}'", "'{1}'"); + //} + whereCondition = string.Format(whereCondition, "'{0}'", "'{1}'"); + whereCondition = string.Format(whereCondition, tempFieldValue1, tempFieldValue2); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue1, DateTime fieldValue2, DateTime fieldValue3) + { + string tempFieldValue1 = Convert.ToString(fieldValue1); + tempFieldValue1 = tempFieldValue1.Replace("'", "''"); + string tempFieldValue2 = Convert.ToString(fieldValue2); + tempFieldValue2 = tempFieldValue2.Replace("'", "''"); + string tempFieldValue3 = Convert.ToString(fieldValue3); + tempFieldValue3 = tempFieldValue3.Replace("'", "''"); + //if (IsOracle) + //{ + // whereCondition = string.Format(whereCondition, "TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')"); + //} + //else + //{ + // //whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')"); + // whereCondition = string.Format(whereCondition, "'{0}'", "'{1}'", "'{2}'"); + //} + whereCondition = string.Format(whereCondition, "'{0}'", "'{1}'", "'{2}'"); + whereCondition = string.Format(whereCondition, tempFieldValue1, tempFieldValue2, tempFieldValue3); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue1, DateTime fieldValue2, DateTime fieldValue3, DateTime fieldValue4) + { + string tempFieldValue1 = Convert.ToString(fieldValue1); + tempFieldValue1 = tempFieldValue1.Replace("'", "''"); + string tempFieldValue2 = Convert.ToString(fieldValue2); + tempFieldValue2 = tempFieldValue2.Replace("'", "''"); + string tempFieldValue3 = Convert.ToString(fieldValue3); + tempFieldValue3 = tempFieldValue3.Replace("'", "''"); + string tempFieldValue4 = Convert.ToString(fieldValue4); + tempFieldValue4 = tempFieldValue4.Replace("'", "''"); + //if (IsOracle) + //{ + // whereCondition = string.Format(whereCondition, "TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{3}','YYYY/MM/DD HH24:MI:SS')"); + //} + //else + //{ + // //whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{3}','YYYY/MM/DD HH24:MI:SS')"); + // whereCondition = string.Format(whereCondition, "'{0}'", "'{1}'", "'{2}'", "'{3}'"); + //} + whereCondition = string.Format(whereCondition, "'{0}'", "'{1}'", "'{2}'", "'{3}'"); + whereCondition = string.Format(whereCondition, tempFieldValue1, tempFieldValue2, tempFieldValue3, tempFieldValue4); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue1, DateTime fieldValue2, DateTime fieldValue3, DateTime fieldValue4, DateTime fieldValue5) + { + string tempFieldValue1 = Convert.ToString(fieldValue1); + tempFieldValue1 = tempFieldValue1.Replace("'", "''"); + string tempFieldValue2 = Convert.ToString(fieldValue2); + tempFieldValue2 = tempFieldValue2.Replace("'", "''"); + string tempFieldValue3 = Convert.ToString(fieldValue3); + tempFieldValue3 = tempFieldValue3.Replace("'", "''"); + string tempFieldValue4 = Convert.ToString(fieldValue4); + tempFieldValue4 = tempFieldValue4.Replace("'", "''"); + string tempFieldValue5 = Convert.ToString(fieldValue5); + tempFieldValue5 = tempFieldValue5.Replace("'", "''"); + //if (IsOracle) + //{ + // whereCondition = string.Format(whereCondition, "TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{3}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{4}','YYYY/MM/DD HH24:MI:SS')"); + //} + //else + //{ + // //whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{3}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{4}','YYYY/MM/DD HH24:MI:SS')"); + // whereCondition = string.Format(whereCondition, "'{0}'", "'{1}'", "'{2}'", "'{3}'", "'{4}'"); + //} + whereCondition = string.Format(whereCondition, "'{0}'", "'{1}'", "'{2}'", "'{3}'", "'{4}'"); + + whereCondition = string.Format(whereCondition, tempFieldValue1, tempFieldValue2, tempFieldValue3, tempFieldValue4, tempFieldValue5); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue1, DateTime fieldValue2, DateTime fieldValue3, DateTime fieldValue4, DateTime fieldValue5, DateTime fieldValue6) + { + string tempFieldValue1 = Convert.ToString(fieldValue1); + tempFieldValue1 = tempFieldValue1.Replace("'", "''"); + string tempFieldValue2 = Convert.ToString(fieldValue2); + tempFieldValue2 = tempFieldValue2.Replace("'", "''"); + string tempFieldValue3 = Convert.ToString(fieldValue3); + tempFieldValue3 = tempFieldValue3.Replace("'", "''"); + string tempFieldValue4 = Convert.ToString(fieldValue4); + tempFieldValue4 = tempFieldValue4.Replace("'", "''"); + string tempFieldValue5 = Convert.ToString(fieldValue5); + tempFieldValue5 = tempFieldValue5.Replace("'", "''"); + string tempFieldValue6 = Convert.ToString(fieldValue6); + tempFieldValue6 = tempFieldValue6.Replace("'", "''"); + //if (IsOracle) + //{ + // whereCondition = string.Format(whereCondition, "TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{3}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{4}','YYYY/MM/DD HH24:MI:SS')", "TO_DATE('{5}','YYYY/MM/DD HH24:MI:SS')"); + //} + //else + //{ + // //whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{3}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{4}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{4}','YYYY/MM/DD HH24:MI:SS')"); + // whereCondition = string.Format(whereCondition, "'{0}'", "'{1}'", "'{2}'", "'{3}'", "'{4}'", "'{5}'"); + //} + whereCondition = string.Format(whereCondition, "'{0}'", "'{1}'", "'{2}'", "'{3}'", "'{4}'", "'{5}'"); + whereCondition = string.Format(whereCondition, tempFieldValue1, tempFieldValue2, tempFieldValue3, tempFieldValue4, tempFieldValue5, tempFieldValue6); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string fieldName, string condition, string fieldValue) + { + this.whereCondition += fieldName.ToUpper() + condition + "'" + fieldValue + "'" + " AND "; + } + public void Where(string fieldName, string condition, Guid fieldValue) + { + this.whereCondition += fieldName.ToUpper() + condition + "'" + fieldValue + "'" + " AND "; + } + public void Where(string fieldName, string condition, decimal fieldValue) + { + this.whereCondition += fieldName.ToUpper() + condition + fieldValue + " AND "; + } + public void Where(string fieldName, string condition, int fieldValue) + { + this.whereCondition += fieldName.ToUpper() + condition + fieldValue + " AND "; + } + public void Where(string fieldName, string condition, bool fieldValue) + { + this.whereCondition += fieldName.ToUpper() + condition + "'" + fieldValue + "'" + " AND "; + } + public void Where(string fieldName, string condition, DateTime fieldValue) + { + + string tempFieldValue = Convert.ToString(fieldValue); + tempFieldValue = tempFieldValue.Replace("'", "''"); + string tempValue = string.Empty; + //if (IsOracle) + //{ + // tempValue = fieldName.ToUpper() + condition + "TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')" + " AND "; + //} + //else + //{ + // //tempValue = fieldName.ToUpper() + condition + "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')" + " AND "; + // tempValue = fieldName.ToUpper() + condition + "'{0}'" + " AND "; + //} + tempValue = fieldName.ToUpper() + condition + "'{0}'" + " AND "; + tempValue = string.Format(tempValue, tempFieldValue); + this.whereCondition += tempValue; + } + /// + /// 对字段解密后进行比较 + /// + /// + /// + /// + public void WhereSecurity(string fieldName, string condition, string fieldValue) + { + this.whereCondition += "cast(decryptbykey(" + fieldName.ToUpper() + ") AS NVARCHAR(256))" + condition + "'" + fieldValue + "'" + " AND "; + } + /// + /// 对字段解密后进行比较 + /// + /// + /// + /// + public void WhereSecurity(string fieldName, string condition, decimal fieldValue) + { + this.whereCondition += "cast(decryptbykey(" + fieldName.ToUpper() + ") AS NVARCHAR(256))" + condition + fieldValue + " AND "; + } + /// + /// 对字段解密后进行比较 + /// + /// + /// + /// + public void WhereSecurity(string fieldName, string condition, int fieldValue) + { + this.whereCondition += "cast(decryptbykey(" + fieldName.ToUpper() + ") AS NVARCHAR(256))" + condition + fieldValue + " AND "; + } + private string GetInternalWhere() + { + string result; + #region 不初始化默认值 + if (IsInitDefaultValue == false) + { + if (string.IsNullOrEmpty(this.whereCondition)) + { + if (string.IsNullOrEmpty(companyId)) + { + result = "1=1"; + } + else + { + result = primaryTableAlias + ".CompanyId='" + companyId + "'"; + } + } + else + { + if (string.IsNullOrEmpty(companyId)) + { + result = this.whereCondition.Substring(0, this.whereCondition.Length - 5); + } + else + { + result = this.whereCondition + primaryTableAlias + ".CompanyId='" + companyId + "'"; + } + } + } + #endregion + #region 初始化默认值 + else + { + if (string.IsNullOrEmpty(this.whereCondition)) + { + if (string.IsNullOrEmpty(companyId)) + { + result = "IsActive='true' AND IsDeleted='false'"; + } + else + { + result = primaryTableAlias + ".CompanyId='" + companyId + "' AND " + primaryTableAlias + ".IsActive='true' AND " + primaryTableAlias + ".IsDeleted='false'"; + } + } + else + { + if (string.IsNullOrEmpty(companyId)) + { + result = this.whereCondition + primaryTableAlias + ".IsActive='true' AND " + primaryTableAlias + ".IsDeleted='false'"; + } + else + { + result = this.whereCondition + primaryTableAlias + ".CompanyId='" + companyId + "' AND " + primaryTableAlias + ".IsActive='true' AND " + primaryTableAlias + ".IsDeleted='false'"; + } + } + } + #endregion + return result; + } + /// + /// 返回SQL的WHERE部分,如:IsActive='true' + /// + /// + public string GetWhere() + { + string result; + if (string.IsNullOrEmpty(this.whereCondition)) + { + result = "1=1"; + } + else + { + result = this.whereCondition.Substring(0, this.whereCondition.Length - 5); + } + return result; + } + #endregion + + #region 排序 + public void OrderBy(string fieldName, string direction) + { + + string tempOrderBy = fieldName + " " + direction + ","; + this.orderBy += tempOrderBy; + } + private string GetOrderBy() + { + string result = string.Empty; + if (!string.IsNullOrEmpty(this.orderBy)) + { + return " ORDER BY " + this.orderBy.Substring(0, this.orderBy.Length - 1); + } + else + { + return result; + } + } + #endregion + + #region 分组 + public void GroupBy(string fieldName) + { + this.groupBy += fieldName + ","; + } + private string GetGroupBy() + { + string result = string.Empty; + if (!string.IsNullOrEmpty(this.groupBy)) + { + return " GROUP BY " + this.groupBy.Substring(0, this.groupBy.Length - 1); + } + else + { + return result; + } + } + #endregion + + #region 返回SQL语句 + /// + /// 返回SQL语句 + /// + /// + public string GetSql() + { + string result = string.Empty; + result = "SELECT " + GetInternalSelect() + " FROM " + tableNames + " WHERE " + GetInternalWhere() + GetGroupBy() + GetOrderBy(); + return result; + } + #endregion + } + + public enum DateFormat + { + Year, + Quarter, + /// + /// YYYY/MM + /// + Month, + Week, + /// + /// YYYY/MM/DD + /// + Day, + /// + /// YYYY/MM/DD HH24 + /// + Hour, + /// + /// YYYY/MM/DD HH24:MI + /// + Minute, + /// + /// YYYY/MM/DD HH24:MI:SS + /// + Second + } +} diff --git a/Tiobon.Core.Common/DB/DbSql/DbUpdate.cs b/Tiobon.Core.Common/DB/DbSql/DbUpdate.cs new file mode 100644 index 00000000..0e02be8c --- /dev/null +++ b/Tiobon.Core.Common/DB/DbSql/DbUpdate.cs @@ -0,0 +1,472 @@ +using EU.Core.UserManager; +using System; +using System.Collections.Generic; +using System.Text; + +namespace EU.Core +{ + public class DbUpdate + { + #region 变量定义 + public string Database { get; set; } + private string sql; + private string whereCondition; + private string sqlTag; + private decimal tag; + public decimal Tag + { + get { return tag; } + set { tag = value; } + } + private bool isInitDefaultValue = true; + /// + /// 是否初始化默认字段,像:LAST_UPD_BY,LAST_UPD_DATE,LAST_UPD_PROGRAM,TAG,MODIFICATION_NUM等,默认会初始化 + /// + public bool IsInitDefaultValue + { + get + { + return isInitDefaultValue; + } + set { isInitDefaultValue = value; } + } + + private string updateProgram = string.Empty; + public string UpdateProgram + { + get { return updateProgram; } + set { updateProgram = value; } + } + #endregion + + #region 构造函数 + public DbUpdate(string tableName) + { + sql = "UPDATE {0} SET WHERE {1}"; + sql = string.Format(sql, tableName.ToUpper(), "1=1"); + sqlTag = "SELECT TAG FROM {0} WHERE {1}"; + sqlTag = string.Format(sqlTag, tableName.ToUpper(), "1=1"); + } + public DbUpdate(string tableName, string condition) + { + sql = "UPDATE {0} SET WHERE {1}"; + sql = string.Format(sql, tableName.ToUpper(), condition); + sqlTag = "SELECT TAG FROM {0} WHERE {1}"; + sqlTag = string.Format(sqlTag, tableName.ToUpper(), condition); + } + public DbUpdate(string tableName, string fieldName, string fieldValue) + { + sql = "UPDATE {0} SET WHERE {1} = N'{2}'"; + sql = string.Format(sql, tableName.ToUpper(), fieldName.ToUpper(), fieldValue); + sqlTag = "SELECT TAG FROM {0} WHERE {1} = N'{2}'"; + sqlTag = string.Format(sqlTag, tableName.ToUpper(), fieldName.ToUpper(), fieldValue); + } + public DbUpdate(string tableName, string fieldName, string fieldValue, string updateProgram) + { + sql = "UPDATE {0} SET WHERE {1} = N'{2}'"; + sql = string.Format(sql, tableName.ToUpper(), fieldName.ToUpper(), fieldValue); + sqlTag = "SELECT TAG FROM {0} WHERE {1} = N'{2}'"; + sqlTag = string.Format(sqlTag, tableName.ToUpper(), fieldName.ToUpper(), fieldValue); + this.updateProgram = updateProgram; + } + #endregion + + #region Set + public void Set(string fieldName, int value) + { + inset(fieldName.ToUpper(), Convert.ToString(value)); + } + + public void Set(string fieldName, decimal value) + { + inset(fieldName.ToUpper(), Convert.ToString(value)); + } + + public void Set(string fieldName, string value) + { + try + { + //if (value != null) + if (value != null) + { + value = value.Trim(); + } + if (!string.IsNullOrEmpty(value)) + { + string s = "N'{0}'"; + value = value.Replace("'", "''"); + s = string.Format(s, value); + inset(fieldName.ToUpper(), s); + } + else + { + inset(fieldName.ToUpper(), null); + } + } + catch (Exception) { throw; } + } + public void Set(string fieldName, Guid? value) + { + try + { + string value1 = value.ToString(); + //if (value != null) + if (value1 != null) + { + value1 = value1.Trim(); + } + if (!string.IsNullOrEmpty(value1)) + { + string s = "N'{0}'"; + value1 = value1.Replace("'", "''"); + s = string.Format(s, value1); + inset(fieldName.ToUpper(), s); + } + else + { + inset(fieldName.ToUpper(), null); + } + } + catch (Exception) + { + throw; + } + } + + public void Set(string fieldName, DateTime value) + { + if (value == DateTime.MinValue) return; + string valTemp = string.Empty; + valTemp = Convert.ToString(value); + valTemp = valTemp.Replace("'", "''"); + string s = string.Empty; + s = "CAST('{0}' AS DATETIME)"; + s = string.Format(s, valTemp); + + inset(fieldName.ToUpper(), s); + } + + public void Set(string fieldName, DateTime? value) + { + if (value == null) return; + string valTemp = string.Empty; + valTemp = Convert.ToString(value); + valTemp = valTemp.Replace("'", "''"); + string s = string.Empty; + s = "CAST('{0}' AS DATETIME)"; + s = string.Format(s, valTemp); + + inset(fieldName.ToUpper(), s); + } + /// + /// 设置计算类型的更新,如SetCompute("TAG","TAG+1") + /// + /// + /// + public void SetCompute(string fieldName, string value) + { + try + { + if (value != null) + { + value = value.Trim(); + } + inset(fieldName.ToUpper(), value); + } + catch (Exception) + { + throw; + } + } + + #region 加密更新 + /// + /// 加密保存 + /// + /// + /// + public void SetAsSecurity(string fieldName, string value) + { + if (string.IsNullOrEmpty(value)) + { + Set(fieldName, value); + } + else + { + string temp = "encryptbykey(key_guid('fookey'),N'" + value + "')"; + inset(fieldName.ToUpper(), temp); + } + } + /// + /// 加密保存 + /// + /// + /// + public void SetAsSecurity(string fieldName, int value) + { + SetAsSecurity(fieldName, Convert.ToString(value)); + } + /// + /// 加密保存 + /// + /// + /// + public void SetAsSecurity(string fieldName, decimal value) + { + SetAsSecurity(fieldName, Convert.ToString(value)); + } + #endregion + + + #endregion + + #region Where + public void Where(string whereCondition) + { + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, params string[] args) + { + this.whereCondition += string.Format(whereCondition, args) + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue) + { + string tempFieldValue = Convert.ToString(fieldValue); + tempFieldValue = tempFieldValue.Replace("'", "''"); + whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')"); + whereCondition = string.Format(whereCondition, tempFieldValue); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue1, DateTime fieldValue2) + { + string tempFieldValue1 = Convert.ToString(fieldValue1); + tempFieldValue1 = tempFieldValue1.Replace("'", "''"); + string tempFieldValue2 = Convert.ToString(fieldValue2); + tempFieldValue2 = tempFieldValue2.Replace("'", "''"); + whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')"); + whereCondition = string.Format(whereCondition, tempFieldValue1, tempFieldValue2); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue1, DateTime fieldValue2, DateTime fieldValue3) + { + string tempFieldValue1 = Convert.ToString(fieldValue1); + tempFieldValue1 = tempFieldValue1.Replace("'", "''"); + string tempFieldValue2 = Convert.ToString(fieldValue2); + tempFieldValue2 = tempFieldValue2.Replace("'", "''"); + string tempFieldValue3 = Convert.ToString(fieldValue3); + tempFieldValue3 = tempFieldValue3.Replace("'", "''"); + whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')"); + whereCondition = string.Format(whereCondition, tempFieldValue1, tempFieldValue2, tempFieldValue3); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue1, DateTime fieldValue2, DateTime fieldValue3, DateTime fieldValue4) + { + string tempFieldValue1 = Convert.ToString(fieldValue1); + tempFieldValue1 = tempFieldValue1.Replace("'", "''"); + string tempFieldValue2 = Convert.ToString(fieldValue2); + tempFieldValue2 = tempFieldValue2.Replace("'", "''"); + string tempFieldValue3 = Convert.ToString(fieldValue3); + tempFieldValue3 = tempFieldValue3.Replace("'", "''"); + string tempFieldValue4 = Convert.ToString(fieldValue4); + tempFieldValue4 = tempFieldValue4.Replace("'", "''"); + whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{3}','YYYY/MM/DD HH24:MI:SS')"); + whereCondition = string.Format(whereCondition, tempFieldValue1, tempFieldValue2, tempFieldValue3, tempFieldValue4); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue1, DateTime fieldValue2, DateTime fieldValue3, DateTime fieldValue4, DateTime fieldValue5) + { + string tempFieldValue1 = Convert.ToString(fieldValue1); + tempFieldValue1 = tempFieldValue1.Replace("'", "''"); + string tempFieldValue2 = Convert.ToString(fieldValue2); + tempFieldValue2 = tempFieldValue2.Replace("'", "''"); + string tempFieldValue3 = Convert.ToString(fieldValue3); + tempFieldValue3 = tempFieldValue3.Replace("'", "''"); + string tempFieldValue4 = Convert.ToString(fieldValue4); + tempFieldValue4 = tempFieldValue4.Replace("'", "''"); + string tempFieldValue5 = Convert.ToString(fieldValue5); + tempFieldValue5 = tempFieldValue5.Replace("'", "''"); + whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{3}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{4}','YYYY/MM/DD HH24:MI:SS')"); + whereCondition = string.Format(whereCondition, tempFieldValue1, tempFieldValue2, tempFieldValue3, tempFieldValue4, tempFieldValue5); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string whereCondition, DateTime fieldValue1, DateTime fieldValue2, DateTime fieldValue3, DateTime fieldValue4, DateTime fieldValue5, DateTime fieldValue6) + { + string tempFieldValue1 = Convert.ToString(fieldValue1); + tempFieldValue1 = tempFieldValue1.Replace("'", "''"); + string tempFieldValue2 = Convert.ToString(fieldValue2); + tempFieldValue2 = tempFieldValue2.Replace("'", "''"); + string tempFieldValue3 = Convert.ToString(fieldValue3); + tempFieldValue3 = tempFieldValue3.Replace("'", "''"); + string tempFieldValue4 = Convert.ToString(fieldValue4); + tempFieldValue4 = tempFieldValue4.Replace("'", "''"); + string tempFieldValue5 = Convert.ToString(fieldValue5); + tempFieldValue5 = tempFieldValue5.Replace("'", "''"); + string tempFieldValue6 = Convert.ToString(fieldValue6); + tempFieldValue6 = tempFieldValue6.Replace("'", "''"); + whereCondition = string.Format(whereCondition, "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{1}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{2}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{3}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{4}','YYYY/MM/DD HH24:MI:SS')", "DBO.TO_DATE('{4}','YYYY/MM/DD HH24:MI:SS')"); + whereCondition = string.Format(whereCondition, tempFieldValue1, tempFieldValue2, tempFieldValue3, tempFieldValue4, tempFieldValue5, tempFieldValue6); + this.whereCondition += whereCondition + " AND "; + } + public void Where(string fieldName, string condition, string fieldValue) + { + this.whereCondition += fieldName.ToUpper() + condition + "'" + fieldValue + "'" + " AND "; + } + public void Where(string fieldName, string condition, Guid? fieldValue) + { + this.whereCondition += fieldName.ToUpper() + condition + "'" + fieldValue + "'" + " AND "; + } + public void Where(string fieldName, string condition, Guid fieldValue) + { + this.whereCondition += fieldName.ToUpper() + condition + "'" + fieldValue + "'" + " AND "; + } + public void Where(string fieldName, string condition, decimal fieldValue) + { + this.whereCondition += fieldName.ToUpper() + condition + fieldValue + " AND "; + } + public void Where(string fieldName, string condition, DateTime fieldValue) + { + + string tempFieldValue = Convert.ToString(fieldValue); + tempFieldValue = tempFieldValue.Replace("'", "''"); + string tempValue = string.Empty; + tempValue = fieldName.ToUpper() + condition + "DBO.TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')" + " AND "; + tempValue = string.Format(tempValue, tempFieldValue); + this.whereCondition += tempValue; + } + /// + /// 对字段解密后进行比较 + /// + /// + /// + /// + public void WhereSecurity(string fieldName, string condition, string fieldValue) + { + this.whereCondition += "cast(decryptbykey(" + fieldName.ToUpper() + ") AS NVARCHAR(256))" + condition + "'" + fieldValue + "'" + " AND "; + } + /// + /// 对字段解密后进行比较 + /// + /// + /// + /// + public void WhereSecurity(string fieldName, string condition, decimal fieldValue) + { + this.whereCondition += "cast(decryptbykey(" + fieldName.ToUpper() + ") AS NVARCHAR(256))" + condition + fieldValue + " AND "; + } + /// + /// 对字段解密后进行比较 + /// + /// + /// + /// + public void WhereSecurity(string fieldName, string condition, int fieldValue) + { + this.whereCondition += "cast(decryptbykey(" + fieldName.ToUpper() + ") AS NVARCHAR(256))" + condition + fieldValue + " AND "; + } + #endregion + + #region 公有函数 + /// + /// 返回SQL语句的WHERE部分,如:DELETE_FLAG='N' + /// + /// + public string GetWhere() + { + string result; + if (string.IsNullOrEmpty(this.whereCondition)) + { + result = "1=1"; + } + else + { + result = this.whereCondition.Substring(0, this.whereCondition.Length - 5); + } + return result; + } + + /// + /// 获取完整SQL语句 + /// + /// + public string GetSql() + { + if (isInitDefaultValue == true) + { + InitDefaultValues(); + } + return sql + " AND " + GetWhere(); + } + #endregion + + #region Private Method + private void inset(string fieldName, string value) + { + sql = sql.Replace("{", "{{"); + sql = sql.Replace("}", "}}"); + int n = sql.IndexOf("SET WHERE"); + if (value == null) + { + if (n == -1) + { + n = sql.LastIndexOf(" WHERE"); + sql = sql.Insert(n, ", {0} = NULL "); + } + else + { + n = sql.LastIndexOf(" WHERE"); + sql = sql.Insert(n, " {0} = NULL "); + } + sql = string.Format(sql, fieldName.ToUpper()); + } + else + { + if (n == -1) + { + n = sql.IndexOf(" WHERE"); + sql = sql.Insert(n, ", {0} = {1} "); + } + else // first time + { + n = sql.IndexOf(" WHERE"); + sql = sql.Insert(n, " {0} = {1} "); + } + sql = string.Format(sql, fieldName.ToUpper(), value); + } + } + /// + /// 初始化Update语句默认需要更新的值 + /// + private void InitDefaultValues() + { + string tempSql = sql.ToUpper(); + //try + //{ + // if (UserContext.Current != null) + // { + // lastUpdBy = UserContext.Current.User_Id; + // } + //} + //catch { } + //if (tempSql.IndexOf("[LAST_UPD_BY]") == -1) + //{ + // Set("LAST_UPD_BY", lastUpdBy); + //} + + Guid? lastUpdBy = UserContext.Current.User_Id; + if (lastUpdBy != Guid.Empty) + Set("UpdateBy", lastUpdBy); + + if (tempSql.IndexOf("[LAST_UPD_DATE]") == -1) + { + Set("UpdateTime", DateTime.Now); + } + //if (tempSql.IndexOf("[LAST_UPD_PROGRAM]") == -1) + //{ + // Set("LAST_UPD_PROGRAM", updateProgram); + //} + } + + + #endregion + } +} diff --git a/Tiobon.Core.Model/Tiobon.Core.Model.csproj b/Tiobon.Core.Model/Tiobon.Core.Model.csproj index 6d6531f5..c39c05e0 100644 --- a/Tiobon.Core.Model/Tiobon.Core.Model.csproj +++ b/Tiobon.Core.Model/Tiobon.Core.Model.csproj @@ -20,6 +20,7 @@ +