Search in sources :

Example 51 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project nutz by nutzam.

the class QueryMapCallback method invoke.

public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
    final ResultSetMetaData meta = rs.getMetaData();
    // ResultSetLooping 封装了遍历结果集的方法,里面包含了针对sqlserver等浮标型分页的支持
    ResultSetLooping ing = new ResultSetLooping() {

        protected boolean createObject(int index, ResultSet rs, SqlContext context, int rowCout) {
            NutMap re = new NutMap();
            Record.create(re, rs, meta);
            list.add(re);
            return true;
        }
    };
    ing.doLoop(rs, sql.getContext());
    return ing.getList();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SqlContext(org.nutz.dao.sql.SqlContext) ResultSetLooping(org.nutz.dao.pager.ResultSetLooping) ResultSet(java.sql.ResultSet) NutMap(org.nutz.lang.util.NutMap)

Example 52 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project OpenAM by OpenRock.

the class LogReadDBHandler method logRecRead.

/**
     * LogReader calls this method. It collects header, records,
     * applies query (if any), sorts (if asked) the records on field, checks
     * the max records to return, collects all the recods and returns.
     *
     * @param tableNames db table names
     * @param logQuery is user specified qury chriteria with sorting requirement
     * @param logMgr the log manager associated with this handler
     * @param sourceData it specifies whether return data should be original
     *        data received by logger (source) or formatted data as in file.
     * @return all the matched records with query
     * @throws IOException if it fails to read log records.
     * @throws NoSuchFieldException if it fails to retrieve the name of field.
     * @throws IllegalArgumentException if query has wrong value.
     * @throws RuntimeException if it fails to retrieve log record.
     * @throws SQLException if it fails to process sql query.
     * @throws Exception if it fails any of operation.
     */
public String[][] logRecRead(Set tableNames, LogQuery logQuery, java.util.logging.LogManager logMgr, boolean sourceData) throws IOException, NoSuchFieldException, IllegalArgumentException, RuntimeException, SQLException, Exception {
    String sortField = null;
    // if the object is persistence use it otherwise don't
    this.cleaner();
    //
    // tblNames is needed for the guaranteed "underscore" form
    // (e.g., "amAuthentication_access") of the table names
    // 
    Set tblNames = new HashSet();
    StringBuilder allTablesSB = new StringBuilder("");
    for (Iterator it = tableNames.iterator(); it.hasNext(); ) {
        String ss = (String) it.next();
        String ss2 = ss.replace('.', '_');
        tblNames.add(ss2);
        allTablesSB.append(ss2);
    }
    //
    try {
        this.databaseURL = logMgr.getProperty(LogConstants.LOG_LOCATION);
        this.dbDriver = logMgr.getProperty(LogConstants.DB_DRIVER);
        this.dbUserName = logMgr.getProperty(LogConstants.DB_USER);
        this.dbPassWord = logMgr.getProperty(LogConstants.DB_PASSWORD);
        this.maxRecordsStr = logMgr.getProperty(LogConstants.MAX_RECORDS);
    } catch (Exception e) {
        Debug.error("DBLogRecReadSet:config: ", e);
        // rethrow the exception
        throw e;
    }
    if (this.dbDriver.toLowerCase().indexOf("oracle") != -1) {
        isMySQL = false;
    } else if (this.dbDriver.toLowerCase().indexOf("mysql") != -1) {
        isMySQL = true;
    } else {
        isMySQL = false;
        Debug.warning("DBlogRecRead:assuming driver: '" + this.dbDriver + "' is Oracle-compatible.");
    }
    try {
        this.maxRecords = Integer.parseInt(maxRecordsStr);
    } catch (NumberFormatException nfe) {
        if (Debug.warningEnabled()) {
            Debug.warning("DBlogRecRead(s): maxRecords error (" + maxRecordsStr + "), set to MAX");
        }
        this.maxRecords = LogConstants.MAX_RECORDS_DEFAULT_INT;
    }
    //
    //  kind of a bad situation here between Oracle and MySQL.
    //  pre-v4 MySQL doesn't support the "union" operator, so multi-
    //  table selects has to be affected as multiple single-table
    //  selects and combining their results.
    //
    //  the Oracle case is more straightforward, and if we decide
    //  to only support >= V4 MySQL, can just use the !isMySQL
    //  branch.
    //
    String selectStr;
    if (!isMySQL) {
        if (sourceData == true) {
            String temps = logQuery.getSortingField();
            if (temps != null) {
                sortField = temps.trim();
            }
            // default all
            String columns = "*";
            ArrayList sCol = logQuery.getColumns();
            if (sCol != null) {
                StringBuilder colSB = new StringBuilder();
                int sSize = sCol.size();
                for (int i = 0; i < sSize; i++) {
                    colSB.append((String) sCol.get(i));
                    if ((i + 1) < sSize) {
                        colSB.append(", ");
                    }
                }
                columns = colSB.toString();
            }
            selectStr = lq2Select(tblNames, columns, logQuery);
            if (Debug.messageEnabled()) {
                Debug.message("logRecRead/4:selectStr = " + selectStr);
            }
        } else {
            // default all
            String columns = "*";
            selectStr = lq2Select(tblNames, columns, null);
            if (Debug.messageEnabled()) {
                Debug.message("logRecRead/4.2:selectStr = " + selectStr);
            }
        }
        String[][] tableResults;
        try {
            connectToDatabase(dbUserName, dbPassWord);
        } catch (SQLException sqe) {
            Debug.error("DBlogRecRead:connect:SQE:code=" + sqe.getErrorCode() + ", msg=" + sqe.getMessage());
            // rethrow for LogReader to catch
            throw sqe;
        } catch (ClassNotFoundException cnfe) {
            // rethrow for LogReader to catch
            throw cnfe;
        }
        String selStr = selectStr;
        Statement stmt = null;
        int numberOfRows = 0;
        try {
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            if (Debug.messageEnabled()) {
                Debug.message("DBlogRecRead:about to execute: " + selStr);
            }
            ResultSet rs = stmt.executeQuery(selStr);
            //
            //  fetchsize appears to be 10 from rs.getFetchSize();
            //
            ResultSetMetaData rsmd = rs.getMetaData();
            int numberOfColumns = rsmd.getColumnCount();
            if (Debug.messageEnabled()) {
                Debug.message("DBlogRecRead:#columns = " + numberOfColumns);
            }
            //
            //  these are the column (field) names
            //
            String[] spltHdrStr = new String[numberOfColumns];
            for (int i = 1; i <= numberOfColumns; i++) {
                String tempstr = rsmd.getColumnName(i);
                if (Debug.messageEnabled()) {
                    Debug.message("DBlogRecRead:col #" + i + " name = " + tempstr);
                }
                spltHdrStr[i - 1] = tempstr;
            }
            listOfValidRecords.add(spltHdrStr);
            // have to figure out #rows
            while (rs.next()) {
                numberOfRows++;
            }
            if (Debug.messageEnabled()) {
                Debug.message("DBlogRecRead:#rows = " + numberOfRows);
            }
            if (numberOfRows == 0) {
                stmt.close();
                try {
                    conn.close();
                } catch (SQLException ex) {
                    //
                    //  might not care about this too much...?
                    //
                    Debug.error("DBlogRecRead:rows=0:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
                }
                //
                //  should be at least the column names
                //
                int recSize = listOfValidRecords.size();
                if (recSize <= 0) {
                    return null;
                }
                queryResult = new String[recSize][];
                for (int i = 0; i < recSize; i++) {
                    queryResult[i] = (String[]) listOfValidRecords.get(i);
                }
                return queryResult;
            }
            if (numberOfRows > this.maxRecords) {
                stmt.close();
                try {
                    conn.close();
                } catch (SQLException ex) {
                    //
                    //  don't care about this too much...
                    //
                    Debug.error("DBlogRecRead:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
                }
                throw new AMLogException(AMLogException.LOG_DB_TOOMANYRECORDS);
            }
            //
            //  reset to the beginning
            //
            boolean isFirst = rs.first();
            if (isFirst == false) {
                Debug.error("DBlogRecRead:first() is false!");
            }
            //
            //  think we're not going to allow MOST_RECENT_MAX_RECORDS
            //  with multi-table query...
            //
            int rowsToAlloc = numberOfRows;
            tableResults = new String[rowsToAlloc][numberOfColumns];
            String result = null;
            int rowCount = 0;
            for (int i = 0; i < numberOfColumns; i++) {
                result = rs.getString(i + 1);
                tableResults[0][i] = result;
            }
            rowCount = 1;
            while (rs.next()) {
                for (int i = 0; i < numberOfColumns; i++) {
                    result = rs.getString(i + 1);
                    tableResults[rowCount][i] = result;
                }
                rowCount++;
            }
            stmt.close();
        } catch (SQLException se) {
            Debug.error("DBlogRecRead:query:SQE:code=" + se.getErrorCode() + ", msg=" + se.getMessage());
            // rethrow for LogReader to catch
            throw se;
        }
        try {
            this.getRecords(tableResults, sourceData);
        } catch (IOException e) {
            // catch & rethrow it
            throw e;
        } catch (IllegalArgumentException e) {
            // catch & rethrow it
            throw e;
        } catch (RuntimeException e) {
            // catch & rethrow it
            throw e;
        } catch (Exception e) {
            // catch & rethrow it
            throw e;
        }
        int recSize = listOfValidRecords.size();
        // checks whether it has got any record or not
        if (recSize <= 0) {
            // if no record found return null
            return null;
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            //
            //  might not care about this too much...?
            //
            Debug.error("DBlogRecRead:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
        }
        queryResult = new String[recSize][];
        for (int i = 0; i < recSize; i++) {
            queryResult[i] = (String[]) listOfValidRecords.get(i);
        }
    } else {
        // else (isMySQL case)
        //
        //  Multi-table select for <V4 MySQL is essentially
        //  looping through the tables and combining the results.
        //
        String columns = null;
        if (sourceData == true) {
            String temps = logQuery.getSortingField();
            if (temps != null) {
                sortField = temps.trim();
            }
            // default all
            columns = "*";
            ArrayList sCol = logQuery.getColumns();
            if (sCol != null) {
                StringBuilder colSB = new StringBuilder();
                int sSize = sCol.size();
                for (int i = 0; i < sSize; i++) {
                    colSB.append((String) sCol.get(i));
                    if ((i + 1) < sSize) {
                        colSB.append(", ");
                    }
                }
                columns = colSB.toString();
            }
        } else {
            // default all
            columns = "*";
        }
        //
        //  do same select on each table
        //
        boolean isFirstTable = true;
        int totalNumberOfRows = 0;
        int recSize = 0;
        for (Iterator it = tblNames.iterator(); it.hasNext(); ) {
            String thisTable = (String) it.next();
            if (sourceData == true) {
                selectStr = lq2Select(thisTable, columns, logQuery);
                if (Debug.messageEnabled()) {
                    Debug.message("logRecRead/5:selectStr = " + selectStr);
                }
            } else {
                selectStr = lq2Select(thisTable, columns, null);
                if (Debug.messageEnabled()) {
                    Debug.message("logRecRead/5.2:selectStr = " + selectStr);
                }
            }
            String[][] tableResults = null;
            try {
                connectToDatabase(dbUserName, dbPassWord);
            } catch (SQLException sqe) {
                Debug.error("DBlogRecRead:connect:SQE:code=" + sqe.getErrorCode() + ", msg=" + sqe.getMessage());
                // rethrow for LogReader to catch
                throw sqe;
            } catch (ClassNotFoundException cnfe) {
                // rethrow for LogReader to catch
                throw cnfe;
            }
            String selStr = selectStr;
            Statement stmt = null;
            int numberOfRows = 0;
            //
            try {
                stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                if (Debug.messageEnabled()) {
                    Debug.message("DBlogRecRead:about to execute: " + selStr);
                }
                ResultSet rs = stmt.executeQuery(selStr);
                //
                //  fetchsize appears to be 10 from rs.getFetchSize();
                //
                ResultSetMetaData rsmd = rs.getMetaData();
                int numberOfColumns = rsmd.getColumnCount();
                if (Debug.messageEnabled()) {
                    Debug.message("DBlogRecRead:#columns = " + numberOfColumns);
                }
                if (isFirstTable) {
                    String[] spltHdrStr = new String[numberOfColumns];
                    for (int i = 1; i <= numberOfColumns; i++) {
                        String tempstr = rsmd.getColumnName(i);
                        if (Debug.messageEnabled()) {
                            Debug.message("DBlogRecRead:col #" + i + " name = " + tempstr);
                        }
                        spltHdrStr[i - 1] = tempstr;
                    }
                    listOfValidRecords.add(spltHdrStr);
                }
                // have to figure out #rows
                numberOfRows = 0;
                while (rs.next()) {
                    numberOfRows++;
                }
                totalNumberOfRows += numberOfRows;
                if (totalNumberOfRows > this.maxRecords) {
                    stmt.close();
                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        //
                        //  don't care about this too much...
                        //
                        Debug.error("DBlogRecRead:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
                    }
                    throw new AMLogException(AMLogException.LOG_DB_TOOMANYRECORDS);
                }
                if (numberOfRows > 0) {
                    //
                    //  reset to the "beginning"
                    //
                    boolean isFirst = rs.first();
                    if (isFirst == false) {
                        Debug.error("DBlogRecRead:first() is false!");
                    }
                    //
                    //  think we're not going to allow 
                    //  MOST_RECENT_MAX_RECORDS with multi-table query...
                    //
                    tableResults = new String[numberOfRows][numberOfColumns];
                    String result = null;
                    int rowCount = 0;
                    do {
                        for (int i = 0; i < numberOfColumns; i++) {
                            result = rs.getString(i + 1);
                            tableResults[rowCount][i] = result;
                        }
                        rowCount++;
                    } while (rs.next());
                }
                //
                //  print the actual results in the debug log
                //
                stmt.close();
            } catch (SQLException se) {
                Debug.error("DBlogRecRead:query:SQE:code=" + se.getErrorCode() + ", msg=" + se.getMessage());
                // rethrow for LogReader to catch
                throw se;
            }
            if (numberOfRows > 0) {
                try {
                    this.getRecords(tableResults, sourceData);
                } catch (IOException e) {
                    // catch & rethrow it
                    throw e;
                } catch (IllegalArgumentException e) {
                    // catch & rethrow it
                    throw e;
                } catch (RuntimeException e) {
                    // catch & rethrow it
                    throw e;
                } catch (Exception e) {
                    // catch & rethrow it
                    throw e;
                }
            }
            if (isFirstTable) {
                isFirstTable = false;
            }
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            //
            //  might not care about this too much...?
            //
            Debug.error("DBlogRecRead:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
        }
        if (logQuery != null) {
            String sortByField = logQuery.getSortingField();
            if (sortByField != null) {
                try {
                    this.sorter = new LogRecordSorter(sortByField, listOfValidRecords);
                    queryResult = this.sorter.getSortedRecords();
                } catch (NoSuchFieldException e) {
                    Debug.error("DBlogRecRead/5:sort:nsfe: " + e.getMessage());
                    throw e;
                } catch (IllegalArgumentException e) {
                    Debug.error("DBlogRecRead/5:sort:iae: " + e.getMessage());
                    throw e;
                } catch (RuntimeException e) {
                    Debug.error("DBlogRecRead/5:sort:rte: " + e.getMessage());
                    throw e;
                } catch (Exception e) {
                    Debug.error("DBlogRecRead/5:sort:ex: " + e.getMessage());
                    throw e;
                }
                return (queryResult);
            }
        }
        recSize = listOfValidRecords.size();
        // checks whether it has got any record or not
        if (recSize <= 0) {
            // if no record found return null
            return null;
        }
        //
        //  can we use the toArray() converter?
        //
        queryResult = new String[recSize][];
        for (int i = 0; i < recSize; i++) {
            queryResult[i] = (String[]) listOfValidRecords.get(i);
        }
    }
    return queryResult;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) LogRecordSorter(com.sun.identity.log.util.LogRecordSorter) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) SQLException(java.sql.SQLException) AMLogException(com.sun.identity.log.AMLogException) ResultSetMetaData(java.sql.ResultSetMetaData) Iterator(java.util.Iterator) ResultSet(java.sql.ResultSet) AMLogException(com.sun.identity.log.AMLogException) HashSet(java.util.HashSet)

Example 53 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project OpenAM by OpenRock.

the class LogReadDBHandler method getNumberOfRows.

/**
     * Return number of records in each table
     * @param logMgr Log Manager that is maintaing table names
     * @return number of records in each table
     */
public long getNumberOfRows(java.util.logging.LogManager logMgr, String fileName) {
    long li = 0;
    try {
        this.databaseURL = logMgr.getProperty(LogConstants.LOG_LOCATION);
        this.dbDriver = logMgr.getProperty(LogConstants.DB_DRIVER);
        this.dbUserName = logMgr.getProperty(LogConstants.DB_USER);
        this.dbPassWord = logMgr.getProperty(LogConstants.DB_PASSWORD);
        this.maxRecordsStr = logMgr.getProperty(LogConstants.MAX_RECORDS);
    } catch (Exception e) {
        return 0;
    }
    try {
        connectToDatabase(dbUserName, dbPassWord);
    } catch (SQLException sqe) {
        Debug.error("DBgetNumberOfRows:connect:SQE:code=" + sqe.getErrorCode() + ", msg=" + sqe.getMessage());
        return 0;
    } catch (ClassNotFoundException cnfe) {
        Debug.error("DBgetgetNumberOfRows:connect:CNFE: " + cnfe.getMessage());
        return 0;
    }
    String fName = fileName.replace('.', '_');
    String queryString = "select count(*) from " + fName;
    Statement stmt = null;
    ResultSet rs = null;
    ResultSetMetaData rsmd = null;
    String result = null;
    try {
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        if (Debug.messageEnabled()) {
            Debug.message("DBgetgetNumberOfRows:about to execute: " + queryString);
        }
        rs = stmt.executeQuery(queryString);
        rsmd = rs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();
        if (Debug.messageEnabled()) {
            Debug.message("DBgetNumberOfRows:#columns = " + numberOfColumns);
        }
        while (rs.next()) {
            for (int i = 0; i < numberOfColumns; i++) {
                result = rs.getString(i + 1);
            }
        }
        stmt.close();
    } catch (SQLException se) {
        Debug.error("DBgetNumberOfRows:query:SQE:code=" + se.getErrorCode() + ", msg=" + se.getMessage());
        return 0;
    }
    try {
        conn.close();
    } catch (SQLException ex) {
        //
        //  might not care about this too much...?
        //
        Debug.error("DBgetNumberOfRows:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
    }
    try {
        Long longval = new Long(result);
        li = longval.longValue();
    } catch (NumberFormatException nfe) {
        Debug.error("DBgetNumberOfRows:got " + result + " as number of rows, returning 0.");
    }
    return li;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) IOException(java.io.IOException) SQLException(java.sql.SQLException) AMLogException(com.sun.identity.log.AMLogException)

Example 54 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project OpenAM by OpenRock.

the class LogReadDBHandler method displayResultSet.

//
//  private, mostly debugging method to display results
//  should be called after the ResultSet has been gone
//  through already, as you can't point the cursor to before
//  the beginning once you've made it move.
//
private void displayResultSet(ResultSet myrs) {
    try {
        ResultSetMetaData rsmd = myrs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();
        Debug.error("displayRS:#columns = " + numberOfColumns);
        StringBuffer sbtemp = new StringBuffer(80);
        for (int i = 1; i <= numberOfColumns; i++) {
            String tempstr = rsmd.getColumnName(i);
            sbtemp.append(tempstr).append("\t");
        }
        Debug.error("displayRS:columns =\n" + sbtemp.toString());
        boolean isFirst = myrs.first();
        int rowNum = 1;
        do {
            sbtemp = new StringBuffer(80);
            for (int i = 1; i <= numberOfColumns; i++) {
                sbtemp.append(myrs.getString(i)).append("\t");
            }
            Debug.error("displayRS:row #" + rowNum + " = " + sbtemp.toString());
            rowNum++;
        } while (myrs.next());
        //
        // put cursor back at beginning
        //
        isFirst = myrs.first();
    } catch (SQLException ex) {
        Debug.error("displayRS:got SQLException: " + ex.getMessage());
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException)

Example 55 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project OpenAM by OpenRock.

the class LogReadDBHandler method logRecRead.

/**
     * LogReader calls this method. It collects header, records,
     * applies query (if any), sorts (if asked) the records on field, checks
     * the max records to return, collects all the recods and returns.
     *
     * @param tableName db table name
     * @param logQuery is user specified qury chriteria with sorting requirement
     * @param logMgr the log manager associated with this handler
     * @param sourceData it specifies whether return data should be original
     *        data received by logger (source) or formatted data as in file.
     * @return all the matched records with query
     * @throws IOException if it fails to read log records.
     * @throws NoSuchFieldException if it fails to retrieve the name of field.
     * @throws IllegalArgumentException if query has wrong value.
     * @throws RuntimeException if it fails to retrieve log record.
     * @throws SQLException if it fails to process sql query.
     * @throws Exception if it fails any of operation.
     */
public String[][] logRecRead(String tableName, LogQuery logQuery, java.util.logging.LogManager logMgr, boolean sourceData) throws IOException, NoSuchFieldException, IllegalArgumentException, RuntimeException, SQLException, Exception {
    String sortField = null;
    // if the object is persistence use it otherwise don't
    this.cleaner();
    tableName = tableName.replace('.', '_');
    try {
        this.databaseURL = logMgr.getProperty(LogConstants.LOG_LOCATION);
        this.dbDriver = logMgr.getProperty(LogConstants.DB_DRIVER);
        this.dbUserName = logMgr.getProperty(LogConstants.DB_USER);
        this.dbPassWord = logMgr.getProperty(LogConstants.DB_PASSWORD);
        this.maxRecordsStr = logMgr.getProperty(LogConstants.MAX_RECORDS);
    } catch (Exception e) {
        Debug.error("DBLogRecRead:config: ", e);
        // rethrow the exception
        throw e;
    }
    if (this.dbDriver.toLowerCase().indexOf("oracle") != -1) {
        isMySQL = false;
    } else if (this.dbDriver.toLowerCase().indexOf("mysql") != -1) {
        isMySQL = true;
    } else {
        isMySQL = false;
        Debug.warning("DBlogRecRead:assuming driver: '" + this.dbDriver + "' is Oracle-compatible.");
    }
    String selectStr;
    if (sourceData == true) {
        String temps = logQuery.getSortingField();
        if (temps != null) {
            sortField = temps.trim();
        }
        // default all
        String columns = "*";
        ArrayList sCol = logQuery.getColumns();
        if (sCol != null) {
            StringBuilder colSB = new StringBuilder();
            int sSize = sCol.size();
            for (int i = 0; i < sSize; i++) {
                colSB.append((String) sCol.get(i));
                if ((i + 1) < sSize) {
                    colSB.append(", ");
                }
            }
            columns = colSB.toString();
        }
        selectStr = lq2Select(tableName, columns, logQuery);
        if (Debug.messageEnabled()) {
            Debug.message("logRecRead/4:selectStr = " + selectStr);
        }
    } else {
        selectStr = lq2Select(tableName, null, null);
        if (Debug.messageEnabled()) {
            Debug.message("logRecRead/4.2:selectStr = " + selectStr);
        }
    }
    try {
        this.maxRecords = Integer.parseInt(maxRecordsStr);
    } catch (NumberFormatException nfe) {
        if (Debug.warningEnabled()) {
            Debug.warning("DBlogRecRead: maxRecords error (" + maxRecordsStr + "), set to MAX");
        }
        this.maxRecords = LogConstants.MAX_RECORDS_DEFAULT_INT;
    }
    String[][] tableResults;
    try {
        connectToDatabase(dbUserName, dbPassWord);
    } catch (SQLException sqe) {
        Debug.error("DBlogRecRead:connect:SQE:code=" + sqe.getErrorCode() + ", msg=" + sqe.getMessage());
        // rethrow for LogReader to catch
        throw sqe;
    } catch (ClassNotFoundException cnfe) {
        // rethrow for LogReader to catch
        throw cnfe;
    }
    String selStr = selectStr;
    Statement stmt = null;
    int numberOfRows = 0;
    try {
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        if (Debug.messageEnabled()) {
            Debug.message("DBlogRecRead:about to execute: " + selStr);
        }
        ResultSet rs = stmt.executeQuery(selStr);
        //
        //  fetchsize appears to be 10 from rs.getFetchSize();
        //
        ResultSetMetaData rsmd = rs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();
        if (Debug.messageEnabled()) {
            Debug.message("DBlogRecRead:#columns = " + numberOfColumns);
        }
        //
        //  these are the column (field) names
        //
        String[] spltHdrStr = new String[numberOfColumns];
        for (int i = 1; i <= numberOfColumns; i++) {
            String tempstr = rsmd.getColumnName(i);
            if (Debug.messageEnabled()) {
                Debug.message("DBlogRecRead:col #" + i + " name = " + tempstr);
            }
            spltHdrStr[i - 1] = tempstr;
        }
        listOfValidRecords.add(spltHdrStr);
        // have to figure out #rows
        while (rs.next()) {
            numberOfRows++;
        }
        if (Debug.messageEnabled()) {
            Debug.message("DBlogRecRead:#rows = " + numberOfRows);
        }
        if (numberOfRows == 0) {
            stmt.close();
            try {
                conn.close();
            } catch (SQLException ex) {
                //
                //  might not care about this too much...?
                //
                Debug.error("DBlogRecRead:rows=0:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
            }
            //
            //  should be at least the column names
            //
            int recSize = listOfValidRecords.size();
            if (recSize <= 0) {
                // if no record found return null
                return null;
            }
            queryResult = new String[recSize][];
            for (int i = 0; i < recSize; i++) {
                queryResult[i] = (String[]) listOfValidRecords.get(i);
            }
            return queryResult;
        }
        if (numberOfRows > this.maxRecords) {
            stmt.close();
            try {
                conn.close();
            } catch (SQLException ex) {
                //
                //  don't care about this too much...
                //
                Debug.error("DBlogRecRead:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
            }
            throw new AMLogException(AMLogException.LOG_DB_TOOMANYRECORDS);
        }
        //
        //  reset to the beginning
        //
        boolean isFirst = rs.first();
        if (isFirst == false) {
            Debug.error("DBlogRecRead:first() is false!");
        }
        int rowsToAlloc = numberOfRows;
        if (logQuery.getNumRecordsWanted() == LogQuery.MOST_RECENT_MAX_RECORDS) {
            //
            if (numberOfRows > this.maxRecords) {
                rowsToAlloc = this.maxRecords;
            }
        }
        tableResults = new String[rowsToAlloc][numberOfColumns];
        String result = null;
        int rowCount = 0;
        //
        //  if LogQuery.MOST_RECENT_MAX_RECORDS selected,
        //  then we just have to get the "this.maxRecords" records.
        //
        int skipThisManyRecords = 0;
        if (logQuery.getNumRecordsWanted() == LogQuery.MOST_RECENT_MAX_RECORDS) {
            if (numberOfRows > this.maxRecords) {
                skipThisManyRecords = numberOfRows - this.maxRecords;
            }
        }
        if (Debug.messageEnabled()) {
            Debug.message("DBlogRecRead:skipThisMany = " + skipThisManyRecords);
        }
        for (int i = 0; i < numberOfColumns; i++) {
            result = rs.getString(i + 1);
            tableResults[0][i] = result;
        }
        rowCount = 1;
        while (rs.next()) {
            if (skipThisManyRecords-- <= 0) {
                for (int i = 0; i < numberOfColumns; i++) {
                    result = rs.getString(i + 1);
                    tableResults[rowCount][i] = result;
                }
                rowCount++;
            }
        }
        stmt.close();
    } catch (SQLException se) {
        Debug.error("DBlogRecRead:query:SQE:code=" + se.getErrorCode() + ", msg=" + se.getMessage());
        // rethrow for LogReader to catch
        throw se;
    }
    try {
        this.getRecords(tableResults, sourceData);
    } catch (IOException e) {
        // catch & rethrow it
        throw e;
    } catch (IllegalArgumentException e) {
        // catch & rethrow it
        throw e;
    } catch (RuntimeException e) {
        // catch & rethrow it
        throw e;
    } catch (Exception e) {
        // catch & rethrow it
        throw e;
    }
    int recSize = listOfValidRecords.size();
    // checks whether it has got any record or not
    if (recSize <= 0) {
        // if no record found return null
        return null;
    }
    try {
        conn.close();
    } catch (SQLException ex) {
        //
        //  might not care about this too much...?
        //
        Debug.error("DBlogRecRead:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
    }
    queryResult = new String[recSize][];
    for (int i = 0; i < recSize; i++) {
        queryResult[i] = (String[]) listOfValidRecords.get(i);
    }
    return queryResult;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) SQLException(java.sql.SQLException) AMLogException(com.sun.identity.log.AMLogException) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) AMLogException(com.sun.identity.log.AMLogException)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)449 ResultSet (java.sql.ResultSet)279 SQLException (java.sql.SQLException)125 Statement (java.sql.Statement)123 Test (org.junit.Test)109 PreparedStatement (java.sql.PreparedStatement)107 ArrayList (java.util.ArrayList)78 Connection (java.sql.Connection)68 HashMap (java.util.HashMap)44 DatabaseMetaData (java.sql.DatabaseMetaData)41 Map (java.util.Map)31 LinkedHashMap (java.util.LinkedHashMap)22 List (java.util.List)18 IOException (java.io.IOException)15 BigDecimal (java.math.BigDecimal)12 HashSet (java.util.HashSet)12 ParameterMetaData (java.sql.ParameterMetaData)10 Properties (java.util.Properties)10 KnownFailure (dalvik.annotation.KnownFailure)9 Field (java.lang.reflect.Field)9