📄 scriptcommand.java
字号:
Row row = cursor.get();
if (buff == null) {
buff = new StringBuffer(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((ValueLob) v);
buff.append("SYSTEM_COMBINE_CLOB(" + id + ")");
} else if (v.getType() == Value.BLOB) {
id = writeLobStream((ValueLob) v);
buff.append("SYSTEM_COMBINE_BLOB(" + id + ")");
} else {
buff.append(v.getSQL());
}
} else {
buff.append(v.getSQL());
}
}
buff.append(")");
if (simple || buff.length() > Constants.IO_BUFFER_SIZE) {
add(buff.toString(), true);
buff = null;
}
}
if (buff != null) {
add(buff.toString(), true);
}
}
}
ObjectArray indexes = table.getIndexes();
for (int j = 0; indexes != null && j < indexes.size(); j++) {
Index index = (Index) indexes.get(j);
if (!index.getIndexType().belongsToConstraint()) {
add(index.getCreateSQL(), false);
}
}
}
if (tempLobTableCreated) {
add("DROP TABLE IF EXISTS SYSTEM_LOB_STREAM", true);
add("CALL SYSTEM_COMBINE_BLOB(-1)", true);
add("DROP ALIAS IF EXISTS SYSTEM_COMBINE_CLOB", true);
add("DROP ALIAS IF EXISTS SYSTEM_COMBINE_BLOB", true);
tempLobTableCreated = false;
}
ObjectArray constraints = db.getAllSchemaObjects(DbObject.CONSTRAINT);
constraints.sort(new Comparator() {
public int compare(Object o1, Object o2) {
Constraint c1 = (Constraint) o1;
Constraint c2 = (Constraint) o2;
return c1.compareTo(c2);
}
});
for (int i = 0; i < constraints.size(); i++) {
Constraint constraint = (Constraint) constraints.get(i);
add(constraint.getCreateSQLWithoutIndexes(), false);
}
ObjectArray triggers = db.getAllSchemaObjects(DbObject.TRIGGER);
for (int i = 0; i < triggers.size(); i++) {
TriggerObject trigger = (TriggerObject) triggers.get(i);
add(trigger.getCreateSQL(), false);
}
ObjectArray rights = db.getAllRights();
for (int i = 0; i < rights.size(); i++) {
Right right = (Right) rights.get(i);
add(right.getCreateSQL(), false);
}
ObjectArray comments = db.getAllComments();
for (int i = 0; i < comments.size(); i++) {
Comment comment = (Comment) comments.get(i);
add(comment.getCreateSQL(), false);
}
if (out != null) {
out.close();
}
} catch (IOException e) {
throw Message.convertIOException(e, fileName);
} finally {
closeIO();
}
result.done();
LocalResult r = result;
reset();
return r;
}
private int writeLobStream(ValueLob v) throws IOException, SQLException {
if (!tempLobTableCreated) {
add("CREATE TABLE IF NOT EXISTS SYSTEM_LOB_STREAM(ID INT, PART INT, CDATA VARCHAR, BDATA BINARY, PRIMARY KEY(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 in = v.getInputStream();
try {
for (int i = 0;; i++) {
StringBuffer buff = new StringBuffer(lobBlockSize * 2);
buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(" + id + ", " + i + ", NULL, '");
int len = IOUtils.readFully(in, bytes, 0, lobBlockSize);
if (len <= 0) {
break;
}
buff.append(ByteUtils.convertBytesToString(bytes, len));
buff.append("')");
String sql = buff.toString();
add(sql, true);
}
} finally {
IOUtils.closeSilently(in);
}
break;
}
case Value.CLOB: {
char[] chars = new char[lobBlockSize];
Reader in = v.getReader();
try {
for (int i = 0;; i++) {
StringBuffer buff = new StringBuffer(lobBlockSize * 2);
buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(" + id + ", " + i + ", ");
int len = IOUtils.readFully(in, chars, lobBlockSize);
if (len < 0) {
break;
}
buff.append(StringUtils.quoteStringSQL(new String(chars, 0, len)));
buff.append(", NULL)");
String sql = buff.toString();
add(sql, true);
}
} finally {
IOUtils.closeSilently(in);
}
break;
}
default:
throw Message.getInternalError("type:" + v.getType());
}
return id;
}
/**
* Combine a BLOB.
* This method is called from the script.
* When calling with id -1, the file is deleted.
*
* @param conn a connection
* @param id the lob id
* @return a stream for the combined data
*/
public static InputStream combineBlob(Connection conn, int id) throws SQLException, IOException {
if (id < 0) {
FileUtils.delete(TEMP_LOB_FILENAME);
return null;
}
ResultSet rs = getLobStream(conn, "BDATA", id);
OutputStream out = FileUtils.openFileOutputStream(TEMP_LOB_FILENAME, false);
while (rs.next()) {
InputStream in = rs.getBinaryStream(1);
IOUtils.copyAndCloseInput(in, out);
}
out.close();
deleteLobStream(conn, id);
return openAutoCloseInput();
}
/**
* Combine a CLOB.
* This method is called from the script.
*
* @param conn a connection
* @param id the lob id
* @return a reader for the combined data
*/
public static Reader combineClob(Connection conn, int id) throws SQLException, IOException {
ResultSet rs = getLobStream(conn, "CDATA", id);
Writer out = FileUtils.openFileWriter(TEMP_LOB_FILENAME, false);
while (rs.next()) {
Reader in = new BufferedReader(rs.getCharacterStream(1));
IOUtils.copyAndCloseInput(in, out);
}
out.close();
deleteLobStream(conn, id);
return IOUtils.getReader(openAutoCloseInput());
}
private static ResultSet getLobStream(Connection conn, String column, int id) throws SQLException {
PreparedStatement prep = conn.prepareStatement(
"SELECT " + column + " FROM SYSTEM_LOB_STREAM WHERE ID=? ORDER BY PART");
prep.setInt(1, id);
return prep.executeQuery();
}
private static void deleteLobStream(Connection conn, int id) throws SQLException {
PreparedStatement prep = conn.prepareStatement("DELETE FROM SYSTEM_LOB_STREAM WHERE ID=?");
prep.setInt(1, id);
prep.execute();
}
private static InputStream openAutoCloseInput() throws IOException {
InputStream in = FileUtils.openFileInputStream(TEMP_LOB_FILENAME);
in = new BufferedInputStream(in);
return new AutoCloseInputStream(in);
}
private void reset() throws SQLException {
result = null;
buffer = null;
lineSeparator = StringUtils.utf8Encode(SysProperties.LINE_SEPARATOR);
}
private void add(String s, boolean insert) throws SQLException, IOException {
if (s == null) {
return;
}
s += ";";
if (out != null) {
byte[] buff = StringUtils.utf8Encode(s);
int len = MathUtils.roundUp(buff.length + lineSeparator.length, Constants.FILE_BLOCK_SIZE);
buffer = ByteUtils.copy(buff, buffer);
if (len > buffer.length) {
buffer = new byte[len];
}
System.arraycopy(buff, 0, buffer, 0, buff.length);
for (int i = buff.length; i < len - lineSeparator.length; i++) {
buffer[i] = ' ';
}
for (int j = 0, i = len - lineSeparator.length; i < len; i++, j++) {
buffer[i] = lineSeparator[j];
}
out.write(buffer, 0, len);
if (!insert) {
Value[] row = new Value[1];
row[0] = ValueString.get(s);
result.addRow(row);
}
} else {
Value[] row = new Value[1];
row[0] = ValueString.get(s);
result.addRow(row);
}
}
public void setSimple(boolean simple) {
this.simple = simple;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -