Java Code Examples for mil.nga.geopackage.db.CoreSQLUtils#quoteUnwrap()

The following examples show how to use mil.nga.geopackage.db.CoreSQLUtils#quoteUnwrap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConstraintParser.java    From geopackage-core-java with MIT License 6 votes vote down vote up
/**
 * Get the constraint name and remaining definition
 * 
 * @param constraintSql
 *            constraint SQL
 * @return array with name or null at index 0, definition at index 1
 */
public static String[] getNameAndDefinition(String constraintSql) {
	String parts[] = null;
	Matcher matcher = CONSTRAINT_PATTERN.matcher(constraintSql.trim());
	if (matcher.find()) {
		String name = CoreSQLUtils
				.quoteUnwrap(matcher.group(CONSTRAINT_PATTERN_NAME_GROUP));
		if (name != null) {
			name = name.trim();
		}
		String definition = matcher
				.group(CONSTRAINT_PATTERN_DEFINITION_GROUP);
		if (definition != null) {
			definition = definition.trim();
		}
		parts = new String[] { name, definition };
	}
	return parts;
}
 
Example 2
Source File: SQLExecAlterTable.java    From geopackage-java with MIT License 6 votes vote down vote up
/**
 * Check for a drop table statement and execute
 * 
 * @param database
 *            database
 * @param sql
 *            SQL statement
 * @return result if dropped table, null if not
 */
private static SQLExecResult dropTable(GeoPackage database, String sql) {

	SQLExecResult result = null;

	Matcher matcher = DROP_TABLE_PATTERN.matcher(sql);
	if (matcher.matches() && SQLExec.isGeoPackage(database)) {
		String tableName = CoreSQLUtils
				.quoteUnwrap(matcher.group(TABLE_NAME_GROUP));
		if (tableName != null) {
			tableName = tableName.trim();
			if (!database.isTable(tableName)) {
				throw new GeoPackageException(
						"Table does not exist: " + tableName);
			}
			database.deleteTable(tableName.trim());
			result = new SQLExecResult();
		}
	}

	return result;
}
 
Example 3
Source File: ConstraintParser.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get the constraint name if it has one
 * 
 * @param constraintSql
 *            constraint SQL
 * @return constraint name or null
 */
public static String getName(String constraintSql) {
	String name = null;
	Matcher matcher = NAME_PATTERN.matcher(constraintSql);
	if (matcher.find()) {
		name = CoreSQLUtils
				.quoteUnwrap(matcher.group(NAME_PATTERN_NAME_GROUP));
	}
	return name;
}
 
Example 4
Source File: SQLExecAlterTable.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Check for a drop column statement and execute
 * 
 * @param database
 *            database
 * @param sql
 *            SQL statement
 * @return result if dropped column, null if not
 */
private static SQLExecResult dropColumn(GeoPackage database, String sql) {

	SQLExecResult result = null;

	Matcher matcher = DROP_COLUMN_PATTERN.matcher(sql);
	if (matcher.find()) {
		String tableName = CoreSQLUtils
				.quoteUnwrap(matcher.group(TABLE_NAME_GROUP));
		if (tableName != null) {
			tableName = tableName.trim();
		}
		String columnName = CoreSQLUtils
				.quoteUnwrap(matcher.group(COLUMN_NAME_GROUP));
		if (columnName != null) {
			columnName = columnName.trim();
		}

		if (tableName != null && columnName != null) {
			AlterTable.dropColumn(database.getDatabase(), tableName,
					columnName);
			result = new SQLExecResult();
		}

	}

	return result;
}
 
Example 5
Source File: SQLExecAlterTable.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Check for a rename table statement and execute
 * 
 * @param database
 *            database
 * @param sql
 *            SQL statement
 * @return result if renamed table, null if not
 */
private static SQLExecResult renameTable(GeoPackage database, String sql) {

	SQLExecResult result = null;

	Matcher matcher = RENAME_TABLE_PATTERN.matcher(sql);
	if (matcher.find() && SQLExec.isGeoPackage(database)) {
		String tableName = CoreSQLUtils
				.quoteUnwrap(matcher.group(TABLE_NAME_GROUP));
		if (tableName != null) {
			tableName = tableName.trim();
		}
		String newTableName = CoreSQLUtils
				.quoteUnwrap(matcher.group(NEW_TABLE_NAME_GROUP));
		if (newTableName != null) {
			newTableName = newTableName.trim();
		}

		if (tableName != null && newTableName != null
				&& database.getTableDataType(tableName) != null) {
			database.renameTable(tableName, newTableName);
			result = new SQLExecResult();
		}

	}

	return result;
}
 
Example 6
Source File: SQLExecAlterTable.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Check for a copy table statement and execute
 * 
 * @param database
 *            database
 * @param sql
 *            SQL statement
 * @return result if copied table, null if not
 */
private static SQLExecResult copyTable(GeoPackage database, String sql) {

	SQLExecResult result = null;

	Matcher matcher = COPY_TABLE_PATTERN.matcher(sql);
	if (matcher.find()) {
		String tableName = CoreSQLUtils
				.quoteUnwrap(matcher.group(TABLE_NAME_GROUP));
		if (tableName != null) {
			tableName = tableName.trim();
		}
		String newTableName = CoreSQLUtils
				.quoteUnwrap(matcher.group(NEW_TABLE_NAME_GROUP));
		if (newTableName != null) {
			newTableName = newTableName.trim();
		}

		if (tableName != null && newTableName != null) {

			if (SQLExec.isGeoPackage(database)) {
				database.copyTable(tableName, newTableName);
			} else {
				AlterTable.copyTable(database.getDatabase(), tableName,
						newTableName);
			}

			result = new SQLExecResult();

		}

	}

	return result;
}
 
Example 7
Source File: ConstraintParser.java    From geopackage-core-java with MIT License 4 votes vote down vote up
/**
 * Attempt to get column constraints by parsing the SQL statement
 * 
 * @param constraintSql
 *            constraint SQL statement
 * @return constraints
 */
public static ColumnConstraints getColumnConstraints(String constraintSql) {

	String[] parts = constraintSql.trim().split("\\s+");
	String columnName = CoreSQLUtils.quoteUnwrap(parts[0]);

	ColumnConstraints constraints = new ColumnConstraints(columnName);

	int constraintIndex = -1;
	ConstraintType constraintType = null;

	for (int i = 1; i < parts.length; i++) {
		String part = parts[i];

		if (Constraint.CONSTRAINT.equalsIgnoreCase(part)) {

			if (constraintType != null) {
				constraints.addConstraint(createConstraint(parts,
						constraintIndex, i, constraintType));
				constraintType = null;
			}

			constraintIndex = i;

		} else {

			ConstraintType type = ConstraintType.getColumnType(part);
			if (type != null) {

				if (constraintType != null) {
					constraints.addConstraint(createConstraint(parts,
							constraintIndex, i, constraintType));
					constraintIndex = -1;
				}

				if (constraintIndex < 0) {
					constraintIndex = i;
				}
				constraintType = type;

			}
		}
	}

	if (constraintType != null) {
		constraints.addConstraint(createConstraint(parts, constraintIndex,
				parts.length, constraintType));
	}

	return constraints;
}