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