SQL keyword conversion Core Algorithm Implementation, SQL keyword capital Algorithm

Source: Internet
Author: User

SQL keyword conversion Core Algorithm Implementation, SQL keyword capital Algorithm

1. Don't talk nonsense about code with you!

/// <Summary> // SQL keyword converter // </summary> public class SqlConverter: IKeywordsConvertible {public SqlConverter (string [] keywords) {Keywords = keywords ;} public SqlConverter () {}/// <summary> // keyword set // </summary> public string [] Keywords {get {return keywords;} set {this. keywords = new string [value. length]; for (int I = 0; I <value. length; I ++) {this. keywords [I] = value [I]. toLower () ;}}} Private string [] keywords; // <summary> // Character Buffer /// </summary> private StringBuilder charBuilder = new StringBuilder (); /// <summary> /// symbol buffer /// </summary> private StringBuilder symboBuilder = new StringBuilder (); /// <summary> /// result buffer /// </summary> private StringBuilder resBuilder = new StringBuilder (); /// <summary> /// whether the previous character is a letter /// </summary> private bool lastIsLetter; // <s Ummary> // Temporary Variable // </summary> private string temp; /// <summary> /// conversion /// </summary> /// <param name = "source"> string to be converted </param> /// <returns> Conversion Result </returns> public string Convert (string source) {charBuilder. clear (); symboBuilder. clear (); resBuilder. clear (); lastIsLetter = true; temp = string. empty; // decompress the source string char [] charArray = source. toArray <char> (); // traverse foreach (var c in charArray) {if (c> = 'A' & c <= 'Z') | (c> = 'A' & c <= 'Z ')) {// if the last symbol is not a letter, push the symbol buffer to if (! LastIsLetter) {PushSymbols ();} charBuilder. append (c); lastIsLetter = true;} else {// if the last symbol is a letter, push the letter buffer zone if (lastIsLetter) {PushLetters ();} symboBuilder. append (c); lastIsLetter = false ;}// process the last buffer if (lastIsLetter) {PushLetters () ;}else {PushSymbols ();} return resBuilder. toString () ;}/// <summary> // push the character buffer to the target buffer zone /// </summary> private void PushLetters () {temp = charBuilder. toString (); if (Keywords. contains (temp. toLower () {resBuilder. append (temp. toUpper ();} else {resBuilder. append (temp);} charBuilder. clear () ;}/// <summary> /// push the symbol buffer to the target buffer /// </summary> private void PushSymbols () {resBuilder. append (symboBuilder. toString (); symboBuilder. clear ();}}
View Code

 

2 Principle

Step 1: split an SQL statement string into a string, and a string.

Step 2: Determine whether the string is a keyword. If yes, convert it to uppercase.

Part 3: combine these strings

3. Implementation

The principle seems simple, but the implementation is not simple.

Two problems to solve

1. Merging cannot be processed after full conversion. Therefore, you must splice and convert

2. Status switch: when to push

Steps

1. Break the source string into a char array.

2 splicing focuses on determining how to determine the status of letters and symbols, and switching the status to push the buffer zone. View code for details

3. Do it again, because the last string is not pushed after traversal.

4. Effect

  

5 postscript

Recently, the company modified the SQL specification and required SQL keywords to be written in uppercase. I have written so many SQL statements that are not available yet. As a programmer, this can certainly be done using code.

So I started Baidu! I don't have to write it myself, so I'm lazy.

Hundreds of times a tool. For security reasons, I decompiled the tool and looked at the code. No. However, the function is actually implemented.

So I first changed all my SQL statements.

But I don't think I can understand it. I want to implement it myself!

So in the next few days, I began to think about how to change the SQL keyword to uppercase, without affecting other parts, including the invisible symbols such as line breaks (that is, they cannot be separated by invisible symbols, because if there is an unsigned link, it will be lost after cutting ).

At the company's annual meeting

I finally thought of it. I was so excited to look at my sisters (my colleagues.

I wrote this in my spare time and thought independently without referring to the code of the tool I was looking.

Core algorithms are developed for learning and communication.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.