Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 45 additions & 45 deletions SQL/SqlCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
namespace CYQ.Data.SQL
{
/// <summary>
/// ���ݲ��������
/// 数据操作语句类
/// </summary>
internal partial class SqlCreate
{
private static List<string> _autoIDItems = new List<string>();
/// <summary>
/// oracle��������
/// oracle序列名称
/// </summary>
public string AutoID
{
Expand All @@ -28,23 +28,23 @@ 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);
}
return key;
}
}
/// <summary>
/// �Ƿ�����ִ��SQL����������Insert��Update��Ч�����SQL�﷨������ܾ�ִ�У�
/// 是否允许执行SQL操作(仅对Insert和Update有效;如果SQL语法错误,则拒绝执行)
/// </summary>
internal bool isCanDo = true;
/// <summary>
/// ���²����ĸ��ӱ���ʽ��
/// 更新操作的附加表达式。
/// </summary>
internal string updateExpression = null;
/// <summary>
/// ָ����ѯ���С�
/// 指定查询的列。
/// </summary>
internal object[] selectColumns = null;
private string TableName
Expand All @@ -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();
Expand All @@ -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") + "',";//如果存在更新列
}
}
}
Expand All @@ -90,9 +90,9 @@ internal string GetDeleteSql(object whereObj)
return "delete from " + TableName + " where " + FormatWhere(whereObj);
}
/// <summary>
/// ���ز�����ַ���
/// 返回插入的字符串
/// </summary>
/// <returns>�����:insert into tableName(ID,Name,Value) values(@ID,@Name,@Value)</returns>
/// <returns>结果如:insert into tableName(ID,Name,Value) values(@ID,@Name,@Value)</returns>
internal string GetInsertSql()
{
isCanDo = false;
Expand All @@ -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();
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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));
//}
Expand All @@ -184,9 +184,9 @@ internal string GetInsertSql()
return sql;
}
/// <summary>
/// ���ز�����Where�������ַ���
/// 返回不包括Where条件的字符串
/// </summary>
/// <returns>�����:Update tableName set Name=@Name,Value=@Value</returns>
/// <returns>结果如:Update tableName set Name=@Name,Value=@Value</returns>
internal string GetUpdateSql(object whereObj)
{
isCanDo = false;
Expand All @@ -195,35 +195,35 @@ 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++)
{
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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -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";
//}
Expand All @@ -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) + ",";
Expand All @@ -386,15 +386,15 @@ internal string GetColumnsSql()
return v_Columns.TrimEnd(',');
}
/// <summary>
/// ��ȡ������ϵ�Where������
/// 获取主键组合的Where条件。
/// </summary>
/// <returns></returns>
internal string GetPrimaryWhere()
{
return GetWhere(_action.DalType, _action.Data.JointPrimaryCell);
}
#endregion
#region ���ú���
#region 共用函数

internal string AddOrderByWithCheck(string where, string primaryKey)
{
Expand Down Expand Up @@ -448,7 +448,7 @@ private string GetWhereFromObj(object whereObj)
string where = string.Empty;
if (cell != null)
{
#region �ӵ�Ԫ����ȡֵ��
#region 从单元格里取值。
if (cell.IsNullOrEmpty)
{
isCanDo = false;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}
Expand All @@ -619,10 +619,10 @@ private static string GetWhereEqual(int groupID, string columnName, string where

switch (dalType)
{
case DalType.Access:// Access��GUID���͵ĸ��£����������������
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) + "'";
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -662,7 +662,7 @@ internal static string GetWhere(DalType dalType, List<MDataCell> cells)
return GetWhere(dalType, true, cells);
}
/// <summary>
/// ����Ԫ���������where������
/// 根据元数据列组合where条件。
/// </summary>
/// <returns></returns>
internal static string GetWhere(DalType dalType, bool isAnd, List<MDataCell> cells)
Expand Down Expand Up @@ -708,7 +708,7 @@ internal static string GetWhere(DalType dalType, bool isAnd, List<MDataCell> 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)
{
Expand Down Expand Up @@ -747,11 +747,11 @@ internal static string GetWhereIn(MCellStruct ms, List<string> 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))
Expand Down Expand Up @@ -792,7 +792,7 @@ internal static string GetWhereIn(MCellStruct ms, List<string> items, DalType da
internal static string OracleSqlIDR = " userid={0} control='{1}'";//sqlldr
internal static string TruncateTable = "truncate table {0}";
/// <summary>
/// ������������������
/// 获得批量导入的列名。
/// </summary>
/// <param name="mdc"></param>
/// <param name="keepID"></param>
Expand All @@ -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(",");
Expand Down