ResultSetMetaData.java
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:12k
源码类别:

mpeg/mp3

开发平台:

C/C++

  1. package postgresql;
  2. import java.lang.*;
  3. import java.sql.*;
  4. import java.util.*;
  5. import postgresql.*;
  6. /**
  7.  * A ResultSetMetaData object can be used to find out about the types and
  8.  * properties of the columns in a ResultSet
  9.  *
  10.  * @see java.sql.ResultSetMetaData
  11.  */
  12. public class ResultSetMetaData implements java.sql.ResultSetMetaData 
  13. {
  14.   Vector rows;
  15.   Field[] fields;
  16.   
  17.   /**
  18.    * Initialise for a result with a tuple set and
  19.    * a field descriptor set
  20.    *
  21.    * @param rows the Vector of rows returned by the ResultSet
  22.    * @param fields the array of field descriptors
  23.    */
  24.   public ResultSetMetaData(Vector rows, Field[] fields)
  25.   {
  26.     this.rows = rows;
  27.     this.fields = fields;
  28.   }
  29.   
  30.   /**
  31.    * Whats the number of columns in the ResultSet?
  32.    *
  33.    * @return the number
  34.    * @exception SQLException if a database access error occurs
  35.    */
  36.   public int getColumnCount() throws SQLException
  37.   {
  38.     return fields.length;
  39.   }
  40.   
  41.   /**
  42.    * Is the column automatically numbered (and thus read-only)
  43.    * I believe that PostgreSQL does not support this feature.
  44.    *
  45.    * @param column the first column is 1, the second is 2...
  46.    * @return true if so
  47.    * @exception SQLException if a database access error occurs
  48.    */
  49.   public boolean isAutoIncrement(int column) throws SQLException
  50.   {
  51.     return false;
  52.   }
  53.   
  54.   /**
  55.    * Does a column's case matter? ASSUMPTION: Any field that is
  56.    * not obviously case insensitive is assumed to be case sensitive
  57.    *
  58.    * @param column the first column is 1, the second is 2...
  59.    * @return true if so
  60.    * @exception SQLException if a database access error occurs
  61.    */
  62.   public boolean isCaseSensitive(int column) throws SQLException
  63.   {
  64.     int sql_type = getField(column).getSQLType();
  65.     
  66.     switch (sql_type)
  67.       {
  68.       case Types.SMALLINT:
  69.       case Types.INTEGER:
  70.       case Types.FLOAT:
  71.       case Types.REAL:
  72.       case Types.DOUBLE:
  73.       case Types.DATE:
  74.       case Types.TIME:
  75.       case Types.TIMESTAMP:
  76. return false;
  77.       default:
  78. return true;
  79.       }
  80.   }
  81.   
  82.   /**
  83.    * Can the column be used in a WHERE clause?  Basically for
  84.    * this, I split the functions into two types: recognised
  85.    * types (which are always useable), and OTHER types (which
  86.    * may or may not be useable).  The OTHER types, for now, I
  87.    * will assume they are useable.  We should really query the
  88.    * catalog to see if they are useable.
  89.    *
  90.    * @param column the first column is 1, the second is 2...
  91.    * @return true if they can be used in a WHERE clause
  92.    * @exception SQLException if a database access error occurs
  93.    */
  94.   public boolean isSearchable(int column) throws SQLException
  95.   {
  96.     int sql_type = getField(column).getSQLType();
  97.     
  98.     // This switch is pointless, I know - but it is a set-up
  99.     // for further expansion.
  100.     switch (sql_type)
  101.       {
  102.       case Types.OTHER:
  103. return true;
  104.       default:
  105. return true;
  106.       }
  107.   }
  108.   
  109.   /**
  110.    * Is the column a cash value?  6.1 introduced the cash/money
  111.    * type, which haven't been incorporated as of 970414, so I
  112.    * just check the type name for both 'cash' and 'money'
  113.    *
  114.    * @param column the first column is 1, the second is 2...
  115.    * @return true if its a cash column
  116.    * @exception SQLException if a database access error occurs
  117.    */
  118.   public boolean isCurrency(int column) throws SQLException
  119.   {
  120.     String type_name = getField(column).getTypeName();
  121.     
  122.     return type_name.equals("cash") || type_name.equals("money");
  123.   }
  124.   
  125.   /**
  126.    * Can you put a NULL in this column?  I think this is always
  127.    * true in 6.1's case.  It would only be false if the field had
  128.    * been defined NOT NULL (system catalogs could be queried?)
  129.    *
  130.    * @param column the first column is 1, the second is 2...
  131.    * @return one of the columnNullable values
  132.    * @exception SQLException if a database access error occurs
  133.    */
  134.   public int isNullable(int column) throws SQLException
  135.   {
  136.     return columnNullable; // We can always put NULL in
  137.   }
  138.   
  139.   /**
  140.    * Is the column a signed number? In PostgreSQL, all numbers
  141.    * are signed, so this is trivial.  However, strings are not
  142.    * signed (duh!)
  143.    * 
  144.    * @param column the first column is 1, the second is 2...
  145.    * @return true if so
  146.    * @exception SQLException if a database access error occurs
  147.    */
  148.   public boolean isSigned(int column) throws SQLException
  149.   {
  150.     int sql_type = getField(column).getSQLType();
  151.     
  152.     switch (sql_type)
  153.       {
  154.       case Types.SMALLINT:
  155.       case Types.INTEGER:
  156.       case Types.FLOAT:
  157.       case Types.REAL:
  158.       case Types.DOUBLE:
  159. return true;
  160.       case Types.DATE:
  161.       case Types.TIME:
  162.       case Types.TIMESTAMP:
  163. return false; // I don't know about these?
  164.       default:
  165. return false;
  166.       }
  167.   }
  168.   
  169.   /**
  170.    * What is the column's normal maximum width in characters?
  171.    *
  172.    * @param column the first column is 1, the second is 2, etc.
  173.    * @return the maximum width
  174.    * @exception SQLException if a database access error occurs
  175.    */
  176.   public int getColumnDisplaySize(int column) throws SQLException
  177.   {
  178.     int max = getColumnLabel(column).length();
  179.     int i;
  180.     
  181.     for (i = 0 ; i < rows.size(); ++i)
  182.       {
  183. byte[][] x = (byte[][])(rows.elementAt(i));
  184. if(x[column-1]!=null) {
  185.   int xl = x[column - 1].length;
  186.   if (xl > max)
  187.     max = xl;
  188. }
  189.       }
  190.     return max;
  191.   }
  192.   
  193.   /**
  194.    * What is the suggested column title for use in printouts and
  195.    * displays?  We suggest the ColumnName!
  196.    *
  197.    * @param column the first column is 1, the second is 2, etc.
  198.    * @return the column label
  199.    * @exception SQLException if a database access error occurs
  200.    */
  201.   public String getColumnLabel(int column) throws SQLException
  202.   {
  203.     return getColumnName(column);
  204.   }
  205.   
  206.   /**
  207.    * What's a column's name?
  208.    *
  209.    * @param column the first column is 1, the second is 2, etc.
  210.    * @return the column name
  211.    * @exception SQLException if a database access error occurs
  212.    */
  213.   public String getColumnName(int column) throws SQLException
  214.   {
  215.     Field f = getField(column);
  216.     if(f!=null)
  217.       return f.name;
  218.     return "field"+column;
  219.   }
  220.   
  221.   /**
  222.    * What is a column's table's schema?  This relies on us knowing
  223.    * the table name....which I don't know how to do as yet.  The 
  224.    * JDBC specification allows us to return "" if this is not
  225.    * applicable.
  226.    *
  227.    * @param column the first column is 1, the second is 2...
  228.    * @return the Schema
  229.    * @exception SQLException if a database access error occurs
  230.    */
  231.   public String getSchemaName(int column) throws SQLException
  232.   {
  233.     return "";
  234.   }
  235.   
  236.   /**
  237.    * What is a column's number of decimal digits.
  238.    *
  239.    * @param column the first column is 1, the second is 2...
  240.    * @return the precision
  241.    * @exception SQLException if a database access error occurs
  242.    */
  243.   public int getPrecision(int column) throws SQLException
  244.   {
  245.     int sql_type = getField(column).getSQLType();
  246.     
  247.     switch (sql_type)
  248.       {
  249.       case Types.SMALLINT:
  250. return 5;
  251.       case Types.INTEGER:
  252. return 10;
  253.       case Types.REAL:
  254. return 8;
  255.       case Types.FLOAT:
  256. return 16;
  257.       case Types.DOUBLE:
  258. return 16;
  259.       case Types.VARCHAR:
  260. return 0;
  261.       default:
  262. return 0;
  263.       }
  264.   }
  265.   
  266.   /**
  267.    * What is a column's number of digits to the right of the
  268.    * decimal point?
  269.    *
  270.    * @param column the first column is 1, the second is 2...
  271.    * @return the scale
  272.    * @exception SQLException if a database access error occurs
  273.    */
  274.   public int getScale(int column) throws SQLException
  275.   {
  276.     int sql_type = getField(column).getSQLType();
  277.     
  278.     switch (sql_type)
  279.       {
  280.       case Types.SMALLINT:
  281. return 0;
  282.       case Types.INTEGER:
  283. return 0;
  284.       case Types.REAL:
  285. return 8;
  286.       case Types.FLOAT:
  287. return 16;
  288.       case Types.DOUBLE:
  289. return 16;
  290.       case Types.VARCHAR:
  291. return 0;
  292.       default:
  293. return 0;
  294.       }
  295.   }
  296.   
  297.   /**
  298.    * Whats a column's table's name?  How do I find this out?  Both
  299.    * getSchemaName() and getCatalogName() rely on knowing the table
  300.    * Name, so we need this before we can work on them.
  301.    *
  302.    * @param column the first column is 1, the second is 2...
  303.    * @return column name, or "" if not applicable
  304.    * @exception SQLException if a database access error occurs
  305.    */
  306.   public String getTableName(int column) throws SQLException
  307.   {
  308.     return "";
  309.   }
  310.   
  311.   /**
  312.    * What's a column's table's catalog name?  As with getSchemaName(),
  313.    * we can say that if getTableName() returns n/a, then we can too -
  314.    * otherwise, we need to work on it.
  315.    * 
  316.    * @param column the first column is 1, the second is 2...
  317.    * @return catalog name, or "" if not applicable
  318.    * @exception SQLException if a database access error occurs
  319.    */
  320.   public String getCatalogName(int column) throws SQLException
  321.   {
  322.     return "";
  323.   }
  324.   
  325.   /**
  326.    * What is a column's SQL Type? (java.sql.Type int)
  327.    *
  328.    * @param column the first column is 1, the second is 2, etc.
  329.    * @return the java.sql.Type value
  330.    * @exception SQLException if a database access error occurs
  331.    * @see postgresql.Field#getSQLType
  332.    * @see java.sql.Types
  333.    */
  334.   public int getColumnType(int column) throws SQLException
  335.   {
  336.     return getField(column).getSQLType();
  337.   }
  338.   
  339.   /**
  340.    * Whats is the column's data source specific type name?
  341.    *
  342.    * @param column the first column is 1, the second is 2, etc.
  343.    * @return the type name
  344.    * @exception SQLException if a database access error occurs
  345.    */
  346.   public String getColumnTypeName(int column) throws SQLException
  347.   {
  348.     return getField(column).getTypeName();
  349.   }
  350.   
  351.   /**
  352.    * Is the column definitely not writable?  In reality, we would
  353.    * have to check the GRANT/REVOKE stuff for this to be effective,
  354.    * and I haven't really looked into that yet, so this will get
  355.    * re-visited.
  356.    *
  357.    * @param column the first column is 1, the second is 2, etc.
  358.    * @return true if so
  359.    * @exception SQLException if a database access error occurs
  360.    */
  361.   public boolean isReadOnly(int column) throws SQLException
  362.   {
  363.     return false;
  364.   }
  365.   
  366.   /**
  367.    * Is it possible for a write on the column to succeed?  Again, we
  368.    * would in reality have to check the GRANT/REVOKE stuff, which
  369.    * I haven't worked with as yet.  However, if it isn't ReadOnly, then
  370.    * it is obviously writable.
  371.    *
  372.    * @param column the first column is 1, the second is 2, etc.
  373.    * @return true if so
  374.    * @exception SQLException if a database access error occurs
  375.    */
  376.   public boolean isWritable(int column) throws SQLException
  377.   {
  378.     if (isReadOnly(column))
  379.       return true;
  380.     else
  381.       return false;
  382.   }
  383.   
  384.   /**
  385.    * Will a write on this column definately succeed?  Hmmm...this
  386.    * is a bad one, since the two preceding functions have not been
  387.    * really defined.  I cannot tell is the short answer.  I thus
  388.    * return isWritable() just to give us an idea.
  389.    *
  390.    * @param column the first column is 1, the second is 2, etc..
  391.    * @return true if so
  392.    * @exception SQLException if a database access error occurs
  393.    */
  394.   public boolean isDefinitelyWritable(int column) throws SQLException
  395.   {
  396.     return isWritable(column);
  397.   }
  398.   
  399.   // ********************************************************
  400.   //  END OF PUBLIC INTERFACE
  401.   // ********************************************************
  402.   
  403.   /**
  404.    * For several routines in this package, we need to convert
  405.    * a columnIndex into a Field[] descriptor.  Rather than do
  406.    * the same code several times, here it is.
  407.    * 
  408.    * @param columnIndex the first column is 1, the second is 2...
  409.    * @return the Field description
  410.    * @exception SQLException if a database access error occurs
  411.    */
  412.   private Field getField(int columnIndex) throws SQLException
  413.   {
  414.     if (columnIndex < 1 || columnIndex > fields.length)
  415.       throw new SQLException("Column index out of range");
  416.     return fields[columnIndex - 1];
  417.   }
  418. }