001: /*
002: * Copyright 2006 Davide Deidda
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: /*
018: * SqlLong.java
019: *
020: * Created on 29 novembre 2003, 12.40
021: */
022:
023: package it.biobytes.ammentos.fieldtypes;
024:
025: import java.sql.Types;
026:
027: import it.biobytes.ammentos.*;
028:
029: import it.biobytes.ammentos.util.*;
030:
031: /**
032: *
033: * @author davide
034: */
035: public class LongType implements FieldType {
036:
037: /** Creates a new instance of SqlLong */
038: public LongType() {
039: }
040:
041: public String formatValue(Object value) {
042: String res = null;
043: if (value != null)
044: res = value.toString();
045: return res;
046: }
047:
048: public Object loadValue(java.sql.ResultSet rs, Field field)
049: throws java.sql.SQLException {
050: Object res = null;
051: res = new Long(rs.getLong(field.getName()));
052: if (rs.wasNull())
053: res = null;
054: return res;
055: }
056:
057: public Object parseValue(String str) throws PersistenceException {
058: try {
059: return Long.valueOf(str);
060: } catch (Exception e) {
061: throw new PersistenceException(e.getMessage());
062: }
063: }
064:
065: public void setParamValue(Object fieldValue,
066: java.sql.PreparedStatement pstmt, int paramIndex)
067: throws java.sql.SQLException {
068: pstmt.setLong(paramIndex, ((Long) fieldValue).longValue());
069: }
070:
071: public Object generateValue() throws PersistenceException {
072: return new Long(Sequencer.nextLong());
073: }
074:
075: public boolean isNumeric() {
076: return true;
077: }
078:
079: public Object[] getPossibleValues() {
080: return null;
081: }
082:
083: /**
084: * Returns the sum of the provided values, or null if summing is not possible
085: *
086: * @param value1 First value to add
087: * @param value2 Second value to add
088: */
089: public Object addValues(Object value1, Object value2) {
090: long v1 = (Long) value1;
091: long v2 = (Long) value2;
092: return (v1 + v2);
093: }
094:
095: /**
096: * Gets the underlying mappes Java class represented from elements of this
097: * type
098: *
099: * @return the Java class mapped from this FieldType
100: */
101: public Class getMappedClass() {
102: return Long.TYPE;
103: }
104:
105: public int getSqlType() {
106: return Types.BIGINT;
107: }
108: }
|