Java String Split splitSQLColumns(String sql)

Here you can find the source of splitSQLColumns(String sql)

Description

split SQL columns like that : from : id, name, CONCAT(name,UPPER(address)), CONCAT(age,name) to [id] [name] [CONCAT(name,UPPER(address))] [CONCAT(age,name)]

License

Open Source License

Declaration


public static String[] splitSQLColumns(String sql) 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//from   w w  w.j a va2s. c  o  m
      * split SQL columns like that :
      * from :
      * id, name, CONCAT(name,UPPER(address)), CONCAT(age,name)
      * to
      * [id] [name] [CONCAT(name,UPPER(address))] [CONCAT(age,name)]
      */

    public static String[] splitSQLColumns(String sql) {
        List<String> result = new ArrayList<String>();
        int blockCount = 0;
        int start = 0;
        for (int i = 0; i < sql.length(); i++) {
            char c = sql.charAt(i);
            if (c == '(') {
                blockCount++;
            } else if (c == ')') {
                blockCount--;
            }

            if ((c == ',' && (blockCount < 1))) {
                result.add(sql.substring(start, i));
                start = i + 1;
            }

            if (i == (sql.length() - 1)) {
                result.add(sql.substring(start));
            }
        }
        return result.toArray(new String[0]);
    }
}

Related

  1. splitSemicolonString(String semicolonString)
  2. splitSentence(String sentence)
  3. splitSimple(String split, String s)
  4. splitSimpleQueries(String queryString)
  5. splitSQL(List list, String sql)
  6. splitStep(String step)
  7. splitStr(String str, int toCount)
  8. splitString(String bigString, String splitter)
  9. splitString(String hexString, int partSize)