Java String Extract extractAggregationPrimKeysFromSql( final String sql)

Here you can find the source of extractAggregationPrimKeysFromSql( final String sql)

Description

Extracts aggregation-definition-prim-keys out of the table-names of the given sql.

License

Open Source License

Parameter

Parameter Description
sql sql-statement of report-definition.

Return

Collection with aggregation-definition prim keys.

Declaration

public static Collection<String> extractAggregationPrimKeysFromSql(
        final String sql) 

Method Source Code

//package com.java2s;
/*/*from   w w w .ja  v  a2 s .  com*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at license/ESCIDOC.LICENSE
 * or http://www.escidoc.de/license.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at license/ESCIDOC.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

import java.util.ArrayList;
import java.util.Collection;

public class Main {
    /**
     * Extracts aggregation-definition-prim-keys out of the table-names of the given sql.
     *
     * @param sql sql-statement of report-definition.
     * @return Collection with aggregation-definition prim keys.
     */
    public static Collection<String> extractAggregationPrimKeysFromSql(
            final String sql) {
        final Collection<String> primKeys = new ArrayList<String>();
        if (sql != null) {
            String workSql = sql.replaceAll("\\s+", " ");
            workSql = workSql.replaceAll("\\s+", " ");
            boolean condition = false;
            if (workSql.matches("(?i).* (where|order by|group by) .*")) {
                condition = true;
            }
            final String fromClause = condition ? workSql.replaceFirst(
                    "(?i).*?from(.*?)(where|order by|group by).*", "$1")
                    : workSql.replaceFirst("(?i).*?from(.*)", "$1");
            final String[] tables = fromClause.split(",");
            for (String table : tables) {
                if (table.matches(".*?_.*")) {
                    table = table.replaceFirst(".*?\\.", "").trim();
                    if (table.startsWith("_")) {
                        table = table.replaceFirst("_", "");
                    }
                    primKeys.add(table.replaceFirst("(.*?)_.*", "$1"));
                }
            }
        }
        return primKeys;
    }
}

Related

  1. extract(final String grammarString, final char separator)
  2. extract(Properties p, String prefix)
  3. extract_tokens(String in_string, String delimiters)
  4. extractArrayData(String input, StringBuilder output)
  5. extractArrayData(String input, StringBuilder output)
  6. extractCharNgram(String content, int n)
  7. extractCommands(String fileContents)