001: /*
002: * tinySQLGlobals
003: *
004: * $Author: $
005: * $Date: $
006: * $Revision: $
007: *
008: * Static class to hold global values.
009: *
010: * This library is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU Lesser General Public
012: * License as published by the Free Software Foundation; either
013: * version 2.1 of the License, or (at your option) any later version.
014: *
015: * This library is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: * Lesser General Public License for more details.
019: *
020: * You should have received a copy of the GNU Lesser General Public
021: * License along with this library; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023: *
024: * Revision History;
025: *
026: * Written by Davis Swan in October, 2006.
027: */
028:
029: package com.sqlmagic.tinysql;
030:
031: import java.io.*;
032: import java.util.*;
033: import java.text.*;
034:
035: public class tinySQLGlobals {
036: static String dataDir = (String) null;
037: static Vector longColumnNames;
038: static String fileSep = System.getProperty("file.separator");
039: static String newLine = System.getProperty("line.separator");
040: static Hashtable DB_INDEX = new Hashtable();
041: static String VERSION = "2.26h";
042: static boolean DEBUG = false;
043: static boolean PARSER_DEBUG = false;
044: static boolean WHERE_DEBUG = false;
045: static boolean EX_DEBUG = false;
046: static int longNamesInFileCount;
047: static boolean debug = false;
048:
049: public static void readLongNames(String inputDataDir) {
050: String fullPath, longNameRecord;
051: String[] fields;
052: FieldTokenizer ft;
053: File longColumnNameFile;
054: dataDir = inputDataDir;
055: BufferedReader longNameReader = (BufferedReader) null;
056: fullPath = dataDir + fileSep + "TINYSQL_LONG_COLUMN_NAMES.dat";
057: longColumnNames = new Vector();
058: longColumnNameFile = new File(fullPath);
059: if (longColumnNameFile.exists()) {
060: try {
061: longNameReader = new BufferedReader(new FileReader(
062: fullPath));
063: while ((longNameRecord = longNameReader.readLine()) != null) {
064: ft = new FieldTokenizer(longNameRecord, '|', false);
065: fields = ft.getFields();
066: longColumnNames.addElement(fields[0]);
067: longColumnNames.addElement(fields[1]);
068: }
069: longNameReader.close();
070: longNamesInFileCount = longColumnNames.size() / 2;
071: if (debug)
072: System.out.println("Long Names read: "
073: + longNamesInFileCount);
074: } catch (Exception readEx) {
075: System.out.println("Reader exception "
076: + readEx.getMessage());
077: longNamesInFileCount = 0;
078: }
079: }
080: }
081:
082: /*
083: * Method to add a long column name to the global Vector. Note that
084: * the entries are keyed by the short column name so that there is always
085: * one and only one short name for any long name.
086: */
087: public static String addLongName(String inputColumnName) {
088: String shortColumnName, countString;
089: countString = "0000"
090: + Integer.toString(longColumnNames.size() / 2);
091: shortColumnName = "COL"
092: + countString.substring(countString.length() - 5);
093: if (debug)
094: System.out.println("Add " + shortColumnName + "|"
095: + inputColumnName);
096: longColumnNames.addElement(shortColumnName);
097: longColumnNames.addElement(inputColumnName);
098: return shortColumnName;
099: }
100:
101: /*
102: * This method checks for the existence of a short column name for the
103: * input name. If one does not exist it is created.
104: */
105: public static String getShortName(String inputColumnName) {
106: String shortColumnName = (String) null, longColumnName;
107: int i;
108: if (inputColumnName.length() < 12)
109: return inputColumnName;
110: for (i = 0; i < longColumnNames.size(); i += 2) {
111: longColumnName = (String) longColumnNames.elementAt(i + 1);
112: if (longColumnName.equalsIgnoreCase(inputColumnName)) {
113: shortColumnName = (String) longColumnNames.elementAt(i);
114: if (debug)
115: System.out.println("Return " + shortColumnName);
116: return shortColumnName;
117: }
118: }
119: if (shortColumnName == (String) null) {
120: /*
121: * A short name has not been set up for this long name yet.
122: */
123: if (debug)
124: System.out.println("Generate short name for "
125: + inputColumnName);
126: return addLongName(inputColumnName);
127: }
128: return inputColumnName;
129: }
130:
131: /*
132: * Get the long column name for the input short name.
133: */
134: public static String getLongName(String inputColumnName) {
135: String longColumnName, shortColumnName;
136: int i;
137: for (i = 0; i < longColumnNames.size(); i += 2) {
138: shortColumnName = (String) longColumnNames.elementAt(i);
139: if (shortColumnName.equalsIgnoreCase(inputColumnName)) {
140: longColumnName = (String) longColumnNames
141: .elementAt(i + 1);
142: if (debug)
143: System.out.println("Return " + longColumnName);
144: return longColumnName;
145: }
146: }
147: return inputColumnName;
148: }
149:
150: public static void writeLongNames() {
151: FileWriter longNameWriter = (FileWriter) null;
152: String fullPath, longColumnName, shortColumnName;
153: int i;
154: if (longColumnNames.size() > longNamesInFileCount * 2) {
155: /*
156: * The file needs to be updated.
157: */
158: fullPath = dataDir + fileSep
159: + "TINYSQL_LONG_COLUMN_NAMES.dat";
160: try {
161: longNameWriter = new FileWriter(fullPath);
162: if (longNameWriter != (FileWriter) null) {
163: for (i = 0; i < longColumnNames.size(); i += 2) {
164: shortColumnName = (String) longColumnNames
165: .elementAt(i);
166: longColumnName = (String) longColumnNames
167: .elementAt(i + 1);
168: longNameWriter.write(shortColumnName + "|"
169: + longColumnName + newLine);
170: }
171: longNameWriter.close();
172: longNamesInFileCount = longColumnNames.size() / 2;
173: } else {
174: System.out
175: .println("Unable to update long column names.");
176: }
177: } catch (Exception writeEx) {
178: System.out.println("Write exception "
179: + writeEx.getMessage());
180: }
181: }
182: }
183: }
|