Java SQL Type adjustLevel(String attribute, int type)

Here you can find the source of adjustLevel(String attribute, int type)

Description

adjust Level

License

Apache License

Declaration

private static String adjustLevel(String attribute, int type) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.sql.Types;
import java.util.ArrayList;

public class Main {
    private static String adjustLevel(String attribute, int type) {
        return adjustLevel(attribute, type, "\"", false);
    }/*from   w  w w  .  ja  v a 2 s  .  co m*/

    private static String adjustLevel(String attribute, int type, String quote, boolean normalize) {
        StringBuffer buffer = new StringBuffer();
        char[] chars = attribute.toCharArray();
        ArrayList lastType = new ArrayList();
        lastType.add(new Integer(type));
        ArrayList lastQuote = new ArrayList();
        lastQuote.add(quote);
        int level = 0;
        int calculatedLevel = 0;
        int firstCalculatedLevel = -2;

        for (int i = 0; i < chars.length; ++i) {
            StringBuffer tempBuffer = null;
            boolean quoteFound = false;

            while (chars[i] == 92 || chars[i] == 34) {
                if (tempBuffer == null) {
                    tempBuffer = new StringBuffer();
                }

                tempBuffer.append(chars[i]);
                ++i;
                if (firstCalculatedLevel >= 0
                        && (double) tempBuffer.length() == Math.pow(2.0D, (double) (level + firstCalculatedLevel))
                        || i == chars.length) {
                    break;
                }
            }

            String replacedQuote;
            if (tempBuffer != null) {
                --i;
                int length = tempBuffer.length();
                calculatedLevel = log(length, 2);
                if (firstCalculatedLevel == -2) {
                    firstCalculatedLevel = calculatedLevel;
                    if (level == 0) {
                        firstCalculatedLevel = calculatedLevel + 1;
                    }
                }

                if (!normalize) {
                    if (calculatedLevel - firstCalculatedLevel >= 0) {
                        replacedQuote = (String) lastQuote.get(calculatedLevel - firstCalculatedLevel);
                    } else {
                        replacedQuote = "";
                    }

                    while (calculatedLevel - firstCalculatedLevel < level - 1) {
                        lastQuote.remove(level);
                        lastType.remove(level);
                        --level;
                    }
                } else {
                    if (level == 0) {
                        replacedQuote = "";
                    } else {
                        replacedQuote = "\"";
                    }

                    while (calculatedLevel - firstCalculatedLevel < level - 1) {
                        --level;
                    }
                }

                buffer.append(replacedQuote);
                quoteFound = true;
            }

            if (!quoteFound) {
                if (level <= calculatedLevel + 1) {
                    if (123 == chars[i]) {
                        if (!normalize) {
                            lastType.add(Types.ARRAY);
                            replacedQuote = getNextQuotes((String) lastQuote.get(level), Types.ARRAY);
                            lastQuote.add(replacedQuote);
                        }

                        ++level;
                    } else if (40 == chars[i]) {
                        if (!normalize) {
                            lastType.add(Types.STRUCT);
                            replacedQuote = getNextQuotes((String) lastQuote.get(level), Types.STRUCT);
                            lastQuote.add(replacedQuote);
                        }

                        ++level;
                    }
                }

                buffer.append(chars[i]);
            }
        }

        return buffer.toString();
    }

    private static int log(int x, int base) {
        return (int) (Math.log((double) x) / Math.log((double) base));
    }

    private static String getNextQuotes(String lastQuotes, int lastType) {
        StringBuffer quotes = new StringBuffer();
        if (lastQuotes.length() == 0) {
            return "\"";
        } else {
            if (lastType == Types.ARRAY) {
                int length = lastQuotes.length();

                for (int i = 0; i < length; ++i) {
                    quotes.append('\\');
                }

                quotes.append(lastQuotes);
            } else {
                quotes.append(lastQuotes).append(lastQuotes);
            }

            return quotes.toString();
        }
    }
}

Related

  1. addParameter(PreparedStatement smt, int index, Object value)
  2. callPro2(String sql, String[] inparameters, Integer[] outparameters)
  3. columnClassName(int columnType)
  4. columnDisplaySize(int columnType)
  5. columnPrecision(int columnType)