📄 transfersqltext.java
字号:
/* Copyright (c) 2001-2005, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.sql.SQLException;import java.util.Enumeration;import java.util.Hashtable;import java.util.NoSuchElementException;import java.util.StringTokenizer;import java.util.Vector;/** * @author Nicolas BAZIN, INGENICO * @version 1.7.0 */class TransferSQLText extends DataAccessPoint { String sFileName = null; BufferedWriter WTextWrite = null; BufferedReader WTextRead = null; protected boolean StructureAlreadyParsed = false; Hashtable DbStmts = null; protected JDBCTypes JDBCT = null; TransferSQLText(String _FileName, Traceable t) throws DataAccessPointException { super(t); sFileName = _FileName; JDBCT = new JDBCTypes(); if (sFileName == null) { throw new DataAccessPointException("File name not initialized"); } } boolean execute(String statement) throws DataAccessPointException { if (WTextWrite == null) { try { WTextWrite = new BufferedWriter(new FileWriter(sFileName)); } catch (IOException e) { throw new DataAccessPointException(e.getMessage()); } } try { WTextWrite.write(statement + "\n"); WTextWrite.flush(); } catch (IOException e) { throw new DataAccessPointException(e.getMessage()); } return true; } void putData(String statement, TransferResultSet r, int iMaxRows) throws DataAccessPointException { int i = 0; if (r == null) { return; } if (WTextWrite == null) { try { WTextWrite = new BufferedWriter(new FileWriter(sFileName)); } catch (IOException e) { throw new DataAccessPointException(e.getMessage()); } } try { while (r.next()) { if (i == 0) { WTextWrite.write(statement + "\n"); WTextWrite.flush(); } transferRow(r); if (iMaxRows != 0 && i == iMaxRows) { break; } i++; if (iMaxRows != 0 || i % 100 == 0) { tracer.trace("Transfered " + i + " rows"); } } } catch (Exception e) { throw new DataAccessPointException(e.getMessage()); } finally { try { if (i > 0) { WTextWrite.write("\tNumber of Rows=" + i + "\n\n"); WTextWrite.flush(); } } catch (IOException e) { throw new DataAccessPointException(e.getMessage()); } } } void close() throws DataAccessPointException { if (WTextWrite != null) { try { WTextWrite.flush(); WTextWrite.close(); } catch (IOException e) {} } } /** * Method declaration * * * @param type * @param r * @param p * * @throws SQLException */ private void transferRow(TransferResultSet r) throws Exception { String sLast = ""; int len = r.getColumnCount(); if (WTextWrite == null) { try { WTextWrite = new BufferedWriter(new FileWriter(sFileName)); } catch (IOException e) { throw new DataAccessPointException(e.getMessage()); } } for (int i = 0; i < len; i++) { int t = r.getColumnType(i + 1); sLast = "column=" + r.getColumnName(i + 1) + " datatype=" + (String) helper.getSupportedTypes().get(new Integer(t)); Object o = r.getObject(i + 1); if (o == null) { sLast += " value=<null>"; } else { o = helper.convertColumnValue(o, i + 1, t); sLast += " value=\'" + o.toString() + "\'"; } WTextWrite.write("\t" + sLast + "\n"); WTextWrite.flush(); } WTextWrite.write("\n"); WTextWrite.flush(); sLast = ""; } class ColumnDef { String columnName; String columnType; String options; int start; int len; public ColumnDef() { columnName = ""; columnType = ""; options = ""; start = 0; len = 0; } } ColumnDef getColumnDef(String ColumnsDesc, int curPos) { int nextPos = 0; ColumnDef columnDef = new TransferSQLText.ColumnDef(); columnDef.start = curPos; if ((ColumnsDesc == null) || (ColumnsDesc.length() == 0) || (curPos >= ColumnsDesc.length())) { return new TransferSQLText.ColumnDef(); } String stbuff = ColumnsDesc.substring(curPos); try { int i = 0; for (; i < stbuff.length(); i++) { int c = stbuff.charAt(i); if (c == ',' || c == ' ' || c == ')' || c == ';') { continue; } else { break; } } if (i == stbuff.length()) { return new TransferSQLText.ColumnDef(); } columnDef.len += i; stbuff = stbuff.substring(i); while (stbuff.charAt(nextPos) != ' ') { nextPos++; } columnDef.columnName = stbuff.substring(0, nextPos); stbuff = stbuff.substring(nextPos); columnDef.len += nextPos; nextPos = 0; if (!columnDef.columnName.toUpperCase().equals("CONSTRAINT")) { i = 0; for (; i < stbuff.length() && stbuff.charAt(i) == ' '; i++) {} stbuff = stbuff.substring(i); columnDef.len += i; while ((stbuff.charAt(nextPos) != '(') && (stbuff.charAt(nextPos) != ',') && (stbuff.charAt(nextPos) != ')') && (stbuff.charAt(nextPos) != ';') && (stbuff.charAt(nextPos) != ' ')) { nextPos++; } columnDef.columnType = stbuff.substring(0, nextPos).toUpperCase(); stbuff = stbuff.substring(nextPos); columnDef.len += nextPos; nextPos = 0; } while ((stbuff.charAt(nextPos) != ',') && (stbuff.charAt(nextPos) != ';') && (nextPos < stbuff.length()) && (stbuff.charAt(nextPos) != ')')) { if (stbuff.charAt(nextPos) == '(') { while (stbuff.charAt(nextPos) != ')') { nextPos++; } } nextPos++; } columnDef.options = stbuff.substring(0, nextPos); columnDef.len += nextPos; } catch (Exception e) { columnDef = new TransferSQLText.ColumnDef(); } return columnDef; } String translateTypes(String CreateLine, TransferTable TTable, DataAccessPoint Dest) throws DataAccessPointException { String translatedLine = ""; JDBCTypes JDBCT = new JDBCTypes(); int currentPos = 0; String columnName = ""; String columnType = ""; int colnum = 0; ColumnDef cDef; currentPos = CreateLine.indexOf('(') + 1; translatedLine = CreateLine.substring(0, currentPos); do { cDef = getColumnDef(CreateLine, currentPos); if (cDef.len == 0) { break; } columnName = cDef.columnName; columnType = cDef.columnType; if (columnName.toUpperCase().indexOf("CONSTRAINT") >= 0) { translatedLine += CreateLine.substring(currentPos, currentPos + cDef.len) + ","; currentPos += cDef.len + 1; colnum++; continue; } columnName = Dest.helper.formatIdentifier(columnName) + " "; try { Integer inttype = new Integer( Dest.helper.convertToType(JDBCT.toInt(columnType))); columnType = (String) TTable.hTypes.get(inttype); } catch (Exception JDBCtypeEx) {} if (cDef.options != null) { columnType += cDef.options; } try { columnType = Dest.helper.fixupColumnDefWrite(TTable, null, columnType, null, colnum); } catch (SQLException SQLe) { return CreateLine; } translatedLine += columnName + " " + columnType + ","; currentPos += cDef.len + 1; colnum++; } while (true); return translatedLine.substring(0, translatedLine.length() - 1) + ");"; } void parseFileForTables() throws DataAccessPointException { StringTokenizer Tokenizer; if (WTextRead == null) { try { WTextRead = new BufferedReader(new FileReader(sFileName)); } catch (IOException e) { throw new DataAccessPointException(e.getMessage()); } } String currentLine = ""; String Token = ""; String name = ""; TransferTable relatedTable = null; try { while ((currentLine = WTextRead.readLine()) != null) { currentLine = currentLine.trim() + ";"; Tokenizer = new StringTokenizer(currentLine); try { Token = Tokenizer.nextToken(); } catch (NoSuchElementException NSE) { continue; } if (Token == null) { continue; } if (!Token.toUpperCase().equals("CREATE")) { continue; } Token = Tokenizer.nextToken().toUpperCase(); if (Token.equals("TABLE") || Token.equals("VIEW")) { try { name = Tokenizer.nextToken(" (;"); relatedTable = new TransferTable(this, name, "", Token, tracer); relatedTable.Stmts.bCreate = false; relatedTable.Stmts.bDelete = false; relatedTable.Stmts.bDrop = false; relatedTable.Stmts.bCreateIndex = false; relatedTable.Stmts.bDropIndex = false; relatedTable.Stmts.bInsert = false; relatedTable.Stmts.bAlter = false; DbStmts.put(relatedTable.Stmts.sSourceTable, relatedTable); } catch (NoSuchElementException NSE) { continue; } } } } catch (Exception IOe) { throw new DataAccessPointException(IOe.getMessage()); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -