public virtual async Task <ExecuteResult> ExecuteSql_InsertUpdate(DbCommand command, string sql, int readMaxRows, int changeMaxRows) { var m = GetInsertUpdateRegex().Match(sql); ExecuteResult result = new ExecuteResult(); if (m.Success) { result.IsException = true; result.Exception = "此SQL语句包含“" + m.Value + "”关键字!"; return(result); } var list = SqlStringList.SplitString(sql); var sqlList = list.SplitBySemi(); try { result.Result = new List <ExecuteResultItem>(); bool isRollback = false; using (var tran = await command.Connection.BeginTransactionAsync()) { foreach (var item in sqlList) { ExecuteResultItem sqlResult = new ExecuteResultItem(); result.Result.Add(sqlResult); var b = await CheckUpdateDelete(command, sqlResult, item, changeMaxRows); if (b) { b = await ExecuteSql(command, sqlResult, item, readMaxRows); } if (b == false) { result.IsException = true; result.Exception = sqlResult.Exception; result.Result = null; isRollback = true; tran.Rollback(); break; } } if (isRollback == false) { await tran.CommitAsync(); } } } catch (Exception x) { result.IsException = true; result.Exception = x.Message; result.Result = null; } return(result); }
public virtual async Task <ExecuteResult> ExecuteSql_AllPermissions(DbCommand command, string sql) { var list = SqlStringList.SplitString(sql); var sqlList = list.SplitBySemi(); ExecuteResult result = new ExecuteResult(); try { result.Result = new List <ExecuteResultItem>(); bool isRollback = false; using (var tran = await command.Connection.BeginTransactionAsync()) { foreach (var item in sqlList) { ExecuteResultItem sqlResult = new ExecuteResultItem(); result.Result.Add(sqlResult); var b = await ExecuteSql(command, sqlResult, item, int.MaxValue); if (b == false) { result.IsException = true; result.Exception = sqlResult.Exception; result.Result = null; isRollback = true; tran.Rollback(); break; } } if (isRollback == false) { await tran.CommitAsync(); } } } catch (Exception x) { result.IsException = true; result.Exception = x.Message; result.Result = null; } return(result); }
public virtual async Task <ExecuteResult> ExecuteSql_Select(DbCommand command, string sql, int readMaxRows) { var m = GetSelectRegex().Match(sql); ExecuteResult result = new ExecuteResult(); if (m.Success) { result.IsException = true; result.Exception = "此SQL语句包含“" + m.Value + "”关键字!"; return(result); } var list = SqlStringList.SplitString(sql); var sqlList = list.SplitBySemi(); try { result.Result = new List <ExecuteResultItem>(); foreach (var item in sqlList) { ExecuteResultItem sqlResult = new ExecuteResultItem(); result.Result.Add(sqlResult); var b = await ExecuteSql(command, sqlResult, item, readMaxRows); if (b == false) { result.IsException = true; result.Exception = sqlResult.Exception; result.Result = null; break; } } } catch (Exception x) { result.IsException = true; result.Exception = x.Message; result.Result = null; } return(result); }
public static SqlStringList SplitString(string sql) { SqlStringList list = new SqlStringList(); bool isInText = false; bool isInComment = false; bool isInLineComment = false; var c = 'a'; var index = 0; var text = ""; while (index < sql.Length) { var ch = sql[index++]; if (isInLineComment) { if (ch == '\r' && index < sql.Length && sql[index] == '\n') { isInLineComment = false; list.Add(text, SqlToken.Comment); list.Add("\r\n", SqlToken.Whitespace); text = ""; } else if (ch == '\r' || ch == '\n') { isInLineComment = false; list.Add(text, SqlToken.Comment); list.Add(ch.ToString(), SqlToken.Whitespace); text = ""; } else { text += ch; } } else if (isInComment) { text += ch; if (ch == '*' && index < sql.Length && sql[index] == '/') { isInComment = false; list.Add(text + "/", SqlToken.Comment); text = ""; index++; } } else if (isInText) { text += ch; if (c == '\\') { text += sql[index++]; } else if (c == '[') { if (ch == ']') { isInText = false; list.Add(text, SqlToken.Field); text = ""; } } else if (c == '\'') { if (ch == '\'' && index < sql.Length && sql[index] == '\'') { text += sql[index++]; break; } if (ch == '\'') { isInText = false; list.Add(text, SqlToken.Field); text = ""; } } else if (c == '"') { if (ch == '"') { isInText = false; list.Add(text, SqlToken.Field); text = ""; } } else if (c == '`') { if (ch == '`') { isInText = false; list.Add(text, SqlToken.Field); text = ""; } } } else if (ch == '#') { isInLineComment = true; list.Add(text, SqlToken.Field); text = "#"; } else if (ch == '-' && index < sql.Length && sql[index] == '-') { isInLineComment = true; list.Add(text, SqlToken.Field); text = "--"; index++; } else if (ch == '/' && index < sql.Length && sql[index] == '*') { isInComment = true; list.Add(text, SqlToken.Field); text = "/*"; index++; } else if (ch == '[' || ch == '\'' || ch == '"' || ch == '`') { c = ch; isInText = true; list.Add(text, SqlToken.Field); text = ch.ToString(); } else if (ch == '(' || ch == '{') { list.Add(text, SqlToken.BracketOpen); text = ch.ToString(); } else if (ch == ')' || ch == '}') { list.Add(text, SqlToken.BracketClose); text = ch.ToString(); } else if (ch == ',' || ch == '.' || ch == ':') { list.Add(text, SqlToken.Field); list.Add(ch.ToString(), SqlToken.Other); text = ""; } else if (ch == ';') { list.Add(text, SqlToken.Field); list.Add(ch.ToString(), SqlToken.Semi); text = ""; } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '=' || ch == ':' || ch == '~' || ch == '|' || ch == '&' || ch == '^' || ch == '<' || ch == '>' || ch == '!') { list.Add(text, SqlToken.Field); list.Add(ch.ToString(), SqlToken.Operator); text = ""; } else if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { list.Add(text, SqlToken.Field); list.Add(ch.ToString(), SqlToken.Whitespace); text = ""; } else { text += ch; } } if (isInComment || isInLineComment) { list.Add(text, SqlToken.Comment); } else { list.Add(text, SqlToken.Field); } list.RemoveAll(q => string.IsNullOrEmpty(q.SqlField)); list.SetLayer(); return(list); }