Search in sources :

Example 1 with PostgreSqlColumnDescription

use of com.axway.ats.core.dbaccess.postgresql.PostgreSqlColumnDescription in project ats-framework by Axway.

the class PostgreSqlEnvironmentHandler method getColumnsToSelect.

@Override
protected List<ColumnDescription> getColumnsToSelect(DbTable table, String userName) throws DbException, ColumnHasNoDefaultValueException {
    // just table name, w/o schema
    String tableName = table.getTableName();
    String schemaName = table.getTableSchema();
    if (StringUtils.isNullOrEmpty(table.getTableSchema())) {
        // public schema in the default schema if not specified. TODO: could also check search_path
        schemaName = "public";
    }
    // Alternative: check with DatabaseMetaData
    String selectColumnsInfo = "SELECT column_name, data_type, is_nullable, column_default " + " FROM information_schema.columns " + " WHERE table_name = '" + tableName + "' AND table_schema = '" + schemaName + "'" + // Use SQL standards defined system schema
    " ORDER BY ordinal_position";
    ArrayList<ColumnDescription> columnsToSelect = new ArrayList<>();
    DbRecordValuesList[] columnsMetaData;
    try {
        columnsMetaData = this.dbProvider.select(selectColumnsInfo);
    } catch (DbException e) {
        throw new DbException("Could not get columns for table " + table.getFullTableName() + ". Check if the table exists and that the user has permissions. See more details in the trace.", e);
    }
    // the Identity column currently not reported. See details below
    table.setIdentityColumnPresent(false);
    for (DbRecordValuesList columnMetaData : columnsMetaData) {
        String columnName = (String) columnMetaData.get("column_name");
        // check if the column should be skipped in the backup
        if (!table.getColumnsToExclude().contains(columnName)) {
            ColumnDescription colDescription = new PostgreSqlColumnDescription(columnName, (String) columnMetaData.get("data_type"));
            columnsToSelect.add(colDescription);
        /* is_identity" This column is available, but not populated by PostgreSQL, details https://www.postgresql.org/docs/current/infoschema-columns.html
                   Object isIdentityObj = columnMetaData.get("is_identity");
                   table.setIdentityColumnPresent(true/false)
                   */
        } else {
            // if this column has no default value, we cannot skip it in the backup
            if (columnMetaData.get("column_default") == null) {
                LOG.error("Cannot skip column named " + columnName + " with no default values while creating backup");
                throw new ColumnHasNoDefaultValueException(table.getFullTableName(), columnName);
            }
        }
    }
    return columnsToSelect;
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) ColumnHasNoDefaultValueException(com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException) PostgreSqlColumnDescription(com.axway.ats.core.dbaccess.postgresql.PostgreSqlColumnDescription) ColumnDescription(com.axway.ats.core.dbaccess.ColumnDescription) ArrayList(java.util.ArrayList) PostgreSqlColumnDescription(com.axway.ats.core.dbaccess.postgresql.PostgreSqlColumnDescription) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Aggregations

ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)1 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)1 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1 PostgreSqlColumnDescription (com.axway.ats.core.dbaccess.postgresql.PostgreSqlColumnDescription)1 ColumnHasNoDefaultValueException (com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException)1 ArrayList (java.util.ArrayList)1