Mercurial > hg > monetdb-java
comparison src/main/java/org/monetdb/util/Exporter.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/Exporter.java@54137aeb1f92 |
children | bf9f6b6ecf40 |
comparison
equal
deleted
inserted
replaced
390:6199e0be3c6e | 391:f523727db392 |
---|---|
1 /* | |
2 * This Source Code Form is subject to the terms of the Mozilla Public | |
3 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
5 * | |
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V. | |
7 */ | |
8 | |
9 package org.monetdb.util; | |
10 | |
11 import java.io.PrintWriter; | |
12 import java.sql.ResultSet; | |
13 import java.sql.SQLException; | |
14 | |
15 public abstract class Exporter { | |
16 protected PrintWriter out; | |
17 protected boolean useSchema; | |
18 | |
19 protected Exporter(final PrintWriter out) { | |
20 this.out = out; | |
21 } | |
22 | |
23 public abstract void dumpSchema( | |
24 final java.sql.DatabaseMetaData dbmd, | |
25 final String type, | |
26 final String schema, | |
27 final String name) throws SQLException; | |
28 | |
29 public abstract void dumpResultSet(final ResultSet rs) throws SQLException; | |
30 | |
31 public abstract void setProperty(final int type, final int value) throws Exception; | |
32 public abstract int getProperty(final int type) throws Exception; | |
33 | |
34 | |
35 //=== shared utilities | |
36 | |
37 public final void useSchemas(final boolean use) { | |
38 useSchema = use; | |
39 } | |
40 | |
41 /** | |
42 * returns the given string between two double quotes for usage as | |
43 * identifier such as column or table or schema name in SQL queries. | |
44 * | |
45 * @param in the string to quote | |
46 * @return the quoted string | |
47 */ | |
48 protected static final String dq(final String in) { | |
49 return "\"" + in.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"") + "\""; | |
50 } | |
51 | |
52 /** | |
53 * returns the given string between two single quotes for usage as | |
54 * string literal in SQL queries. | |
55 * | |
56 * @param in the string to quote | |
57 * @return the quoted string | |
58 */ | |
59 protected static final String q(final String in) { | |
60 return "'" + in.replaceAll("\\\\", "\\\\\\\\").replaceAll("'", "\\\\'") + "'"; | |
61 } | |
62 | |
63 /** | |
64 * Simple helper function to repeat a given character a number of times. | |
65 * | |
66 * @param chr the character to repeat | |
67 * @param cnt the number of times to repeat chr | |
68 * @return a String holding cnt times chr | |
69 */ | |
70 protected static final String repeat(final char chr, final int cnt) { | |
71 final char[] buf = new char[cnt]; | |
72 java.util.Arrays.fill(buf, chr); | |
73 return new String(buf); | |
74 } | |
75 | |
76 /** | |
77 * Utility method to fetch the "query" value from sys.tables for a specific view or table in a specific schema | |
78 * The "query" value contains the original SQL view creation text or the ON clause text when it is a REMOTE TABLE | |
79 * | |
80 * @param con the JDBC connection, may not be null | |
81 * @param schema the schem name, may not be null or empty | |
82 * @param name the view or table name, may not be null or empty | |
83 * @return the value of the "query" field for the specified view/table name and schema. It can return null. | |
84 */ | |
85 protected static final String fetchSysTablesQueryValue( | |
86 final java.sql.Connection con, | |
87 final String schema, | |
88 final String name) | |
89 { | |
90 java.sql.Statement stmt = null; | |
91 ResultSet rs = null; | |
92 String val = null; | |
93 try { | |
94 stmt = con.createStatement(); | |
95 final String cmd = "SELECT query FROM sys.tables WHERE name = '" + name | |
96 + "' and schema_id IN (SELECT id FROM sys.schemas WHERE name = '" + schema + "')"; | |
97 rs = stmt.executeQuery(cmd); | |
98 if (rs != null) { | |
99 if (rs.next()) { | |
100 val = rs.getString(1); | |
101 } | |
102 } | |
103 } catch (SQLException se) { | |
104 /* ignore */ | |
105 } finally { | |
106 // free resources | |
107 if (rs != null) { | |
108 try { rs.close(); } catch (SQLException se) { /* ignore */ } | |
109 } | |
110 if (stmt != null) { | |
111 try { stmt.close(); } catch (SQLException se) { /* ignore */ } | |
112 } | |
113 } | |
114 return val; | |
115 } | |
116 } |