annotate src/main/java/org/monetdb/util/SQLExporter.java @ 391:f523727db392

Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.* This naming complies to the Java Package Naming convention as MonetDB's main website is www.monetdb.org.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 12 Nov 2020 22:02:01 +0100 (2020-11-12)
parents src/main/java/nl/cwi/monetdb/util/SQLExporter.java@31e79dd9e658
children bf9f6b6ecf40
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1 /*
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
2 * This Source Code Form is subject to the terms of the Mozilla Public
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
5 *
350
54137aeb1f92 Update Copyright year.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 318
diff changeset
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V.
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
7 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
8
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 374
diff changeset
9 package org.monetdb.util;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
10
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
11 import java.sql.DatabaseMetaData;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
12 import java.sql.ResultSet;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
13 import java.sql.ResultSetMetaData;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
14 import java.sql.SQLException;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
15 import java.sql.Types;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
16 import java.util.Iterator;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
17 import java.util.LinkedHashSet;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
18 import java.util.Map;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
19 import java.util.Set;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
20 import java.util.Stack;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
21
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
22 public final class SQLExporter extends Exporter {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
23 private int outputMode;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
24 private Stack<String> lastSchema;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
25
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
26 public final static short TYPE_OUTPUT = 1;
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
27 public final static short VALUE_INSERT = 0;
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
28 public final static short VALUE_COPY = 1;
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
29 public final static short VALUE_TABLE = 2;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
30
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
31 public SQLExporter(final java.io.PrintWriter out) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
32 super(out);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
33 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
34
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
35 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
36 * A helper method to generate SQL CREATE code for a given table.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
37 * This method performs all required lookups to find all relations and
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
38 * column information, as well as additional indices.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
39 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
40 * @param dbmd a DatabaseMetaData object to query on (not null)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
41 * @param type the type of the object, e.g. VIEW, TABLE (not null)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
42 * @param schema the schema the object is in (not null)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
43 * @param name the table to describe in SQL CREATE format (not null)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
44 * @throws SQLException if a database related error occurs
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
45 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
46 public void dumpSchema(
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
47 final DatabaseMetaData dbmd,
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
48 final String type,
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
49 final String schema,
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
50 final String name)
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
51 throws SQLException
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
52 {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
53 assert dbmd != null;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
54 assert type != null;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
55 assert schema != null;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
56 assert name != null;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
57
313
f2750ea9f93d When exporting table/view definition always include the schema name.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 312
diff changeset
58 final String fqname = dq(schema) + "." + dq(name);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
59
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
60 if (useSchema)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
61 changeSchema(schema);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
62
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
63 // handle views directly
318
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
64 if (type.endsWith("VIEW")) { // for types: VIEW and SYSTEM VIEW
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
65 final String viewDDL = fetchSysTablesQueryValue(dbmd.getConnection(), schema, name);
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
66 if (viewDDL != null)
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
67 out.println(viewDDL);
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
68 else
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
69 out.println("-- unknown " + type + " " + fqname + ": no SQL view definition found!");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
70 return;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
71 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
72
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
73 out.println("CREATE " + type + " " + fqname + " (");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
74
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
75 // add all columns with their type, nullability and default definition
315
4793f9b80bb3 Removed the catalog parameter from the Exporter.dumpSchema method as it is always null for MonetDB.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 314
diff changeset
76 ResultSet cols = dbmd.getColumns(null, schema, name, null);
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
77 int colNmIndex = cols.findColumn("COLUMN_NAME");
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
78 final int colTypeNmIndex = cols.findColumn("TYPE_NAME");
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
79 final int datatypeIndex = cols.findColumn("DATA_TYPE");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
80 final int sizeIndex = cols.findColumn("COLUMN_SIZE");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
81 final int digitsIndex = cols.findColumn("DECIMAL_DIGITS");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
82 final int isNotNullIndex = cols.findColumn("NULLABLE");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
83 final int defaultValueIndex = cols.findColumn("COLUMN_DEF");
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
84 final ResultSetMetaData rsmd = cols.getMetaData();
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
85 final int colwidth = rsmd.getColumnDisplaySize(colNmIndex);
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
86 int typewidth = rsmd.getColumnDisplaySize(colTypeNmIndex);
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
87 if (typewidth < 13)
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
88 typewidth = 13; // use minimal 13 characters for the typename (same as used in mclient)
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
89
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
90 final StringBuilder sb = new StringBuilder(128);
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
91 int i;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
92 for (i = 0; cols.next(); i++) {
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
93 if (i > 0)
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
94 out.println(",");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
95
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
96 // print column name (with double quotes)
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
97 String s = dq(cols.getString(colNmIndex));
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
98 out.print("\t" + s + repeat(' ', (colwidth - s.length() + 3)));
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
99
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
100 int digits = cols.getInt(digitsIndex);
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
101 s = cols.getString(colTypeNmIndex).toUpperCase(); // ANSI SQL uses uppercase data type names
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
102 // do some data type substitutions to match SQL standard
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
103 if (s.equals("INT")) {
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
104 s = "INTEGER";
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
105 } else if (s.equals("SEC_INTERVAL")) {
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
106 s = "INTERVAL SECOND";
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
107 } else if (s.equals("MONTH_INTERVAL")) {
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
108 s = "INTERVAL MONTH";
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
109 } else if (s.equals("TIMETZ")) {
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
110 s = "TIME";
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
111 // small hack to get desired behaviour: set digits when we have
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
112 // a time with time zone and at the same time masking the internal types
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
113 digits = 1;
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
114 } else if (s.equals("TIMESTAMPTZ")) {
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
115 s = "TIMESTAMP";
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
116 // small hack to get desired behaviour: set digits when we have
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
117 // a timestamp with time zone and at the same time masking the internal types
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
118 digits = 1;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
119 }
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
120 sb.append(s); // add the data type for this column
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
121
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
122 int ctype = cols.getInt(datatypeIndex);
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
123 int size = cols.getInt(sizeIndex);
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
124 // do some SQL/MonetDB type length/precision and scale specifics
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
125 switch (ctype) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
126 case Types.CHAR:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
127 case Types.VARCHAR:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
128 case Types.LONGVARCHAR:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
129 case Types.CLOB:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
130 case Types.BLOB:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
131 case Types.FLOAT:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
132 if (size > 0)
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
133 sb.append('(').append(size).append(')');
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
134 break;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
135 case Types.TIME:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
136 case Types.TIMESTAMP:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
137 if (size > 1)
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
138 sb.append('(').append(size -1).append(')');
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
139 if (digits == 1) // flag is set to include suffix: WITH TIME ZONE
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
140 sb.append(" WITH TIME ZONE");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
141 break;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
142 case Types.DECIMAL:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
143 case Types.NUMERIC:
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
144 sb.append('(').append(size);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
145 if (digits != 0)
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
146 sb.append(',').append(digits);
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
147 sb.append(')');
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
148 break;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
149 }
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
150
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
151 boolean isNotNull = cols.getInt(isNotNullIndex) == DatabaseMetaData.columnNoNulls;
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
152 String defaultValue = cols.getString(defaultValueIndex);
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
153 boolean hasDefault = (defaultValue != null && !defaultValue.isEmpty());
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
154 if (isNotNull || hasDefault) {
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
155 final int spaces = typewidth - sb.length();
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
156 if (spaces > 0)
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
157 sb.append(repeat(' ', spaces));
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
158 if (isNotNull)
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
159 sb.append(" NOT NULL");
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
160 if (hasDefault)
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
161 sb.append(" DEFAULT ").append(defaultValue);
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
162 }
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
163
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
164 // print column data type, optional length and scale, optional NOT NULL, optional DEFAULT value
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
165 out.print(sb.toString());
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
166
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
167 sb.setLength(0); // clear the buffer for next column
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
168 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
169 cols.close();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
170
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
171 // add the primary key constraint definition
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
172 // unfortunately some idiot defined that getPrimaryKeys()
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
173 // returns the primary key columns sorted by column name, not
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
174 // key sequence order. So we have to sort ourself :(
315
4793f9b80bb3 Removed the catalog parameter from the Exporter.dumpSchema method as it is always null for MonetDB.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 314
diff changeset
175 cols = dbmd.getPrimaryKeys(null, schema, name);
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
176 int colKeySeq = cols.findColumn("KEY_SEQ");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
177 // first make a 'index' of the KEY_SEQ columns
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
178 final java.util.SortedMap<Integer, Integer> seqIndex = new java.util.TreeMap<Integer, Integer>();
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
179 for (i = 1; cols.next(); i++) {
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
180 seqIndex.put(Integer.valueOf(cols.getInt(colKeySeq)), Integer.valueOf(i));
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
181 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
182 if (seqIndex.size() > 0) {
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
183 cols.absolute(1); // reset to first pk column row
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
184 // terminate the previous line
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
185 out.println(",");
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
186 out.print("\tCONSTRAINT " + dq(cols.getString("PK_NAME")) + " PRIMARY KEY (");
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
187
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
188 colNmIndex = cols.findColumn("COLUMN_NAME");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
189 final Iterator<Map.Entry<Integer, Integer>> it = seqIndex.entrySet().iterator();
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
190 for (i = 0; it.hasNext(); i++) {
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
191 final Map.Entry<Integer, Integer> e = it.next();
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
192 cols.absolute(e.getValue().intValue());
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
193 if (i > 0)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
194 out.print(", ");
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
195 out.print(dq(cols.getString(colNmIndex)));
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
196 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
197 out.print(")");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
198 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
199 cols.close();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
200
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
201 // add unique constraint definitions
306
7c79ef41b840 Undo change cf372fae2adb. The problem was in the dump code in JdbcClient.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
202 // we use getIndexInfo to get unique indexes, but need to exclude
7c79ef41b840 Undo change cf372fae2adb. The problem was in the dump code in JdbcClient.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
203 // the indexes which are generated by the system for pkey constraints
315
4793f9b80bb3 Removed the catalog parameter from the Exporter.dumpSchema method as it is always null for MonetDB.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 314
diff changeset
204 cols = dbmd.getIndexInfo(null, schema, name, true, true);
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
205 int colIndexNm = cols.findColumn("INDEX_NAME");
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
206 colNmIndex = cols.findColumn("COLUMN_NAME");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
207 while (cols.next()) {
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
208 final String idxname = cols.getString(colIndexNm);
306
7c79ef41b840 Undo change cf372fae2adb. The problem was in the dump code in JdbcClient.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
209 if (idxname != null && !idxname.endsWith("_pkey")) {
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
210 out.println(",");
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
211 out.print("\tCONSTRAINT " + dq(idxname) + " UNIQUE (" + dq(cols.getString(colNmIndex)));
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
212
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
213 boolean next;
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
214 while ((next = cols.next()) && idxname.equals(cols.getString(colIndexNm))) {
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
215 out.print(", " + dq(cols.getString(colNmIndex)));
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
216 }
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
217 // go back one, we've gone one too far
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
218 if (next)
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
219 cols.previous();
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
220
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
221 out.print(")");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
222 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
223 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
224 cols.close();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
225
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
226 // add foreign keys definitions
315
4793f9b80bb3 Removed the catalog parameter from the Exporter.dumpSchema method as it is always null for MonetDB.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 314
diff changeset
227 cols = dbmd.getImportedKeys(null, schema, name);
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
228 final int colFkNm = cols.findColumn("FK_NAME");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
229 final int colFkColNm = cols.findColumn("FKCOLUMN_NAME");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
230 final int colPkColNm = cols.findColumn("PKCOLUMN_NAME");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
231 colKeySeq = cols.findColumn("KEY_SEQ");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
232 final int colPkTblSch = cols.findColumn("PKTABLE_SCHEM");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
233 final int colPkTblNm = cols.findColumn("PKTABLE_NAME");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
234 while (cols.next()) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
235 out.println(",");
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
236 out.print("\tCONSTRAINT " + dq(cols.getString(colFkNm)) + " FOREIGN KEY (");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
237
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
238 final Set<String> fk = new LinkedHashSet<String>();
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
239 fk.add(cols.getString(colFkColNm).intern());
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
240 final Set<String> pk = new LinkedHashSet<String>();
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
241 pk.add(cols.getString(colPkColNm).intern());
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
242
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
243 boolean next;
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
244 while ((next = cols.next()) && cols.getInt(colKeySeq) != 1) {
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
245 fk.add(cols.getString(colFkColNm).intern());
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
246 pk.add(cols.getString(colPkColNm ).intern());
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
247 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
248 // go back one
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
249 if (next)
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
250 cols.previous();
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
251
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
252 Iterator<String> it = fk.iterator();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
253 for (i = 0; it.hasNext(); i++) {
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
254 if (i > 0)
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
255 out.print(", ");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
256 out.print(dq(it.next()));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
257 }
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
258 out.print(") REFERENCES " + dq(cols.getString(colPkTblSch)) + "." + dq(cols.getString(colPkTblNm)) + " (");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
259 it = pk.iterator();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
260 for (i = 0; it.hasNext(); i++) {
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
261 if (i > 0)
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
262 out.print(", ");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
263 out.print(dq(it.next()));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
264 }
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
265 out.print(")");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
266 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
267 cols.close();
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
268
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
269 out.println();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
270 // end the create table statement
318
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
271 if (type.equals("REMOTE TABLE")) {
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
272 final String on_clause = fetchSysTablesQueryValue(dbmd.getConnection(), schema, name);
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
273 out.println(") ON '" + ((on_clause != null) ? on_clause : "!!missing mapi:monetdb:// spec") + "';");
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
274 } else
13a9d5316e37 Corrected JdbcClient program: the dumping of a view DDL was incorrect when the view creation statement was not starting with "create view ", such as when it was created using "CREATE VIEW" or "create or replace view".
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 315
diff changeset
275 out.println(");");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
276
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
277 // create the non unique indexes defined for this table
306
7c79ef41b840 Undo change cf372fae2adb. The problem was in the dump code in JdbcClient.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
278 // we use getIndexInfo to get non-unique indexes, but need to exclude
7c79ef41b840 Undo change cf372fae2adb. The problem was in the dump code in JdbcClient.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
279 // the indexes which are generated by the system for fkey constraints
7c79ef41b840 Undo change cf372fae2adb. The problem was in the dump code in JdbcClient.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
280 // (and pkey and unique constraints but those are marked as unique and not requested)
315
4793f9b80bb3 Removed the catalog parameter from the Exporter.dumpSchema method as it is always null for MonetDB.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 314
diff changeset
281 cols = dbmd.getIndexInfo(null, schema, name, false, true);
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
282 colIndexNm = cols.findColumn("INDEX_NAME");
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
283 colNmIndex = cols.findColumn("COLUMN_NAME");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
284 final int tblNmIndex = cols.findColumn("TABLE_NAME");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
285 final int tblSchIndex = cols.findColumn("TABLE_SCHEM");
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
286 final int nonUniqIndex = cols.findColumn("NON_UNIQUE");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
287 while (cols.next()) {
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
288 if (cols.getBoolean(nonUniqIndex)) {
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
289 // We only process non-unique indexes here.
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
290 // The unique indexes are already covered as UNIQUE constraints in the CREATE TABLE above
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
291 final String idxname = cols.getString(colIndexNm);
306
7c79ef41b840 Undo change cf372fae2adb. The problem was in the dump code in JdbcClient.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
292 if (idxname != null && !idxname.endsWith("_fkey")) {
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
293 out.print("CREATE INDEX " + dq(idxname) + " ON " +
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
294 dq(cols.getString(tblSchIndex)) + "." +
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
295 dq(cols.getString(tblNmIndex)) + " (" +
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
296 dq(cols.getString(colNmIndex)));
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
297
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
298 boolean next;
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
299 while ((next = cols.next()) && idxname.equals(cols.getString(colIndexNm))) {
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
300 out.print(", " + dq(cols.getString(colNmIndex)));
5
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
301 }
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
302 // go back one
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
303 if (next)
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
304 cols.previous();
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
305
b07b4940c3ed Improve output of table definition dump to match output of mclient table definition dump
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
306 out.println(");");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
307 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
308 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
309 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
310 cols.close();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
311 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
312
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
313 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
314 * Dumps the given ResultSet as specified in the form variable.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
315 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
316 * @param rs the ResultSet to dump
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
317 * @throws SQLException if a database error occurs
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
318 */
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
319 public void dumpResultSet(final ResultSet rs) throws SQLException {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
320 switch (outputMode) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
321 case VALUE_INSERT:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
322 resultSetToSQL(rs);
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
323 break;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
324 case VALUE_COPY:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
325 resultSetToSQLDump(rs);
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
326 break;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
327 case VALUE_TABLE:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
328 resultSetToTable(rs);
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
329 break;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
330 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
331 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
332
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
333 public void setProperty(final int type, final int value) throws Exception {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
334 switch (type) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
335 case TYPE_OUTPUT:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
336 switch (value) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
337 case VALUE_INSERT:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
338 case VALUE_COPY:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
339 case VALUE_TABLE:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
340 outputMode = value;
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
341 break;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
342 default:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
343 throw new Exception("Illegal value " + value + " for TYPE_OUTPUT");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
344 }
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
345 break;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
346 default:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
347 throw new Exception("Illegal type " + type);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
348 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
349 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
350
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
351 public int getProperty(final int type) throws Exception {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
352 switch (type) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
353 case TYPE_OUTPUT:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
354 return outputMode;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
355 default:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
356 throw new Exception("Illegal type " + type);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
357 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
358 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
359
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
360 private static final short AS_IS = 0;
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
361 private static final short QUOTE = 1;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
362
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
363 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
364 * Helper method to dump the contents of a table in SQL INSERT INTO
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
365 * format.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
366 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
367 * @param rs the ResultSet to convert into INSERT INTO statements
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
368 * @param absolute if true, dumps table name prepended with schema name
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
369 * @throws SQLException if a database related error occurs
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
370 */
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
371 private void resultSetToSQL(final ResultSet rs)
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
372 throws SQLException
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
373 {
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
374 final ResultSetMetaData rsmd = rs.getMetaData();
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
375 final int cols = rsmd.getColumnCount();
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
376 // get for each output column whether it requires quotes around the value based on data type
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
377 final short[] types = new short[cols +1];
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
378 for (int i = 1; i <= cols; i++) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
379 switch (rsmd.getColumnType(i)) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
380 case Types.CHAR:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
381 case Types.VARCHAR:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
382 case Types.LONGVARCHAR:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
383 case Types.CLOB:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
384 case Types.BLOB:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
385 case Types.DATE:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
386 case Types.TIME:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
387 case Types.TIMESTAMP:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
388 types[i] = QUOTE;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
389 break;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
390 case Types.NUMERIC:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
391 case Types.DECIMAL:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
392 case Types.BIT: // we don't use type BIT, it's here for completeness
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
393 case Types.BOOLEAN:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
394 case Types.TINYINT:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
395 case Types.SMALLINT:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
396 case Types.INTEGER:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
397 case Types.BIGINT:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
398 case Types.REAL:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
399 case Types.FLOAT:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
400 case Types.DOUBLE:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
401 types[i] = AS_IS;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
402 break;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
403 default:
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
404 // treat all other types (such as inet,url,json,objects) as complex types requiring quotes
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
405 types[i] = QUOTE;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
406 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
407 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
408
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
409 final StringBuilder strbuf = new StringBuilder(1024);
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
410 strbuf.append("INSERT INTO ");
313
f2750ea9f93d When exporting table/view definition always include the schema name.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 312
diff changeset
411 final String schema = rsmd.getSchemaName(1);
f2750ea9f93d When exporting table/view definition always include the schema name.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 312
diff changeset
412 if (schema != null && !schema.isEmpty())
f2750ea9f93d When exporting table/view definition always include the schema name.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 312
diff changeset
413 strbuf.append(dq(schema)).append(".");
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
414 strbuf.append(dq(rsmd.getTableName(1))).append(" VALUES (");
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
415 final int cmdpart = strbuf.length();
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
416
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
417 while (rs.next()) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
418 for (int i = 1; i <= cols; i++) {
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
419 final String val = rs.getString(i);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
420 if (i > 1)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
421 strbuf.append(", ");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
422 if (val == null || rs.wasNull()) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
423 strbuf.append("NULL");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
424 } else {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
425 strbuf.append((types[i] == QUOTE) ? q(val) : val);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
426 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
427 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
428 strbuf.append(");");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
429 out.println(strbuf.toString());
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
430 // clear the variable part of the buffer contents for next data row
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
431 strbuf.setLength(cmdpart);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
432 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
433 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
434
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
435 public void resultSetToSQLDump(final ResultSet rs) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
436 // TODO: write copy into statement
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
437 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
438
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
439 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
440 * Helper method to write a ResultSet in a convenient table format
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
441 * to the output writer.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
442 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
443 * @param rs the ResultSet to write out
194
1296dbcc4958 Resolved javadoc many errors and warnings, such as:
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
444 * @throws SQLException if a database related error occurs
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
445 */
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
446 public void resultSetToTable(final ResultSet rs) throws SQLException {
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
447 final ResultSetMetaData md = rs.getMetaData();
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
448 final int cols = md.getColumnCount();
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
449 // find the optimal display widths of the columns
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
450 final int[] width = new int[cols + 1];
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
451 final boolean[] isSigned = new boolean[cols + 1]; // used for controlling left or right alignment of data
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
452 for (int j = 1; j < width.length; j++) {
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
453 final int coldisplaysize = md.getColumnDisplaySize(j);
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
454 final int collabellength = md.getColumnLabel(j).length();
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
455 final int maxwidth = (coldisplaysize > collabellength) ? coldisplaysize : collabellength;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
456 // the minimum width should be 4 to represent: "NULL"
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
457 width[j] = (maxwidth > 4) ? maxwidth : 4;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
458 isSigned[j] = md.isSigned(j);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
459 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
460
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
461 // use a buffer to construct the text lines
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
462 final StringBuilder strbuf = new StringBuilder(1024);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
463
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
464 // construct the frame lines and header text
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
465 strbuf.append('+');
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
466 for (int j = 1; j < width.length; j++)
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
467 strbuf.append(repeat('-', width[j] + 1)).append("-+");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
468
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
469 final String outsideLine = strbuf.toString();
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
470
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
471 strbuf.setLength(0); // clear the buffer
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
472 strbuf.append('|');
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
473 for (int j = 1; j < width.length; j++) {
309
8a96a4a13528 Add additional "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 307
diff changeset
474 final String colLabel = md.getColumnLabel(j);
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
475 strbuf.append(' ').append(colLabel);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
476 strbuf.append(repeat(' ', width[j] - colLabel.length()));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
477 strbuf.append(" |");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
478 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
479 // print the header text
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
480 out.println(outsideLine);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
481 out.println(strbuf.toString());
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
482 out.println(outsideLine.replace('-', '='));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
483
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
484 // print formatted data of each row from resultset
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
485 long count = 0;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
486 for (; rs.next(); count++) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
487 strbuf.setLength(0); // clear the buffer
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
488 strbuf.append('|');
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
489 for (int j = 1; j < width.length; j++) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
490 String data = rs.getString(j);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
491 if (data == null || rs.wasNull()) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
492 data = "NULL";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
493 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
494
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
495 int filler_length = width[j] - data.length();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
496 if (filler_length <= 0) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
497 if (filler_length == 0) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
498 strbuf.append(' ');
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
499 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
500 strbuf.append(data);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
501 } else {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
502 strbuf.append(' ');
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
503 if (isSigned[j]) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
504 // we have a numeric type here, right align
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
505 strbuf.append(repeat(' ', filler_length));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
506 strbuf.append(data);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
507 } else {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
508 // all other left align
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
509 strbuf.append(data);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
510 strbuf.append(repeat(' ', filler_length));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
511 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
512 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
513 strbuf.append(" |");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
514 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
515 out.println(strbuf.toString());
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
516 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
517
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
518 // print the footer text
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
519 out.println(outsideLine);
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
520 out.print(count);
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
521 out.println((count != 1) ? " rows" : " row");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
522 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
523
307
05549bc7ed26 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 306
diff changeset
524 private void changeSchema(final String schema) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
525 if (lastSchema == null) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
526 lastSchema = new Stack<String>();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
527 lastSchema.push(null);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
528 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
529
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
530 if (!schema.equals(lastSchema.peek())) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
531 if (!lastSchema.contains(schema)) {
312
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
532 // do not generate CREATE SCHEMA cmds for existing system schemas
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
533 if (!schema.equals("sys")
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
534 && !schema.equals("tmp")
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
535 && !schema.equals("json")
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
536 && !schema.equals("profiler")
374
31e79dd9e658 Add 3 new system schemas for which no CREATE SCHEMA command should to be generated.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
537 && !schema.equals("wlc") // added in Nov2019
31e79dd9e658 Add 3 new system schemas for which no CREATE SCHEMA command should to be generated.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
538 && !schema.equals("wlr") // added in Nov2019
31e79dd9e658 Add 3 new system schemas for which no CREATE SCHEMA command should to be generated.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
539 && !schema.equals("logging") // added in Jun2020
312
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
540 && !schema.equals("bam")) {
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
541 // create schema
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
542 out.print("CREATE SCHEMA ");
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
543 out.print(dq(schema));
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
544 out.println(";\n");
df815a403bf4 Improved JdbcClient program by including the schema prefix when dumping schema objects. It now behaves more similar to mclient -D -N.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 311
diff changeset
545 }
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
546 lastSchema.push(schema);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
547 }
314
6d98a1d3af00 Optimize and improve SQL and XML exporters
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 313
diff changeset
548
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
549 out.print("SET SCHEMA ");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
550 out.print(dq(schema));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
551 out.println(";\n");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
552 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
553 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
554 }