1 not with you more nonsense on the code!
/// <summary> ///SQL keyword Converter/// </summary> Public classsqlconverter:ikeywordsconvertible { PublicSqlconverter (string[] keywords) {Keywords=keywords; } PublicSqlconverter () {}/// <summary> ///keyword Collection/// </summary> Public string[] Keywords {Get{returnkeywords;} Set { This. Keywords =New string[value. Length]; for(inti =0; I < value. Length; i++) { This. keywords[i] =Value[i]. ToLower (); } } } Private string[] keywords; /// <summary> ///character Buffers/// </summary> PrivateStringBuilder Charbuilder =NewStringBuilder (); /// <summary> ///symbol Buffers/// </summary> PrivateStringBuilder Symbobuilder =NewStringBuilder (); /// <summary> ///result Buffers/// </summary> PrivateStringBuilder Resbuilder =NewStringBuilder (); /// <summary> ///whether the previous character is a letter/// </summary> Private BOOLLastisletter; /// <summary> ///Temp Variable/// </summary> Private stringtemp; /// <summary> ///Conversion/// </summary> /// <param name= "source" >the string to convert</param> /// <returns>Conversion Results</returns> Public stringConvert (stringsource) {charbuilder.clear (); Symbobuilder.clear (); Resbuilder.clear (); Lastisletter=true; Temp=string. Empty; //Break the source string Char[] Chararray = source. toarray<Char>(); //Traverse foreach(varCinchChararray) { if(c >='a'&& C <='Z') || (c >='A'&& C <='Z')) { //If the previous symbol is not a letter, push the symbol buffer if(!lastisletter) {Pushsymbols (); } charbuilder.append (c); Lastisletter=true; } Else { //If the previous symbol is a letter, push the letter buffer if(lastisletter) {pushletters (); } symbobuilder.append (c); Lastisletter=false; } } //processing the last buffer if(lastisletter) {pushletters (); } Else{pushsymbols (); } returnresbuilder.tostring (); } /// <summary> ///push the character buffer to the target buffer/// </summary> Private voidpushletters () {temp=charbuilder.tostring (); if(Keywords.contains (temp. ToLower ())) {Resbuilder.append (temp. ToUpper ()); } Else{resbuilder.append (temp); } charbuilder.clear (); } /// <summary> ///pushing a symbol buffer to the target buffer/// </summary> Private voidPushsymbols () {resbuilder.append (symbobuilder.tostring ()); Symbobuilder.clear (); } }View Code
2 principle
First step: Take a SQL statement string apart and split it into string-symbol string-string-symbol string-symbol string-string so
The second step: then determine whether the string is a keyword, yes, then turn it into uppercase
Part III: Put these strings together again
3 implementation
The principle seems very simple, but the implementation is not simple.
To handle two questions
1 cannot be converted after the processing of stitching, so it must be stitching edge conversion
2 status switch, when to push
Implementation steps
1 source string broken into char array
2 stitching focuses on determining how to determine the letter and symbol status, as well as switching the buffer to push in the state. See the code for details
3 finally do it again because the last string after the traversal has no chance of being pushed
4 Use Effect
5 PostScript
Recently the company modified the SQL specification, require SQL keyword capitalization, eh, I wrote so many did not go online, have to change. As a programmer, this must be done in code.
So, I do Baidu! To me not to write myself, I am lazy.
Hundred to a tool, for the sake of safety, I decompile the tool, looked at the code. No danger. But Ah, can not understand, the function is realized.
So I first used to change my SQL.
But I think, I do not understand Ah, did not learn. I want to realize it myself!
So the next few days I began to think about how to capitalize the SQL keyword, without affecting the other parts, including the carriage return of these invisible symbols (it is not possible to do the segmentation with invisible symbols, because if there is a connected unsigned, the cut will be lost).
At the company's annual meeting
I finally thought, look at my side of the sisters (my colleague), Ah, very excited.
Using my spare time to write this, independent thinking does not refer to the code of the tool I am looking for.
The core algorithms are sent out to learn and communicate together.
SQL keyword conversion capitalization core algorithm implementation