/** * Execute a SQL INSERT, UPDATE or DELETE statement. In addition SQL statements that return * nothing such as SQL DDL statements can be executed * * <p>Any IDs generated for AUTO_INCREMENT fields can be retrieved by casting this Statement to * org.gjt.mm.mysql.Statement and calling the getLastInsertID() method. * * @param Sql a SQL statement * @return either a row count, or 0 for SQL commands * @exception java.sql.SQLException if a database access error occurs */ public int executeUpdate(String Sql) throws java.sql.SQLException { if (Driver.trace) { Object[] Args = {Sql}; Debug.methodCall(this, "executeUpdate", Args); } if (_escapeProcessing) { Sql = _Escaper.escapeSQL(Sql); } if (Sql.indexOf("||") != -1) { Sql = _Escaper.doConcat(Sql); } // The checking and changing of catalogs // must happen in sequence, so synchronize // on the same mutex that _Conn is using ResultSet RS = null; synchronized (_Conn.getMutex()) { String OldCatalog = null; if (!_Conn.getCatalog().equals(_Catalog)) { OldCatalog = _Conn.getCatalog(); _Conn.setCatalog(_Catalog); } RS = _Conn.execSQL(Sql, -1); RS.setConnection(_Conn); if (OldCatalog != null) { _Conn.setCatalog(OldCatalog); } } if (RS.reallyResult()) { throw new java.sql.SQLException("Results returned for UPDATE ONLY.", "01S03"); } else { _update_count = RS.getUpdateCount(); int truncated_update_count = 0; if (_update_count > Integer.MAX_VALUE) { truncated_update_count = Integer.MAX_VALUE; } else { truncated_update_count = (int) _update_count; } _last_insert_id = RS.getUpdateID(); return truncated_update_count; } }
/** * getLongUpdateCount returns the current result as an update count, if the result is a ResultSet * or there are no more results, -1 is returned. It should only be called once per result. * * <p>This method returns longs as MySQL server versions newer than 3.22.4 return 64-bit values * for update counts * * @return the current result as an update count. * @exception java.sql.SQLException if a database access error occurs */ public long getLongUpdateCount() { if (Driver.trace) { Object[] Args = new Object[0]; Debug.methodCall(this, "getLongUpdateCount", Args); } if (_Results == null) { return -1; } if (_Results.reallyResult()) { return -1; } return _update_count; }
/** * getUpdateCount returns the current result as an update count, if the result is a ResultSet or * there are no more results, -1 is returned. It should only be called once per result. * * @return the current result as an update count. * @exception java.sql.SQLException if a database access error occurs */ public int getUpdateCount() throws java.sql.SQLException { if (Driver.trace) { Object[] Args = new Object[0]; Debug.methodCall(this, "getUpdateCount", Args); } if (_Results == null) { return -1; } if (_Results.reallyResult()) { return -1; } int truncated_update_count = 0; if (_Results.getUpdateCount() > Integer.MAX_VALUE) { truncated_update_count = Integer.MAX_VALUE; } else { truncated_update_count = (int) _Results.getUpdateCount(); } return truncated_update_count; }
/** * Execute a SQL statement that may return multiple results. We don't have to worry about this * since we do not support multiple ResultSets. You can use getResultSet or getUpdateCount to * retrieve the result. * * @param sql any SQL statement * @return true if the next result is a ResulSet, false if it is an update count or there are no * more results * @exception java.sql.SQLException if a database access error occurs */ public boolean execute(String Sql) throws java.sql.SQLException { if (Driver.trace) { Object[] Args = {Sql}; Debug.methodCall(this, "execute", Args); } if (_escapeProcessing) { Sql = _Escaper.escapeSQL(Sql); } if (Sql.indexOf("||") != -1) { Sql = _Escaper.doConcat(Sql); } if (_Results != null) { _Results.close(); } ResultSet RS = null; // If there isn't a limit clause in the SQL // then limit the number of rows to return in // an efficient manner. Only do this if // setMaxRows() hasn't been used on any Statements // generated from the current Connection (saves // a query, and network traffic). synchronized (_Conn.getMutex()) { String OldCatalog = null; if (!_Conn.getCatalog().equals(_Catalog)) { OldCatalog = _Conn.getCatalog(); _Conn.setCatalog(_Catalog); } if (_Conn.useMaxRows()) { if (Sql.toUpperCase().indexOf("LIMIT") != -1) { RS = _Conn.execSQL(Sql, _max_rows); } else { if (_max_rows <= 0) { _Conn.execSQL("SET OPTION SQL_SELECT_LIMIT=" + MysqlDefs.MAX_ROWS, -1); } else { _Conn.execSQL("SET OPTION SQL_SELECT_LIMIT=" + _max_rows, -1); } RS = _Conn.execSQL(Sql, -1); } } else { RS = _Conn.execSQL(Sql, -1); } if (OldCatalog != null) { _Conn.setCatalog(OldCatalog); } } _last_insert_id = RS.getUpdateID(); if (RS != null) { _Results = RS; } RS.setConnection(_Conn); return (RS != null && RS.reallyResult()); }