⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dblook.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*   Derby - Class org.apache.derby.tools.dblook   Copyright 2003, 2004 The Apache Software Foundation or its licensors, as applicable.   Licensed under the Apache License, Version 2.0 (the "License");   you may not use this file except in compliance with the License.   You may obtain a copy of the License at      http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License. */package org.apache.derby.tools;import java.io.BufferedReader;import java.io.StringReader;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Connection;import java.sql.Statement;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.SQLWarning;import java.sql.Timestamp;import java.util.HashMap;import java.util.StringTokenizer;import java.util.ArrayList;import org.apache.derby.iapi.tools.i18n.LocalizedResource;import org.apache.derby.impl.tools.dblook.DB_Check;import org.apache.derby.impl.tools.dblook.DB_Index;import org.apache.derby.impl.tools.dblook.DB_Jar;import org.apache.derby.impl.tools.dblook.DB_Key;import org.apache.derby.impl.tools.dblook.DB_Table;import org.apache.derby.impl.tools.dblook.DB_Schema;import org.apache.derby.impl.tools.dblook.DB_Alias;import org.apache.derby.impl.tools.dblook.DB_Trigger;import org.apache.derby.impl.tools.dblook.DB_View;import org.apache.derby.impl.tools.dblook.Logs;public class dblook {	// DB2 enforces a maximum of 30 tables to be specified as part of	// the table list.	public static final int DB2_MAX_NUMBER_OF_TABLES = 30;	private Connection conn;	private static PreparedStatement getColNameFromNumberQuery;	// Mappings from id to name for schemas and tables (for ease	// of reference).	protected static HashMap schemaMap;	protected static HashMap tableIdToNameMap;	// Command-line Parameters.	protected static String sourceDBUrl;	protected static String ddlFileName;	protected static String stmtDelimiter;	protected static boolean appendLogs;	protected static ArrayList tableList;	protected static String schemaParam;	protected static String targetSchema;	protected static boolean skipViews;	protected static boolean verbose;	private static String sourceDBName;	private static String lookLogName = "dblook.log";	private final static String DEFAULT_LOCALE= "en";	private final static String DEFAULT_LOCALE_COUNTRY="US";	private static LocalizedResource langUtil;	/* ************************************************	 * main:	 * Initialize program state by creating a dblook object,	 * and then start the DDL generation by calling "go".	 * ****/	public static void main(String[] args) {		try {			new dblook(args);		} catch (Exception e) {		// All "normal" errors are logged and printed to		// console according to command line arguments,		// so if we get here, something unexpected must		// have happened; print to error stream.			e.printStackTrace();		}	}	/* ************************************************	 * Constructor:	 * Parse the command line, initialize logs, echo program variables,	 * and load the Derby driver.	 * @param args Array of dblook command-line arguments.	 * ****/	public dblook(String[] args) throws Exception {        // Adjust the application in accordance with derby.ui.locale		// and derby.ui.codeset		langUtil = LocalizedResource.getInstance();		// Initialize class variables.		initState();		// Parse the command line.		if (!parseArgs(args)) {			System.out.println(lookupMessage("DBLOOK_Usage"));			return;		}		showVariables();		if (!loadDriver()) {		// Failed when loading the driver.  We already logged		// the exception, so just return.			return;		}		schemaMap = new HashMap();		tableIdToNameMap = new HashMap();		// Now run the utility.		go();	}	/* ************************************************	 * initState:	 * Initialize class variables.	 ****/	private void initState() {		sourceDBUrl = null;		ddlFileName = null;		stmtDelimiter = null;		appendLogs = false;		tableList = null;		targetSchema = null;		schemaParam = null;		skipViews = false;		verbose= false;		sourceDBName = null;		return;	}	/* ************************************************	 * parseArgs:	 * Parse the command-line arguments.	 * @param args args[0] is the url for the source database.	 * @return true if all parameters were loaded and the output	 *  files were successfully created; false otherwise.	 ****/	private boolean parseArgs(String[] args) {		if (args.length < 2)		// must have minimum of 2 args: "-d" and "<dbUrl>".			return false;		int st = 0;		for (int i = 0; i < args.length; i++) {			st = loadParam(args, i);			if (st == -1)				return false;			i = st;		}		if (sourceDBUrl == null) {		// must have at least a database url.			return false;			}		// At this point, all parameters should have been read into		// their respective class variables.  Use those		// variables for some further processing.		// Setup logs.		boolean okay = Logs.initLogs(lookLogName, ddlFileName, appendLogs,		 	verbose, (stmtDelimiter == null ? ";" : stmtDelimiter));		// Get database name.		sourceDBName = extractDBNameFromUrl(sourceDBUrl);		// Set up schema restriction.		if ((schemaParam != null) && (schemaParam.length() > 0) &&			(schemaParam.charAt(0) != '"'))		// not quoted, so upper case, then add quotes.		{			targetSchema = addQuotes(expandDoubleQuotes(				schemaParam.toUpperCase(java.util.Locale.ENGLISH)));		}		else			targetSchema = addQuotes(expandDoubleQuotes(stripQuotes(schemaParam)));		return okay;	}	/* ************************************************	 * loadParam:	 * Read in a flag and its corresponding values from	 * list of command line arguments, starting at	 * the start'th argument.	 * @return The position of the argument that was	 *  most recently processed.	 ****/	private int loadParam(String [] args, int start) {		if ((args[start].length() == 0) || args[start].charAt(0) != '-')		// starting argument should be a flag; if it's		// not, ignore it.			return start;		boolean haveVal = (args.length > start + 1);		switch (args[start].charAt(1)) {			case 'd':				if (!haveVal)					return -1;				if (args[start].length() == 2) {					sourceDBUrl = stripQuotes(args[++start]);					return start;				}				return -1;			case 'z':				if (!haveVal)					return -1;				if (args[start].length() == 2) {					schemaParam = args[++start];					return start;				}				return -1;			case 't':				if (!haveVal)					return -1;				if (args[start].equals("-td")) {					stmtDelimiter = args[++start];					return start;				}				else if (args[start].equals("-t"))				// list of tables.					return extractTableNamesFromList(args, start+1);				return -1;			case 'o':				if (!haveVal)					return -1;				if ((args[start].length() == 2) && (args[start+1].length() > 0)) {					ddlFileName = args[++start];					return start;				}				return -1;			case 'a':				if (args[start].equals("-append")) {					appendLogs = true;					return start;				}				return -1;			case 'n':				if (args[start].equals("-noview")) {					skipViews = true;					return start;				}				return -1;			case 'v':				if (args[start].equals("-verbose")) {					verbose = true;					return start;				}				return -1;			default:				return -1;		}	}	/* ************************************************	 * loadDriver:	 * Load derby driver.	 * @param precondition sourceDBUrl has been loaded.	 * @return false if anything goes wrong; true otherwise.	 ****/	private boolean loadDriver() {		String derbyDriver = System.getProperty("driver");		if (derbyDriver == null) {			if (sourceDBUrl.indexOf(":net://") != -1)				derbyDriver = "com.ibm.db2.jcc.DB2Driver";			else if (sourceDBUrl.startsWith("jdbc:derby://"))			   derbyDriver = "org.apache.derby.jdbc.ClientDriver";			else				derbyDriver = "org.apache.derby.jdbc.EmbeddedDriver";	    }		try {			Class.forName(derbyDriver).newInstance();	    }		catch (Exception e)		{			Logs.debug(e);			return false;		}		return true;	}	/* ************************************************	 * extractDBNameFromUrl:	 * Given a database url, parse out the actual name	 * of the database.  This is required for creation	 * the DB2JJARS directory (the database name is part	 * of the path to the jar).	 * @param dbUrl The database url from which to extract the	 *  the database name.	 * @return the name of the database (including its	 *  path, if provided) that is referenced by the url.	 ****/	protected String extractDBNameFromUrl(String dbUrl) {		if (dbUrl == null)		// shouldn't happen; ignore it here, as an error		// will be thrown we try to connect.			return "";		int start = dbUrl.indexOf("jdbc:derby:");		if (start == -1)		// not a valid url; just ignore it (an error		// will be thrown when we try to connect).			return "";		start = dbUrl.indexOf("://");		if (start == -1)		// standard url (jdbc:derby:<dbname>).  Database		// name starts right after "derby:".  The "6" in		// the following line is the length of "derby:".			start = dbUrl.indexOf("derby:") + 6;		else		// Network Server url.  Database name starts right		// after next slash (":net://hostname:port/<dbname>).		// The "3" in the following line is the length of		// "://".			start = dbUrl.indexOf("/", start+3) + 1;		int stop = -1;		if (dbUrl.charAt(start) == '"') {		// database name is quoted; end of the name is the		// closing quote.			start++;			stop = dbUrl.indexOf("\"", start);		}		else {		// Database name ends with the start of a list of connection	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -