Search in sources :

Example 11 with RowDescriptor

use of org.firebirdsql.gds.ng.fields.RowDescriptor in project jaybird by FirebirdSQL.

the class FBDatabaseMetaData method getImportedKeys.

/**
 * Gets a description of the primary key columns that are
 * referenced by a table's foreign key columns (the primary keys
 * imported by a table).  They are ordered by PKTABLE_CAT,
 * PKTABLE_SCHEM, PKTABLE_NAME, and KEY_SEQ.
 *
 * <P>Each primary key column description has the following columns:
 *  <OL>
 *  <LI><B>PKTABLE_CAT</B> String => primary key table catalog
 *      being imported (may be null)
 *  <LI><B>PKTABLE_SCHEM</B> String => primary key table schema
 *      being imported (may be null)
 *  <LI><B>PKTABLE_NAME</B> String => primary key table name
 *      being imported
 *  <LI><B>PKCOLUMN_NAME</B> String => primary key column name
 *      being imported
 *  <LI><B>FKTABLE_CAT</B> String => foreign key table catalog (may be null)
 *  <LI><B>FKTABLE_SCHEM</B> String => foreign key table schema (may be null)
 *  <LI><B>FKTABLE_NAME</B> String => foreign key table name
 *  <LI><B>FKCOLUMN_NAME</B> String => foreign key column name
 *  <LI><B>KEY_SEQ</B> short => sequence number within foreign key
 *  <LI><B>UPDATE_RULE</B> short => What happens to
 *       foreign key when primary is updated:
 *      <UL>
 *      <LI> importedNoAction - do not allow update of primary
 *               key if it has been imported
 *      <LI> importedKeyCascade - change imported key to agree
 *               with primary key update
 *      <LI> importedKeySetNull - change imported key to NULL if
 *               its primary key has been updated
 *      <LI> importedKeySetDefault - change imported key to default values
 *               if its primary key has been updated
 *      <LI> importedKeyRestrict - same as importedKeyNoAction
 *                                 (for ODBC 2.x compatibility)
 *      </UL>
 *  <LI><B>DELETE_RULE</B> short => What happens to
 *      the foreign key when primary is deleted.
 *      <UL>
 *      <LI> importedKeyNoAction - do not allow delete of primary
 *               key if it has been imported
 *      <LI> importedKeyCascade - delete rows that import a deleted key
 *      <LI> importedKeySetNull - change imported key to NULL if
 *               its primary key has been deleted
 *      <LI> importedKeyRestrict - same as importedKeyNoAction
 *                                 (for ODBC 2.x compatibility)
 *      <LI> importedKeySetDefault - change imported key to default if
 *               its primary key has been deleted
 *      </UL>
 *  <LI><B>FK_NAME</B> String => foreign key name (may be null)
 *  <LI><B>PK_NAME</B> String => primary key name (may be null)
 *  <LI><B>DEFERRABILITY</B> short => can the evaluation of foreign key
 *      constraints be deferred until commit
 *      <UL>
 *      <LI> importedKeyInitiallyDeferred - see SQL92 for definition
 *      <LI> importedKeyInitiallyImmediate - see SQL92 for definition
 *      <LI> importedKeyNotDeferrable - see SQL92 for definition
 *      </UL>
 *  </OL>
 *
 * @param catalog a catalog name; "" retrieves those without a
 * catalog; null means drop catalog name from the selection criteria
 * @param schema a schema name; "" retrieves those
 * without a schema
 * @param table a table name
 * @return <code>ResultSet</code> - each row is a primary key column description
 * @exception SQLException if a database access error occurs
 * @see #getExportedKeys
 */
public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException {
    final RowDescriptor rowDescriptor = new RowDescriptorBuilder(14, datatypeCoder).at(0).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "PKTABLE_CAT", "COLUMNINFO").addField().at(1).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "PKTABLE_SCHEM", "COLUMNINFO").addField().at(2).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "PKTABLE_NAME", "COLUMNINFO").addField().at(3).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "PKCOLUMN_NAME", "COLUMNINFO").addField().at(4).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "FKTABLE_CAT", "COLUMNINFO").addField().at(5).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "FKTABLE_SCHEM", "COLUMNINFO").addField().at(6).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "FKTABLE_NAME", "COLUMNINFO").addField().at(7).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "FKCOLUMN_NAME", "COLUMNINFO").addField().at(8).simple(SQL_SHORT, 0, "KEY_SEQ", "COLUMNINFO").addField().at(9).simple(SQL_SHORT, 0, "UPDATE_RULE", "COLUMNINFO").addField().at(10).simple(SQL_SHORT, 0, "DELETE_RULE", "COLUMNINFO").addField().at(11).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "FK_NAME", "COLUMNINFO").addField().at(12).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "PK_NAME", "COLUMNINFO").addField().at(13).simple(SQL_SHORT, 0, "DEFERRABILITY", "COLUMNINFO").addField().toRowDescriptor();
    List<String> params = Collections.singletonList(table);
    try (ResultSet rs = doQuery(GET_IMPORTED_KEYS, params)) {
        // if nothing found, return an empty result set
        if (!rs.next()) {
            return new FBResultSet(rowDescriptor, Collections.<RowValue>emptyList());
        }
        final List<RowValue> rows = new ArrayList<>();
        final RowValueBuilder valueBuilder = new RowValueBuilder(rowDescriptor);
        do {
            rows.add(valueBuilder.at(2).set(getBytes(rs.getString("PKTABLE_NAME"))).at(3).set(getBytes(rs.getString("PKCOLUMN_NAME"))).at(6).set(getBytes(rs.getString("FKTABLE_NAME"))).at(7).set(getBytes(rs.getString("FKCOLUMN_NAME"))).at(8).set(createShort(rs.getShort("KEY_SEQ"))).at(9).set(mapAction(rs.getString("UPDATE_RULE"))).at(10).set(mapAction(rs.getString("DELETE_RULE"))).at(11).set(getBytes(rs.getString("FK_NAME"))).at(12).set(getBytes(rs.getString("PK_NAME"))).at(13).set(IMPORTED_KEY_NOT_DEFERRABLE).toRowValue(true));
        } while (rs.next());
        return new FBResultSet(rowDescriptor, rows);
    }
}
Also used : RowDescriptorBuilder(org.firebirdsql.gds.ng.fields.RowDescriptorBuilder) RowValueBuilder(org.firebirdsql.gds.ng.fields.RowValueBuilder) RowValue(org.firebirdsql.gds.ng.fields.RowValue) RowDescriptor(org.firebirdsql.gds.ng.fields.RowDescriptor)

Example 12 with RowDescriptor

use of org.firebirdsql.gds.ng.fields.RowDescriptor in project jaybird by FirebirdSQL.

the class FBDatabaseMetaData method getColumnPrivileges.

/**
 * Gets a description of the access rights for a table's columns.
 *
 * <P>Only privileges matching the column name criteria are
 * returned.  They are ordered by COLUMN_NAME and PRIVILEGE.
 *
 * <P>Each privilige description has the following columns:
 *  <OL>
 *  <LI><B>TABLE_CAT</B> String => table catalog (may be null)
 *  <LI><B>TABLE_SCHEM</B> String => table schema (may be null)
 *  <LI><B>TABLE_NAME</B> String => table name
 *  <LI><B>COLUMN_NAME</B> String => column name
 *  <LI><B>GRANTOR</B> => grantor of access (may be null)
 *  <LI><B>GRANTEE</B> String => grantee of access
 *  <LI><B>PRIVILEGE</B> String => name of access (SELECT,
 *      INSERT, UPDATE, REFRENCES, ...)
 *  <LI><B>IS_GRANTABLE</B> String => "YES" if grantee is permitted
 *      to grant to others; "NO" if not; null if unknown
 *  </OL>
 *
 * @param catalog a catalog name; "" retrieves those without a
 * catalog; null means drop catalog name from the selection criteria
 * @param schema a schema name; "" retrieves those without a schema
 * @param table a table name
 * @param columnNamePattern a column name pattern
 * @return <code>ResultSet</code> - each row is a column privilege description
 * @exception SQLException if a database access error occurs
 * @see #getSearchStringEscape
 */
public ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern) throws SQLException {
    final RowDescriptor rowDescriptor = new RowDescriptorBuilder(8, datatypeCoder).at(0).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "TABLE_CAT", "COLUMNPRIV").addField().at(1).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "TABLE_SCHEM", "COLUMNPRIV").addField().at(2).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "TABLE_NAME", "COLUMNPRIV").addField().at(3).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "COLUMN_NAME", "COLUMNPRIV").addField().at(4).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "GRANTOR", "COLUMNPRIV").addField().at(5).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "GRANTEE", "COLUMNPRIV").addField().at(6).simple(SQL_VARYING, 31, "PRIVILEGE", "COLUMNPRIV").addField().at(7).simple(SQL_VARYING, 31, "IS_GRANTABLE", "COLUMNPRIV").addField().toRowDescriptor();
    Clause columnClause = new Clause("RF.RDB$FIELD_NAME", columnNamePattern);
    String sql = GET_COLUMN_PRIVILEGES_START;
    sql += columnClause.getCondition();
    sql += GET_COLUMN_PRIVILEGES_END;
    List<String> params = new ArrayList<>(2);
    params.add(table);
    if (columnClause.hasCondition()) {
        params.add(columnClause.getValue());
    }
    try (ResultSet rs = doQuery(sql, params)) {
        // return empty result set
        if (!rs.next()) {
            return new FBResultSet(rowDescriptor, Collections.<RowValue>emptyList());
        }
        final List<RowValue> rows = new ArrayList<>();
        final RowValueBuilder valueBuilder = new RowValueBuilder(rowDescriptor);
        do {
            rows.add(valueBuilder.at(2).set(getBytes(rs.getString("TABLE_NAME"))).at(3).set(getBytes(rs.getString("COLUMN_NAME"))).at(4).set(getBytes(rs.getString("GRANTOR"))).at(5).set(getBytes(rs.getString("GRANTEE"))).at(6).set(mapPrivilege(rs.getString("PRIVILEGE"))).at(7).set(rs.getShort("IS_GRANTABLE") == 0 ? NO_BYTES : YES_BYTES).toRowValue(true));
        } while (rs.next());
        return new FBResultSet(rowDescriptor, rows);
    }
}
Also used : RowDescriptorBuilder(org.firebirdsql.gds.ng.fields.RowDescriptorBuilder) RowValueBuilder(org.firebirdsql.gds.ng.fields.RowValueBuilder) RowValue(org.firebirdsql.gds.ng.fields.RowValue) RowDescriptor(org.firebirdsql.gds.ng.fields.RowDescriptor)

Example 13 with RowDescriptor

use of org.firebirdsql.gds.ng.fields.RowDescriptor in project jaybird by FirebirdSQL.

the class AbstractPreparedStatement method prepareFixedStatement.

/**
 * Prepare fixed statement and initialize parameters.
 */
protected void prepareFixedStatement(String sql) throws SQLException {
    super.prepareFixedStatement(sql);
    RowDescriptor rowDescriptor = fbStatement.getParameterDescriptor();
    assert rowDescriptor != null : "RowDescriptor should not be null after prepare";
    isParamSet = new boolean[rowDescriptor.getCount()];
    fieldValues = rowDescriptor.createDefaultFieldValues();
    fields = new FBField[rowDescriptor.getCount()];
    for (int i = 0; i < isParamSet.length; i++) {
        FieldDataProvider dataProvider = fieldValues.getFieldValue(i);
        // FIXME check if we can safely pass cached here
        fields[i] = FBField.createField(getParameterDescriptor(i + 1), dataProvider, gdsHelper, false);
    }
    this.isExecuteProcedureStatement = fbStatement.getType() == StatementType.STORED_PROCEDURE;
}
Also used : FieldDataProvider(org.firebirdsql.jdbc.field.FieldDataProvider) RowDescriptor(org.firebirdsql.gds.ng.fields.RowDescriptor)

Example 14 with RowDescriptor

use of org.firebirdsql.gds.ng.fields.RowDescriptor in project jaybird by FirebirdSQL.

the class OODatabaseMetaData method getTablePrivileges.

@Override
public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) throws SQLException {
    final RowDescriptor rowDescriptor = buildTablePrivilegeRSMetaData();
    Clause tableClause1 = new Clause("RDB$RELATION_NAME", tableNamePattern);
    Clause tableClause2 = new Clause("RDB$RELATION_NAME", tableNamePattern);
    String sql = GET_TABLE_PRIVILEGES_START_1;
    sql += tableClause1.getCondition();
    sql += GET_TABLE_PRIVILEGES_END_1;
    sql += GET_TABLE_PRIVILEGES_START_2;
    sql += tableClause2.getCondition();
    sql += GET_TABLE_PRIVILEGES_END_2;
    List<String> params = new ArrayList<>(2);
    if (tableClause1.hasCondition()) {
        params.add(tableClause1.getValue());
    }
    if (tableClause2.hasCondition()) {
        params.add(tableClause2.getValue());
    }
    try (ResultSet rs = doQuery(sql, params)) {
        // if nothing found, return an empty result set
        if (!rs.next()) {
            return new FBResultSet(rowDescriptor, Collections.<RowValue>emptyList());
        }
        return processTablePrivileges(rowDescriptor, rs);
    }
}
Also used : ArrayList(java.util.ArrayList) FBResultSet(org.firebirdsql.jdbc.FBResultSet) ResultSet(java.sql.ResultSet) FBResultSet(org.firebirdsql.jdbc.FBResultSet) RowDescriptor(org.firebirdsql.gds.ng.fields.RowDescriptor)

Example 15 with RowDescriptor

use of org.firebirdsql.gds.ng.fields.RowDescriptor in project jaybird by FirebirdSQL.

the class FBDatabaseMetaData method getProcedures.

/**
 * Gets a description of the stored procedures available in a
 * catalog.
 *
 * <P>Only procedure descriptions matching the schema and
 * procedure name criteria are returned.  They are ordered by
 * PROCEDURE_SCHEM, and PROCEDURE_NAME.
 *
 * <P>Each procedure description has the the following columns:
 *  <OL>
 *  <LI><B>PROCEDURE_CAT</B> String => procedure catalog (may be null)
 *  <LI><B>PROCEDURE_SCHEM</B> String => procedure schema (may be null)
 *  <LI><B>PROCEDURE_NAME</B> String => procedure name
 *  <LI> reserved for future use
 *  <LI> reserved for future use
 *  <LI> reserved for future use
 *  <LI><B>REMARKS</B> String => explanatory comment on the procedure
 *  <LI><B>PROCEDURE_TYPE</B> short => kind of procedure:
 *      <UL>
 *      <LI> procedureResultUnknown - May return a result
 *      <LI> procedureNoResult - Does not return a result
 *      <LI> procedureReturnsResult - Returns a result
 *      </UL>
 *  <LI><B>SPECIFIC_NAME</B> String => The name which uniquely identifies this procedure within its schema.
 *  </OL>
 *
 * @param catalog a catalog name; "" retrieves those without a
 * catalog; null means drop catalog name from the selection criteria
 * @param schemaPattern a schema name pattern; "" retrieves those
 * without a schema
 * @param procedureNamePattern a procedure name pattern
 * @return <code>ResultSet</code> - each row is a procedure description
 * @exception SQLException if a database access error occurs
 * @see #getSearchStringEscape
 */
public ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) throws SQLException {
    final RowDescriptor rowDescriptor = new RowDescriptorBuilder(9, datatypeCoder).at(0).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "PROCEDURE_CAT", "PROCEDURES").addField().at(1).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "PROCEDURE_SCHEM", "ROCEDURES").addField().at(2).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "PROCEDURE_NAME", "PROCEDURES").addField().at(3).simple(SQL_VARYING, 31, "FUTURE1", "PROCEDURES").addField().at(4).simple(SQL_VARYING, 31, "FUTURE2", "PROCEDURES").addField().at(5).simple(SQL_VARYING, 31, "FUTURE3", "PROCEDURES").addField().at(6).simple(SQL_VARYING, Integer.MAX_VALUE, "REMARKS", "PROCEDURES").addField().at(7).simple(SQL_SHORT, 0, "PROCEDURE_TYPE", "PROCEDURES").addField().at(8).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "SPECIFIC_NAME", "PROCEDURES").addField().toRowDescriptor();
    Clause procedureClause = new Clause("RDB$PROCEDURE_NAME", procedureNamePattern);
    String sql = GET_PROCEDURES_START;
    sql += procedureClause.getCondition();
    sql += GET_PROCEDURES_END;
    List<String> params = procedureClause.hasCondition() ? Collections.singletonList(procedureClause.getValue()) : Collections.<String>emptyList();
    try (ResultSet rs = doQuery(sql, params)) {
        if (!rs.next()) {
            return new FBResultSet(rowDescriptor, Collections.<RowValue>emptyList());
        }
        final List<RowValue> rows = new ArrayList<>();
        final RowValueBuilder valueBuilder = new RowValueBuilder(rowDescriptor);
        do {
            rows.add(valueBuilder.at(2).set(getBytes(rs.getString("PROCEDURE_NAME"))).at(6).set(getBytes(rs.getString("REMARKS"))).at(7).set(rs.getShort("PROCEDURE_TYPE") == 0 ? PROCEDURE_NO_RESULT : PROCEDURE_RETURNS_RESULT).at(8).set(valueBuilder.get(2)).toRowValue(true));
        } while (rs.next());
        return new FBResultSet(rowDescriptor, rows);
    }
}
Also used : RowDescriptorBuilder(org.firebirdsql.gds.ng.fields.RowDescriptorBuilder) RowValueBuilder(org.firebirdsql.gds.ng.fields.RowValueBuilder) RowValue(org.firebirdsql.gds.ng.fields.RowValue) RowDescriptor(org.firebirdsql.gds.ng.fields.RowDescriptor)

Aggregations

RowDescriptor (org.firebirdsql.gds.ng.fields.RowDescriptor)23 RowValue (org.firebirdsql.gds.ng.fields.RowValue)13 RowDescriptorBuilder (org.firebirdsql.gds.ng.fields.RowDescriptorBuilder)12 RowValueBuilder (org.firebirdsql.gds.ng.fields.RowValueBuilder)10 Test (org.junit.Test)7 FieldDescriptor (org.firebirdsql.gds.ng.fields.FieldDescriptor)6 FirebirdSupportInfo (org.firebirdsql.util.FirebirdSupportInfo)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLNonTransientException (java.sql.SQLNonTransientException)1 SQLTransientException (java.sql.SQLTransientException)1 ArrayList (java.util.ArrayList)1 Encoding (org.firebirdsql.encodings.Encoding)1 FieldValue (org.firebirdsql.gds.ng.fields.FieldValue)1 SimpleStatementListener (org.firebirdsql.gds.ng.wire.SimpleStatementListener)1 FBResultSet (org.firebirdsql.jdbc.FBResultSet)1 FieldDataProvider (org.firebirdsql.jdbc.field.FieldDataProvider)1