Java Formatting for SQL

Source: Internet
Author: User

 Public classsqlformat{ Public Static voidMain (string[] args) {String SQL="";    Sqlformat (SQL); }     Public Staticmap<string, string> map =NewHashmap<string, string> ();//fields that require line breaks     Public StaticMap<string, string> bracket=NewHashmap<string, string> ();//pre-parenthesis keyword    Static{map.put ("Select", "select"); Map.put (' From ', ' from '); Map.put ("GROUP BY" and "Group by"); Map.put ("Where", "where"); Map.put ("Order BY", "Order BY"); Bracket.put (' Not ', ' not '); Map.put ("(", "("); Map.put (")", ")"); }     Public Staticstring Sqlformat (String sql) {SQL= Sql.trim (). ReplaceAll (",", ","). ReplaceAll ("+", ""). ReplaceAll ("\\s+", ""). Replace ("", "|"). Replace ("(", "| (|)"). Replace (")", "|) |"). Replace ("| |", "|"). Replace ("| |", "|"); SQL=sql.tolowercase (); intsj=0;//string[] Sqlarray= Sql.split ("\\|"); intLength =sqlarray.length; Stack Stack=NewStack (); Stack stacktemp=NewStack ();  for(inti=0;i<length; i++){            if("(". Equals (Sqlarray[i]). Trim ())) {                if(Map.get (Sqlarray[i+1].trim ())! =NULL{//after keyword Stack.push ("(");                Printlnsql (Sqlarray[i], SJ); }Else if(Bracket.get (Sqlarray[i-1].trim ())! =NULL{//before the keyword Stack.push ("(");                Printlnsql (Sqlarray[i], SJ); }Else{Stacktemp.push ("(");                System.out.println (Sqlarray[i]); }            }Else if(")". Equals (Sqlarray[i].trim ())) {                if(Stacktemp.size () >0) {stacktemp.pop ();                System.out.println (Sqlarray[i]); }Else{stacktemp.pop (); SJ=stack.size ();                Printlnsql (Sqlarray[i], SJ); }            }Else if("". Equals (Sqlarray[i].trim ())) {            }Else if("(". Equals (Sqlarray[i].trim ()) && Map.get (Sqlarray[i-1].trim ())! =NULL){            }Else if(Map.get (Sqlarray[i].trim ())! =NULL) {printlnsql (Sqlarray[i], SJ); }Else{System.out.print (sqlarray[i]); }        }        returnSQL; } pubulicStatic voidPrintlnsql (String sqlsub,inti) {                if(i>0) {System.out.println ("\ n");  for(intj=0; j<i; J + +) {System.out.println ("\t\t"); }                }Else{System.out.println (' \ n ' +sqlsub); }        }}

Java Formatting for SQL

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.