001: /*
002: * SourceTableArgument.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.sql.wbcommands;
013:
014: import java.sql.SQLException;
015: import java.util.ArrayList;
016: import java.util.Collections;
017: import java.util.LinkedList;
018: import java.util.List;
019: import workbench.db.TableIdentifier;
020: import workbench.db.WbConnection;
021: import workbench.util.StringUtil;
022: import workbench.util.WbStringTokenizer;
023:
024: /**
025: *
026: * @author support@sql-workbench.net
027: */
028: public class SourceTableArgument {
029: private List<TableIdentifier> tables = new ArrayList<TableIdentifier>();
030: private boolean wildcardsPresent = false;
031:
032: public SourceTableArgument(String argument, WbConnection dbConn)
033: throws SQLException {
034: if (StringUtil.isEmptyString(argument))
035: return;
036: if (dbConn == null)
037: return;
038:
039: List<String> args = getObjectNames(argument);
040: int argCount = args.size();
041:
042: if (argCount <= 0)
043: return;
044:
045: String t = args.get(0);
046:
047: // If only one table argument is present, we'll have to
048: // to check for wildcards e.g. -sourcetable=theschema.*
049: if (argCount == 1
050: && (t.indexOf('*') > -1 || t.indexOf('%') > -1)) {
051: this .wildcardsPresent = true;
052: TableIdentifier tbl = new TableIdentifier(t);
053: if (tbl.getSchema() == null) {
054: tbl.setSchema(dbConn.getMetadata().getSchemaToUse());
055: }
056: tbl.adjustCase(dbConn);
057: List<TableIdentifier> l = dbConn.getMetadata()
058: .getTableList(tbl.getTableName(), tbl.getSchema());
059: this .tables.addAll(l);
060: } else {
061: for (int i = 0; i < argCount; i++) {
062: tables.add(new TableIdentifier(args.get(i)));
063: }
064: }
065: }
066:
067: /**
068: * Returns all DB Object names from the comma separated list.
069: * This is different to stringToList() as it keeps any quotes that
070: * are present in the list.
071: *
072: * @param list a comma separated list of elements (optionally with quotes)
073: * @return a List of Strings as defined by the input string
074: */
075: public List<String> getObjectNames(String list) {
076: if (StringUtil.isEmptyString(list))
077: return Collections.emptyList();
078: WbStringTokenizer tok = new WbStringTokenizer(list, ",");
079: tok.setDelimiterNeedsWhitspace(false);
080: tok.setCheckBrackets(false);
081: tok.setKeepQuotes(true);
082: List<String> result = new LinkedList<String>();
083: while (tok.hasMoreTokens()) {
084: String element = tok.nextToken();
085: if (element == null)
086: continue;
087: element = element.trim();
088: if (element.length() > 0) {
089: result.add(element);
090: }
091: }
092: return result;
093: }
094:
095: public List<TableIdentifier> getTables() {
096: return Collections.unmodifiableList(this .tables);
097: }
098:
099: public boolean wasWildCardArgument() {
100: return this.wildcardsPresent;
101: }
102: }
|