KickJava   Java API By Example, From Geeks To Geeks.

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