public SubqueryCursor(TableFilter filter, SearchRow first, SearchRow last) { StringBuilder buff = new StringBuilder("SELECT * FROM "); buff.append(filter.getTable().getSQL()); Expression filterCondition = filter.getFilterCondition(); if (filterCondition != null) { buff.append(" WHERE ").append(StringUtils.unEnclose(filterCondition.getSQL())); } Prepared prepared = filter.getSession().prepare(buff.toString(), true); subqueryResult = prepared.query(-1); }
/** * INTERNAL. Parse and set the CSV options. * * @param options the the options * @return the character set */ public String setOptions(String options) { String charset = null; String[] keyValuePairs = StringUtils.arraySplit(options, ' ', false); for (String pair : keyValuePairs) { if (pair.length() == 0) { continue; } int index = pair.indexOf('='); String key = StringUtils.trim(pair.substring(0, index), true, true, " "); String value = pair.substring(index + 1); char ch = value.length() == 0 ? 0 : value.charAt(0); if (isParam(key, "escape", "esc", "escapeCharacter")) { setEscapeCharacter(ch); } else if (isParam(key, "fieldDelimiter", "fieldDelim")) { setFieldDelimiter(ch); } else if (isParam(key, "fieldSeparator", "fieldSep")) { setFieldSeparatorRead(ch); setFieldSeparatorWrite(value); } else if (isParam(key, "lineComment", "lineCommentCharacter")) { setLineCommentCharacter(ch); } else if (isParam(key, "lineSeparator", "lineSep")) { setLineSeparator(value); } else if (isParam(key, "null", "nullString")) { setNullString(value); } else if (isParam(key, "rowSeparator", "rowSep")) { setRowSeparatorWrite(value); } else if (isParam(key, "charset", "characterSet")) { charset = value; } else if (isParam(key, "preserveWhitespace")) { setPreserveWhitespace(Boolean.parseBoolean(value)); } else if (isParam(key, "writeColumnHeader")) { setWriteColumnHeader(Boolean.parseBoolean(value)); } else if (isParam(key, "caseSensitiveColumnNames")) { setCaseSensitiveColumnNames(Boolean.parseBoolean(value)); } else { throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, key); } } return charset; }
private String getSQL(String s) { String lower = StringUtils.toLowerEnglish(s); if (lower.startsWith("show max_identifier_length")) { s = "CALL 63"; } else if (lower.startsWith("set client_encoding to")) { s = "set DATESTYLE ISO"; } // s = StringUtils.replaceAll(s, "i.indkey[ia.attnum-1]", "0"); if (server.getTrace()) { server.trace(s + ";"); } return s; }
private void sendRowDescription(ResultSetMetaData meta) throws Exception { if (meta == null) { sendNoData(); } else { int columns = meta.getColumnCount(); int[] types = new int[columns]; int[] precision = new int[columns]; String[] names = new String[columns]; for (int i = 0; i < columns; i++) { String name = meta.getColumnName(i + 1); names[i] = name; int type = meta.getColumnType(i + 1); type = PgServer.convertType(type); // the ODBC client needs the column pg_catalog.pg_index // to be of type 'int2vector' // if (name.equalsIgnoreCase("indkey") && // "pg_index".equalsIgnoreCase(meta.getTableName(i + 1))) { // type = PgServer.PG_TYPE_INT2VECTOR; // } precision[i] = meta.getColumnDisplaySize(i + 1); server.checkType(type); types[i] = type; } startMessage('T'); writeShort(columns); for (int i = 0; i < columns; i++) { writeString(StringUtils.toLowerEnglish(names[i])); // object ID writeInt(0); // attribute number of the column writeShort(0); // data type writeInt(types[i]); // pg_type.typlen writeShort(getTypeSize(types[i], precision[i])); // pg_attribute.atttypmod writeInt(-1); // text writeShort(0); } sendMessage(); } }
private void connectServer(ConnectionInfo ci) { String name = ci.getName(); if (name.startsWith("//")) { name = name.substring("//".length()); } int idx = name.indexOf('/'); if (idx < 0) { throw ci.getFormatException(); } databaseName = name.substring(idx + 1); String server = name.substring(0, idx); traceSystem = new TraceSystem(null); String traceLevelFile = ci.getProperty(SetTypes.TRACE_LEVEL_FILE, null); if (traceLevelFile != null) { int level = Integer.parseInt(traceLevelFile); String prefix = getFilePrefix(SysProperties.CLIENT_TRACE_DIRECTORY); try { traceSystem.setLevelFile(level); if (level > 0) { String file = FileUtils.createTempFile(prefix, Constants.SUFFIX_TRACE_FILE, false, false); traceSystem.setFileName(file); } } catch (IOException e) { throw DbException.convertIOException(e, prefix); } } String traceLevelSystemOut = ci.getProperty(SetTypes.TRACE_LEVEL_SYSTEM_OUT, null); if (traceLevelSystemOut != null) { int level = Integer.parseInt(traceLevelSystemOut); traceSystem.setLevelSystemOut(level); } trace = traceSystem.getTrace(Trace.JDBC); String serverList = null; if (server.indexOf(',') >= 0) { serverList = StringUtils.quoteStringSQL(server); ci.setProperty("CLUSTER", Constants.CLUSTERING_ENABLED); } autoReconnect = Boolean.valueOf(ci.getProperty("AUTO_RECONNECT", "false")).booleanValue(); // AUTO_SERVER implies AUTO_RECONNECT boolean autoServer = Boolean.valueOf(ci.getProperty("AUTO_SERVER", "false")).booleanValue(); if (autoServer && serverList != null) { throw DbException.getUnsupportedException("autoServer && serverList != null"); } autoReconnect |= autoServer; if (autoReconnect) { String className = ci.getProperty("DATABASE_EVENT_LISTENER"); if (className != null) { className = StringUtils.trim(className, true, true, "'"); try { eventListener = (DatabaseEventListener) Utils.loadUserClass(className).newInstance(); } catch (Throwable e) { throw DbException.convert(e); } } } cipher = ci.getProperty("CIPHER"); if (cipher != null) { fileEncryptionKey = MathUtils.secureRandomBytes(32); } String[] servers; if (ci.isDynamic()) { servers = new String[] {ci.getOnlineServer(server)}; } else { servers = StringUtils.arraySplit(server, ',', true); if (servers.length > 1 && !ci.removeProperty("USE_H2_CLUSTER_MODE", false)) servers = new String[] {servers[random.nextInt(servers.length)]}; } int len = servers.length; transferList.clear(); sessionId = StringUtils.convertBytesToHex(MathUtils.secureRandomBytes(32)); // TODO cluster: support more than 2 connections boolean switchOffCluster = false; try { for (int i = 0; i < len; i++) { String s = servers[i]; try { Transfer trans = initTransfer(ci, databaseName, s); transferList.add(trans); } catch (IOException e) { if (len == 1) { throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, e, e + ": " + s); } switchOffCluster = true; } } checkClosed(); if (switchOffCluster) { switchOffCluster(); } checkClusterDisableAutoCommit(serverList); } catch (DbException e) { traceSystem.close(); throw e; } }