1 24 25 package org.objectweb.cjdbc.common.sql; 26 27 import java.io.IOException ; 28 import java.io.Serializable ; 29 import java.sql.SQLException ; 30 import java.util.ArrayList ; 31 import java.util.StringTokenizer ; 32 33 import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn; 34 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema; 35 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable; 36 import org.objectweb.cjdbc.common.sql.schema.TableColumn; 37 import org.objectweb.cjdbc.common.stream.CJDBCInputStream; 38 import org.objectweb.cjdbc.common.stream.CJDBCOutputStream; 39 40 51 public class InsertRequest extends AbstractWriteRequest implements Serializable 52 { 53 private static final long serialVersionUID = 4184570467338568383L; 54 55 private final boolean returnsRS; 59 78 public InsertRequest(String sqlQuery, boolean escapeProcessing, int timeout, 79 String lineSeparator, DatabaseSchema schema, int granularity, 80 boolean isCaseSensitive, boolean isRead) throws SQLException 81 { 82 this(sqlQuery, escapeProcessing, timeout, lineSeparator, isRead); 83 parse(schema, granularity, isCaseSensitive); 84 } 85 86 103 public InsertRequest(String sqlQuery, boolean escapeProcessing, int timeout, 104 String lineSeparator, boolean isRead) 105 { 106 super(sqlQuery, escapeProcessing, timeout, lineSeparator, 107 RequestType.INSERT); 108 this.returnsRS = isRead; 109 } 110 111 114 public InsertRequest(CJDBCInputStream in) throws IOException 115 { 116 super(in, RequestType.INSERT); 117 returnsRS = in.readBoolean(); 118 if (returnsRS) 119 receiveResultSetParams(in); 120 121 } 122 123 127 public void sendToStream(CJDBCOutputStream out, boolean needSkeleton) 128 throws IOException 129 { 130 super.sendToStream(out, needSkeleton); 131 out.writeBoolean(returnsRS); 132 if (returnsRS) 133 sendResultSetParams(out); 134 } 135 136 143 public void parse(DatabaseSchema schema, int granularity, 144 boolean isCaseSensitive) throws SQLException 145 { 146 if (granularity == ParsingGranularities.NO_PARSING) 147 { 148 isParsed = true; 149 return; 150 } 151 152 if (schema == null) 154 throw new SQLException ( 155 "Unable to parse request with an undefined database schema"); 156 157 String originalSQL = this.trimCarriageReturnAndTabs(); 158 String sql = originalSQL.toLowerCase(); 159 160 sql = sql.substring(7).trim().substring(5).trim(); 162 163 int endIdx = sql.indexOf(" values "); 165 if (endIdx == -1) 166 { 167 endIdx = sql.indexOf(" values("); 168 if (endIdx == -1) 169 { 170 endIdx = sql.indexOf("select "); 171 if (endIdx == -1) 172 throw new SQLException ( 173 "Unable to find the VALUES or SELECT keyword in this INSERT statement: '" 174 + sqlQuery + "'"); 175 } 176 } 177 178 if (isCaseSensitive) 179 { 180 int shift = originalSQL.length() - sql.length(); 181 sql = originalSQL.substring(shift, shift + endIdx).trim(); 182 } 183 else 184 sql = sql.substring(0, endIdx).trim(); 185 186 int openParenthesisIdx = sql.indexOf("("); 187 188 String insertTable; 190 if (openParenthesisIdx == -1) 191 insertTable = sql; 193 else 194 insertTable = sql.substring(0, openParenthesisIdx).trim(); 196 197 DatabaseTable t = schema.getTable(insertTable, isCaseSensitive); 198 if (t == null) 199 throw new SQLException ("Unknown table '" + insertTable 200 + "' in this INSERT statement: '" + sqlQuery + "'"); 201 else 202 tableName = t.getName(); 203 204 if ((granularity == ParsingGranularities.COLUMN) 205 || (granularity == ParsingGranularities.COLUMN_UNIQUE)) 206 { 207 if (openParenthesisIdx != -1) 208 { 209 int closingParenthesisIdx = sql.indexOf(")"); 211 if ((closingParenthesisIdx == -1) || (closingParenthesisIdx > endIdx)) 212 { 213 tableName = null; 214 columns = null; 215 throw new SQLException ( 216 "Syntax error in columns definition for this INSERT statement: '" 217 + sqlQuery + "'"); 218 } 219 220 StringTokenizer columnTokens = new StringTokenizer (sql.substring( 222 openParenthesisIdx + 1, closingParenthesisIdx), ","); 223 columns = new ArrayList (); 224 DatabaseColumn col = null; 225 while (columnTokens.hasMoreTokens()) 226 { 227 String token = columnTokens.nextToken().trim(); 228 if ((col = t.getColumn(token)) == null) 229 { 230 tableName = null; 231 columns = null; 232 throw new SQLException ("Unknown column name '" + token 233 + "' in this INSERT statement: '" + sqlQuery + "'"); 234 } 235 else 236 { 237 columns.add(new TableColumn(tableName, col.getName())); 238 } 239 } 240 } 241 else 242 { 243 columns = new ArrayList (); 245 ArrayList cols = t.getColumns(); 246 int size = cols.size(); 247 for (int j = 0; j < size; j++) 248 { 249 columns.add(new TableColumn(tableName, ((DatabaseColumn) cols.get(j)) 250 .getName())); 251 } 252 } 253 } 254 255 isParsed = true; 256 } 257 258 261 public void cloneParsing(AbstractRequest request) 262 { 263 if (!request.isParsed()) 264 return; 265 cloneTableNameAndColumns((AbstractWriteRequest) request); 266 isParsed = true; 267 } 268 269 272 public boolean needsMacroProcessing() 273 { 274 return true; 275 } 276 277 280 public boolean returnsResultSet() 281 { 282 return returnsRS; 283 } 284 285 288 public void debug() 289 { 290 super.debug(); 291 if (tableName != null) 292 System.out.println("Inserted table: " + tableName); 293 else 294 System.out.println("No information about inserted table"); 295 296 if (columns != null) 297 { 298 System.out.println("Inserted columns:"); 299 for (int i = 0; i < columns.size(); i++) 300 System.out.println(" " 301 + ((TableColumn) columns.get(i)).getColumnName()); 302 } 303 else 304 System.out.println("No information about inserted columns"); 305 306 System.out.println(""); 307 } 308 309 } | Popular Tags |