view src/main/java/nl/cwi/monetdb/util/Exporter.java @ 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". It now uses the same DDL text as when it was created, pulled from sys.tables.query, similar to mclient. Also corrected the missing ON clause part when dumping a REMOTE TABLE definition.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Wed, 04 Sep 2019 17:19:08 +0200 (2019-09-04)
parents 4793f9b80bb3
children 54137aeb1f92
line wrap: on
line source
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
 */

package nl.cwi.monetdb.util;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

public abstract class Exporter {
	protected PrintWriter out;
	protected boolean useSchema;

	protected Exporter(final PrintWriter out) {
		this.out = out;
	}

	public abstract void dumpSchema(
			final java.sql.DatabaseMetaData dbmd,
			final String type,
			final String schema,
			final String name) throws SQLException;

	public abstract void dumpResultSet(final ResultSet rs) throws SQLException;

	public abstract void setProperty(final int type, final int value) throws Exception;
	public abstract int getProperty(final int type) throws Exception;


	//=== shared utilities

	public final void useSchemas(final boolean use) {
		useSchema = use;
	}

	/**
	 * returns the given string between two double quotes for usage as
	 * identifier such as column or table or schema name in SQL queries.
	 *
	 * @param in the string to quote
	 * @return the quoted string
	 */
	protected static final String dq(final String in) {
		return "\"" + in.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"") + "\"";
	}

	/**
	 * returns the given string between two single quotes for usage as
	 * string literal in SQL queries.
	 *
	 * @param in the string to quote
	 * @return the quoted string
	 */
	protected static final String q(final String in) {
		return "'" + in.replaceAll("\\\\", "\\\\\\\\").replaceAll("'", "\\\\'") + "'";
	}

	/**
	 * Simple helper function to repeat a given character a number of times.
	 *
	 * @param chr the character to repeat
	 * @param cnt the number of times to repeat chr
	 * @return a String holding cnt times chr
	 */
	protected static final String repeat(final char chr, final int cnt) {
		final char[] buf = new char[cnt];
		java.util.Arrays.fill(buf, chr);
		return new String(buf);
	}

	/**
	 * Utility method to fetch the "query" value from sys.tables for a specific view or table in a specific schema
	 * The "query" value contains the original SQL view creation text or the ON clause text when it is a REMOTE TABLE
	 *
	 * @param con the JDBC connection, may not be null
	 * @param schema the schem name, may not be null or empty
	 * @param name the view or table name, may not be null or empty
	 * @return the value of the "query" field for the specified view/table name and schema. It can return null.
	 */
	protected static final String fetchSysTablesQueryValue(
		final java.sql.Connection con,
		final String schema,
		final String name)
	{
		java.sql.Statement stmt = null;
		ResultSet rs = null;
		String val = null;
		try {
			stmt = con.createStatement();
			final String cmd = "SELECT query FROM sys.tables WHERE name = '" + name
				+ "' and schema_id IN (SELECT id FROM sys.schemas WHERE name = '" + schema + "')";
			rs = stmt.executeQuery(cmd);
			if (rs != null) {
				if (rs.next()) {
					val = rs.getString(1);
				}
			}
		} catch (SQLException se) {
			/* ignore */
		} finally {
			// free resources
			if (rs != null) {
				try { rs.close(); } catch (SQLException se) { /* ignore */ }
			}
			if (stmt != null) {
				try { stmt.close(); } catch (SQLException se) { /* ignore */ }
			}
		}
		return val;
	}
}