001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/warehouse/api-impl/src/java/org/theospi/portfolio/util/db/MySqlHandler.java $
003: * $Id:MySqlHandler.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.util.db;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.xml.sax.Attributes;
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.Locator;
027: import org.xml.sax.SAXException;
028:
029: /**
030: * MySQL table handler
031: *
032: * @see GenericTableHandler
033: * @author <a href="felipeen@udel.edu">Luis F.C. Mendes</a> - University of Delaware
034: * @version $Revision 1.0 $
035: */
036: public class MySqlHandler implements ContentHandler {
037:
038: private static final int UNSET = -1;
039: private static final int DROP = 0;
040: private static final int CREATE = 1;
041: private static final int ALTER = 2;
042: private static final int INDEX = 3;
043: private static int mode = UNSET;
044: private static StringBuffer stmtBuffer;
045: private int treeLevel;
046: private String tmpType;
047: private String tmpParm;
048: private DbLoader loader;
049:
050: protected final Log logger = LogFactory.getLog(getClass());
051:
052: public MySqlHandler(DbLoader loader) {
053: logger.debug("MySQL table handler...");
054: this .loader = loader;
055: }
056:
057: public void startDocument() {
058: }
059:
060: public void endDocument() {
061: //System.out.println();
062: }
063:
064: public void startElement(String namespaceURI, String localName,
065: String qName, Attributes atts) {
066: if (qName.equals("statement")) {
067: tmpType = "";
068: tmpParm = "";
069: treeLevel = 0;
070: stmtBuffer = new StringBuffer(1024);
071: String statementType = atts.getValue("type");
072:
073: if (mode == UNSET || mode != DROP && statementType != null
074: && statementType.equals("drop")) {
075: mode = DROP;
076:
077: logger.debug("Dropping tables...");
078:
079: if (!this .loader.isDropTables())
080: logger.debug("disabled.");
081: } else if (mode == UNSET || mode != CREATE
082: && statementType != null
083: && statementType.equals("create")) {
084: mode = CREATE;
085:
086: logger.debug("Creating tables...");
087:
088: if (!this .loader.isCreateTables())
089: logger.debug("disabled.");
090: } else if (mode == UNSET || mode != ALTER
091: && statementType != null
092: && statementType.equals("alter")) {
093: mode = ALTER;
094:
095: logger.debug("Altering tables...");
096:
097: if (!this .loader.isAlterTables())
098: logger.debug("disabled.");
099: } else if (mode == UNSET || mode != INDEX
100: && statementType != null
101: && statementType.equals("index")) {
102: mode = INDEX;
103:
104: logger.debug("Indexing tables...");
105:
106: if (!this .loader.isIndexTables())
107: logger.debug("disabled.");
108: }
109:
110: }
111: if (qName.equals("column-type")) {
112: ++treeLevel;
113: tmpType = "";
114: }
115: if (qName.equals("type-param")) {
116: ++treeLevel;
117: tmpParm = "";
118: }
119:
120: }
121:
122: public void endElement(String namespaceURI, String localName,
123: String qName) {
124: if (qName.equals("statement")) {
125: treeLevel = 0;
126: String statement = stmtBuffer.toString();
127: switch (mode) {
128: case DROP:
129: if (this .loader.isDropTables()) {
130: statement += " CASCADE";
131: this .loader.dropTable(statement);
132: }
133: break;
134: case CREATE:
135: if (this .loader.isCreateTables()) {
136: //System.out.println("CREATE TABLES: " + statement);
137: this .loader.createTable(statement);
138: }
139: break;
140: case ALTER:
141: if (this .loader.isAlterTables()) {
142: //System.out.println("ALTER TABLES: " + statement);
143: this .loader.alterTable(statement);
144: }
145: break;
146: case INDEX:
147: if (this .loader.isIndexTables()) {
148: //System.out.println("INDEX TABLES: " + statement);
149: this .loader.indexTable(statement);
150: }
151: break;
152: default:
153: break;
154: }
155: }
156:
157: if (qName.equals("column-type"))
158: --treeLevel;
159: if (qName.equals("type-param"))
160: --treeLevel;
161: if (treeLevel == 0)
162: parseParamToDatabase();
163: }
164:
165: public void characters(char ch[], int start, int length) {
166: if (treeLevel == 0)
167: stmtBuffer.append(ch, start, length);
168: else if (treeLevel == 1)
169: tmpType = new String(ch, start, length);
170: else if (treeLevel == 2)
171: tmpParm = new String(ch, start, length);
172: }
173:
174: private void parseParamToDatabase() {
175: int parm = 0;
176:
177: //check if this datatype is varchar, if > 255, if MySQL
178: if (tmpParm != null && tmpParm.length() > 0) {
179:
180: parm = Integer.parseInt(tmpParm);
181:
182: if (tmpType.equals("VARCHAR")) {
183: if (parm >= 256) {
184: stmtBuffer.append("TEXT");
185: } else {
186: stmtBuffer.append(tmpType.trim());
187: stmtBuffer.append("(" + tmpParm.trim() + ")");
188: }
189: } else {
190: stmtBuffer.append(tmpType.trim());
191: stmtBuffer.append("(" + tmpParm.trim() + ")");
192: }
193: } else {
194: stmtBuffer.append(tmpType.trim());
195: }
196:
197: tmpParm = "";
198: tmpType = "";
199: }
200:
201: public void setDocumentLocator(Locator locator) {
202: }
203:
204: public void processingInstruction(String target, String data) {
205: }
206:
207: public void ignorableWhitespace(char[] ch, int start, int length) {
208: }
209:
210: public void startPrefixMapping(String prefix, String uri)
211: throws SAXException {
212: };
213:
214: public void endPrefixMapping(String prefix) throws SAXException {
215: };
216:
217: public void skippedEntity(String name) throws SAXException {
218: };
219:
220: }
|