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: StringMapping.java,v 1.6 2004/01/31 19:45:55 jackknifebarber 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: import javax.jdo.JDOUserException;
020:
021: public class StringMapping extends ColumnMapping {
022: public StringMapping(DatabaseAdapter dba, Class type) {
023: super (dba, type);
024:
025: initTypeInfo();
026: }
027:
028: public StringMapping(Column col) {
029: super (col);
030:
031: col.checkString();
032:
033: if (col.getLengthType() == Column.MAXIMUM_LENGTH) {
034: int maxlength = dba.getTypeInfo(Types.VARCHAR).precision;
035:
036: if (col.getPrecision() <= 0
037: || col.getPrecision() > maxlength) {
038: throw new JDOUserException("String max length of "
039: + col.getPrecision()
040: + " is outside the acceptable range [0, "
041: + maxlength + "]");
042: }
043: }
044:
045: initTypeInfo();
046: }
047:
048: public StringMapping(ClassBaseTable table, int relativeFieldNumber) {
049: this (table.newColumn(relativeFieldNumber));
050: }
051:
052: protected TypeInfo getTypeInfo() {
053: TypeInfo ti;
054:
055: if (col == null)
056: ti = dba.getTypeInfo(Types.VARCHAR);
057: else {
058: switch (col.getLengthType()) {
059: case Column.FIXED_LENGTH:
060: ti = dba.getTypeInfo(Types.CHAR);
061: break;
062:
063: case Column.MAXIMUM_LENGTH:
064: ti = dba.getTypeInfo(Types.VARCHAR);
065: break;
066:
067: case Column.UNLIMITED_LENGTH:
068: default:
069: ti = dba.getTypeInfo(new int[] { Types.LONGVARCHAR,
070: Types.CLOB });
071: break;
072: }
073: }
074:
075: return ti;
076: }
077:
078: public void setString(PersistenceManager pm, PreparedStatement ps,
079: int param, String value) {
080: try {
081: if (value == null)
082: ps.setNull(param, typeInfo.dataType);
083: else
084: ps.setString(param, value);
085: } catch (SQLException e) {
086: throw dba.newDataStoreException(
087: "Can't set String parameter: value = " + value, e);
088: }
089: }
090:
091: public String getString(PersistenceManager pm, ResultSet rs,
092: int param) {
093: String value;
094:
095: try {
096: value = rs.getString(param);
097: } catch (SQLException e) {
098: throw dba.newDataStoreException(
099: "Can't get String result: param = " + param, e);
100: }
101:
102: return value;
103: }
104:
105: public void setObject(PersistenceManager pm, PreparedStatement ps,
106: int param, Object value) {
107: try {
108: if (value == null)
109: ps.setNull(param, typeInfo.dataType);
110: else
111: ps.setString(param, (String) value);
112: } catch (SQLException e) {
113: throw dba.newDataStoreException(
114: "Can't set String parameter: value = " + value, e);
115: }
116: }
117:
118: public Object getObject(PersistenceManager pm, ResultSet rs,
119: int param) {
120: Object value;
121:
122: try {
123: String s = rs.getString(param);
124: value = rs.wasNull() ? null : s;
125: } catch (SQLException e) {
126: throw dba.newDataStoreException(
127: "Can't get String result: param = " + param, e);
128: }
129:
130: return value;
131: }
132:
133: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
134: return new CharacterLiteral(qs, ((String) value));
135: }
136:
137: public SQLExpression newSQLExpression(QueryStatement qs,
138: QueryStatement.QueryColumn qsc, String fieldName) {
139: return new CharacterExpression(qs, qsc);
140: }
141: }
|