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.hibernate.scrollable;
018:
019: import java.sql.Types;
020: import java.util.Date;
021:
022: import org.compass.gps.device.jdbc.mapping.ColumnMapping;
023: import org.compass.gps.device.jdbc.mapping.VersionColumnMapping;
024: import org.hibernate.ScrollableResults;
025: import org.hibernate.type.DateType;
026: import org.hibernate.type.IntegerType;
027: import org.hibernate.type.StringType;
028: import org.hibernate.type.Type;
029:
030: public class Hibernate3Dialect {
031:
032: private int getIndex(String[] returnAliases, ColumnMapping mapping) {
033: int index = -1;
034: String key = mapping.getColumnName();
035: for (int i = 0; i != returnAliases.length; i++) {
036: if (returnAliases[i].equals(key)) {
037: index = i;
038: break;
039: }
040: }
041: if (index < 0) {
042: throw new RuntimeException("unknown columnName: " + key
043: + ", returnAliases: " + returnAliases);
044: }
045: return index;
046: }
047:
048: protected Long getIntegerAsLong(ScrollableResults rs,
049: String[] returnAliases, ColumnMapping columnMapping) {
050: Integer integer;
051: if (columnMapping.isUsingColumnIndex()) {
052: integer = rs.getInteger(columnMapping.getColumnIndex());
053: } else {
054: integer = rs.getInteger(getIndex(returnAliases,
055: columnMapping));
056: }
057: return new Long(integer.longValue());
058: }
059:
060: protected Long getLong(ScrollableResults rs,
061: String[] returnAliases, ColumnMapping columnMapping) {
062: int index = -1;
063: if (columnMapping.isUsingColumnIndex()) {
064: index = columnMapping.getColumnIndex();
065: } else {
066: index = getIndex(returnAliases, columnMapping);
067: }
068: Type type = rs.getType(index);
069: Long result;
070: if (type instanceof IntegerType) {
071: result = new Long(rs.getInteger(index).longValue());
072: } else if (type instanceof DateType) {
073: result = new Long(rs.getDate(index).getTime());
074: } else {
075: result = rs.getLong(index);
076: }
077: return result;
078: }
079:
080: protected Long getDateAsLong(ScrollableResults rs,
081: String[] returnAliases, ColumnMapping columnMapping) {
082: Date date = null;
083: if (columnMapping.isUsingColumnIndex()) {
084: date = rs.getDate(columnMapping.getColumnIndex());
085: } else {
086: date = rs.getDate(getIndex(returnAliases, columnMapping));
087: }
088: return new Long(date.getTime());
089: }
090:
091: protected Long getTimeAsLong(ScrollableResults rs,
092: String[] returnAliases, ColumnMapping columnMapping) {
093: Date date = null;
094: if (columnMapping.isUsingColumnIndex()) {
095: date = rs.getDate(columnMapping.getColumnIndex());
096: } else {
097: date = rs.getDate(getIndex(returnAliases, columnMapping));
098: }
099: return new Long(date.getTime());
100: }
101:
102: protected Long getTimestampAsLong(ScrollableResults rs,
103: String[] returnAliases, ColumnMapping columnMapping) {
104: Date timestamp = null;
105: if (columnMapping.isUsingColumnIndex()) {
106: timestamp = rs.getDate(columnMapping.getColumnIndex());
107: } else {
108: timestamp = rs.getDate(getIndex(returnAliases,
109: columnMapping));
110: }
111: return new Long(timestamp.getTime());
112: }
113:
114: public Long getVersion(ScrollableResults rs,
115: String[] returnAliases, VersionColumnMapping versionMapping) {
116: Long result = null;
117: int sqlType = versionMapping.getSqlType();
118: if (sqlType == Types.INTEGER) {
119: result = getIntegerAsLong(rs, returnAliases, versionMapping);
120: } else if (sqlType == Types.DATE) {
121: result = getDateAsLong(rs, returnAliases, versionMapping);
122: } else if (sqlType == Types.TIMESTAMP) {
123: result = getTimestampAsLong(rs, returnAliases,
124: versionMapping);
125: } else if (sqlType == Types.TIME) {
126: result = getTimeAsLong(rs, returnAliases, versionMapping);
127: } else {
128: result = getLong(rs, returnAliases, versionMapping);
129: }
130: return result;
131: }
132:
133: public String getStringValue(ScrollableResults rs,
134: String[] returnAliases, ColumnMapping mapping) {
135: int index = -1;
136: if (mapping.isUsingColumnIndex()) {
137: index = mapping.getColumnIndex();
138: } else {
139: index = getIndex(returnAliases, mapping);
140: }
141: Type type = rs.getType(index);
142: String result;
143: if (type instanceof StringType) {
144: result = rs.getString(index);
145: } else {
146: result = rs.get(index).toString();
147: }
148: return result;
149: }
150:
151: public Object getValue(ScrollableResults rs,
152: String[] returnAliases, ColumnMapping mapping) {
153: int index = -1;
154: if (mapping.isUsingColumnIndex()) {
155: index = mapping.getColumnIndex();
156: } else {
157: index = getIndex(returnAliases, mapping);
158: }
159: return rs.get(index);
160: }
161:
162: public String getStringValue(ScrollableResults rs, int columnIndex) {
163: Type type = rs.getType(columnIndex);
164: String result;
165: if (type instanceof StringType) {
166: result = rs.getString(columnIndex);
167: } else {
168: result = rs.get(columnIndex).toString();
169: }
170: return result;
171: }
172: }
|