private int generateInsertValues(int count, Table table) throws IOException { PlanItem plan = table.getBestPlanItem(session, null, null); Index index = plan.getIndex(); Cursor cursor = index.find(session, null, null); Column[] columns = table.getColumns(); StatementBuilder buff = new StatementBuilder("INSERT INTO "); buff.append(table.getSQL()).append('('); for (Column col : columns) { buff.appendExceptFirst(", "); buff.append(Parser.quoteIdentifier(col.getName())); } buff.append(") VALUES"); if (!simple) { buff.append('\n'); } buff.append('('); String ins = buff.toString(); buff = null; while (cursor.next()) { Row row = cursor.get(); if (buff == null) { buff = new StatementBuilder(ins); } else { buff.append(",\n("); } for (int j = 0; j < row.getColumnCount(); j++) { if (j > 0) { buff.append(", "); } Value v = row.getValue(j); if (v.getPrecision() > lobBlockSize) { int id; if (v.getType() == Value.CLOB) { id = writeLobStream(v); buff.append("SYSTEM_COMBINE_CLOB(" + id + ")"); } else if (v.getType() == Value.BLOB) { id = writeLobStream(v); buff.append("SYSTEM_COMBINE_BLOB(" + id + ")"); } else { buff.append(v.getSQL()); } } else { buff.append(v.getSQL()); } } buff.append(')'); count++; if ((count & 127) == 0) { checkCanceled(); } if (simple || buff.length() > Constants.IO_BUFFER_SIZE) { add(buff.toString(), true); buff = null; } } if (buff != null) { add(buff.toString(), true); } return count; }
/** * Compare two values with the current comparison mode. The values may be of different type. * * @param a the first value * @param b the second value * @return 0 if both values are equal, -1 if the first value is smaller, and 1 otherwise */ public int compareTypeSave(Value a, Value b) { if (a == b) { return 0; } int dataType = Value.getHigherOrder(a.getType(), b.getType()); a = a.convertTo(dataType); b = b.convertTo(dataType); return a.compareTypeSave(b, compareMode); }
private int writeLobStream(Value v) throws IOException { if (!tempLobTableCreated) { add( "CREATE TABLE IF NOT EXISTS SYSTEM_LOB_STREAM" + "(ID INT NOT NULL, PART INT NOT NULL, CDATA VARCHAR, BDATA BINARY)", true); add( "CREATE PRIMARY KEY SYSTEM_LOB_STREAM_PRIMARY_KEY " + "ON SYSTEM_LOB_STREAM(ID, PART)", true); add( "CREATE ALIAS IF NOT EXISTS " + "SYSTEM_COMBINE_CLOB FOR \"" + this.getClass().getName() + ".combineClob\"", true); add( "CREATE ALIAS IF NOT EXISTS " + "SYSTEM_COMBINE_BLOB FOR \"" + this.getClass().getName() + ".combineBlob\"", true); tempLobTableCreated = true; } int id = nextLobId++; switch (v.getType()) { case Value.BLOB: { byte[] bytes = new byte[lobBlockSize]; InputStream input = v.getInputStream(); try { for (int i = 0; ; i++) { StringBuilder buff = new StringBuilder(lobBlockSize * 2); buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(" + id + ", " + i + ", NULL, '"); int len = IOUtils.readFully(input, bytes, lobBlockSize); if (len <= 0) { break; } buff.append(StringUtils.convertBytesToHex(bytes, len)).append("')"); String sql = buff.toString(); add(sql, true); } } finally { IOUtils.closeSilently(input); } break; } case Value.CLOB: { char[] chars = new char[lobBlockSize]; Reader reader = v.getReader(); try { for (int i = 0; ; i++) { StringBuilder buff = new StringBuilder(lobBlockSize * 2); buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(" + id + ", " + i + ", "); int len = IOUtils.readFully(reader, chars, lobBlockSize); if (len < 0) { break; } buff.append(StringUtils.quoteStringSQL(new String(chars, 0, len))).append(", NULL)"); String sql = buff.toString(); add(sql, true); } } finally { IOUtils.closeSilently(reader); } break; } default: DbException.throwInternalError("type:" + v.getType()); } return id; }