Example usage for javax.sql.rowset CachedRowSet size

List of usage examples for javax.sql.rowset CachedRowSet size

Introduction

In this page you can find the example usage for javax.sql.rowset CachedRowSet size.

Prototype

public int size();

Source Link

Document

Returns the number of rows in this CachedRowSet object.

Usage

From source file:entity.Chart.java

private void init() {
    try {// w w  w  .  j  a v  a  2  s  .  c o m
        if (this.reportID < 0) {
            this.reportName = "Maj_Runs";
            this.sql = "exec sp_getMaj_Runs @BotID = " + Math.abs(reportID);
            this.server = SQLHelper.LOCAL_IP;
            this.database = SQLHelper.DB_QA_DATA;
            this.reportType = "tablechart";
            this.comments = "";
            this.startRow = 1;
            this.referQueryID = -1;
            this.groupClause = null;
            return;
        }
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("node", reportID);
        SqlCommand command = new SqlCommand(SQLHelper.LOCAL_IP, SQLHelper.DB_QA, CommandType.StoredProcedure,
                "sp_getQuery", params);
        HashMap<String, Object> cells = SQLHelper.executeCommand(command, new MapRowHandler());

        this.reportName = (String) cells.get("name");
        this.sql = (String) cells.get("queryText");
        this.server = (String) cells.get("server");
        if (this.server.equals("127.0.0.1") || this.server.equals("localhost")) {
            this.server = SQLHelper.LOCAL_IP;
        }
        this.database = (String) cells.get("database");
        this.reportType = (String) cells.get("queryType");
        this.comments = (String) cells.get("comments");
        this.startRow = (Integer) cells.get("startRow");
        this.referQueryID = (Integer) cells.get("ReferQueryID");
        this.groupClause = (String) cells.get("GroupClause");
        this.totalColumnName = (String) cells.get("TotalItemColumn");
        //set report parameters
        command = new SqlCommand(SQLHelper.LOCAL_IP, SQLHelper.DB_QA, CommandType.Text,
                "SELECT * FROM Qa_Query_Params WHERE ReportID = " + reportID);
        CachedRowSet rowSet = SQLHelper.executeCommand(command, new CachedRowSetResultHandler());
        rParams = new QueryParameter[rowSet.size()];
        int i = 0;
        while (rowSet.next()) {
            rParams[i] = new QueryParameter();
            rParams[i].name = rowSet.getString("Name");
            rParams[i].value = rowSet.getString("DefaultValue");
            rParams[i].type = rowSet.getString("DataType");
            i++;
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:uk.ac.ox.it.ords.api.database.structure.services.impl.hibernate.ConstraintServiceImpl.java

@Override
public void createConstraint(OrdsPhysicalDatabase database, String tableName, String constraintName,
        ConstraintRequest newConstraint, boolean staging) throws Exception {
    String query = "";

    String databaseName = database.getDbConsumedName();
    if (staging) {
        databaseName = this.calculateStagingName(databaseName);
    }/* ww w. j  a  va  2  s .c om*/
    String server = database.getDatabaseServer();
    // message = String.format(emd.getMessage("Rst048").getText(),
    // constraintName);
    Boolean isUnique = newConstraint.isUnique();
    Boolean isForeign = newConstraint.isForeign();
    Boolean isPrimary = newConstraint.isPrimary();

    List<String> columnsList = new ArrayList<String>();
    String[] columnsArray = newConstraint.getColumns();
    String columns = "";

    if ((isUnique != null && isUnique) || (isForeign != null && isForeign)
            || (isPrimary != null && isPrimary)) {
        // If we're creating a Unique, Foreign or Primary Key constraint
        if (columnsArray == null || columnsArray.length == 0) {
            // If no columns are specified, return an error
            // message = emd.getMessage("Rst035").getText();
            throw new BadParameterException("No columns specified");
        } else if ((isUnique != null && isUnique) || (isPrimary != null && isPrimary)) {
            // If we're creating a Unique or Primary Key constraint,
            // join the columns into a string.
            for (String column : newConstraint.getColumns()) {
                columnsList.add(quote_ident(column));
            }
            columns = StringUtils.join(columnsList.iterator(), ",");
        } else {
            // If we're creating a foreign key, make sure there's
            // only one column
            if (columnsArray.length > 1) {
                // message = emd.getMessage("Rst068").getText();
                throw new BadParameterException("Only 1 column can be specified for a foreign key");
            }
        }
    }
    // Check that the specified table exists
    if (!this.checkTableExists(tableName, databaseName, server)) {
        log.error("Tried to create constraint %s for non-existant table %s", constraintName, tableName);
        // message = String.format(emd.getMessage("Rst052").getText(),
        // tableName);
        throw new NotFoundException();
    }

    // Get the next value from a special sequence created when the
    // database is first cloned in ORDS, which makes sure we can
    // create a unique name for the constraint
    String conIdQuery = "SELECT nextval('ords_constraint_seq'::regclass) AS id";
    String uniqueConstraintName = "";
    CachedRowSet result = this.runJDBCQuery(conIdQuery, null, server, databaseName);

    // Object result = this.singleResultQuery(conIdQuery, databaseName,
    // userName, password);
    if (result != null && result.size() > 0) {
        // Actually generate a name for the constraint
        result.first();
        int conId = result.getInt("id");
        uniqueConstraintName = String.format(constraintName + "_%d", conId);
    } else {
        uniqueConstraintName = constraintName;
    }

    // Generate the SQL for creating the constraint
    if (isUnique != null && isUnique) {
        query = String.format("ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s)", quote_ident(tableName),
                quote_ident(uniqueConstraintName), columns);
    } else if (isPrimary != null && isPrimary) {
        query = String.format("ALTER TABLE %s ADD CONSTRAINT %s PRIMARY KEY (%s)", quote_ident(tableName),
                quote_ident(uniqueConstraintName), columns);
    } else if (isForeign != null && isForeign) {
        String column = newConstraint.getColumns()[0];
        String refTable = newConstraint.getReftable();

        String refColumn = newConstraint.getRefcolumn();

        // If this is a foreign key, make sure there is a
        // referenced table specified
        if (refTable == null || refTable.isEmpty()) {
            // message = emd.getMessage("Rst049").getText();
            throw new BadParameterException("A foreign key must have a reference table specified");
        }

        // Make sure there is a referenced column specified
        if (refColumn == null || refColumn.isEmpty()) {
            // message = emd.getMessage("Rst051").getText();
            throw new BadParameterException("A reference column must be specified");
        }

        query = String.format("ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) " + "REFERENCES %s (%s)",
                quote_ident(tableName), quote_ident(uniqueConstraintName), quote_ident(column),
                quote_ident(refTable), quote_ident(refColumn));
    } else {
        // If this isn't a Unique, Foreign or Primary key constraint
        // make sure a check expression is defined and generate the
        // SQL. Check constraints currently aren't implemented in
        // the Schema Designer interface.
        String checkExpression = newConstraint.getCheckExpression();
        if (checkExpression == null || checkExpression.isEmpty()) {
            // message = emd.getMessage("Rst051").getText();
            throw new BadParameterException("Check constraints are not supported");
        }

        query = String.format("ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s) ", quote_ident(tableName),
                quote_ident(uniqueConstraintName), checkExpression);
    }

    // Check that a constraint with this name doesn't already exist.
    if (this.checkConstraintExists(tableName, uniqueConstraintName, databaseName, server)) {
        log.error("Tried to create duplicate constraint name %s on table %s", uniqueConstraintName, tableName);
        // message = String.format(emd.getMessage("Rst053").getText(),
        // uniqueConstraintName,
        // tableName);
        throw new NamingConflictException("Can't duplication constraint name");
    }

    // Create the constraint
    this.runJDBCQuery(query, null, server, databaseName);
}