1 package org.apache.ojb.broker.platforms;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.io.ByteArrayInputStream;
19 import java.io.InputStreamReader;
20 import java.io.Reader;
21 import java.io.StringReader;
22 import java.sql.PreparedStatement;
23 import java.sql.SQLException;
24 import java.sql.Types;
25 import java.sql.CallableStatement;
26 import java.sql.Connection;
27
28 import org.apache.ojb.broker.query.LikeCriteria;
29
30
31
32
33
34
35 public class PlatformMySQLImpl extends PlatformDefaultImpl
36 {
37 private static final String LAST_INSERT = "SELECT LAST_INSERT_ID() FROM ";
38 private static final String LIMIT = " LIMIT 1";
39
40
41
42
43 public void setObjectForStatement(PreparedStatement ps, int index, Object value, int sqlType) throws SQLException
44 {
45 switch (sqlType)
46 {
47 case Types.BIT :
48 ps.setObject(index, value);
49 break;
50
51 case Types.BLOB :
52 case Types.LONGVARBINARY :
53 case Types.VARBINARY :
54 if (value instanceof byte[])
55 {
56 byte buf[] = (byte[]) value;
57 ByteArrayInputStream inputStream = new ByteArrayInputStream(buf);
58 ps.setBinaryStream(index, inputStream, buf.length);
59
60 break;
61 }
62
63 case Types.CLOB :
64 Reader reader = null;
65 int length = 0;
66
67 if (value instanceof String)
68 {
69 reader = new StringReader((String) value);
70 length = (((String) value)).length();
71 }
72 else if (value instanceof char[])
73 {
74 String string = new String((char[])value);
75 reader = new StringReader(string);
76 length = string.length();
77 }
78 else if (value instanceof byte[])
79 {
80 byte buf[] = (byte[]) value;
81 ByteArrayInputStream inputStream = new ByteArrayInputStream(buf);
82 reader = new InputStreamReader(inputStream);
83 }
84
85 ps.setCharacterStream(index, reader, length);
86 break;
87
88 default :
89 super.setObjectForStatement(ps, index, value, sqlType);
90
91 }
92 }
93
94
95
96
97 public byte getJoinSyntaxType()
98 {
99 return SQL92_NOPAREN_JOIN_SYNTAX;
100 }
101
102 public String getLastInsertIdentityQuery(String tableName)
103 {
104 return LAST_INSERT + tableName + LIMIT;
105 }
106
107
108
109
110
111
112 public void addPagingSql(StringBuffer anSqlString)
113 {
114 anSqlString.append(" LIMIT ?,?");
115 }
116
117
118
119
120 public String createSequenceQuery(String sequenceName)
121 {
122 return "insert into ojb_nextval_seq (seq_name) " +
123 "values ('" + sequenceName + "')";
124 }
125
126
127
128
129 public String nextSequenceQuery(String sequenceName)
130 {
131 return "select ojb_nextval_func ('" + sequenceName + "')";
132 }
133
134
135
136
137 public String dropSequenceQuery(String sequenceName)
138 {
139 return "delete from ojb_nextval_seq where seq_name='" +
140 sequenceName + "'";
141 }
142
143
144
145
146 public CallableStatement prepareNextValProcedureStatement (Connection con,
147 String procedureName, String sequenceName) throws
148 PlatformException
149 {
150 try {
151 String sp = " { call " + procedureName + " (?,?) } ";
152 CallableStatement cs = con.prepareCall(sp);
153 cs.registerOutParameter(1, Types.BIGINT);
154 cs.setString(2, sequenceName);
155 return cs;
156 } catch (Exception e) {
157 throw new PlatformException(e);
158 }
159 }
160
161
162
163
164
165
166 public boolean supportsPaging()
167 {
168 return true;
169 }
170
171
172
173
174 public String concatenate(String[] theColumns)
175 {
176 if (theColumns.length == 1)
177 {
178 return theColumns[0];
179 }
180
181 StringBuffer buf = new StringBuffer();
182
183 buf.append("concat(");
184 for (int i = 0; i < theColumns.length; i++)
185 {
186 if (i > 0)
187 {
188 buf.append(",");
189 }
190 buf.append(theColumns[i]);
191 }
192
193 buf.append(")");
194 return buf.toString();
195 }
196
197
198
199
200 public String getEscapeClause(LikeCriteria aCriteria)
201 {
202 if (LikeCriteria.getEscapeCharacter() != LikeCriteria.DEFAULT_ESCPAPE_CHARACTER)
203 {
204
205 return super.getEscapeClause(aCriteria);
206 }
207 else
208 {
209 return "";
210 }
211 }
212 }