001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.jdbc.dialect;
018:
019: import java.sql.ParameterMetaData;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.sql.Timestamp;
024: import java.sql.Types;
025: import java.util.Date;
026:
027: import org.compass.gps.device.jdbc.mapping.ColumnMapping;
028: import org.compass.gps.device.jdbc.mapping.VersionColumnMapping;
029:
030: /**
031: *
032: * @author kimchy
033: *
034: */
035: public class DefaultJdbcDialect implements JdbcDialect {
036:
037: protected Long getIntegerAsLong(ResultSet rs,
038: ColumnMapping columnMapping) throws SQLException {
039: int integer;
040: if (columnMapping.isUsingColumnIndex()) {
041: integer = rs.getInt(columnMapping.getColumnIndex());
042: } else {
043: integer = rs.getInt(columnMapping.getColumnName());
044: }
045: return new Long(integer);
046: }
047:
048: protected Long getNumericAsLong(ResultSet rs,
049: ColumnMapping columnMapping) throws SQLException {
050: if (columnMapping.isUsingColumnIndex()) {
051: return new Long(rs.getLong(columnMapping.getColumnIndex()));
052: }
053: return new Long(rs.getLong(columnMapping.getColumnName()));
054: }
055:
056: protected Long getLong(ResultSet rs, ColumnMapping columnMapping)
057: throws SQLException {
058: if (columnMapping.isUsingColumnIndex()) {
059: return (Long) rs.getObject(columnMapping.getColumnIndex());
060: }
061: return (Long) rs.getObject(columnMapping.getColumnName());
062: }
063:
064: protected Long getDateAsLong(ResultSet rs,
065: ColumnMapping columnMapping) throws SQLException {
066: Date date = null;
067: if (columnMapping.isUsingColumnIndex()) {
068: date = rs.getDate(columnMapping.getColumnIndex());
069: } else {
070: date = rs.getDate(columnMapping.getColumnName());
071: }
072: return new Long(date.getTime());
073: }
074:
075: protected Long getTimeAsLong(ResultSet rs,
076: ColumnMapping columnMapping) throws SQLException {
077: Date date = null;
078: if (columnMapping.isUsingColumnIndex()) {
079: date = rs.getTime(columnMapping.getColumnIndex());
080: } else {
081: date = rs.getTime(columnMapping.getColumnName());
082: }
083: return new Long(date.getTime());
084: }
085:
086: protected Long getTimestampAsLong(ResultSet rs,
087: ColumnMapping columnMapping) throws SQLException {
088: Timestamp timestamp = null;
089: if (columnMapping.isUsingColumnIndex()) {
090: timestamp = rs.getTimestamp(columnMapping.getColumnIndex());
091: } else {
092: timestamp = rs.getTimestamp(columnMapping.getColumnName());
093: }
094: return new Long(timestamp.getTime());
095: }
096:
097: public Long getVersion(ResultSet rs,
098: VersionColumnMapping versionMapping) throws SQLException {
099: Long result = null;
100: int sqlType = versionMapping.getSqlType();
101: if (sqlType == Types.INTEGER) {
102: result = getIntegerAsLong(rs, versionMapping);
103: } else if (sqlType == Types.DATE) {
104: result = getDateAsLong(rs, versionMapping);
105: } else if (sqlType == Types.TIMESTAMP) {
106: result = getTimestampAsLong(rs, versionMapping);
107: } else if (sqlType == Types.TIME) {
108: result = getTimeAsLong(rs, versionMapping);
109: } else if (sqlType == Types.NUMERIC) {
110: result = getNumericAsLong(rs, versionMapping);
111: } else {
112: result = getLong(rs, versionMapping);
113: }
114: return result;
115: }
116:
117: public void setParameter(PreparedStatement ps, int paramIndex,
118: String value) throws SQLException {
119: ParameterMetaData metaData = ps.getParameterMetaData();
120: int type = metaData.getParameterType(paramIndex);
121: if (type == Types.BIGINT) {
122: long lValue = Long.parseLong(value);
123: ps.setLong(paramIndex, lValue);
124: } else if (type == Types.INTEGER) {
125: int iValue = Integer.parseInt(value);
126: ps.setInt(paramIndex, iValue);
127: } else if (type == Types.SMALLINT) {
128: short iValue = Short.parseShort(value);
129: ps.setShort(paramIndex, iValue);
130: } else if (type == Types.VARCHAR || type == Types.LONGVARCHAR
131: || type == Types.CHAR) {
132: ps.setString(paramIndex, value);
133: } else {
134: throw new IllegalArgumentException(
135: "Failed to set parameter for type ["
136: + metaData.getParameterTypeName(paramIndex)
137: + "], not supported");
138: }
139: }
140:
141: public String getStringValue(ResultSet rs, ColumnMapping mapping)
142: throws SQLException {
143: String value = null;
144: if (mapping.isUsingColumnIndex()) {
145: value = rs.getString(mapping.getColumnIndex());
146: } else {
147: value = rs.getString(mapping.getColumnName());
148: }
149: if (rs.wasNull()) {
150: return null;
151: }
152: return value;
153: }
154:
155: public String getStringValue(ResultSet rs, int columnIndex)
156: throws SQLException {
157: String value = rs.getString(columnIndex);
158: if (rs.wasNull()) {
159: return null;
160: }
161: return value;
162: }
163: }
|