コード例 #1
0
ファイル: TinySQLCmd.cs プロジェクト: bastie/NetVampire
        /**
         * Formatted output to stdout
         * @return number of tuples
         */
        static int displayResults(java.sql.ResultSet rs)
        {//throws java.sql.SQLException
            {
                if (rs == null)
                {
                    java.lang.SystemJ.err.println("ERROR in displayResult(): No data in ResultSet");
                    return(0);
                }
                java.sql.Date testDate;
                int           numCols = 0, nameLength;
                java.sql.ResultSetMetaData meta = rs.getMetaData();
                int    cols   = meta.getColumnCount();
                int[]  width  = new int[cols];
                String dashes = "=============================================";

                /*
                 *    Display column headers
                 */
                bool first = true;
                java.lang.StringBuffer head = new java.lang.StringBuffer();
                java.lang.StringBuffer line = new java.lang.StringBuffer();

                /*
                 *    Fetch each row
                 */
                while (rs.next())
                {
                    /*
                     *       Get the column, and see if it matches our expectations
                     */
                    String text = "";
                    for (int ii = 0; ii < cols; ii++)
                    {
                        String value = rs.getString(ii + 1);
                        if (first)
                        {
                            width[ii] = meta.getColumnDisplaySize(ii + 1);
                            if (TinySQLGlobals.DEBUG &
                                meta.getColumnType(ii + 1) == java.sql.Types.DATE)
                            {
                                testDate = rs.getDate(ii + 1);
                                java.lang.SystemJ.outJ.println("Value " + value + ", Date "
                                                               + testDate.toString());
                            }
                            nameLength = meta.getColumnName(ii + 1).length();
                            if (nameLength > width[ii])
                            {
                                width[ii] = nameLength;
                            }
                            head.append(padString(meta.getColumnName(ii + 1), width[ii]));
                            head.append(" ");
                            line.append(padString(dashes + dashes, width[ii]));
                            line.append(" ");
                        }
                        text += padString(value, width[ii]);
                        text += " ";   // the gap between the columns
                    }
                    try
                    {
                        if (first)
                        {
                            if (spoolFileWriter != (java.io.FileWriter)null)
                            {
                                spoolFileWriter.write(head.toString() + newLine);
                                spoolFileWriter.write(head.toString() + newLine);
                            }
                            else
                            {
                                java.lang.SystemJ.outJ.println(head.toString());
                                java.lang.SystemJ.outJ.println(line.toString());
                            }
                            first = false;
                        }
                        if (spoolFileWriter != (java.io.FileWriter)null)
                        {
                            spoolFileWriter.write(text + newLine);
                        }
                        else
                        {
                            java.lang.SystemJ.outJ.println(text);
                        }
                        numCols++;
                    }
                    catch (Exception writeEx)
                    {
                        java.lang.SystemJ.outJ.println("Exception writing to spool file "
                                                       + writeEx.getMessage());
                    }
                }
                return(numCols);
            }
        }
コード例 #2
0
        /**
         * Gets a description of table columns available in
         * the specified catalog.
         *
         * <P>Only column descriptions matching the catalog, schema, table
         * and column name criteria are returned.  They are ordered by
         * TABLE_SCHEM, TABLE_NAME and ORDINAL_POSITION.
         *
         * <P>Each column 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)sRow record = new TsRow();
         *
         *  <LI><B>TABLE_NAME</B> String => table name
         *  <LI><B>COLUMN_NAME</B> String => column name
         *  <LI><B>DATA_TYPE</B> short => SQL type from java.sql.Types
         *  <LI><B>TYPE_NAME</B> String => Data source dependent type name,
         *  for a UDT the type name is fully qualified
         *  <LI><B>COLUMN_SIZE</B> int => column size.  For char or date
         *      types this is the maximum number of characters, for numeric or
         *      decimal types this is precision.
         *  <LI><B>BUFFER_LENGTH</B> is not used.
         *  <LI><B>DECIMAL_DIGITS</B> int => the number of fractional digits
         *  <LI><B>NUM_PREC_RADIX</B> int => Radix (typically either 10 or 2)
         *  <LI><B>NULLABLE</B> int => is NULL allowed?
         *      <UL>
         *      <LI> columnNoNulls - might not allow NULL values
         *      <LI> columnNullable - definitely allows NULL values
         *      <LI> columnNullableUnknown - nullability unknown
         *      </UL>
         *  <LI><B>REMARKS</B> String => comment describing column (may be null)
         *  <LI><B>COLUMN_DEF</B> String => default value (may be null)
         *  <LI><B>SQL_DATA_TYPE</B> int => unused
         *  <LI><B>SQL_DATETIME_SUB</B> int => unused
         *  <LI><B>CHAR_OCTET_LENGTH</B> int => for char types the
         *       maximum number of bytes in the column
         *  <LI><B>ORDINAL_POSITION</B> int => index of column in table
         *      (starting at 1)
         *  <LI><B>IS_NULLABLE</B> String => "NO" means column definitely
         *      does not allow NULL values; "YES" means the column might
         *      allow NULL values.  An empty string means nobody knows.
         *  </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 tableNamePattern a table name pattern
         * @param columnNamePattern a column name pattern
         * @return ResultSet - each row is a column description
         * @exception SQLException if a database access error occurs
         * @see #getSearchStringEscape
         */
        public override java.sql.ResultSet getColumns(String catalog, String schemaPattern,
                                                      String tableNamePattern, String columnNamePattern)
        {
            int    i;
            String columnNameKey;

            try
            {
                String dataDir = getDataDir();

                Utils.log("Entering getColumns(tableNamePattern='" + tableNamePattern + "')");

                if (dataDir == null)
                {
                    return(null);
                }

                java.sql.ResultSet tableRs = getTables(catalog, schemaPattern, tableNamePattern, null);

                TsResultSet jrs = new TsResultSet();

                TsColumn jsc = new TsColumn("TABLE_CAT");
                jsc.type = java.sql.Types.CHAR;
                jsc.size = 9;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("TABLE_SCHEM");
                jsc.type = java.sql.Types.CHAR;
                jsc.size = 11;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("TABLE_NAME");
                jsc.type = java.sql.Types.CHAR;
                jsc.size = 10;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("COLUMN_NAME");
                jsc.type = java.sql.Types.CHAR;
                jsc.size = 11;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("DATA_TYPE");
                jsc.type = java.sql.Types.INTEGER;
                jsc.size = 6;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("TYPE_NAME");
                jsc.type = java.sql.Types.CHAR;
                jsc.size = 9;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("COLUMN_SIZE");
                jsc.type = java.sql.Types.INTEGER;
                jsc.size = 8;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("BUFFER_LENGTH");
                jsc.type = java.sql.Types.INTEGER;
                jsc.size = 8;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("DECIMAL_DIGITS");
                jsc.type = java.sql.Types.INTEGER;
                jsc.size = 8;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("NUM_PREC_RADIX");
                jsc.type = java.sql.Types.INTEGER;
                jsc.size = 8;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("NULLABLE");
                jsc.type = java.sql.Types.INTEGER;
                jsc.size = 8;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("REMARKS");
                jsc.type = java.sql.Types.CHAR;
                jsc.size = 128;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("COLUMN_DEF");
                jsc.type = java.sql.Types.CHAR;
                jsc.size = 128;
                jrs.addColumn(jsc);

                jsc      = new TsColumn("SQL_DATA_TYPE");
                jsc.type = java.sql.Types.INTEGER;
                jsc.size = 128;
                jrs.addColumn(jsc);

                //      Several parameters missing.

                jsc      = new TsColumn("IS_NULLABLE");
                jsc.type = java.sql.Types.CHAR;
                jsc.size = 3;
                jrs.addColumn(jsc);

                while (tableRs.next())
                { // process each DBF file and extract column info ...
                    String tableName = tableRs.getString("TABLE_NAME");

                    DBFFileTable tbl;
                    try
                    {
                        tbl = new DBFFileTable(dataDir, tableName);
                    }
                    catch (Exception)
                    {
                        continue; // ignore buggy and empty (zero byte size) DBF files
                    }

                    Utils.log("Accessing column info for table " + tableName);

                    java.util.Hashtable <Object, Object> column_info = tbl.column_info;
                    for (i = 0; i < tbl.columnNameKeys.size(); i++)
                    {
                        columnNameKey = (String)tbl.columnNameKeys.elementAt(i);
                        TsColumn tsc = (TsColumn)column_info.get(columnNameKey);
                        // process each column of the current table ...
                        TsRow record = new TsRow();
                        record.put("TABLE_CAT", "");
                        record.put("TABLE_SCHEM", "");
                        record.put("TABLE_NAME", tableName);
                        record.put("COLUMN_NAME", TinySQLGlobals.getLongName(tsc.name));
                        record.put("DATA_TYPE", new java.lang.Integer(tsc.type).toString());
                        record.put("TYPE_NAME", DBFFile.typeToLiteral(tsc.type).toString());
                        record.put("COLUMN_SIZE", new java.lang.Integer(tsc.size).toString());
                        record.put("DECIMAL_DIGITS", new java.lang.Integer(tsc.decimalPlaces).toString());
                        int nullable = columnNoNulls;
                        if (tsc.notNull == true)
                        {
                            nullable = columnNullable;
                        }
                        record.put("NULLABLE", new java.lang.Integer(nullable).toString());
                        record.put("REMARKS", "noRemarks");
                        String defaultVal = tsc.defaultVal;
                        if (defaultVal == null)
                        {
                            defaultVal = "";
                        }
                        record.put("COLUMN_DEF", defaultVal);
                        String isNullable = "NO";
                        if (tsc.notNull == true)
                        {
                            isNullable = "YES";
                        }
                        record.put("IS_NULLABLE", isNullable);

                        /*
                         *          Suppress any sorting of the ResultSet.  Column Metadata should
                         *          be presented in the order the columns exist in the dBase header.
                         */

                        jrs.addRow(record, false);
                    }

                    tbl.close();
                    tbl = null;
                }

                return(new TinySQLResultSet(jrs, (TinySQLStatement)null));
            }
            catch (Exception)
            {
                return(null);
            }
        }
コード例 #3
0
ファイル: TinySQLStatement.cs プロジェクト: bastie/NetVampire
 /**
  *
  * Returns the last result set
  * @see java.sql.Statement#getResultSet
  * @return null if no result set is available, otherwise a result set
  *
  */
 public java.sql.ResultSet getResultSet()
 {                                  //throws SQLException {
     java.sql.ResultSet r = result; // save the existing result set
     result = null;                 // null out the existing result set
     return(r);                     // return the previously extant result set
 }