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

org.eobjects.metamodel.jdbc.JdbcUpdateCallback Maven / Gradle / Ivy

/**
 * eobjects.org MetaModel
 * Copyright (C) 2010 eobjects.org
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.eobjects.metamodel.jdbc;

import java.sql.Connection;

import org.eobjects.metamodel.UpdateCallback;
import org.eobjects.metamodel.create.TableCreationBuilder;
import org.eobjects.metamodel.insert.RowInsertionBuilder;
import org.eobjects.metamodel.schema.Schema;
import org.eobjects.metamodel.schema.Table;

final class JdbcUpdateCallback implements UpdateCallback {

	private JdbcDataContext _dataContext;
	private Connection _connection;

	public JdbcUpdateCallback(JdbcDataContext dataContext) {
		_dataContext = dataContext;
	}

	@Override
	public TableCreationBuilder createTable(Schema schema, String name)
			throws IllegalArgumentException, IllegalStateException {
		return new JdbcCreateTableBuilder(this, schema, name);
	}

	@Override
	public RowInsertionBuilder insertInto(Table table)
			throws IllegalArgumentException, IllegalStateException {
		return new JdbcInsertBuilder(this, table);
	}

	protected Connection getConnection() {
		if (_connection == null) {
			_connection = _dataContext.getConnection();
		}
		return _connection;
	}

	protected void close() {
		if (_connection != null) {
			_dataContext.close(_connection, null, null);
		}
	}

	protected String quoteIfNescesary(String identifier) {
		if (identifier == null) {
			return null;
		}
		final String quote = _dataContext.getIdentifierQuoteString();
		if (quote == null) {
			return identifier;
		}
		boolean quotes = false;
		if (identifier.indexOf(' ') != -1) {
			quotes = true;
		} else {
			if (SqlKeywords.isKeyword(identifier)) {
				quotes = true;
			}
		}

		if (quotes) {
			identifier = quote + identifier + quote;
		}
		return identifier;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy