@Override public Boolean run(PreparedStatement stm) throws SQLException { if (!DatabaseUtil.tableExists(stm.getConnection(), SHARED_BAGS)) { LOG.info("Creating shared bag table"); stm.execute(); return Boolean.TRUE; } return Boolean.FALSE; } };
@Override public Void run(PreparedStatement stm) throws SQLException { if (!DatabaseUtil.tableExists(stm.getConnection(), SharingInvite.TABLE_NAME)) { LOG.info("Creating shared bag invite table"); stm.execute(); } return null; } };
@Override public Void run(PreparedStatement stm) throws SQLException { if (!DatabaseUtil.tableExists(stm.getConnection(), SharingInvite.TABLE_NAME)) { LOG.info("Creating shared bag invite table"); stm.execute(); } return null; } };
@Override public Void run(PreparedStatement stm) throws SQLException { if (!DatabaseUtil.tableExists(stm.getConnection(), TABLE_NAME)) { LOG.info("Creating preferences table"); stm.execute(); } return null; } });
@Override public Boolean run(PreparedStatement stm) throws SQLException { if (!DatabaseUtil.tableExists(stm.getConnection(), SHARED_BAGS)) { LOG.info("Creating shared bag table"); stm.execute(); return Boolean.TRUE; } return Boolean.FALSE; } };
@Override public Void run(PreparedStatement stm) throws SQLException { if (!DatabaseUtil.tableExists(stm.getConnection(), TABLE_NAME)) { LOG.info("Creating preferences table"); stm.execute(); } return null; } });
/** * Create the table where the tracker saves data * @throws Exception when a database error access is verified * @param connection the userprofile connection */ @Override public void createTrackerTable(Connection connection) throws Exception { try { if (trackTableName != null && !"".equals(trackTableName)) { if (!DatabaseUtil.tableExists(connection, trackTableName)) { connection.createStatement().execute(getStatementCreatingTable()); } } else { LOG.warn("trackTableName is null or empty"); } } catch (SQLException e) { trackerLogger = null; throw e; } }
/** * Create the table where the tracker saves data * @throws Exception when a database error access is verified * @param connection the userprofile connection */ @Override public void createTrackerTable(Connection connection) throws Exception { try { if (trackTableName != null && !"".equals(trackTableName)) { if (!DatabaseUtil.tableExists(connection, trackTableName)) { connection.createStatement().execute(getStatementCreatingTable()); } } else { LOG.warn("trackTableName is null or empty"); } } catch (SQLException e) { trackerLogger = null; throw e; } }
/** * * @param con database connection * @param tableName table name * @param columnName column name * @param columnType column type * @return true if column exists */ public static boolean verifyColumnType (Connection con, String tableName, String columnName, int columnType) { try { if (DatabaseUtil.tableExists(con, tableName)) { ResultSet res = con.getMetaData().getColumns(null, null, tableName, columnName); while (res.next()) { if (res.getString(3).equals(tableName) && columnName.equals(res.getString(4)) && res.getInt(5) == columnType) { return true; } return false; } } } catch (SQLException sqle) { sqle.printStackTrace(); } return true; }
/** * Add a column in the table specified in input. A connection is obtained to the database * and automatically released after the addition of the column. * @param database the database to use * @param tableName the table where to add the column * @param columnName the column to add * @param type the type * @throws SQLException if there is a database problem */ public static void addColumn(Database database, String tableName, String columnName, Type type) throws SQLException { Connection connection = database.getConnection(); if (DatabaseUtil.tableExists(connection, tableName)) { try { addColumn(connection, tableName, columnName, type); } finally { connection.close(); } } }
/** * Add a column in the table specified in input. A connection is obtained to the database * and automatically released after the addition of the column. * @param database the database to use * @param tableName the table where to add the column * @param columnName the column to add * @param type the type * @throws SQLException if there is a database problem */ public static void addColumn(Database database, String tableName, String columnName, Type type) throws SQLException { Connection connection = database.getConnection(); if (DatabaseUtil.tableExists(connection, tableName)) { try { addColumn(connection, tableName, columnName, type); } finally { connection.close(); } } }
/** * Add a column to an existing database table, if it does not already exist. * It is the users responsibility to close the connection after use. * @param con A connection to the database. * @param tableName The table to add the database too * @param columnName The column to add * @param type The SQL type to add * @throws SQLException if something goes wrong */ public static void addColumn(Connection con, String tableName, String columnName, Type type) throws SQLException { if (!DatabaseUtil.tableExists(con, tableName)) { throw new IllegalArgumentException("there is no table named " + tableName + " in this" + " database to add a new column to"); } if (DatabaseUtil.columnExists(con, tableName, columnName)) { return; } if (!DatabaseUtil.isLegalColumnName(columnName)) { throw new IllegalArgumentException("This is not a legal column name: " + columnName); } String sql = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + type.getSQLType(); PreparedStatement stmt = con.prepareStatement(sql); LOG.info(stmt.toString()); stmt.executeUpdate(); }
/** * Add a column to an existing database table, if it does not already exist. * It is the users responsibility to close the connection after use. * @param con A connection to the database. * @param tableName The table to add the database too * @param columnName The column to add * @param type The SQL type to add * @throws SQLException if something goes wrong */ public static void addColumn(Connection con, String tableName, String columnName, Type type) throws SQLException { if (!DatabaseUtil.tableExists(con, tableName)) { throw new IllegalArgumentException("there is no table named " + tableName + " in this" + " database to add a new column to"); } if (DatabaseUtil.columnExists(con, tableName, columnName)) { return; } if (!DatabaseUtil.isLegalColumnName(columnName)) { throw new IllegalArgumentException("This is not a legal column name: " + columnName); } String sql = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + type.getSQLType(); PreparedStatement stmt = con.prepareStatement(sql); LOG.info(stmt.toString()); stmt.executeUpdate(); }
public void testTableExistsNullConnection() throws Exception { try { DatabaseUtil.tableExists(null, "table1"); fail("Expected: NullPointerException"); } catch (NullPointerException e) { } }
public void testTableExistsNullTable() throws Exception { try { DatabaseUtil.tableExists(con, null); fail("Expected: NullPointerException"); } catch (NullPointerException e) { } }
public void testTableNotExists() throws Exception { synchronized (con) { try { con.createStatement().execute("DROP TABLE table2"); } catch (SQLException e) { } createTable(); assertTrue(!(DatabaseUtil.tableExists(con, "table2"))); dropTable("table1", false); } }
public void testTableExists() throws Exception { synchronized (con) { createTable(); assertTrue(DatabaseUtil.tableExists(con, "table1")); dropTable("table1", false); } }
public void testAddMultiple() throws Exception { Connection con = database.getConnection(); con.setAutoCommit(false); PrecomputedTableManager ptm = new PrecomputedTableManager(database); try { createTable(); ptm.add(pt1); assertTrue(ptm.getPrecomputedTables().contains(pt1)); assertTrue(DatabaseUtil.tableExists(con, "precompA")); PrecomputedTable pt2 = new PrecomputedTable(pt1.getQuery(), pt1.getQuery().getSQLString(), "precompB", "test", con); try { ptm.add(pt2); fail("Should have received an exception"); } catch (IllegalArgumentException e) { } assertFalse(ptm.getPrecomputedTables().contains(pt2)); assertFalse(DatabaseUtil.tableExists(con, "precompB")); ptm.delete(pt1); assertTrue(!(ptm.getPrecomputedTables().contains(pt1))); assertTrue(!(DatabaseUtil.tableExists(con, "precompA"))); } catch (SQLException e) { throw (SQLException) Util.verboseException(e); } finally { deleteTable(); con.close(); } }
public void testAddDelete() throws Exception { Connection con = database.getConnection(); con.setAutoCommit(false); PrecomputedTableManager ptm = new PrecomputedTableManager(database); try { createTable(); ptm.add(pt1); assertTrue(ptm.getPrecomputedTables().contains(pt1)); assertEquals(pt1, ptm.lookupSql("test", pt1.getOriginalSql())); assertTrue(DatabaseUtil.tableExists(con, "precompA")); ptm.delete(pt1); assertTrue(!(ptm.getPrecomputedTables().contains(pt1))); assertNull(ptm.lookupSql("test", pt1.getOriginalSql())); assertTrue(!(DatabaseUtil.tableExists(con, "precompA"))); } catch (SQLException e) { throw (SQLException) Util.verboseException(e); } finally { deleteTable(); con.close(); } }
public void testAddDeleteWithConnection() throws Exception { Connection con = database.getConnection(); con.setAutoCommit(false); PrecomputedTableManager ptm = new PrecomputedTableManager(con); try { createTable(); ptm.add(pt1); assertTrue(ptm.getPrecomputedTables().contains(pt1)); assertEquals(pt1, ptm.lookupSql("test", pt1.getOriginalSql())); assertTrue(DatabaseUtil.tableExists(con, "precompA")); ptm.delete(pt1); assertTrue(!(ptm.getPrecomputedTables().contains(pt1))); assertNull(ptm.lookupSql("test", pt1.getOriginalSql())); assertTrue(!(DatabaseUtil.tableExists(con, "precompA"))); } catch (SQLException e) { throw (SQLException) Util.verboseException(e); } finally { deleteTable(); con.close(); } }