All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.univocity.api.entity.jdbc.DefaultEscaper Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2013 uniVocity Software Pty Ltd. All rights reserved.
 * This file is subject to the terms and conditions defined in file
 * 'LICENSE.txt', which is part of this source code package.
 ******************************************************************************/
package com.univocity.api.entity.jdbc;

/**
 * A default implementation of an {@link IdentifierEscaper}
 *
 * @see JdbcDataStoreConfiguration
 * @see SqlProducer
 * @see IdentifierEscaper
 *
 * @author uniVocity Software Pty Ltd - [email protected]
 *
 */
public class DefaultEscaper extends IdentifierEscaper {

	private final String escapeStart;
	private final String escapeEnd;
	private boolean alwaysEscape = false;

	/**
	 * Creates an {@link IdentifierEscaper} that encloses identifiers within double quotes.
	 */
	public DefaultEscaper() {
		this('"');
	}

	/**
	 * Creates an {@link IdentifierEscaper} that encloses identifiers within a given escape character.
	 * @param escape The escape character that will enclose identifiers in SQL statements generated by uniVocity
	 */
	public DefaultEscaper(char escape) {
		this(String.valueOf(escape));
	}

	/**
	 * Creates an {@link IdentifierEscaper} that encloses identifiers within a given escape sequence.
	 * @param escape The escape sequence that will enclose identifiers in SQL statements generated by uniVocity
	 */
	public DefaultEscaper(String escape) {
		this(escape, escape);
	}

	/**
	 * Creates an {@link IdentifierEscaper} that encloses identifiers in a specific escape sequence (possibly an escape function).
	 * @param escapeStart The prefix of the escape sequence that will enclose identifiers in SQL statements generated by uniVocity
	 * @param escapeEnd The suffix of the escape sequence that will enclose identifiers in SQL statements generated by uniVocity
	 */
	public DefaultEscaper(String escapeStart, String escapeEnd) {
		if (escapeStart == null || escapeStart.trim().isEmpty()) {
			throw new IllegalArgumentException("Start of escape sequence cannot be null or blank");
		}
		if (escapeEnd == null || escapeEnd.trim().isEmpty()) {
			throw new IllegalArgumentException("End of escape sequence cannot be null or blank");
		}

		this.escapeStart = escapeStart;
		this.escapeEnd = escapeEnd;
	}

	@Override
	public boolean alwaysEscape() {
		return alwaysEscape;
	}

	/**
	 * Configures this {@link IdentifierEscaper} to escape non-reserved words as well as reserved words in SQL statements.
	 *
	 * @param escapeAlways a flag indicating whether or not all identifiers should be enclosed within the configured escape sequence in SQL statements generated by uniVocity.
	 */
	public void setAlwaysEscape(boolean escapeAlways) {
		this.alwaysEscape = escapeAlways;
	}

	@Override
	public String escape(String reservedWord) {
		return escapeStart + reservedWord + escapeEnd;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy