comparison src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java @ 278:1d6062d94377

Reduce duplicate code.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 18 Jul 2019 13:19:25 +0200 (2019-07-18)
parents 4880267d0fe1
children e5b99c929a2d
comparison
equal deleted inserted replaced
276:fab4e6165be9 278:1d6062d94377
272 server.setLanguage(language); 272 server.setLanguage(language);
273 273
274 // we're debugging here... uhm, should be off in real life 274 // we're debugging here... uhm, should be off in real life
275 if (debug) { 275 if (debug) {
276 try { 276 try {
277 String fname = props.getProperty("logfile", "monet_" + 277 String fname = props.getProperty("logfile", "monet_" + System.currentTimeMillis() + ".log");
278 System.currentTimeMillis() + ".log");
279 File f = new File(fname); 278 File f = new File(fname);
280 int ext = fname.lastIndexOf('.'); 279 int ext = fname.lastIndexOf('.');
281 if (ext < 0) 280 if (ext < 0)
282 ext = fname.length(); 281 ext = fname.length();
283 String pre = fname.substring(0, ext); 282 String pre = fname.substring(0, ext);
285 284
286 for (int i = 1; f.exists(); i++) { 285 for (int i = 1; f.exists(); i++) {
287 f = new File(pre + "-" + i + suf); 286 f = new File(pre + "-" + i + suf);
288 } 287 }
289 288
290 server.debug(f.getAbsolutePath()); 289 server.debug(f.getAbsolutePath()); // enable logging on the MapiSocket level
291 } catch (IOException ex) { 290 } catch (IOException ex) {
292 throw new SQLNonTransientConnectionException("Opening logfile failed: " + ex.getMessage(), "08M01"); 291 throw new SQLNonTransientConnectionException("Opening logfile failed: " + ex.getMessage(), "08M01");
293 } 292 }
294 } 293 }
295 294
1764 l.close(); 1763 l.close();
1765 } 1764 }
1766 } 1765 }
1767 1766
1768 /** 1767 /**
1769 * Sends the given string to MonetDB as regular statement, making 1768 * Sends the given string to MonetDB as regular SQL statement/query using queryTempl
1770 * sure there is a prompt after the command is sent. All possible 1769 * Making sure there is a prompt after the command is sent. All possible
1771 * returned information is discarded. Encountered errors are 1770 * returned information is discarded. Encountered errors are reported.
1772 * reported.
1773 * 1771 *
1774 * @param command the exact string to send to MonetDB 1772 * @param command the exact string to send to MonetDB
1775 * @throws SQLException if an IO exception or a database error occurs 1773 * @throws SQLException if an IO exception or a database error occurs
1776 */ 1774 */
1777 void sendIndependentCommand(String command) throws SQLException { 1775 private void sendIndependentCommand(String command) throws SQLException {
1776 sendCommand(command, true);
1777 }
1778
1779 /**
1780 * Sends the given string to MonetDB as control statement using commandTempl
1781 * Making sure there is a prompt after the command is sent. All possible
1782 * returned information is discarded. Encountered errors are reported.
1783 *
1784 * @param command the exact string to send to MonetDB
1785 * @throws SQLException if an IO exception or a database error occurs
1786 */
1787 void sendControlCommand(String command) throws SQLException {
1788 // send X command
1789 sendCommand(command, false);
1790 }
1791
1792 /**
1793 * Sends the given string to MonetDB as command/query using commandTempl or queryTempl
1794 * Making sure there is a prompt after the command is sent. All possible
1795 * returned information is discarded. Encountered errors are reported.
1796 *
1797 * @param command the exact string to send to MonetDB
1798 * @param usequeryTempl send the command using a queryTempl? else it is send using commandTempl
1799 * @throws SQLException if an IO exception or a database error occurs
1800 */
1801 private void sendCommand(String command, boolean usequeryTempl) throws SQLException {
1802 String cmd = usequeryTempl ?
1803 (queryTempl[0] == null ? "" : queryTempl[0]) + command + (queryTempl[1] == null ? "" : queryTempl[1])
1804 :
1805 (commandTempl[0] == null ? "" : commandTempl[0]) + command + (commandTempl[1] == null ? "" : commandTempl[1]);
1806
1778 synchronized (server) { 1807 synchronized (server) {
1779 try { 1808 try {
1780 out.writeLine( 1809 out.writeLine(cmd);
1781 (queryTempl[0] == null ? "" : queryTempl[0]) +
1782 command +
1783 (queryTempl[1] == null ? "" : queryTempl[1]));
1784 String error = in.waitForPrompt();
1785 if (error != null)
1786 throw new SQLException(error.substring(6), error.substring(0, 5));
1787 } catch (SocketTimeoutException e) {
1788 close(); // JDBC 4.1 semantics: abort()
1789 throw new SQLNonTransientConnectionException("connection timed out", "08M33");
1790 } catch (IOException e) {
1791 throw new SQLNonTransientConnectionException(e.getMessage(), "08000");
1792 }
1793 }
1794 }
1795
1796 /**
1797 * Sends the given string to MonetDB as control statement, making
1798 * sure there is a prompt after the command is sent. All possible
1799 * returned information is discarded. Encountered errors are
1800 * reported.
1801 *
1802 * @param command the exact string to send to MonetDB
1803 * @throws SQLException if an IO exception or a database error occurs
1804 */
1805 void sendControlCommand(String command) throws SQLException {
1806 // send X command
1807 synchronized (server) {
1808 try {
1809 out.writeLine(
1810 (commandTempl[0] == null ? "" : commandTempl[0]) +
1811 command +
1812 (commandTempl[1] == null ? "" : commandTempl[1]));
1813 String error = in.waitForPrompt(); 1810 String error = in.waitForPrompt();
1814 if (error != null) 1811 if (error != null)
1815 throw new SQLException(error.substring(6), error.substring(0, 5)); 1812 throw new SQLException(error.substring(6), error.substring(0, 5));
1816 } catch (SocketTimeoutException e) { 1813 } catch (SocketTimeoutException e) {
1817 close(); // JDBC 4.1 semantics, abort() 1814 close(); // JDBC 4.1 semantics, abort()
2708 // If the query is larger than the TCP buffer size, use a 2705 // If the query is larger than the TCP buffer size, use a
2709 // special send thread to avoid deadlock with the server due 2706 // special send thread to avoid deadlock with the server due
2710 // to blocking behaviour when the buffer is full. Because 2707 // to blocking behaviour when the buffer is full. Because
2711 // the server will be writing back results to us, it will 2708 // the server will be writing back results to us, it will
2712 // eventually block as well when its TCP buffer gets full, 2709 // eventually block as well when its TCP buffer gets full,
2713 // as we are blocking an not consuming from it. The result 2710 // as we are blocking and not consuming from it. The result
2714 // is a state where both client and server want to write, 2711 // is a state where both client and server want to write,
2715 // but block. 2712 // but block.
2716 if (query.length() > MapiSocket.BLOCK) { 2713 if (query.length() > MapiSocket.BLOCK) {
2717 // get a reference to the send thread 2714 // get a reference to the send thread
2718 if (sendThread == null) 2715 if (sendThread == null)