xdev.db.ingres.jdbc.IngresJDBCConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xapi-db-ingres Show documentation
Show all versions of xapi-db-ingres Show documentation
XAPI SqlEngine Database Adapter for Ingres
The newest version!
/*
* SqlEngine Database Adapter Ingres - XAPI SqlEngine Database Adapter for Ingres
* Copyright © 2003 XDEV Software (https://xdev.software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 program. If not, see .
*/
package xdev.db.ingres.jdbc;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import xdev.db.DBException;
import xdev.db.jdbc.JDBCConnection;
public class IngresJDBCConnection extends JDBCConnection
{
public IngresJDBCConnection(final IngresJDBCDataSource dataSource)
{
super(dataSource);
}
@Override
protected void setPreparedStatementParameter(
final PreparedStatement statement, final Object parameter,
final int jdbcIndex) throws SQLException, DBException
{
final boolean inTransaction = this.isInTransaction();
if(!inTransaction && parameter instanceof Blob)
{
final Blob blob = (Blob)parameter;
statement.setBytes(jdbcIndex, blob.getBytes(1, (int)blob.length()));
}
else if(!inTransaction && parameter instanceof Clob)
{
final Clob clob = (Clob)parameter;
statement.setString(jdbcIndex, clob.getSubString(1, (int)clob.length()));
}
else
{
super.setPreparedStatementParameter(statement, parameter, jdbcIndex);
}
}
@Override
public void createTable(
final String tableName, final String primaryKey, final Map columnMap,
final boolean isAutoIncrement, final Map foreignKeys) throws Exception
{
final Connection connection = super.getConnection();
final Statement statement = connection.createStatement();
try
{
if(!this.checkIfTableExists(connection.createStatement(), tableName))
{
if(!columnMap.containsKey(primaryKey))
{
columnMap.put(primaryKey, "INTEGER"); //$NON-NLS-1$
}
StringBuffer createStatement = null;
if(isAutoIncrement)
{
createStatement = new StringBuffer(
"CREATE TABLE \"" + tableName + "\"(\"" //$NON-NLS-1$ //$NON-NLS-2$
+ primaryKey
+ "\" " + columnMap.get(primaryKey)
+ " GENERATED BY DEFAULT AS IDENTITY (START WITH 201 INCREMENT BY 1) NOT NULL,"); //$NON
// -NLS-1$ //$NON-NLS-2$
}
else
{
createStatement = new StringBuffer("CREATE TABLE " + tableName + "(" //$NON-NLS-1$ //$NON-NLS-2$
+ primaryKey + " " + columnMap.get(primaryKey) + ","); //$NON-NLS-1$ //$NON-NLS-2$
}
for(final String keySet : columnMap.keySet())
{
if(!keySet.equals(primaryKey))
{
createStatement.append(
"\"" + keySet + "\" " + columnMap.get(keySet) + ","); //$NON-NLS-1$ //$NON-NLS-2$
}
}
createStatement.append(" PRIMARY KEY (\"" + primaryKey + "\"))"); //$NON-NLS-1$ //$NON-NLS-2$
if(log.isDebugEnabled())
{
log.debug("SQL Statement to create a table: " + createStatement); //$NON-NLS-1$
}
statement.execute(createStatement.toString());
}
}
catch(final Exception e)
{
throw e;
}
finally
{
statement.close();
connection.close();
}
}
private boolean checkIfTableExists(final Statement statement, final String tableName) throws Exception
{
final String sql = "select * from iitables where table_name = '" + tableName + "'"; //$NON-NLS-1$ //$NON-NLS-2$
ResultSet resultSet = null;
try
{
statement.execute(sql);
resultSet = statement.getResultSet();
}
catch(final Exception e)
{
if(resultSet != null)
{
resultSet.close();
}
statement.close();
throw e;
}
if(resultSet != null)
{
if(resultSet.next())
{
resultSet.close();
statement.close();
return true;
}
resultSet.close();
}
statement.close();
return false;
}
}