Android examples for Database:Primary Key
Extracts aggregation-definition-primary-keys out of the table-names of the given sql.
/*/*from w ww . j a va 2 s. co m*/ * 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 */ //package com.java2s; import java.util.ArrayList; import java.util.Collection; public class Main { public static void main(String[] argv) throws Exception { String sql = "java2s.com"; System.out.println(extractAggregationPrimKeysFromSql(sql)); } /** * 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; } }