<commit_before>package mil.nga.geopackage.io;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import mil.nga.geopackage.GeoPackage;
import mil.nga.geopackage.db.AlterTable;
import mil.nga.geopackage.db.CoreSQLUtils;

/**
 * Execute special Alter Table statement cases including unsupported, non
 * compliant, and those requiring additional modifications
 * 
 * @author osbornb
 * @since 3.3.0
 */
public class SQLExecAlterTable {

	/**
	 * Regex prefix for ignoring: case insensitive, dotall mode (match line
	 * terminators), and start of line
	 */
	private static final String REGEX_PREFIX = "(?i)(?s)^";

	/**
	 * Match a table or column name, with or without quotes
	 */
	private static final String REGEX_NAME = "(\".+\"|\\S+)";

	/**
	 * Alter table prefix regex with table name
	 */
	private static final String REGEX_ALTER_TABLE = REGEX_PREFIX
			+ "ALTER\\s+TABLE\\s+" + REGEX_NAME + "\\s+";

	/**
	 * Alter table pattern
	 */
	public static final Pattern ALTER_TABLE_PATTERN = Pattern
			.compile(REGEX_ALTER_TABLE + ".+");

	/**
	 * Drop column pattern
	 */
	public static final Pattern DROP_COLUMN_PATTERN = Pattern
			.compile(REGEX_ALTER_TABLE + "DROP(\\s+COLUMN)?\\s+" + REGEX_NAME);

	/**
	 * Rename table pattern
	 */
	public static final Pattern RENAME_TABLE_PATTERN = Pattern
			.compile(REGEX_ALTER_TABLE + "RENAME\\s+TO\\s+" + REGEX_NAME);

	/**
	 * Copy table pattern
	 */
	public static final Pattern COPY_TABLE_PATTERN = Pattern
			.compile(REGEX_ALTER_TABLE + "COPY\\s+TO\\s+" + REGEX_NAME);

	/**
	 * Table name pattern group
	 */
	public static final int TABLE_NAME_GROUP = 1;

	/**
	 * Column name pattern group
	 */
	public static final int COLUMN_NAME_GROUP = 3;

	/**
	 * New table name pattern group
	 */
	public static final int NEW_TABLE_NAME_GROUP = 2;

	/**
	 * Handle alter table statements that are unsupported, non spec compliant,
	 * or require additional modifications
	 * 
	 * @param geoPackage
	 *            open GeoPackage
	 * @param sql
	 *            SQL statement
	 * @return results if handled, null if not a special case
	 */
	public static SQLExecResult alterTable(GeoPackage geoPackage, String sql) {

		SQLExecResult result = null;

		if (ALTER_TABLE_PATTERN.matcher(sql).matches()) {

			// ALTER TABLE table_name DROP column_name
			// ALTER TABLE table_name DROP COLUMN column_name
			result = dropColumn(geoPackage, sql);

			if (result == null) {

				// ALTER TABLE table_name RENAME TO new_table_name
				result = renameTable(geoPackage, sql);

				if (result == null) {

					// ALTER TABLE table_name COPY TO new_table_name
					result = copyTable(geoPackage, sql);

				}

			}

		}

		return result;
	}

	/**
	 * Check for a drop column statement and execute
	 * <commit_msg>remove end semicolon before alter table special cases<commit_after>package mil.nga.geopackage.io;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import mil.nga.geopackage.GeoPackage;
import mil.nga.geopackage.db.AlterTable;
import mil.nga.geopackage.db.CoreSQLUtils;

/**
 * Execute special Alter Table statement cases including unsupported, non
 * compliant, and those requiring additional modifications
 * 
 * @author osbornb
 * @since 3.3.0
 */
public class SQLExecAlterTable {

	/**
	 * Regex prefix for ignoring: case insensitive, dotall mode (match line
	 * terminators), and start of line
	 */
	private static final String REGEX_PREFIX = "(?i)(?s)^";

	/**
	 * Match a table or column name, with or without quotes
	 */
	private static final String REGEX_NAME = "(\".+\"|\\S+)";

	/**
	 * Alter table prefix regex with table name
	 */
	private static final String REGEX_ALTER_TABLE = REGEX_PREFIX
			+ "ALTER\\s+TABLE\\s+" + REGEX_NAME + "\\s+";

	/**
	 * Alter table pattern
	 */
	public static final Pattern ALTER_TABLE_PATTERN = Pattern
			.compile(REGEX_ALTER_TABLE + ".+");

	/**
	 * Drop column pattern
	 */
	public static final Pattern DROP_COLUMN_PATTERN = Pattern
			.compile(REGEX_ALTER_TABLE + "DROP(\\s+COLUMN)?\\s+" + REGEX_NAME);

	/**
	 * Rename table pattern
	 */
	public static final Pattern RENAME_TABLE_PATTERN = Pattern
			.compile(REGEX_ALTER_TABLE + "RENAME\\s+TO\\s+" + REGEX_NAME);

	/**
	 * Copy table pattern
	 */
	public static final Pattern COPY_TABLE_PATTERN = Pattern
			.compile(REGEX_ALTER_TABLE + "COPY\\s+TO\\s+" + REGEX_NAME);

	/**
	 * Table name pattern group
	 */
	public static final int TABLE_NAME_GROUP = 1;

	/**
	 * Column name pattern group
	 */
	public static final int COLUMN_NAME_GROUP = 3;

	/**
	 * New table name pattern group
	 */
	public static final int NEW_TABLE_NAME_GROUP = 2;

	/**
	 * Handle alter table statements that are unsupported, non spec compliant,
	 * or require additional modifications
	 * 
	 * @param geoPackage
	 *            open GeoPackage
	 * @param sql
	 *            SQL statement
	 * @return results if handled, null if not a special case
	 */
	public static SQLExecResult alterTable(GeoPackage geoPackage, String sql) {

		SQLExecResult result = null;

		sql = sql.trim();

		if (ALTER_TABLE_PATTERN.matcher(sql).matches()) {

			if (sql.endsWith(";")) {
				sql = sql.substring(0, sql.length() - 1);
			}

			// ALTER TABLE table_name DROP column_name
			// ALTER TABLE table_name DROP COLUMN column_name
			result = dropColumn(geoPackage, sql);

			if (result == null) {

				// ALTER TABLE table_name RENAME TO new_table_name
				result = renameTable(geoPackage, sql);

				if (result == null) {

					// ALTER TABLE table_name COPY TO new_table_name
					result = copyTable(geoPackage, sql);

				}

			}

		}

		return result;
	}

	/**
	 * Check for a drop column statement and execute
	 * 
{'fraction_non_alphanumeric': 0.11016799292661361, 'fraction_numerical': 0.002475685234305924, 'mean_word_length': 3.2848484848484847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3399807', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1736, 'n_words': 527}