001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: CharacterMapping.java,v 1.6 2003/10/10 22:22:14 pierreg0 Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.PersistenceManager;
014: import java.sql.PreparedStatement;
015: import java.sql.ResultSet;
016: import java.sql.SQLException;
017: import java.sql.Types;
018: import javax.jdo.JDODataStoreException;
019:
020: public class CharacterMapping extends ColumnMapping {
021: public CharacterMapping(DatabaseAdapter dba, Class type) {
022: super (dba, type);
023:
024: initTypeInfo();
025: }
026:
027: public CharacterMapping(Column col) {
028: super (col);
029:
030: col.setFixedLength(1);
031: col.checkString();
032:
033: initTypeInfo();
034: }
035:
036: public CharacterMapping(ClassBaseTable table,
037: int relativeFieldNumber) {
038: this (table.newColumn(relativeFieldNumber));
039: }
040:
041: protected TypeInfo getTypeInfo() {
042: return dba.getTypeInfo(Types.CHAR);
043: }
044:
045: public void setChar(PersistenceManager pm, PreparedStatement ps,
046: int param, char value) {
047: try {
048: ps.setString(param, new Character(value).toString());
049: } catch (SQLException e) {
050: throw dba.newDataStoreException(
051: "Can't set char parameter: value = " + value, e);
052: }
053: }
054:
055: public char getChar(PersistenceManager pm, ResultSet rs, int param) {
056: char value;
057:
058: try {
059: String s = rs.getString(param);
060:
061: if (rs.wasNull())
062: throw new NullValueException(
063: "Illegal null value in column " + col);
064:
065: value = s.length() == 0 ? '\000' : s.charAt(0);
066: } catch (SQLException e) {
067: throw dba.newDataStoreException(
068: "Can't get char result: param = " + param, e);
069: }
070:
071: return value;
072: }
073:
074: public void setObject(PersistenceManager pm, PreparedStatement ps,
075: int param, Object value) {
076: try {
077: if (value == null)
078: ps.setNull(param, typeInfo.dataType);
079: else
080: ps.setString(param, ((Character) value).toString());
081: } catch (SQLException e) {
082: throw dba.newDataStoreException(
083: "Can't set Character parameter: value = " + value,
084: e);
085: }
086: }
087:
088: public Object getObject(PersistenceManager pm, ResultSet rs,
089: int param) {
090: Object value;
091:
092: try {
093: String s = rs.getString(param);
094: value = rs.wasNull() ? null : new Character(
095: s.length() == 0 ? '\000' : s.charAt(0));
096: } catch (SQLException e) {
097: throw dba.newDataStoreException(
098: "Can't get Character result: param = " + param, e);
099: }
100:
101: return value;
102: }
103:
104: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
105: return new CharacterLiteral(qs, ((Character) value).toString());
106: }
107:
108: public SQLExpression newSQLExpression(QueryStatement qs,
109: QueryStatement.QueryColumn qsc, String fieldName) {
110: return new CharacterExpression(qs, qsc);
111: }
112: }
|