📄 connectionjdbc2.java
字号:
} // If charset is still unknown and the collation is not set either, // determine the charset by querying (we're using Sybase or SQL Server // 6.5) if ((serverCharset == null || serverCharset.length() == 0) && collation == null) { loadCharset(determineServerCharset()); } // Initial database settings. // Sets: auto commit mode = true // transaction isolation = read committed. if (serverType == Driver.SYBASE) { baseTds.submitSQL(SYBASE_INITIAL_SQL); } else { // Also discover the maximum decimal precision (28 for MS SQL pre // 2000, configurable to 28/38 for 2000 and later) Statement stmt = this.createStatement(); ResultSet rs = stmt.executeQuery(SQL_SERVER_INITIAL_SQL); if (rs.next()) { maxPrecision = rs.getByte(1); } rs.close(); stmt.close(); } } /** * Retrive the shared socket. * * @return The <code>SharedSocket</code> object. */ SharedSocket getSocket() { return this.socket; } /** * Retrieve the TDS protocol version. * * @return The TDS version as an <code>int</code>. */ int getTdsVersion() { return this.tdsVersion; } /** * Retrieves the next unique stored procedure name. * <p>Notes: * <ol> * <li>Some versions of Sybase require an id with * a length of <= 10. * <li>The format of this name works for sybase and Microsoft * and allows for 16M names per session. * <li>The leading '#jtds' indicates this is a temporary procedure and * the '#' is removed by the lower level TDS5 routines. * </ol> * Not synchronized because it's only called from the synchronized * {@link #prepareSQL} method. * * @return the next temporary SP name as a <code>String</code> */ String getProcName() { String seq = "000000" + Integer.toHexString(spSequenceNo++).toUpperCase(); return "#jtds" + seq.substring(seq.length() - 6, seq.length()); } /** * Retrieves the next unique cursor name. * * @return the next cursor name as a <code>String</code> */ synchronized String getCursorName() { String seq = "000000" + Integer.toHexString(cursorSequenceNo++).toUpperCase(); return "_jtds" + seq.substring(seq.length() - 6, seq.length()); } /** * Try to convert the SQL statement into a statement prepare. * <p> * Synchronized because it accesses the procedure cache and the * <code>baseTds</code>, but the method call also needs to made in a * <code>synchronized (connection)</code> block together with the execution * (if the prepared statement is actually executed) to ensure the * transaction isn't rolled back between this method call and the actual * execution. * * @param pstmt the target prepared statement * @param sql the SQL statement to prepare * @param params the parameters * @param returnKeys indicates whether the statement will return * generated keys * @param cursorNeeded indicates whether a cursor prepare is needed * @return the SQL procedure name as a <code>String</code> or null if the * SQL cannot be prepared */ synchronized String prepareSQL(JtdsPreparedStatement pstmt, String sql, ParamInfo[] params, boolean returnKeys, boolean cursorNeeded) throws SQLException { if (prepareSql == TdsCore.UNPREPARED || prepareSql == TdsCore.EXECUTE_SQL) { return null; // User selected not to use procs } if (serverType == Driver.SYBASE) { if (tdsVersion != Driver.TDS50) { return null; // No longer support stored procs with 4.2 } if (returnKeys) { return null; // Sybase cannot use @@IDENTITY in proc } if (cursorNeeded) { // // We are going to use the CachedResultSet so there is // no point in preparing the SQL as it will be discarded // in favour of a version with "FOR BROWSE" appended. // return null; } } // // Check parameters set and obtain native types // for (int i = 0; i < params.length; i++) { if (!params[i].isSet) { throw new SQLException(Messages.get("error.prepare.paramnotset", Integer.toString(i+1)), "07000"); } TdsData.getNativeType(this, params[i]); if (serverType == Driver.SYBASE) { if (params[i].sqlType.equals("text") || params[i].sqlType.equals("image")) { return null; // Sybase does not support text/image params } } } String key = Support.getStatementKey(sql, params, serverType, getCatalog(), autoCommit, cursorNeeded); // // See if we have already built this one // ProcEntry proc = (ProcEntry) statementCache.get(key); if (proc != null) { // Yes found in cache OK pstmt.setColMetaData(proc.getColMetaData()); if (serverType == Driver.SYBASE) { pstmt.setParamMetaData(proc.getParamMetaData()); } } else { // // No, so create the stored procedure now // proc = new ProcEntry(); if (serverType == Driver.SQLSERVER) { proc.setName( baseTds.microsoftPrepare( sql, params, cursorNeeded, pstmt.getResultSetType(), pstmt.getResultSetConcurrency())); if (proc.toString() == null) { proc.setType(ProcEntry.PREP_FAILED); } else if (prepareSql == TdsCore.TEMPORARY_STORED_PROCEDURES) { proc.setType(ProcEntry.PROCEDURE); } else { proc.setType((cursorNeeded) ? ProcEntry.CURSOR : ProcEntry.PREPARE); // Meta data may be returned by sp_prepare proc.setColMetaData(baseTds.getColumns()); pstmt.setColMetaData(proc.getColMetaData()); } // TODO Find some way of getting parameter meta data for MS } else { proc.setName(baseTds.sybasePrepare(sql, params)); if (proc.toString() == null) { proc.setType(ProcEntry.PREP_FAILED); } else { proc.setType(ProcEntry.PROCEDURE); } // Sybase gives us lots of useful information about the result set proc.setColMetaData(baseTds.getColumns()); proc.setParamMetaData(baseTds.getParameters()); pstmt.setColMetaData(proc.getColMetaData()); pstmt.setParamMetaData(proc.getParamMetaData()); } // OK we have built a proc so add it to the cache. addCachedProcedure(key, proc); } // Add the handle to the prepared statement so that the handles // can be used to clean up the statement cache properly when the // prepared statement is closed. if (pstmt.handles == null) { pstmt.handles = new ArrayList(1); } pstmt.handles.add(proc); // Give the user the name will be null if prepare failed return proc.toString(); } /** * Add a stored procedure to the cache. * <p> * Not explicitly synchronized because it's only called by synchronized * methods. * * @param key The signature of the procedure to cache. * @param proc The stored procedure descriptor. */ void addCachedProcedure(String key, ProcEntry proc) { statementCache.put(key, proc); if (!autoCommit && proc.getType() == ProcEntry.PROCEDURE && serverType == Driver.SQLSERVER) { procInTran.add(key); } } /** * Remove a stored procedure from the cache. * <p> * Not explicitly synchronized because it's only called by synchronized * methods. * * @param key The signature of the procedure to remove from the cache. */ void removeCachedProcedure(String key) { statementCache.remove(key); if (!autoCommit) { procInTran.remove(key); } } /** * Retrieves the maximum statement cache size. * * @return the maximum statement cache size */ int getMaxStatements() { return maxStatements; } /** * Retrieves the server type. * * @return the server type as an <code>int</code> where 1 == SQLSERVER and * 2 == SYBASE. */ public int getServerType() { return this.serverType; } /** * Sets the network packet size. * * @param size the new packet size */ void setNetPacketSize(int size) { this.netPacketSize = size; } /** * Retrieves the network packet size. * * @return the packet size as an <code>int</code> */ int getNetPacketSize() { return this.netPacketSize; } /** * Retrieves the current row count on this connection. * * @return the row count as an <code>int</code> */ int getRowCount() { return this.rowCount; } /** * Sets the current row count on this connection. * * @param count the new row count */ void setRowCount(int count) { rowCount = count; } /** * Retrieves the current maximum textsize on this connection. * * @return the maximum textsize as an <code>int</code> */ public int getTextSize() { return textSize; } /** * Sets the current maximum textsize on this connection. * * @param textSize the new maximum textsize */ public void setTextSize(int textSize) { this.textSize = textSize; } /** * Retrieves the status of the lastUpdateCount flag. * * @return the lastUpdateCount flag as a <code>boolean</code> */ boolean isLastUpdateCount() { return this.lastUpdateCount; } /** * Retrieves the maximum decimal precision. * * @return the precision as an <code>int</code> */ int getMaxPrecision() { return this.maxPrecision; } /** * Retrieves the LOB buffer size. * * @return the LOB buffer size as a <code>long</code> */ long getLobBuffer() { return this.lobBuffer; } /** * Retrieves the Prepared SQL method. * * @return the Prepared SQL method */ int getPrepareSql() { return this.prepareSql; } /** * Retrieves the batch size to be used internally. * * @return the batch size as an <code>int</code> */ int getBatchSize() { return this.batchSize; } /** * Retrieves the boolean indicating whether metadata caching * is enabled. * * @return <code>true</code> if metadata caching is enabled, * <code>false</code> if caching is disabled */ boolean getUseMetadataCache() { return this.useMetadataCache;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -