KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > progress > 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.progress.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 if (DbType.ORACLE_ROWID == targetTypeCode) {
67             retVal = convertToRowid(expr);
68         } else {
69             Debug.assertTrue(false, "NYI!!");
70         }
71         //Trace.exit(this, "castTypeIgnoreName(Expression expr, SqlTypeAtom _originalType, SqlTypeAtom targetType)");
72
return retVal;
73     }
74
75     private String JavaDoc convertToString(String JavaDoc expr) {
76         //Trace.enter(this, "convertToString(String expr)", expr);
77
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
78
79         int originalTypeCode = _originalType.getJDBCType();
80         if (_originalType.isString()) {
81             retVal.append(expr);
82         } else if (DbType.ORACLE_ROWID == originalTypeCode) {
83             retVal.append("ROWIDTOCHAR(");
84             retVal.append(expr);
85             retVal.append(")");
86         } else if (Types.DATE == originalTypeCode) {
87             retVal.append("LTRIM(TO_CHAR(");
88             retVal.append(expr);
89             retVal.append(",'SYYYY-MM-DD'))");
90         } else if (Types.TIME == originalTypeCode) {
91             retVal.append("TO_CHAR(");
92             retVal.append(expr);
93             retVal.append(",'HH24:MI:SS')");
94         } else if (Types.TIMESTAMP == originalTypeCode) {
95             retVal.append("LTRIM(TO_CHAR(");
96             retVal.append(expr);
97             retVal.append(",'SYYYY-MM-DD\"T\"HH24:MI:SS'))");
98         } else if (_originalType.isNumeric()) {
99             retVal.append("TO_CHAR(");
100             retVal.append(expr);
101             retVal.append(")");
102         } else if (Types.NULL == originalTypeCode) {
103             retVal.append("TO_CHAR(");
104             retVal.append(expr);
105             retVal.append(")");
106         } else {
107             Debug.assertTrue(false, "cas imprevu");
108         }
109
110         //Trace.exit(this, "convertToString(String expr)", expr);
111
return retVal.toString();
112     }
113
114     private String JavaDoc convertToDate(String JavaDoc expr) {
115         //Trace.enter(this, "convertToDate(String expr)", expr);
116
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
117
118         int originalTypeCode = _originalType.getJDBCType();
119         if (_originalType.isString() || _originalType.isNull()) {
120             retVal.append("TO_DATE(");
121             retVal.append(expr);
122             retVal.append(", 'SYYYY-MM-DD')");
123         } else if (Types.DATE == originalTypeCode) {
124             retVal.append(expr);
125         } else {
126             Debug.assertTrue(false, "cas imprevu");
127         }
128
129         //Trace.exit(this, "convertToDate(String expr)", expr);
130
return retVal.toString();
131     }
132
133     private String JavaDoc convertToDateTime(String JavaDoc expr) {
134         //Trace.enter(this, "convertToDateTime(String expr)", expr);
135
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
136
137         int originalTypeCode = _originalType.getJDBCType();
138         if (_originalType.isString() || _originalType.isNull()) {
139             retVal.append("TO_DATE(");
140             retVal.append(expr);
141             retVal.append(", 'SYYYY-MM-DD\"T\"HH24:MI:SS')");
142         } else if (Types.TIMESTAMP == originalTypeCode) {
143             retVal.append(expr);
144         } else {
145             Debug.assertTrue(false, "cas imprevu");
146         }
147
148         //Trace.exit(this, "convertToDateTime(String expr)", expr);
149
return retVal.toString();
150     }
151
152     private String JavaDoc convertToTime(String JavaDoc expr) {
153         //Trace.enter(this, "convertToTime(String expr)", expr);
154
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
155
156         int originalTypeCode = _originalType.getJDBCType();
157         if (_originalType.isString() || _originalType.isNull()) {
158             retVal.append("TO_DATE(");
159             retVal.append(expr);
160             retVal.append(", 'HH24:MI:SS')");
161         } else if (Types.TIME == originalTypeCode) {
162             retVal.append(expr);
163         } else {
164             Debug.assertTrue(false, "cas imprevu");
165         }
166
167         //Trace.exit(this, "convertToTime(String expr)", expr);
168
return retVal.toString();
169     }
170
171     private String JavaDoc convertToNumber(String JavaDoc expr) {
172         //Trace.enter(this, "convertToNumber(String expr)", expr);
173
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
174
175         int originalTypeCode = _originalType.getJDBCType();
176         if (_originalType.isNumeric()) {
177             retVal.append(expr);
178         } else if (_originalType.isNull()) {
179             retVal.append("TO_NUMBER(");
180             retVal.append(expr);
181             retVal.append(")");
182         } else if (_originalType.isString()) {
183             retVal.append("TO_NUMBER(");
184             retVal.append(expr);
185             retVal.append(")");
186         } else {
187             Debug.assertTrue(false, "cas imprevu");
188         }
189         //Trace.exit(this, "convertToNumber(String expr)", expr);
190
return retVal.toString();
191     }
192     private String JavaDoc convertToRowid(String JavaDoc expr) {
193         //Trace.enter(this, "convertToRowid(String expr)", expr);
194
StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
195
196         int originalTypeCode = _originalType.getJDBCType();
197         if (_originalType.isString() || _originalType.isNull()) {
198             retVal.append(" CHARTOROWID(");
199             retVal.append(expr);
200             retVal.append(")");
201         } else {
202             Debug.assertTrue(false, "cas imprevu");
203         }
204
205         //Trace.exit(this, "convertToRowid(String expr)", expr);
206
return retVal.toString();
207     }
208
209 }
210
Popular Tags