From b644eaabe538c364bb3350ff6a562dcd54904d7e Mon Sep 17 00:00:00 2001 From: enh Date: Wed, 15 Mar 2017 15:17:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=97=B6=E9=97=B4=E6=97=B6=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SQL/SqlCreate.cs | 90 ++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/SQL/SqlCreate.cs b/SQL/SqlCreate.cs index 1d71509e..657c230a 100644 --- a/SQL/SqlCreate.cs +++ b/SQL/SqlCreate.cs @@ -13,13 +13,13 @@ namespace CYQ.Data.SQL { /// - /// ݲ + /// 数据操作语句类 /// internal partial class SqlCreate { private static List _autoIDItems = new List(); /// - /// oracle + /// oracle序列名称 /// public string AutoID { @@ -28,7 +28,7 @@ public string AutoID string key = string.Format(AppConfig.DB.AutoID, TableName); if (!_autoIDItems.Contains(key)) { - //ⲢԶ + //检测并自动创建。 Tool.DBTool.CheckAndCreateOracleSequence(key, _action.dalHelper.conn, _action.Data.PrimaryCell.ColumnName, TableName); _autoIDItems.Add(key); } @@ -36,15 +36,15 @@ public string AutoID } } /// - /// ǷִSQLInsertUpdateЧSQL﷨ִܾУ + /// 是否允许执行SQL操作(仅对Insert和Update有效;如果SQL语法错误,则拒绝执行) /// internal bool isCanDo = true; /// - /// ²ĸӱʽ + /// 更新操作的附加表达式。 /// internal string updateExpression = null; /// - /// ָѯС + /// 指定查询的列。 /// internal object[] selectColumns = null; private string TableName @@ -60,8 +60,8 @@ public SqlCreate(MAction action) _action = action; } - #region Sql - #region SQL䴦 + #region 组合Sql语句 + #region SQL语句处理 internal string GetDeleteToUpdateSql(object whereObj) { string editTime = GetEditTimeSql(); @@ -78,7 +78,7 @@ private string GetEditTimeSql() { if (_action.Data.Columns.Contains(item) && (_action.Data[item].IsNullOrEmpty || _action.Data[item].State < 2)) { - return item + "='" + DateTime.Now + "',";//ڸ + return item + "='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',";//如果存在更新列 } } } @@ -90,9 +90,9 @@ internal string GetDeleteSql(object whereObj) return "delete from " + TableName + " where " + FormatWhere(whereObj); } /// - /// زַ + /// 返回插入的字符串 /// - /// :insert into tableName(ID,Name,Value) values(@ID,@Name,@Value) + /// 结果如:insert into tableName(ID,Name,Value) values(@ID,@Name,@Value) internal string GetInsertSql() { isCanDo = false; @@ -104,7 +104,7 @@ internal string GetInsertSql() int groupID = DataType.GetGroup(primaryCell.Struct.SqlType); string defaultValue = Convert.ToString(primaryCell.Struct.DefaultValue); if (primaryCell.IsNullOrEmpty && (groupID == 4 || (groupID == 0 && (primaryCell.Struct.MaxSize <= 0 || primaryCell.Struct.MaxSize >= 36) && - (defaultValue == "" || defaultValue == "newid" || defaultValue == SqlValue.Guid))))//guid + (defaultValue == "" || defaultValue == "newid" || defaultValue == SqlValue.Guid))))//guid类型 { primaryCell.Value = Guid.NewGuid(); } @@ -114,7 +114,7 @@ internal string GetInsertSql() cell = _action.Data[i]; if (cell.Struct.IsAutoIncrement && !_action.AllowInsertID) { - continue;//С + continue;//跳过自增列。 } if (cell.IsNull && !cell.Struct.IsCanNull && cell.Struct.DefaultValue == null) { @@ -168,12 +168,12 @@ internal string GetInsertSql() { sql += string.Format(" select '{0}' as OutPutValue", primaryCell.Value); } - if (_action.AllowInsertID && !_action.dalHelper.isOpenTrans && primaryCell.Struct.IsAutoIncrement)//ʱ + if (_action.AllowInsertID && !_action.dalHelper.isOpenTrans && primaryCell.Struct.IsAutoIncrement)//非批量操作时 { sql = "set identity_insert " + SqlFormat.Keyword(TableName, _action.dalHelper.dalType) + " on " + sql + " set identity_insert " + SqlFormat.Keyword(TableName, _action.dalHelper.dalType) + " off"; } break; - //if (!(Parent.AllowInsertID && !primaryCell.IsNull)) // вIDģԶID + //if (!(Parent.AllowInsertID && !primaryCell.IsNull)) // 对于自行插入ID的,跳过,主操作会自动返回ID。 //{ // sql += ((groupID == 1 && (primaryCell.IsNull || primaryCell.ToString() == "0")) ? " select cast(scope_Identity() as int) as OutPutValue" : string.Format(" select '{0}' as OutPutValue", primaryCell.Value)); //} @@ -184,9 +184,9 @@ internal string GetInsertSql() return sql; } /// - /// زWhereַ + /// 返回不包括Where条件的字符串 /// - /// :Update tableName set Name=@Name,Value=@Value + /// 结果如:Update tableName set Name=@Name,Value=@Value internal string GetUpdateSql(object whereObj) { isCanDo = false; @@ -195,13 +195,13 @@ internal string GetUpdateSql(object whereObj) if (!string.IsNullOrEmpty(updateExpression)) { _TempSql.Append(SqlCompatible.Format(updateExpression, _action.DalType) + ","); - updateExpression = null;//ȡֵֵ + updateExpression = null;//取完值后清除值。 isCanDo = true; } - string editTime = GetEditTimeSql();//ڲжϸֶûֵŻ¡ + string editTime = GetEditTimeSql();//内部判断该字段没有值才会更新。 if (!string.IsNullOrEmpty(editTime)) { - _TempSql.Append(editTime);//Դβ, + _TempSql.Append(editTime);//自带尾,号 } MDataCell cell = null; for (int i = 0; i < _action.Data.Count; i++) @@ -209,21 +209,21 @@ internal string GetUpdateSql(object whereObj) cell = _action.Data[i]; if (cell.Struct.IsPrimaryKey || cell.Struct.IsAutoIncrement) { - continue;//С + continue;//跳过自增或主键列。 } if (cell.CellValue.State > 1 && (cell.Struct.IsCanNull || !cell.IsNull)) { if (cell.Struct.SqlType == SqlDbType.Timestamp && (_action.DalType == DalType.MsSql || _action.DalType == DalType.Sybase)) { - //ʱ¡ + //更新时间戳不允许更新。 continue; } object value = cell.Value; DbType dbType = DataType.GetDbType(cell.Struct.SqlType.ToString(), _action.DalType); if (_action.DalType == DalType.Oracle && dbType == DbType.String && cell.strValue == "" && !cell.Struct.IsCanNull) { - value = " ";//Oracle not null ֶΣÿֵ + value = " ";//Oracle not null 字段,不允许设置空值。 } _action.dalHelper.AddParameters(_action.dalHelper.Pre + cell.ColumnName, value, dbType, cell.Struct.MaxSize, ParameterDirection.Input); @@ -322,7 +322,7 @@ internal string GetSelectTableName(ref string where) { tName = tName.Substring(0, i + 1); } - if (selectColumns == null || selectColumns.Length == 0)//ûָҪϲѯ + if (selectColumns == null || selectColumns.Length == 0)//没有指定要组合查询。 { return SqlFormat.Keyword(tName, _action.DalType); } @@ -332,7 +332,7 @@ internal string GetSelectTableName(ref string where) int orderbyIndex = where.ToLower().IndexOf("order by"); if (orderbyIndex > -1) { - whereOnly = " where " + where.Substring(0, orderbyIndex - 1);//-1ȥո + whereOnly = " where " + where.Substring(0, orderbyIndex - 1);//-1是去掉空格 where = "1=1 " + where.Substring(orderbyIndex); } else @@ -343,7 +343,7 @@ internal string GetSelectTableName(ref string where) whereOnly = SqlFormat.RemoveWhereOneEqualsOne(whereOnly); } string sql = "(select " + GetColumnsSql() + " from " + TableName + " " + whereOnly + ") v"; - //if (_action.DalType != DalType.Oracle) // Oracle ȡ˴洢̡ + //if (_action.DalType != DalType.Oracle) // Oracle 取消了存储过程。 //{ // sql += "v"; //} @@ -368,7 +368,7 @@ internal string GetColumnsSql() } else { - int i = _action.Data.Columns.GetIndex(columnName);//ֶӳ + int i = _action.Data.Columns.GetIndex(columnName);//兼容字段映射 if (i > -1) { v_Columns += SqlFormat.Keyword(_action.Data.Columns[i].ColumnName, _action.dalHelper.dalType) + ","; @@ -386,7 +386,7 @@ internal string GetColumnsSql() return v_Columns.TrimEnd(','); } /// - /// ȡϵWhere + /// 获取主键组合的Where条件。 /// /// internal string GetPrimaryWhere() @@ -394,7 +394,7 @@ internal string GetPrimaryWhere() return GetWhere(_action.DalType, _action.Data.JointPrimaryCell); } #endregion - #region ú + #region 共用函数 internal string AddOrderByWithCheck(string where, string primaryKey) { @@ -448,7 +448,7 @@ private string GetWhereFromObj(object whereObj) string where = string.Empty; if (cell != null) { - #region ӵԪȡֵ + #region 从单元格里取值。 if (cell.IsNullOrEmpty) { isCanDo = false; @@ -511,7 +511,7 @@ internal static string FormatWhere(string where, MDataColumn mdc, DalType dalTyp { if (mdc.JointPrimary.Count > 1 && where.Contains(";")) { - #region + #region 多个主键 StringBuilder sb = new StringBuilder(); string[] items = where.Split(','); MDataRow row = mdc.ToRow("row"); @@ -545,15 +545,15 @@ internal static string FormatWhere(string where, MDataColumn mdc, DalType dalTyp } else { - #region + #region 单个主键 MCellStruct ms = mdc.FirstPrimary; string[] items = where.Split(','); if (items.Length == 1) { - //ֵֻ - int primaryGroupID = DataType.GetGroup(ms.SqlType);//ƥ + //只处理单个值的情况 + int primaryGroupID = DataType.GetGroup(ms.SqlType);//优先匹配主键 switch (primaryGroupID) { case 4: @@ -608,7 +608,7 @@ private static string GetWhereEqual(int groupID, string columnName, string where { where = where.Trim('\''); } - if (groupID == 1)//int ͵ģΪbit͡ + if (groupID == 1)//int 类型的,主键不为bit型。 { where = columnName + "=" + where; } @@ -619,10 +619,10 @@ private static string GetWhereEqual(int groupID, string columnName, string where switch (dalType) { - case DalType.Access:// AccessGUID͵ĸ£ + case DalType.Access:// Access的GUID类型的更新,必须带{}包含。 where = "{" + where.Trim('{', '}') + "}"; break; - case DalType.SQLite://SQLite 16ֽڴ洢Ҫתܲѯ + case DalType.SQLite://SQLite 以16字节存储,需要转换才能查询。 return columnName + "=x'" + StaticTool.ToGuidByteString(where) + "'"; } } @@ -652,7 +652,7 @@ internal static string AddOrderBy(string where, string primaryKey) } else if (where.IndexOf(" asc", StringComparison.OrdinalIgnoreCase) == -1 && where.IndexOf(" desc", StringComparison.OrdinalIgnoreCase) == -1) { - //order by û asc + //有order by 但没 asc where += " asc"; } return where; @@ -662,7 +662,7 @@ internal static string GetWhere(DalType dalType, List cells) return GetWhere(dalType, true, cells); } /// - /// Ԫwhere + /// 根据元数据列组合where条件。 /// /// internal static string GetWhere(DalType dalType, bool isAnd, List cells) @@ -708,7 +708,7 @@ internal static string GetWhere(DalType dalType, bool isAnd, List cel where += columnName + "='" + guid + "'"; } } - else if (groupID == 2 && dalType == DalType.Oracle) // OracleʱҪת + else if (groupID == 2 && dalType == DalType.Oracle) // Oracle的日期时间要转类型 { if (cell.Struct.SqlType == SqlDbType.Timestamp) { @@ -747,11 +747,11 @@ internal static string GetWhereIn(MCellStruct ms, List items, DalType da item = items[i]; if (groupID != 0) { - item = item.Trim('\'');//ĸȥֺ + item = item.Trim('\'');//不是字母都尝试去掉分号 } - if (dalType == DalType.Oracle && i > 999 && i % 1000 == 0)//oracle беΪ1000 + if (dalType == DalType.Oracle && i > 999 && i % 1000 == 0)//oracle 列表中的最大表达数为1000 { - sb.Remove(sb.Length - 1, 1);//Ƴһ,š + sb.Remove(sb.Length - 1, 1);//移除最后一个,号。 sb.Append(") or " + SqlFormat.Keyword(ms.ColumnName, dalType) + " In ("); } if (!string.IsNullOrEmpty(item)) @@ -792,7 +792,7 @@ internal static string GetWhereIn(MCellStruct ms, List items, DalType da internal static string OracleSqlIDR = " userid={0} control='{1}'";//sqlldr internal static string TruncateTable = "truncate table {0}"; /// - /// + /// 获得批量导入的列名。 /// /// /// @@ -811,7 +811,7 @@ internal static string GetColumnName(MDataColumn mdc, bool keepID, DalType dalTy sb.Append(SqlFormat.Keyword(ms.ColumnName, dalType)); if (dalType == DalType.Oracle && DataType.GetGroup(ms.SqlType) == 2) { - // + //日期 sb.Append(" DATE \"YYYY-MM-DD HH24:MI:SS\" NULLIF (" + ms.ColumnName + "=\"NULL\")"); } sb.Append(",");