KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > mysql > 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.mysql.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.7 $";
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 (_originalType.isNumeric()) {
81             retVal.append("CONCAT(");
82             // no other conversion function : advised in MySQL documentation
83
retVal.append(expr);
84             retVal.append(")");
85         } else
86             switch (originalTypeCode) {
87                 case Types.DATE :
88                     retVal.append("DATE_FORMAT(");
89                     retVal.append(expr);
90                     retVal.append(",'%Y-%m-%d')");
91                     break;
92                 case Types.TIME :
93                     retVal.append("TIME_FORMAT(");
94                     retVal.append(expr);
95                     retVal.append(",'%H:%i:%s')");
96                     break;
97                 case Types.TIMESTAMP :
98                     retVal.append("DATE_FORMAT(");
99                     retVal.append(expr);
100                     retVal.append(",'%Y-%m-%dT%H:%i:%s')");
101                     break;
102                 case Types.NULL :
103                     retVal.append("null");
104                     break;
105                 default :
106                     Debug.assertTrue(false, originalTypeCode + " type not expected.");
107             }
108
109         //Trace.exit(this, "convertToString(String expr)", expr);
110
return retVal.toString();
111     }
112
113     private String JavaDoc convertToDate(String JavaDoc expr) {
114         //Trace.enter(this, "convertToDate(String expr)", expr);
115
// MySQL uses almost the same syntax as XQuery (except for the 'T' separator but it is tolerated)
116
//Trace.exit(this, "convertToDate(String expr)", expr);
117
return expr;
118     }
119
120     private String JavaDoc convertToDateTime(String JavaDoc expr) {
121         //Trace.enter(this, "convertToDateTime(String expr)", expr);
122
// MySQL uses almost the same syntax as XQuery (except for the 'T' separator but it is tolerated)
123
//Trace.exit(this, "convertToDateTime(String expr)", expr);
124
return expr;
125     }
126
127     private String JavaDoc convertToTime(String JavaDoc expr) {
128         //Trace.enter(this, "convertToTime(String expr)", expr);
129
// MySQL uses almost the same syntax as XQuery
130
//Trace.exit(this, "convertToTime(String expr)", expr);
131
return expr;
132     }
133
134     private String JavaDoc convertToNumber(String JavaDoc expr) {
135         //Trace.enter(this, "convertToNumber(String expr)", expr);
136
// MySQL converts automatically types to number when needed. No explicit conversion function found
137
//Trace.exit(this, "convertToNumber(String expr)", expr);
138
return expr;
139     }
140 }
141
Popular Tags