Debian Codegraph @

src/main/org/h2/command/dml/ScriptCommand.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.command.dml;
7
8 import java.io.BufferedInputStream;
9 import java.io.BufferedReader;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.Reader;
13 import java.nio.charset.Charset;
14 import java.nio.charset.StandardCharsets;
15 import java.sql.Connection;
16 import java.sql.PreparedStatement;
17 import java.sql.ResultSet;
18 import java.sql.SQLException;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Comparator;
23 import java.util.Set;
24 import java.util.TreeMap;
25 import java.util.TreeSet;
26 import org.h2.api.ErrorCode;
27 import org.h2.command.CommandInterface;
28 import org.h2.constraint.Constraint;
29 import org.h2.constraint.Constraint.Type;
30 import org.h2.engine.Comment;
31 import org.h2.engine.Constants;
32 import org.h2.engine.Database;
33 import org.h2.engine.DbObject;
34 import org.h2.engine.Right;
35 import org.h2.engine.RightOwner;
36 import org.h2.engine.Role;
37 import org.h2.engine.SessionLocal;
38 import org.h2.engine.Setting;
39 import org.h2.engine.User;
40 import org.h2.expression.Expression;
41 import org.h2.expression.ExpressionColumn;
42 import org.h2.index.Cursor;
43 import org.h2.index.Index;
44 import org.h2.message.DbException;
45 import org.h2.mvstore.DataUtils;
46 import org.h2.result.LocalResult;
47 import org.h2.result.ResultInterface;
48 import org.h2.result.Row;
49 import org.h2.schema.Constant;
50 import org.h2.schema.Domain;
51 import org.h2.schema.Schema;
52 import org.h2.schema.SchemaObject;
53 import org.h2.schema.Sequence;
54 import org.h2.schema.TriggerObject;
55 import org.h2.schema.UserDefinedFunction;
56 import org.h2.table.Column;
57 import org.h2.table.PlanItem;
58 import org.h2.table.Table;
59 import org.h2.table.TableType;
60 import org.h2.util.HasSQL;
61 import org.h2.util.IOUtils;
62 import org.h2.util.MathUtils;
63 import org.h2.util.StringUtils;
64 import org.h2.util.Utils;
65 import org.h2.value.TypeInfo;
66 import org.h2.value.Value;
67 import org.h2.value.ValueVarchar;
68
69 /**
70 * This class represents the statement
71 * SCRIPT
72 */
73 public class ScriptCommand extends ScriptBase {
74
75 private static final Comparator<? super DbObject> BY_NAME_COMPARATOR = (o1, o2) -> {
76 if (o1 instanceof SchemaObject && o2 instanceof SchemaObject) {
77 int cmp = ((SchemaObject) o1).getSchema().getName().compareTo(((SchemaObject) o2).getSchema().getName());
78 if (cmp != 0) {
79 return cmp;
80 }
81 }
82 return o1.getName().compareTo(o2.getName());
83 };
84
85 private Charset charset = StandardCharsets.UTF_8;
86 private Set<String> schemaNames;
87 private Collection<Table> tables;
88 private boolean passwords;
89
90 // true if we're generating the INSERT..VALUES statements for row values
91 private boolean data;
92 private boolean settings;
93
94 // true if we're generating the DROP statements
95 private boolean drop;
96 private boolean simple;
97 private boolean withColumns;
98 private boolean version = true;
99
100 private LocalResult result;
101 private String lineSeparatorString;
102 private byte[] lineSeparator;
103 private byte[] buffer;
104 private boolean tempLobTableCreated;
105 private int nextLobId;
106 private int lobBlockSize = Constants.IO_BUFFER_SIZE;
107
108 public ScriptCommand(SessionLocal session) {
109 super(session);
110 }
111
112 @Override
113 public boolean isQuery() {
114 return true;
115 }
116
117 // TODO lock all tables for 'script' command
118
119 public void setSchemaNames(Set<String> schemaNames) {
120 this.schemaNames = schemaNames;
121 }
122
123 public void setTables(Collection<Table> tables) {
124 this.tables = tables;
125 }
126
127 public void setData(boolean data) {
128 this.data = data;
129 }
130
131 public void setPasswords(boolean passwords) {
132 this.passwords = passwords;
133 }
134
135 public void setSettings(boolean settings) {
136 this.settings = settings;
137 }
138
139 public void setLobBlockSize(long blockSize) {
140 this.lobBlockSize = MathUtils.convertLongToInt(blockSize);
141 }
142
143 public void setDrop(boolean drop) {
144 this.drop = drop;
145 }
146
147 @Override
148 public ResultInterface queryMeta() {
149 LocalResult r = createResult();
150 r.done();
151 return r;
152 }
153
154 private LocalResult createResult() {
155 return new LocalResult(session, new Expression[] {
156 new ExpressionColumn(getDatabase(), new Column("SCRIPT", TypeInfo.TYPE_VARCHAR)) }, 1, 1);
157 }
158
159 @Override
160 public ResultInterface query(long maxrows) {
161 session.getUser().checkAdmin();
162 reset();
163 Database db = getDatabase();
164 if (schemaNames != null) {
165 for (String schemaName : schemaNames) {
166 Schema schema = db.findSchema(schemaName);
167 if (schema == null) {
168 throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1,
169 schemaName);
170 }
171 }
172 }
173 try {
174 result = createResult();
175 deleteStore();
176 openOutput();
177 if (out != null) {
178 buffer = new byte[Constants.IO_BUFFER_SIZE];
179 }
180 if (version) {
181 add("-- H2 " + Constants.VERSION, true);
182 }
183 if (settings) {
184 for (Setting setting : db.getAllSettings()) {
185 if (setting.getName().equals(SetTypes.getTypeName(
186 SetTypes.CREATE_BUILD))) {
187 // don't add CREATE_BUILD to the script
188 // (it is only set when creating the database)
189 continue;
190 }
191 add(setting.getCreateSQL(), false);
192 }
193 }
194 if (out != null) {
195 add("", true);
196 }
197 RightOwner[] rightOwners = db.getAllUsersAndRoles().toArray(new RightOwner[0]);
198 // ADMIN users first, other users next, roles last
199 Arrays.sort(rightOwners, (o1, o2) -> {
200 boolean b = o1 instanceof User;
201 if (b != o2 instanceof User) {
202 return b ? -1 : 1;
203 }
204 if (b) {
205 b = ((User) o1).isAdmin();
206 if (b != ((User) o2).isAdmin()) {
207 return b ? -1 : 1;
208 }
209 }
210 return o1.getName().compareTo(o2.getName());
211 });
212 for (RightOwner rightOwner : rightOwners) {
213 if (rightOwner instanceof User) {
214 add(((User) rightOwner).getCreateSQL(passwords), false);
215 } else {
216 add(((Role) rightOwner).getCreateSQL(true), false);
217 }
218 }
219 ArrayList<Schema> schemas = new ArrayList<>();
220 for (Schema schema : db.getAllSchemas()) {
221 if (excludeSchema(schema)) {
222 continue;
223 }
224 schemas.add(schema);
225 add(schema.getCreateSQL(), false);
226 }
227 dumpDomains(schemas);
228 for (Schema schema : schemas) {
229 for (Constant constant : sorted(schema.getAllConstants(), Constant.class)) {
230 add(constant.getCreateSQL(), false);
231 }
232 }
233
234 final ArrayList<Table> tables = db.getAllTablesAndViews();
235 // sort by id, so that views are after tables and views on views
236 // after the base views
237 tables.sort(Comparator.comparingInt(Table::getId));
238
239 // Generate the DROP XXX ... IF EXISTS
240 for (Table table : tables) {
241 if (excludeSchema(table.getSchema())) {
242 continue;
243 }
244 if (excludeTable(table)) {
245 continue;
246 }
247 if (table.isHidden()) {
248 continue;
249 }
250 table.lock(session, Table.READ_LOCK);
251 String sql = table.getCreateSQL();
252 if (sql == null) {
253 // null for metadata tables
254 continue;
255 }
256 if (drop) {
257 add(table.getDropSQL(), false);
258 }
259 }
260 for (Schema schema : schemas) {
261 for (UserDefinedFunction userDefinedFunction : sorted(schema.getAllFunctionsAndAggregates(),
262 UserDefinedFunction.class)) {
263 if (drop) {
264 add(userDefinedFunction.getDropSQL(), false);
265 }
266 add(userDefinedFunction.getCreateSQL(), false);
267 }
268 }
269 for (Schema schema : schemas) {
270 for (Sequence sequence : sorted(schema.getAllSequences(), Sequence.class)) {
271 if (sequence.getBelongsToTable()) {
272 continue;
273 }
274 if (drop) {
275 add(sequence.getDropSQL(), false);
276 }
277 add(sequence.getCreateSQL(), false);
278 }
279 }
280
281 // Generate CREATE TABLE and INSERT...VALUES
282 int count = 0;
283 for (Table table : tables) {
284 if (excludeSchema(table.getSchema())) {
285 continue;
286 }
287 if (excludeTable(table)) {
288 continue;
289 }
290 if (table.isHidden()) {
291 continue;
292 }
293 table.lock(session, Table.READ_LOCK);
294 String createTableSql = table.getCreateSQL();
295 if (createTableSql == null) {
296 // null for metadata tables
297 continue;
298 }
299 final TableType tableType = table.getTableType();
300 add(createTableSql, false);
301 final ArrayList<Constraint> constraints = table.getConstraints();
302 if (constraints != null) {
303 for (Constraint constraint : constraints) {
304 if (Constraint.Type.PRIMARY_KEY == constraint.getConstraintType()) {
305 add(constraint.getCreateSQLWithoutIndexes(), false);
306 }
307 }
308 }
309 if (TableType.TABLE == tableType) {
310 if (table.canGetRowCount(session)) {
311 StringBuilder builder = new StringBuilder("-- ")
312 .append(table.getRowCountApproximation(session))
313 .append(" +/- SELECT COUNT(*) FROM ");
314 table.getSQL(builder, HasSQL.TRACE_SQL_FLAGS);
315 add(builder.toString(), false);
316 }
317 if (data) {
318 count = generateInsertValues(count, table);
319 }
320 }
321 final ArrayList<Index> indexes = table.getIndexes();
322 for (int j = 0; indexes != null && j < indexes.size(); j++) {
323 Index index = indexes.get(j);
324 if (!index.getIndexType().getBelongsToConstraint()) {
325 add(index.getCreateSQL(), false);
326 }
327 }
328 }
329 if (tempLobTableCreated) {
330 add("DROP TABLE IF EXISTS SYSTEM_LOB_STREAM", true);
331 add("DROP ALIAS IF EXISTS SYSTEM_COMBINE_CLOB", true);
332 add("DROP ALIAS IF EXISTS SYSTEM_COMBINE_BLOB", true);
333 tempLobTableCreated = false;
334 }
335 // Generate CREATE CONSTRAINT ...
336 ArrayList<Constraint> constraints = new ArrayList<>();
337 for (Schema schema : schemas) {
338 for (Constraint constraint : schema.getAllConstraints()) {
339 if (excludeTable(constraint.getTable())) {
340 continue;
341 }
342 Type constraintType = constraint.getConstraintType();
343 if (constraintType != Type.DOMAIN && constraint.getTable().isHidden()) {
344 continue;
345 }
346 if (constraintType != Constraint.Type.PRIMARY_KEY) {
347 constraints.add(constraint);
348 }
349 }
350 }
351 constraints.sort(null);
352 for (Constraint constraint : constraints) {
353 add(constraint.getCreateSQLWithoutIndexes(), false);
354 }
355 // Generate CREATE TRIGGER ...
356 for (Schema schema : schemas) {
357 for (TriggerObject trigger : schema.getAllTriggers()) {
358 if (excludeTable(trigger.getTable())) {
359 continue;
360 }
361 add(trigger.getCreateSQL(), false);
362 }
363 }
364 // Generate GRANT ...
365 dumpRights(db);
366 // Generate COMMENT ON ...
367 for (Comment comment : db.getAllComments()) {
368 add(comment.getCreateSQL(), false);
369 }
370 if (out != null) {
371 out.close();
372 }
373 } catch (IOException e) {
374 throw DbException.convertIOException(e, getFileName());
375 } finally {
376 closeIO();
377 }
378 result.done();
379 LocalResult r = result;
380 reset();
381 return r;
382 }
383
384 private void dumpDomains(ArrayList<Schema> schemas) throws IOException {
385 TreeMap<Domain, TreeSet<Domain>> referencingDomains = new TreeMap<>(BY_NAME_COMPARATOR);
386 TreeSet<Domain> known = new TreeSet<>(BY_NAME_COMPARATOR);
387 for (Schema schema : schemas) {
388 for (Domain domain : sorted(schema.getAllDomains(), Domain.class)) {
389 Domain parent = domain.getDomain();
390 if (parent == null) {
391 addDomain(domain);
392 } else {
393 TreeSet<Domain> set = referencingDomains.get(parent);
394 if (set == null) {
395 set = new TreeSet<>(BY_NAME_COMPARATOR);
396 referencingDomains.put(parent, set);
397 }
398 set.add(domain);
399 if (parent.getDomain() == null || !schemas.contains(parent.getSchema())) {
400 known.add(parent);
401 }
402 }
403 }
404 }
405 while (!referencingDomains.isEmpty()) {
406 TreeSet<Domain> known2 = new TreeSet<>(BY_NAME_COMPARATOR);
407 for (Domain d : known) {
408 TreeSet<Domain> set = referencingDomains.remove(d);
409 if (set != null) {
410 for (Domain d2 : set) {
411 addDomain(d2);
412 known2.add(d2);
413 }
414 }
415 }
416 known = known2;
417 }
418 }
419
420 private void dumpRights(Database db) throws IOException {
421 Right[] rights = db.getAllRights().toArray(new Right[0]);
422 Arrays.sort(rights, (o1, o2) -> {
423 Role r1 = o1.getGrantedRole(), r2 = o2.getGrantedRole();
424 if ((r1 == null) != (r2 == null)) {
425 return r1 == null ? -1 : 1;
426 }
427 if (r1 == null) {
428 DbObject g1 = o1.getGrantedObject(), g2 = o2.getGrantedObject();
429 if ((g1 == null) != (g2 == null)) {
430 return g1 == null ? -1 : 1;
431 }
432 if (g1 != null) {
433 if (g1 instanceof Schema != g2 instanceof Schema) {
434 return g1 instanceof Schema ? -1 : 1;
435 }
436 int cmp = g1.getName().compareTo(g2.getName());
437 if (cmp != 0) {
438 return cmp;
439 }
440 }
441 } else {
442 int cmp = r1.getName().compareTo(r2.getName());
443 if (cmp != 0) {
444 return cmp;
445 }
446 }
447 return o1.getGrantee().getName().compareTo(o2.getGrantee().getName());
448 });
449 for (Right right : rights) {
450 DbObject object = right.getGrantedObject();
451 if (object != null) {
452 if (object instanceof Schema) {
453 if (excludeSchema((Schema) object)) {
454 continue;
455 }
456 } else if (object instanceof Table) {
457 Table table = (Table) object;
458 if (excludeSchema(table.getSchema())) {
459 continue;
460 }
461 if (excludeTable(table)) {
462 continue;
463 }
464 }
465 }
466 add(right.getCreateSQL(), false);
467 }
468 }
469
470 private void addDomain(Domain domain) throws IOException {
471 if (drop) {
472 add(domain.getDropSQL(), false);
473 }
474 add(domain.getCreateSQL(), false);
475 }
476
477 private static <T extends DbObject> T[] sorted(Collection<T> collection, Class<T> clazz) {
478 @SuppressWarnings("unchecked")
479 T[] array = collection.toArray((T[]) java.lang.reflect.Array.newInstance(clazz, 0));
480 Arrays.sort(array, BY_NAME_COMPARATOR);
481 return array;
482 }
483
484 private int generateInsertValues(int count, Table table) throws IOException {
485 PlanItem plan = table.getBestPlanItem(session, null, null, -1, null, null);
486 Index index = plan.getIndex();
487 Cursor cursor = index.find(session, null, null);
488 Column[] columns = table.getColumns();
489 boolean withGenerated = false, withGeneratedAlwaysAsIdentity = false;
490 for (Column c : columns) {
491 if (c.isGeneratedAlways()) {
492 if (c.isIdentity()) {
493 withGeneratedAlwaysAsIdentity = true;
494 } else {
495 withGenerated = true;
496 }
497 }
498 }
499 StringBuilder builder = new StringBuilder("INSERT INTO ");
500 table.getSQL(builder, HasSQL.DEFAULT_SQL_FLAGS);
501 if (withGenerated || withGeneratedAlwaysAsIdentity || withColumns) {
502 builder.append('(');
503 boolean needComma = false;
504 for (Column column : columns) {
505 if (!column.isGenerated()) {
506 if (needComma) {
507 builder.append(", ");
508 }
509 needComma = true;
510 column.getSQL(builder, HasSQL.DEFAULT_SQL_FLAGS);
511 }
512 }
513 builder.append(')');
514 if (withGeneratedAlwaysAsIdentity) {
515 builder.append(" OVERRIDING SYSTEM VALUE");
516 }
517 }
518 builder.append(" VALUES");
519 if (!simple) {
520 builder.append('\n');
521 }
522 builder.append('(');
523 String ins = builder.toString();
524 builder = null;
525 int columnCount = columns.length;
526 while (cursor.next()) {
527 Row row = cursor.get();
528 if (builder == null) {
529 builder = new StringBuilder(ins);
530 } else {
531 builder.append(",\n(");
532 }
533 boolean needComma = false;
534 for (int i = 0; i < columnCount; i++) {
535 if (columns[i].isGenerated()) {
536 continue;
537 }
538 if (needComma) {
539 builder.append(", ");
540 }
541 needComma = true;
542 Value v = row.getValue(i);
543 if (v.getType().getPrecision() > lobBlockSize) {
544 int id;
545 if (v.getValueType() == Value.CLOB) {
546 id = writeLobStream(v);
547 builder.append("SYSTEM_COMBINE_CLOB(").append(id).append(')');
548 } else if (v.getValueType() == Value.BLOB) {
549 id = writeLobStream(v);
550 builder.append("SYSTEM_COMBINE_BLOB(").append(id).append(')');
551 } else {
552 v.getSQL(builder, HasSQL.NO_CASTS);
553 }
554 } else {
555 v.getSQL(builder, HasSQL.NO_CASTS);
556 }
557 }
558 builder.append(')');
559 count++;
560 if ((count & 127) == 0) {
561 checkCanceled();
562 }
563 if (simple || builder.length() > Constants.IO_BUFFER_SIZE) {
564 add(builder.toString(), true);
565 builder = null;
566 }
567 }
568 if (builder != null) {
569 add(builder.toString(), true);
570 }
571 return count;
572 }
573
574 private int writeLobStream(Value v) throws IOException {
575 if (!tempLobTableCreated) {
576 add("CREATE CACHED LOCAL TEMPORARY TABLE IF NOT EXISTS SYSTEM_LOB_STREAM" +
577 "(ID INT NOT NULL, PART INT NOT NULL, " +
578 "CDATA VARCHAR, BDATA VARBINARY)",
579 true);
580 add("ALTER TABLE SYSTEM_LOB_STREAM ADD CONSTRAINT SYSTEM_LOB_STREAM_PRIMARY_KEY PRIMARY KEY(ID, PART)",
581 true);
582 String className = getClass().getName();
583 add("CREATE ALIAS IF NOT EXISTS " + "SYSTEM_COMBINE_CLOB FOR '" + className + ".combineClob'", true);
584 add("CREATE ALIAS IF NOT EXISTS " + "SYSTEM_COMBINE_BLOB FOR '" + className + ".combineBlob'", true);
585 tempLobTableCreated = true;
586 }
587 int id = nextLobId++;
588 switch (v.getValueType()) {
589 case Value.BLOB: {
590 byte[] bytes = new byte[lobBlockSize];
591 try (InputStream input = v.getInputStream()) {
592 for (int i = 0;; i++) {
593 StringBuilder buff = new StringBuilder(lobBlockSize * 2);
594 buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(").append(id)
595 .append(", ").append(i).append(", NULL, X'");
596 int len = IOUtils.readFully(input, bytes, lobBlockSize);
597 if (len <= 0) {
598 break;
599 }
600 StringUtils.convertBytesToHex(buff, bytes, len).append("')");
601 String sql = buff.toString();
602 add(sql, true);
603 }
604 }
605 break;
606 }
607 case Value.CLOB: {
608 char[] chars = new char[lobBlockSize];
609
610 try (Reader reader = v.getReader()) {
611 for (int i = 0;; i++) {
612 StringBuilder buff = new StringBuilder(lobBlockSize * 2);
613 buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(").append(id).append(", ").append(i)
614 .append(", ");
615 int len = IOUtils.readFully(reader, chars, lobBlockSize);
616 if (len == 0) {
617 break;
618 }
619 StringUtils.quoteStringSQL(buff, new String(chars, 0, len)).
620 append(", NULL)");
621 String sql = buff.toString();
622 add(sql, true);
623 }
624 }
625 break;
626 }
627 default:
628 throw DbException.getInternalError("type:" + v.getValueType());
629 }
630 return id;
631 }
632
633 /**
634 * Combine a BLOB.
635 * This method is called from the script.
636 * When calling with id -1, the file is deleted.
637 *
638 * @param conn a connection
639 * @param id the lob id
640 * @return a stream for the combined data
641 * @throws SQLException on failure
642 */
643 public static InputStream combineBlob(Connection conn, int id)
644 throws SQLException {
645 if (id < 0) {
646 return null;
647 }
648 final ResultSet rs = getLobStream(conn, "BDATA", id);
649 return new InputStream() {
650 private InputStream current;
651 private boolean closed;
652 @Override
653 public int read() throws IOException {
654 while (true) {
655 try {
656 if (current == null) {
657 if (closed) {
658 return -1;
659 }
660 if (!rs.next()) {
661 close();
662 return -1;
663 }
664 current = rs.getBinaryStream(1);
665 current = new BufferedInputStream(current);
666 }
667 int x = current.read();
668 if (x >= 0) {
669 return x;
670 }
671 current = null;
672 } catch (SQLException e) {
673 throw DataUtils.convertToIOException(e);
674 }
675 }
676 }
677 @Override
678 public void close() throws IOException {
679 if (closed) {
680 return;
681 }
682 closed = true;
683 try {
684 rs.close();
685 } catch (SQLException e) {
686 throw DataUtils.convertToIOException(e);
687 }
688 }
689 };
690 }
691
692 /**
693 * Combine a CLOB.
694 * This method is called from the script.
695 *
696 * @param conn a connection
697 * @param id the lob id
698 * @return a reader for the combined data
699 * @throws SQLException on failure
700 */
701 public static Reader combineClob(Connection conn, int id) throws SQLException {
702 if (id < 0) {
703 return null;
704 }
705 final ResultSet rs = getLobStream(conn, "CDATA", id);
706 return new Reader() {
707 private Reader current;
708 private boolean closed;
709 @Override
710 public int read() throws IOException {
711 while (true) {
712 try {
713 if (current == null) {
714 if (closed) {
715 return -1;
716 }
717 if (!rs.next()) {
718 close();
719 return -1;
720 }
721 current = rs.getCharacterStream(1);
722 current = new BufferedReader(current);
723 }
724 int x = current.read();
725 if (x >= 0) {
726 return x;
727 }
728 current = null;
729 } catch (SQLException e) {
730 throw DataUtils.convertToIOException(e);
731 }
732 }
733 }
734 @Override
735 public void close() throws IOException {
736 if (closed) {
737 return;
738 }
739 closed = true;
740 try {
741 rs.close();
742 } catch (SQLException e) {
743 throw DataUtils.convertToIOException(e);
744 }
745 }
746 @Override
747 public int read(char[] buffer, int off, int len) throws IOException {
748 if (len == 0) {
749 return 0;
750 }
751 int c = read();
752 if (c == -1) {
753 return -1;
754 }
755 buffer[off] = (char) c;
756 int i = 1;
757 for (; i < len; i++) {
758 c = read();
759 if (c == -1) {
760 break;
761 }
762 buffer[off + i] = (char) c;
763 }
764 return i;
765 }
766 };
767 }
768
769 private static ResultSet getLobStream(Connection conn, String column, int id)
770 throws SQLException {
771 PreparedStatement prep = conn.prepareStatement("SELECT " + column +
772 " FROM SYSTEM_LOB_STREAM WHERE ID=? ORDER BY PART");
773 prep.setInt(1, id);
774 return prep.executeQuery();
775 }
776
777 private void reset() {
778 result = null;
779 buffer = null;
780 lineSeparatorString = System.lineSeparator();
781 lineSeparator = lineSeparatorString.getBytes(charset);
782 }
783
784 private boolean excludeSchema(Schema schema) {
785 if (schemaNames != null && !schemaNames.contains(schema.getName())) {
786 return true;
787 }
788 if (tables != null) {
789 // if filtering on specific tables, only include those schemas
790 for (Table table : schema.getAllTablesAndViews(session)) {
791 if (tables.contains(table)) {
792 return false;
793 }
794 }
795 return true;
796 }
797 return false;
798 }
799
800 private boolean excludeTable(Table table) {
801 return tables != null && !tables.contains(table);
802 }
803
804 private void add(String s, boolean insert) throws IOException {
805 if (s == null) {
806 return;
807 }
808 if (lineSeparator.length > 1 || lineSeparator[0] != '\n') {
809 s = StringUtils.replaceAll(s, "\n", lineSeparatorString);
810 }
811 s += ";";
812 if (out != null) {
813 byte[] buff = s.getBytes(charset);
814 int len = MathUtils.roundUpInt(buff.length +
815 lineSeparator.length, Constants.FILE_BLOCK_SIZE);
816 buffer = Utils.copy(buff, buffer);
817
818 if (len > buffer.length) {
819 buffer = new byte[len];
820 }
821 System.arraycopy(buff, 0, buffer, 0, buff.length);
822 for (int i = buff.length; i < len - lineSeparator.length; i++) {
823 buffer[i] = ' ';
824 }
825 for (int j = 0, i = len - lineSeparator.length; i < len; i++, j++) {
826 buffer[i] = lineSeparator[j];
827 }
828 out.write(buffer, 0, len);
829 if (!insert) {
830 result.addRow(ValueVarchar.get(s));
831 }
832 } else {
833 result.addRow(ValueVarchar.get(s));
834 }
835 }
836
837 public void setSimple(boolean simple) {
838 this.simple = simple;
839 }
840
841 public void setWithColumns(boolean withColumns) {
842 this.withColumns = withColumns;
843 }
844
845 public void setVersion(boolean version) {
846 this.version = version;
847 }
848
849 public void setCharset(Charset charset) {
850 this.charset = charset;
851 }
852
853 @Override
854 public int getType() {
855 return CommandInterface.SCRIPT;
856 }
857
858 }