KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > microsoft > sql > SqlConvert


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.microsoft.sql;
24
25 import java.sql.Types JavaDoc;
26
27 import org.xquark.extractor.common.Debug;
28 import org.xquark.extractor.sql.Context;
29 import org.xquark.extractor.sql.SqlExpression;
30 import org.xquark.extractor.sql.SqlSelect;
31 import org.xquark.jdbc.typing.DbType;
32
33
34 public class SqlConvert extends org.xquark.extractor.sql.SqlConvert{
35     private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
36     private static final String JavaDoc RCSName = "$Name: $";
37
38     public SqlConvert() {
39         super();
40     }
41
42     public SqlConvert(SqlExpression expr, DbType originalType, DbType targetType)
43     {
44         super(expr, originalType, targetType);
45     }
46
47     public String JavaDoc toSql (Context context) {
48         //Trace.enter(this,"toSql");
49
String JavaDoc retVal = null;
50
51         String JavaDoc expr = getExpression().toSql(context);
52         if (getExpression() instanceof SqlSelect) {
53             expr = '('+ expr +')';
54         }
55
56         int targetTypeCode = _targetType.getJDBCType();
57
58         if (_targetType.isNumeric()) {
59             retVal = convertToNumber(expr);
60         }
61         else if (_targetType.isString()) {
62             retVal = convertToString(expr);
63         }
64         else if (Types.DATE == targetTypeCode) {
65             retVal = convertToDate(expr);
66         }
67         else if (Types.TIME == targetTypeCode) {
68             retVal = convertToTime(expr);
69         }
70         else if (Types.TIMESTAMP == targetTypeCode) {
71             retVal = convertToDateTime(expr);
72         }
73         else {
74             Debug.assertTrue (false, "NYI!!");
75         }
76         //Trace.exit(this, "castTypeIgnoreName(Expression expr, SqlTypeAtom _originalType, SqlTypeAtom targetType)");
77
return retVal;
78     }
79
80     private String JavaDoc convertToString(String JavaDoc expr) {
81         //Trace.enter(this, "convertToString(String expr)", expr);
82
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
83
84         int originalTypeCode = _originalType.getJDBCType();
85         if (_originalType.isString()) {
86             retVal.append(expr);
87         }
88         else if (Types.DATE == originalTypeCode) {
89                 retVal.append("STUFF(STUFF(RTRIM(CONVERT(char,");
90                 retVal.append(expr);
91                 retVal.append(", 102)), 5,1,\'-\'),8,1,\'-\')");
92         }
93         else if (Types.TIME == originalTypeCode) {
94                 retVal.append("SUBSTRING(CONVERT(char,");
95                 retVal.append(expr);
96                 retVal.append(",8),1,8)");
97         }
98         else if (Types.TIMESTAMP == originalTypeCode) {
99                 retVal.append("STUFF(STUFF(RTRIM(CONVERT(char,");
100                 retVal.append(expr);
101                 retVal.append(", 102)), 5,1,\'-\'),8,1,\'-\') + \'T\' + SUBSTRING(CONVERT(char, ");
102                 retVal.append(expr);
103                 retVal.append(",8),1,8)");
104         }
105         else if (_originalType.isNumeric() ){
106                 retVal.append("RTRIM(CONVERT(varchar,");
107                 retVal.append(expr);
108                 retVal.append("))");
109         }
110         else if (_originalType.isNull() ){
111                 retVal.append("CONVERT(varchar,");
112                 retVal.append(expr);
113                 retVal.append(")");
114         }
115         else {
116             Debug.assertTrue(false, "cas imprevu");
117         }
118
119         //Trace.exit(this, "convertToString(String expr)", expr);
120
return retVal.toString();
121     }
122
123     private String JavaDoc convertToDate(String JavaDoc expr) {
124         //Trace.enter(this, "convertToDate(String expr)", expr);
125
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
126
127         int originalTypeCode = _originalType.getJDBCType();
128         if (_originalType.isString() || _originalType.isNull()) {
129             retVal.append("CONVERT(DATETIME,");
130             retVal.append(expr);
131             retVal.append(")");
132         }
133         else if (Types.DATE ==originalTypeCode) {
134             retVal.append(expr);
135         }
136         else {
137             Debug.assertTrue(false, "cas imprevu");
138         }
139
140         //Trace.exit(this, "convertToDate(String expr)", expr);
141
return retVal.toString();
142     }
143
144     private String JavaDoc convertToDateTime(String JavaDoc expr) {
145         //Trace.enter(this, "convertToDateTime(String expr)", expr);
146
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
147
148         int originalTypeCode = _originalType.getJDBCType();
149         if (_originalType.isString() || _originalType.isNull()) {
150             retVal.append("CONVERT(DATETIME,");
151             retVal.append(expr);
152             retVal.append(")");
153         }
154         else if (Types.TIMESTAMP ==originalTypeCode) {
155             retVal.append(expr);
156         }
157         else {
158             Debug.assertTrue(false, "cas imprevu");
159         }
160
161         //Trace.exit(this, "convertToDateTime(String expr)", expr);
162
return retVal.toString();
163     }
164
165     private String JavaDoc convertToTime(String JavaDoc expr) {
166         //Trace.enter(this, "convertToTime(String expr)", expr);
167
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
168
169         int originalTypeCode = _originalType.getJDBCType();
170         if (_originalType.isString() || _originalType.isNull()) {
171             retVal.append("CONVERT(DATETIME,");
172             retVal.append(expr);
173             retVal.append(")");
174         }
175         else if (Types.TIME ==originalTypeCode) {
176             retVal.append(expr);
177         }
178         else {
179             Debug.assertTrue(false, "cas imprevu");
180         }
181
182         //Trace.exit(this, "convertToTime(String expr)", expr);
183
return retVal.toString();
184     }
185
186     private String JavaDoc convertToNumber(String JavaDoc expr) {
187         //Trace.enter(this, "convertToNumber(String expr)", expr);
188
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
189
190         int originalTypeCode = _originalType.getJDBCType();
191         if (_originalType.isNull()) {
192             retVal.append(expr);
193         }
194         else {
195             int targetTypeCode = _targetType.getJDBCType();
196             switch (targetTypeCode) {
197                 case Types.DECIMAL:
198                     retVal.append("CONVERT(decimal, ");
199                     break;
200                 case Types.TINYINT:
201                     retVal.append("CONVERT(tinyint, ");
202                     break;
203                 case Types.SMALLINT:
204                     retVal.append("CONVERT(smallint, ");
205                     break;
206                 case Types.INTEGER:
207                     retVal.append("CONVERT(integer, ");
208                     break;
209                 case Types.BIGINT:
210                     retVal.append("CONVERT(bigint, ");
211                     break;
212                 case Types.REAL:
213                     retVal.append("CONVERT(real, ");
214                     break;
215                 case Types.FLOAT:
216                     retVal.append("CONVERT(float, ");
217                     break;
218                 case Types.DOUBLE:
219                     retVal.append("CONVERT(float(16), ");
220                     break;
221                 default:
222                     Debug.assertTrue(false, "Data covertion not implemented");
223                     break;
224             }
225             retVal.append(expr);
226             retVal.append(")");
227         }
228         //Trace.exit(this, "convertToNumber(String expr)", expr);
229
return retVal.toString();
230     }
231 }
232
Popular Tags