/// <summary> /// SQL 서버용 키워드는 소문자 또는 대문자로 변경해서 리턴함. /// </summary> /// <param name="Stmt">SQL문 자체, 또는 SQL문을 포함하는 문자열</param> /// <returns>변환된 문자열</returns> /// <example> /// <code> /// string Sql = "select * from mytable where id = 'between a to b' and name like '박%'"; /// string s = CSql.GetSqlServerKeywordTo(Sql, true); /// Console.WriteLine(s); /// //--결과 /// //SELECT * FROM mytable WHERE id = 'between a to b' AND name LIKE '박%' /// </code> /// </example> private static string GetSqlServerKeywordTo(string Stmt, bool IsUcase) { string[] aKeywords = GetSqlServerKeyword(); SortedList slIndexValue = new SortedList(); // \w+: 연속된(+) 글자(w), 즉 단어 // ': 작은 따옴표 int QuoteEnd = -1; Regex r = new Regex(@"\w+|'", RegexOptions.Compiled); for (Match m = r.Match(Stmt); m.Success; m = m.NextMatch()) { if (m.Index <= QuoteEnd) { continue; } switch (m.Value) { case "'": QuoteEnd = CDelim.GetQuoteEnd(Stmt, m.Index, '\''); break; default: if (CArray.IndexOf(aKeywords, m.Value, true) >= 0) { slIndexValue.Add(m.Index, new string[] { m.Value, (IsUcase ? m.Value.ToUpper() : m.Value.ToLower()) }); } else { slIndexValue.Add(m.Index, new string[] { m.Value, m.Value }); } break; } } string s = GetIndexValueMerged(Stmt, slIndexValue); return(s); }
/// <summary> /// /// </summary> /// <param name="Stmt"></param> /// <param name="aTableFieldName"></param> /// <param name="CaseFrom"></param> /// <param name="CaseTo"></param> /// <param name="IsReplaceOnlyInDoubleQuote"></param> /// <returns></returns> /// <example> /// <code> /// string[] aNamePascal = new string[] { "UserName", "UserId" }; /// string s = "SELECT a.UserName, a.UserId, a.UserJumin FROM UserInfo AS a WHERE a.UserId LIKE 'UserId%'"; /// /// string s2 = CSql.GetTableFieldNameToCase(s, aNamePascal, CSql.CaseTypes.PascalCase, CSql.CaseTypes.UpperCaseUnderbar); /// Console.WriteLine(s2); /// //--결과 /// //SELECT a.USER_NAME, a.USER_ID, a.UserJumin FROM UserInfo AS a WHERE a.USER_ID LIKE 'UserId%' /// /// string[] aNameUpper = new string[] { "USER_NAME", "USER_ID" }; /// string s3 = CSql.GetTableFieldNameToCase(s2, aNameUpper, CSql.CaseTypes.UpperCaseUnderbar, CSql.CaseTypes.PascalCase); /// Console.WriteLine(s3); /// //--결과 /// //SELECT a.UserName, a.UserId, a.UserJumin FROM UserInfo AS a WHERE a.UserId LIKE 'UserId%' /// </code> /// </example> public static string GetTableFieldNameToCase(string Stmt, string[] aTableFieldName, CaseTypes CaseFrom, CaseTypes CaseTo, bool IsReplaceOnlyInDoubleQuote) { string[] aKeywords = GetSqlServerKeyword(); SortedList slIndexValue = new SortedList(); // \w+: 연속된(+) 글자(w), 즉 단어 // ': 작은 따옴표 // ": 큰 따옴표 bool IsDoubleQuotStarted = false; int QuoteEnd = -1; Regex r = new Regex(@"\w+|'|""", RegexOptions.Compiled); for (Match m = r.Match(Stmt); m.Success; m = m.NextMatch()) { if (m.Index <= QuoteEnd) { continue; } switch (m.Value) { case "\"": if (IsDoubleQuotStarted) { IsDoubleQuotStarted = false; } else { IsDoubleQuotStarted = true; } break; case "'": QuoteEnd = CDelim.GetQuoteEnd(Stmt, m.Index, '\''); break; default: bool IsReplace = true; //따옴표 안의 문자열만 변경하는 옵션일 때 따옴표 안에 있지 않거나, //현재 Value가 Table, Field 이름이 아닌 경우. if ((IsReplaceOnlyInDoubleQuote && !IsDoubleQuotStarted) || (CArray.IndexOf(aTableFieldName, m.Value, true) == -1)) { IsReplace = false; } if (IsReplace) { string Value = GetCaseConverted(m.Value, CaseFrom, CaseTo); slIndexValue.Add(m.Index, new string[] { m.Value, Value }); } else { slIndexValue.Add(m.Index, new string[] { m.Value, m.Value }); } break; } } string s = GetIndexValueMerged(Stmt, slIndexValue); return(s); }
/// <summary> /// 태그를 삽입할 위치와 태그 문자열 정보를 NameValueCollection으로 리턴함. /// </summary> /// <param name="Code"></param> /// <returns></returns> private NameValueCollection GetPosAndTag(string Code) { NameValueCollection nv = new NameValueCollection(); // \w+: 연속된(+) 글자(w), 즉 단어 // //: 한줄 주석 // /\*: 여러줄 주석 시작 // @": @-quoted // ": quoted // ': single quoted int PosCur = 0; Regex r = new Regex(@"\w+|//|/\*|@""|""|'", RegexOptions.Compiled); for (Match m = r.Match(Code) /*1*/; m.Success /*2*/; m = m.NextMatch()) /*3*/ { if (m.Index < PosCur) { continue; } switch (m.Value) { case @"//": PosCur = GetCommentEnd(Code, m.Index) + 1; if (PosCur != 0) { nv.Add(m.Index.ToString(), drCommentTagStart); nv.Add(PosCur.ToString(), drTagEnd); } else { return(GetErrInfo(nv, m.Index, Code.Length)); } break; case @"/*": PosCur = GetMultiCommentEnd(Code, m.Index) + 1; if (PosCur != 0) { nv.Add(m.Index.ToString(), drCommentTagStart); nv.Add(PosCur.ToString(), drTagEnd); } else { return(GetErrInfo(nv, m.Index, Code.Length)); } break; case @"@""": PosCur = GetAtQuoteEnd(Code, m.Index) + 1; if (PosCur != 0) { nv.Add(m.Index.ToString(), drQuoteTagStart); nv.Add(PosCur.ToString(), drTagEnd); } else { return(GetErrInfo(nv, m.Index, Code.Length)); } break; case @"""": PosCur = CDelim.GetQuoteEnd(Code, m.Index, '"') + 1; if (PosCur != 0) { nv.Add(m.Index.ToString(), drQuoteTagStart); nv.Add(PosCur.ToString(), drTagEnd); } else { return(GetErrInfo(nv, m.Index, Code.Length)); } break; case @"'": PosCur = CDelim.GetQuoteEnd(Code, m.Index, '\'') + 1; if (PosCur != 0) { nv.Add(m.Index.ToString(), drQuoteTagStart); nv.Add(PosCur.ToString(), drTagEnd); } else { return(GetErrInfo(nv, m.Index, Code.Length)); } break; default: // \w+ if ((CArray.IndexOf(aKeywords, m.Value, true) >= 0) || (CArray.IndexOf(aDataTypes, m.Value, true) >= 0) || (CArray.IndexOf(aOperators, m.Value, true) >= 0) || (CArray.IndexOf(aLiterals, m.Value, true) >= 0)) { nv.Add(m.Index.ToString(), drKeywordTagStart); nv.Add((m.Index + m.Value.Length).ToString(), drTagEnd); } break; } } //마지막 문자열을 GetNewCode에서 읽을 수 있도록 위치만을 지정함. //if (!DrCollection.HasKey(nv, Code.Length.ToString())) if (nv[Code.Length.ToString()] == null) { nv.Add(Code.Length.ToString(), ""); } return(nv); }