1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.melati.poem;
47
48 import java.sql.PreparedStatement;
49 import java.sql.ResultSet;
50 import java.sql.SQLException;
51 import java.sql.Types;
52
53 import org.melati.poem.dbms.Dbms;
54 import org.melati.poem.util.StringUtils;
55
56
57
58
59 public class StringPoemType extends SizedAtomPoemType<String> {
60
61
62 public static final StringPoemType nullableInstance = new StringPoemType(true, -1);
63
64
65
66
67
68
69 public StringPoemType(boolean nullable, int size) {
70 super(Types.VARCHAR, "VARCHAR", nullable, size);
71 }
72
73 protected void _assertValidRaw(Object raw)
74 throws ValidationPoemException {
75 if (raw != null) {
76 if (!(raw instanceof String))
77 throw new TypeMismatchPoemException(raw, this);
78 if (!sizeGreaterEqual(getSize(), ((String)raw).length()))
79 throw new StringLengthValidationPoemException(this, (String)raw);
80 }
81 }
82
83 protected String _getRaw(ResultSet rs, int col) throws SQLException {
84 return rs.getString(col);
85 }
86
87 protected void _setRaw(PreparedStatement ps, int col, Object string)
88 throws SQLException {
89 ps.setString(col, (String)string);
90 }
91
92 protected String _rawOfString(String rawString) {
93 return rawString;
94 }
95
96 protected String _sqlDefinition(Dbms dbms) {
97 try {
98 return dbms.getStringSqlDefinition(getSize());
99 } catch (SQLException e) {
100 throw new SQLSeriousPoemException(e);
101 }
102 }
103
104
105
106
107
108 protected boolean _canRepresent(SQLPoemType<?> other) {
109 return
110 other instanceof StringPoemType &&
111 sizeGreaterEqual(getSize(), ((StringPoemType)other).getSize());
112 }
113
114
115
116
117
118 public String toString() {
119 return (getNullable() ? "nullable " : "") + "String(" + getSize() + ")";
120 }
121
122
123
124
125
126 public String toDsdType() {
127 return "String";
128 }
129
130 protected void _saveColumnInfo(ColumnInfo columnInfo)
131 throws AccessPoemException {
132 columnInfo.setTypefactory(PoemTypeFactory.STRING);
133 columnInfo.setSize(getSize());
134 }
135
136 protected String _quotedRaw(Object raw) {
137 return StringUtils.quoted((String)raw, '\'');
138 }
139
140 }