00001
00025
package org.objectweb.cjdbc.common.sql;
00026
00027
import java.io.Serializable;
00028
import java.sql.SQLException;
00029
import java.util.ArrayList;
00030
import java.util.StringTokenizer;
00031
00032
import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn;
00033
import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
00034
import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
00035
import org.objectweb.cjdbc.common.sql.schema.TableColumn;
00036
00048 public class InsertRequest extends AbstractWriteRequest implements Serializable
00049 {
00068 public InsertRequest(String sqlQuery,
boolean escapeProcessing,
int timeout,
00069 String lineSeparator,
DatabaseSchema schema,
int granularity,
00070
boolean isCaseSensitive)
throws SQLException
00071 {
00072
this(sqlQuery, escapeProcessing, timeout, lineSeparator);
00073
parse(schema, granularity, isCaseSensitive);
00074 }
00075
00091 public InsertRequest(String sqlQuery,
boolean escapeProcessing,
int timeout,
00092 String lineSeparator)
00093 {
00094 super(sqlQuery, escapeProcessing, timeout, lineSeparator);
00095 cacheable =
RequestType.UNCACHEABLE;
00096 isParsed =
false;
00097 }
00098
00105 public void parse(
DatabaseSchema schema,
int granularity,
00106
boolean isCaseSensitive)
throws SQLException
00107 {
00108
if (granularity ==
ParsingGranularities.NO_PARSING)
00109 {
00110 isParsed =
true;
00111
return;
00112 }
00113
00114
00115
if (schema == null)
00116
throw new SQLException(
00117
"Unable to parse request with an undefined database schema");
00118
00119 String originalSQL =
this.trimCarriageReturn();
00120 String sql = originalSQL.toLowerCase();
00121
00122
00123 sql = sql.substring(7).trim().substring(5).trim();
00124
00125
00126
int endIdx = sql.indexOf(
"values ");
00127
if (endIdx == -1)
00128 {
00129 endIdx = sql.indexOf(
"values(");
00130
if (endIdx == -1)
00131 {
00132 endIdx = sql.indexOf(
"select ");
00133
if (endIdx == -1)
00134
throw new SQLException(
00135
"Unable to find the VALUES or SELECT keyword in this INSERT statement: '"
00136 + sqlQuery +
"'");
00137 }
00138 }
00139
00140
if (isCaseSensitive)
00141 {
00142
int shift = originalSQL.length() - sql.length();
00143 sql = originalSQL.substring(shift, shift + endIdx).trim();
00144 }
00145
else
00146 sql = sql.substring(0, endIdx).trim();
00147
00148
int openParenthesisIdx = sql.indexOf(
"(");
00149
00150
00151 String insertTable;
00152
if (openParenthesisIdx == -1)
00153 insertTable = sql;
00154
else
00155 insertTable = sql.substring(0, openParenthesisIdx).trim();
00156
00157
DatabaseTable t = schema.getTable(insertTable, isCaseSensitive);
00158
if (t == null)
00159
throw new SQLException(
"Unknown table '" + insertTable
00160 +
"' in this INSERT statement: '" + sqlQuery +
"'");
00161
else
00162 tableName = t.
getName();
00163
00164
if ((granularity ==
ParsingGranularities.COLUMN)
00165 || (granularity ==
ParsingGranularities.COLUMN_UNIQUE))
00166 {
00167
if (openParenthesisIdx != -1)
00168 {
00169
00170
int closingParenthesisIdx = sql.indexOf(
")");
00171
if ((closingParenthesisIdx == -1) || (closingParenthesisIdx > endIdx))
00172 {
00173 tableName = null;
00174 columns = null;
00175
throw new SQLException(
00176
"Syntax error in columns definition for this INSERT statement: '"
00177 + sqlQuery +
"'");
00178 }
00179
00180
00181 StringTokenizer columnTokens =
new StringTokenizer(sql.substring(
00182 openParenthesisIdx + 1, closingParenthesisIdx),
",");
00183 columns =
new ArrayList();
00184
DatabaseColumn col = null;
00185
while (columnTokens.hasMoreTokens())
00186 {
00187 String token = columnTokens.nextToken().trim();
00188
if ((col = t.
getColumn(token)) == null)
00189 {
00190 tableName = null;
00191 columns = null;
00192
throw new SQLException(
"Unknown column name '" + token
00193 +
"' in this INSERT statement: '" + sqlQuery +
"'");
00194 }
00195
else
00196 {
00197 columns.add(
new TableColumn(tableName, col.
getName()));
00198 }
00199 }
00200 }
00201
else
00202 {
00203
00204 columns =
new ArrayList();
00205 ArrayList cols = t.
getColumns();
00206
int size = cols.size();
00207
for (
int j = 0; j < size; j++)
00208 {
00209 columns.add(
new TableColumn(tableName, ((
DatabaseColumn) cols.get(j))
00210 .getName()));
00211 }
00212 }
00213 }
00214
00215 isParsed =
true;
00216 }
00217
00221 public void cloneParsing(
AbstractRequest request)
00222 {
00223
if (!request.
isParsed())
00224
return;
00225
cloneTableNameAndColumns((
AbstractWriteRequest) request);
00226 isParsed =
true;
00227 }
00228
00233 public boolean isInsert()
00234 {
00235
return true;
00236 }
00237
00242 public boolean isUpdate()
00243 {
00244
return false;
00245 }
00246
00251 public boolean isDelete()
00252 {
00253
return false;
00254 }
00255
00260 public boolean isCreate()
00261 {
00262
return false;
00263 }
00264
00269 public boolean isDrop()
00270 {
00271
return false;
00272 }
00273
00277 public void debug()
00278 {
00279 super.debug();
00280
if (tableName != null)
00281 System.out.println(
"Inserted table: " + tableName);
00282
else
00283 System.out.println(
"No information about inserted table");
00284
00285
if (columns != null)
00286 {
00287 System.out.println(
"Inserted columns:");
00288
for (
int i = 0; i < columns.size(); i++)
00289 System.out.println(
" "
00290 + ((
TableColumn) columns.get(i)).getColumnName());
00291 }
00292
else
00293 System.out.println(
"No information about inserted columns");
00294
00295 System.out.println(
"");
00296 }
00300 public boolean isAlter()
00301 {
00302
return false;
00303 }
00304 }