Search in sources :

Example 71 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project adempiere by adempiere.

the class TableCreateColumns method doIt.

//	prepare
/**
	 * 	Process
	 *	@return info
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    if (p_AD_Table_ID == 0)
        throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_AD_Table_ID);
    log.info("EntityType=" + p_EntityType + ", AllTables=" + p_AllTables + ", AD_Table_ID=" + p_AD_Table_ID);
    //
    Connection conn = null;
    try {
        conn = DB.getConnectionRO();
        AdempiereDatabase db = DB.getDatabase();
        DatabaseMetaData md = conn.getMetaData();
        String catalog = db.getCatalog();
        String schema = db.getSchema();
        if (p_AllTables)
            addTable(md, catalog, schema);
        else {
            MTable table = new MTable(getCtx(), p_AD_Table_ID, get_TrxName());
            if (table == null || table.get_ID() == 0)
                throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_AD_Table_ID);
            log.info(table.getTableName() + ", EntityType=" + p_EntityType);
            String tableName = table.getTableName();
            if (DB.isOracle())
                tableName = tableName.toUpperCase();
            // globalqss 2005-10-24
            if (DB.isPostgreSQL())
                tableName = tableName.toLowerCase();
            // end globalqss 2005-10-24
            ResultSet rs = md.getColumns(catalog, schema, tableName, null);
            addTableColumn(rs, table);
        }
        return "#" + m_count;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : AdempiereDatabase(org.compiere.db.AdempiereDatabase) MTable(org.compiere.model.MTable) AdempiereSystemError(org.compiere.util.AdempiereSystemError) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 72 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project adempiere by adempiere.

the class CreateAdempiere method copy.

//  dropDatabase	
/**
	 * 	Create Tables and copy data
	 * 	@param whereClause optional where clause
	 * 	@param dropFirst drop first
	 *	@return true if executed
	 */
public boolean copy(String whereClause, boolean dropFirst) {
    log.info(whereClause);
    if (getConnection(false, true) == null)
        return false;
    //
    boolean success = true;
    int count = 0;
    ArrayList<String> list = new ArrayList<String>();
    String sql = "SELECT * FROM AD_Table";
    if (whereClause != null && whereClause.length() > 0)
        sql += " WHERE " + whereClause;
    sql += " ORDER BY TableName";
    //
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        //jz: pstmt.getConnection() could be null
        Connection conn = pstmt.getConnection();
        DatabaseMetaData md = null;
        if (conn != null)
            md = conn.getMetaData();
        else {
            //jz: globalization issue??
            throw new DBException("No Connection");
        }
        rs = pstmt.executeQuery();
        while (rs.next() && success) {
            MTable table = new MTable(m_ctx, rs, null);
            if (table.isView())
                continue;
            if (dropFirst) {
                executeCommands(new String[] { "DROP TABLE " + table.getTableName() }, m_conn, false, false);
            }
            //
            if (createTable(table, md)) {
                list.add(table.getTableName());
                count++;
            } else
                success = false;
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
        success = false;
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    if (!success)
        return false;
    /**	Enable Contraints */
    enableConstraints(list);
    databaseBuild();
    log.info("#" + count);
    try {
        if (m_conn != null)
            m_conn.close();
    } catch (SQLException e2) {
        log.log(Level.SEVERE, "close connection", e2);
    }
    m_conn = null;
    return success;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DatabaseMetaData(java.sql.DatabaseMetaData) DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) MTable(org.compiere.model.MTable) ResultSet(java.sql.ResultSet)

Example 73 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project calcite-avatica by apache.

the class MetaResultSetTest method testGetProcedureColumns.

@Test
public void testGetProcedureColumns() throws SQLException {
    DatabaseMetaData metadata = getDatabaseMetadata();
    try (ResultSet rs = metadata.getProcedureColumns(null, null, null, null)) {
        ResultSetMetaData rsMeta = rs.getMetaData();
        assertEquals(20, rsMeta.getColumnCount());
        assertColumn(rsMeta, 1, "PROCEDURE_CAT", Types.VARCHAR, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 2, "PROCEDURE_SCHEM", Types.VARCHAR, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 3, "PROCEDURE_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 4, "COLUMN_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 5, "COLUMN_TYPE", Types.SMALLINT, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 6, "DATA_TYPE", Types.INTEGER, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 7, "TYPE_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 8, "PRECISION", Types.INTEGER, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 9, "LENGTH", Types.INTEGER, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 10, "SCALE", Types.SMALLINT, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 11, "RADIX", Types.SMALLINT, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 12, "NULLABLE", Types.SMALLINT, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 13, "REMARKS", Types.VARCHAR, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 14, "COLUMN_DEF", Types.VARCHAR, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 15, "SQL_DATA_TYPE", Types.INTEGER, DatabaseMetaData.columnNullableUnknown);
        assertColumn(rsMeta, 16, "SQL_DATETIME_SUB", Types.INTEGER, DatabaseMetaData.columnNullableUnknown);
        assertColumn(rsMeta, 17, "CHAR_OCTET_LENGTH", Types.INTEGER, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 18, "ORDINAL_POSITION", Types.INTEGER, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 19, "IS_NULLABLE", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 20, "SPECIFIC_NAME", Types.VARCHAR, DatabaseMetaData.columnNullable);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 74 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project calcite-avatica by apache.

the class MetaResultSetTest method testGetSuperTables.

@Test
public void testGetSuperTables() throws SQLException {
    DatabaseMetaData metadata = getDatabaseMetadata();
    try (ResultSet rs = metadata.getSuperTables(null, null, null)) {
        ResultSetMetaData rsMeta = rs.getMetaData();
        assertEquals(4, rsMeta.getColumnCount());
        assertColumn(rsMeta, 1, "TABLE_CAT", Types.VARCHAR, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 2, "TABLE_SCHEM", Types.VARCHAR, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 3, "TABLE_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 4, "SUPERTABLE_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 75 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project calcite-avatica by apache.

the class MetaResultSetTest method testGetTablePrivileges.

@Test
public void testGetTablePrivileges() throws SQLException {
    DatabaseMetaData metadata = getDatabaseMetadata();
    try (ResultSet rs = metadata.getTablePrivileges(null, null, null)) {
        ResultSetMetaData rsMeta = rs.getMetaData();
        assertEquals(7, rsMeta.getColumnCount());
        assertColumn(rsMeta, 1, "TABLE_CAT", Types.VARCHAR, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 2, "TABLE_SCHEM", Types.VARCHAR, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 3, "TABLE_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 4, "GRANTOR", Types.VARCHAR, DatabaseMetaData.columnNullable);
        assertColumn(rsMeta, 5, "GRANTEE", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 6, "PRIVILEGE", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
        assertColumn(rsMeta, 7, "IS_GRANTABLE", Types.VARCHAR, DatabaseMetaData.columnNullable);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Aggregations

DatabaseMetaData (java.sql.DatabaseMetaData)360 ResultSet (java.sql.ResultSet)215 Connection (java.sql.Connection)169 SQLException (java.sql.SQLException)154 Test (org.junit.Test)87 Statement (java.sql.Statement)49 ResultSetMetaData (java.sql.ResultSetMetaData)42 ArrayList (java.util.ArrayList)40 PreparedStatement (java.sql.PreparedStatement)38 IOException (java.io.IOException)26 Properties (java.util.Properties)25 HashSet (java.util.HashSet)16 PhoenixDatabaseMetaData (org.apache.phoenix.jdbc.PhoenixDatabaseMetaData)16 HashMap (java.util.HashMap)15 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)15 PrintWriter (java.io.PrintWriter)10 Savepoint (java.sql.Savepoint)10 List (java.util.List)10 Map (java.util.Map)9 DataSource (javax.sql.DataSource)9