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

mpeg/mp3

开发平台:

C/C++

  1. package postgresql;
  2. import java.sql.*;
  3. /**
  4.  * A Statement object is used for executing a static SQL statement and
  5.  * obtaining the results produced by it.
  6.  *
  7.  * <p>Only one ResultSet per Statement can be open at any point in time.  
  8.  * Therefore, if the reading of one ResultSet is interleaved with the
  9.  * reading of another, each must have been generated by different
  10.  * Statements.  All statement execute methods implicitly close a
  11.  * statement's current ResultSet if an open one exists.
  12.  *
  13.  * @see java.sql.Statement
  14.  * @see ResultSet
  15.  */
  16. public class Statement implements java.sql.Statement
  17. {
  18. Connection connection; // The connection who created us
  19. ResultSet result = null; // The current results
  20. SQLWarning warnings = null; // The warnings chain.
  21. int timeout = 0; // The timeout for a query (not used)
  22. boolean escapeProcessing = true;// escape processing flag
  23. /**
  24.  * Constructor for a Statement.  It simply sets the connection
  25.  * that created us.
  26.  *
  27.  * @param c the Connection instantation that creates us
  28.  */
  29. public Statement (Connection c)
  30. {
  31. connection = c;
  32. }
  33. /**
  34.  * Execute a SQL statement that retruns a single ResultSet
  35.  *
  36.  * @param sql typically a static SQL SELECT statement
  37.  * @return a ResulSet that contains the data produced by the query
  38.  * @exception SQLException if a database access error occurs
  39.  */
  40. public java.sql.ResultSet executeQuery(String sql) throws SQLException
  41. {
  42. this.execute(sql);
  43. while (result != null && !result.reallyResultSet())
  44. result = result.getNext();
  45. if (result == null)
  46. throw new SQLException("no results returned");
  47. return result;
  48. }
  49. /**
  50.  * Execute a SQL INSERT, UPDATE or DELETE statement.  In addition
  51.  * SQL statements that return nothing such as SQL DDL statements
  52.  * can be executed
  53.  *
  54.  * @param sql a SQL statement
  55.  * @return either a row count, or 0 for SQL commands
  56.  * @exception SQLException if a database access error occurs
  57.  */
  58. public int executeUpdate(String sql) throws SQLException
  59. {
  60. this.execute(sql);
  61. if (result.reallyResultSet())
  62. throw new SQLException("results returned");
  63. return this.getUpdateCount();
  64. }
  65. /**
  66.  * In many cases, it is desirable to immediately release a
  67.  * Statement's database and JDBC resources instead of waiting
  68.  * for this to happen when it is automatically closed.  The
  69.  * close method provides this immediate release.
  70.  *
  71.  * <p><B>Note:</B> A Statement is automatically closed when it is 
  72.  * garbage collected.  When a Statement is closed, its current 
  73.  * ResultSet, if one exists, is also closed.
  74.  *
  75.  * @exception SQLException if a database access error occurs (why?)
  76.  */
  77. public void close() throws SQLException
  78. {
  79. result = null;
  80. }
  81. /**
  82.  * The maxFieldSize limit (in bytes) is the maximum amount of
  83.  * data returned for any column value; it only applies to
  84.  * BINARY, VARBINARY, LONGVARBINARY, CHAR, VARCHAR and LONGVARCHAR
  85.  * columns.  If the limit is exceeded, the excess data is silently
  86.  * discarded.
  87.  *
  88.  * @return the current max column size limit; zero means unlimited
  89.  * @exception SQLException if a database access error occurs
  90.  */
  91. public int getMaxFieldSize() throws SQLException
  92. {
  93. return 8192; // We cannot change this
  94. }
  95. /**
  96.  * Sets the maxFieldSize - NOT! - We throw an SQLException just
  97.  * to inform them to stop doing this.
  98.  *
  99.  * @param max the new max column size limit; zero means unlimited
  100.  * @exception SQLException if a database access error occurs
  101.  */
  102. public void setMaxFieldSize(int max) throws SQLException
  103. {
  104. throw new SQLException("Attempt to setMaxFieldSize failed - compile time default");
  105. }
  106. /**
  107.  * The maxRows limit is set to limit the number of rows that
  108.  * any ResultSet can contain.  If the limit is exceeded, the
  109.  * excess rows are silently dropped.
  110.  *
  111.  * @return the current maximum row limit; zero means unlimited
  112.  * @exception SQLException if a database access error occurs
  113.  */
  114. public int getMaxRows() throws SQLException
  115. {
  116. return connection.maxrows;
  117. }
  118. /**
  119.  * Set the maximum number of rows
  120.  *
  121.  * @param max the new max rows limit; zero means unlimited
  122.  * @exception SQLException if a database access error occurs
  123.  * @see getMaxRows
  124.  */
  125. public void setMaxRows(int max) throws SQLException
  126. {
  127.   connection.maxrows = max;
  128. }
  129. /**
  130.  * If escape scanning is on (the default), the driver will do escape
  131.  * substitution before sending the SQL to the database.  
  132.  *
  133.  * @param enable true to enable; false to disable
  134.  * @exception SQLException if a database access error occurs
  135.  */
  136. public void setEscapeProcessing(boolean enable) throws SQLException
  137. {
  138. escapeProcessing = enable;
  139. }
  140. /**
  141.  * The queryTimeout limit is the number of seconds the driver
  142.  * will wait for a Statement to execute.  If the limit is
  143.  * exceeded, a SQLException is thrown.
  144.  *
  145.  * @return the current query timeout limit in seconds; 0 = unlimited
  146.  * @exception SQLException if a database access error occurs
  147.  */
  148. public int getQueryTimeout() throws SQLException
  149. {
  150. return timeout;
  151. }
  152. /**
  153.  * Sets the queryTimeout limit
  154.  *
  155.  * @param seconds - the new query timeout limit in seconds
  156.  * @exception SQLException if a database access error occurs
  157.  */
  158. public void setQueryTimeout(int seconds) throws SQLException
  159. {
  160. timeout = seconds;
  161. }
  162. /**
  163.  * Cancel can be used by one thread to cancel a statement that
  164.  * is being executed by another thread.  However, PostgreSQL is
  165.  * a sync. sort of thing, so this really has no meaning - we 
  166.  * define it as a no-op (i.e. you can't cancel, but there is no
  167.  * error if you try.)
  168.  *
  169.  * @exception SQLException only because thats the spec.
  170.  */
  171. public void cancel() throws SQLException
  172. {
  173. // No-op
  174. }
  175. /**
  176.  * The first warning reported by calls on this Statement is
  177.  * returned.  A Statement's execute methods clear its SQLWarning
  178.  * chain.  Subsequent Statement warnings will be chained to this
  179.  * SQLWarning.
  180.  *
  181.  * <p>The Warning chain is automatically cleared each time a statement
  182.  * is (re)executed.
  183.  *
  184.  * <p><B>Note:</B>  If you are processing a ResultSet then any warnings
  185.  * associated with ResultSet reads will be chained on the ResultSet
  186.  * object.
  187.  *
  188.  * @return the first SQLWarning on null
  189.  * @exception SQLException if a database access error occurs
  190.  */
  191. public SQLWarning getWarnings() throws SQLException
  192. {
  193. return warnings;
  194. }
  195. /**
  196.  * After this call, getWarnings returns null until a new warning
  197.  * is reported for this Statement.
  198.  *
  199.  * @exception SQLException if a database access error occurs (why?)
  200.  */
  201. public void clearWarnings() throws SQLException
  202. {
  203. warnings = null;
  204. }
  205. /**
  206.  * setCursorName defines the SQL cursor name that will be used by
  207.  * subsequent execute methods.  This name can then be used in SQL
  208.  * positioned update/delete statements to identify the current row
  209.  * in the ResultSet generated by this statement.  If a database
  210.  * doesn't support positioned update/delete, this method is a
  211.  * no-op.
  212.  *
  213.  * <p><B>Note:</B> By definition, positioned update/delete execution
  214.  * must be done by a different Statement than the one which
  215.  * generated the ResultSet being used for positioning.  Also, cursor
  216.  * names must be unique within a Connection.
  217.  *
  218.  * <p>We throw an additional constriction.  There can only be one
  219.  * cursor active at any one time.
  220.  *
  221.  * @param name the new cursor name
  222.  * @exception SQLException if a database access error occurs
  223.  */
  224. public void setCursorName(String name) throws SQLException
  225. {
  226. connection.setCursorName(name);
  227. }
  228. /**
  229.  * Execute a SQL statement that may return multiple results. We
  230.  * don't have to worry about this since we do not support multiple
  231.  * ResultSets.   You can use getResultSet or getUpdateCount to 
  232.  * retrieve the result.
  233.  *
  234.  * @param sql any SQL statement
  235.  * @return true if the next result is a ResulSet, false if it is
  236.  *  an update count or there are no more results
  237.  * @exception SQLException if a database access error occurs
  238.  */
  239. public boolean execute(String sql) throws SQLException
  240. {
  241. result = connection.ExecSQL(sql);
  242. return (result != null && result.reallyResultSet());
  243. }
  244. /**
  245.  * getResultSet returns the current result as a ResultSet.  It
  246.  * should only be called once per result.
  247.  *
  248.  * @return the current result set; null if there are no more
  249.  * @exception SQLException if a database access error occurs (why?)
  250.  */
  251. public java.sql.ResultSet getResultSet() throws SQLException
  252. {
  253. return result;
  254. }
  255. /**
  256.  * getUpdateCount returns the current result as an update count,
  257.  * if the result is a ResultSet or there are no more results, -1
  258.  * is returned.  It should only be called once per result.
  259.  *
  260.  * @return the current result as an update count.
  261.  * @exception SQLException if a database access error occurs
  262.  */
  263. public int getUpdateCount() throws SQLException
  264. {
  265. if (result == null)  return -1;
  266. if (result.reallyResultSet()) return -1;
  267. return result.getResultCount();
  268. }
  269. /**
  270.  * getMoreResults moves to a Statement's next result.  If it returns
  271.  * true, this result is a ResulSet.
  272.  *
  273.  * @return true if the next ResultSet is valid
  274.  * @exception SQLException if a database access error occurs
  275.  */
  276. public boolean getMoreResults() throws SQLException
  277. {
  278. result = result.getNext();
  279. return (result != null && result.reallyResultSet());
  280. }
  281.    
  282.    /**
  283.     * Returns the status message from the current Result.<p>
  284.     * This is used internally by the driver.
  285.     *
  286.     * @return status message from backend
  287.     */
  288.    public String getResultStatusString()
  289.    {
  290.      if(result == null)
  291.        return null;
  292.      return result.getStatusString();
  293.    }
  294. }