001: package net.sourceforge.squirrel_sql.plugins.sqlscript.table_script;
002:
003: import java.util.Hashtable;
004:
005: import net.sourceforge.squirrel_sql.client.session.ISession;
006: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
007: import net.sourceforge.squirrel_sql.plugins.sqlscript.prefs.SQLScriptPreferenceBean;
008: import net.sourceforge.squirrel_sql.plugins.sqlscript.prefs.SQLScriptPreferencesManager;
009:
010: public class ScriptUtil {
011:
012: private static SQLScriptPreferenceBean prefs = SQLScriptPreferencesManager
013: .getPreferences();
014:
015: Hashtable<String, String> _uniqueColNames = new Hashtable<String, String>();
016:
017: /**
018: * This method provides unique column names.
019: * Use a new instance of this class for
020: * every meta data result set
021: */
022: public String getColumnDef(String sColumnName, String sType,
023: int columnSize, int decimalDigits) {
024: String decimalDigitsString = 0 == decimalDigits ? "" : ","
025: + decimalDigits;
026:
027: sColumnName = makeColumnNameUnique(sColumnName);
028:
029: StringBuffer sbColDef = new StringBuffer();
030: String sLower = sType.toLowerCase();
031: sbColDef.append(sColumnName).append(" ");
032: sbColDef.append(sType);
033:
034: if (sLower.indexOf("char") != -1) {
035: sbColDef.append("(");
036: sbColDef.append(columnSize).append(decimalDigitsString);
037: sbColDef.append(")");
038: } else if (sLower.equals("numeric")) {
039: sbColDef.append("(");
040: sbColDef.append(columnSize).append(decimalDigitsString);
041: sbColDef.append(")");
042: } else if (sLower.equals("number")) {
043: sbColDef.append("(");
044: sbColDef.append(columnSize).append(decimalDigitsString);
045: sbColDef.append(")");
046: } else if (sLower.equals("decimal")) {
047: sbColDef.append("(");
048: sbColDef.append(columnSize).append(decimalDigitsString);
049: sbColDef.append(")");
050: }
051:
052: return sbColDef.toString();
053: }
054:
055: /**
056: * This method provides unique column names.
057: * Use a new instance of this class for
058: * every meta data result set.
059: *
060: *
061: */
062: public String makeColumnNameUnique(String sColumnName) {
063: return makeColumnNameUniqueIntern(sColumnName, 0);
064: }
065:
066: private String makeColumnNameUniqueIntern(String sColumnName,
067: int postFixSeed) {
068: String upperCaseColumnName = sColumnName.toUpperCase();
069: String sRet = sColumnName;
070:
071: if (0 < postFixSeed) {
072: sRet += "_" + postFixSeed;
073: upperCaseColumnName += "_" + postFixSeed;
074: }
075:
076: if (null == _uniqueColNames.get(upperCaseColumnName)) {
077: _uniqueColNames.put(upperCaseColumnName,
078: upperCaseColumnName);
079: return sRet;
080: } else {
081: return makeColumnNameUniqueIntern(sColumnName,
082: ++postFixSeed);
083: }
084: }
085:
086: public static String getStatementSeparator(ISession session) {
087: String statementSeparator = session.getQueryTokenizer()
088: .getSQLStatementSeparator();
089:
090: if (1 < statementSeparator.length()) {
091: statementSeparator = "\n" + statementSeparator + "\n";
092: }
093:
094: return statementSeparator;
095: }
096:
097: /**
098: * Use specified preferences to decide whether or not to "qualify" the
099: * specified table with it's schema/catalog. If schema is not available then
100: * catalog will be used. If neither is available, then only the table name
101: * will be used.
102: *
103: * @param prefs
104: * @param ti
105: * @return
106: */
107: public static String getTableName(ITableInfo ti) {
108: String result = ti.getSimpleName();
109: if (prefs.isQualifyTableNames()) {
110: result = ti.getQualifiedName();
111: }
112: return result;
113: }
114:
115: }
|