001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.util;
032:
033: import java.sql.Types;
034:
035: // sqlbob@users 20020325 - patch 1.7.0 - reengineering
036:
037: /**
038: * Conversions from SQLServer7 databases
039: *
040: * @version 1.7.0
041: */
042: class SqlServerTransferHelper extends TransferHelper {
043:
044: private boolean firstTinyintRow;
045: private boolean firstSmallintRow;
046:
047: SqlServerTransferHelper() {
048: super ();
049: }
050:
051: SqlServerTransferHelper(TransferDb database, Traceable t, String q) {
052: super (database, t, q);
053: }
054:
055: String formatTableName(String t) {
056:
057: if (t == null) {
058: return t;
059: }
060:
061: if (t.equals("")) {
062: return t;
063: }
064:
065: if (t.indexOf(' ') != -1) {
066: return ("[" + t + "]");
067: } else {
068: return (formatIdentifier(t));
069: }
070: }
071:
072: int convertFromType(int type) {
073:
074: // MS SQL 7 specific problems (Northwind database)
075: if (type == 11) {
076: tracer.trace("Converted DATETIME (type 11) to TIMESTAMP");
077:
078: type = Types.TIMESTAMP;
079: } else if (type == -9) {
080: tracer.trace("Converted NVARCHAR (type -9) to VARCHAR");
081:
082: type = Types.VARCHAR;
083: } else if (type == -8) {
084: tracer.trace("Converted NCHAR (type -8) to VARCHAR");
085:
086: type = Types.VARCHAR;
087: } else if (type == -10) {
088: tracer.trace("Converted NTEXT (type -10) to VARCHAR");
089:
090: type = Types.VARCHAR;
091: } else if (type == -1) {
092: tracer.trace("Converted LONGTEXT (type -1) to LONGVARCHAR");
093:
094: type = Types.LONGVARCHAR;
095: }
096:
097: return (type);
098: }
099:
100: void beginTransfer() {
101: firstSmallintRow = true;
102: firstTinyintRow = true;
103: }
104:
105: Object convertColumnValue(Object value, int column, int type) {
106:
107: // solves a problem for MS SQL 7
108: if ((type == Types.SMALLINT) && (value instanceof Integer)) {
109: if (firstSmallintRow) {
110: firstSmallintRow = false;
111:
112: tracer.trace("SMALLINT: Converted column " + column
113: + " Integer to Short");
114: }
115:
116: value = new Short((short) ((Integer) value).intValue());
117: } else if ((type == Types.TINYINT)
118: && (value instanceof Integer)) {
119: if (firstTinyintRow) {
120: firstTinyintRow = false;
121:
122: tracer.trace("TINYINT: Converted column " + column
123: + " Integer to Byte");
124: }
125:
126: value = new Byte((byte) ((Integer) value).intValue());
127: }
128:
129: return (value);
130: }
131: }
|