1 24 25 package org.objectweb.cjdbc.common.sql; 26 27 import java.io.IOException ; 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 31 import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn; 32 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema; 33 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable; 34 import org.objectweb.cjdbc.common.sql.schema.TableColumn; 35 import org.objectweb.cjdbc.common.stream.CJDBCInputStream; 36 37 43 public class AlterRequest extends AbstractWriteRequest 44 { 45 private static final long serialVersionUID = -6511969618172455539L; 46 47 48 private transient DatabaseTable table = null; 49 50 51 private transient DatabaseColumn column = null; 52 53 private transient boolean isDrop = false; 54 private transient boolean isAdd = false; 55 56 71 public AlterRequest(String sqlQuery, boolean escapeProcessing, int timeout, 72 String lineSeparator) 73 { 74 super(sqlQuery, escapeProcessing, timeout, lineSeparator, RequestType.ALTER); 75 } 76 77 95 public AlterRequest(String sqlQuery, boolean escapeProcessing, int timeout, 96 String lineSeparator, DatabaseSchema schema, int granularity, 97 boolean isCaseSensitive) throws SQLException 98 { 99 this(sqlQuery, escapeProcessing, timeout, lineSeparator); 100 parse(schema, granularity, isCaseSensitive); 101 } 102 103 106 public AlterRequest(CJDBCInputStream in) throws IOException 107 { 108 super(in, RequestType.ALTER); 109 } 110 111 114 public boolean needsMacroProcessing() 115 { 116 return false; 117 } 118 119 122 public boolean returnsResultSet() 123 { 124 return false; 125 } 126 127 131 public void parse(DatabaseSchema schema, int granularity, 132 boolean isCaseSensitive) throws SQLException 133 { 134 138 139 if (granularity == ParsingGranularities.NO_PARSING) 140 { 141 isParsed = true; 142 return; 143 } 144 145 String originalSQL = this.trimCarriageReturnAndTabs(); 146 String sql = originalSQL.toLowerCase(); 147 148 int tableIdx = sql.indexOf("table"); 150 if (tableIdx == -1) 151 throw new SQLException ( 152 "Malformed Alter Request. Should start with [ALTER TABLE]"); 153 sql = sql.substring(tableIdx + 5).trim(); 154 155 int addIdx = sql.indexOf(" add "); 157 158 int dropIdx = sql.indexOf(" drop "); 160 161 if (addIdx != -1) 162 isAdd = true; 163 if (dropIdx != -1) 164 isDrop = true; 165 166 if (!isAdd && !isDrop) 167 throw new SQLException ( 168 "Malformed Alter Request. No drop or add condition"); 169 170 if (isCaseSensitive) sql = originalSQL.substring(originalSQL.length() - sql.length()); 172 173 int index = (isAdd) ? addIdx : dropIdx; 174 175 tableName = sql.substring(0, index).trim(); 176 table = new DatabaseTable(tableName); 177 178 if (granularity > ParsingGranularities.TABLE) 179 { 180 181 int subsIndex = index + 6 + 2; if (isAdd) 185 subsIndex += 3; 186 else 187 subsIndex += 4; 189 190 columns = new ArrayList (); 191 sql = sql.substring(subsIndex).trim(); 192 193 if (isAdd) 194 { 195 int colIndex = sql.indexOf(' '); 196 String colName = sql.substring(0, colIndex); 197 198 int uniqueIndex = sql.toLowerCase().indexOf("unique"); 199 int primary = sql.toLowerCase().indexOf("primary"); 200 if (uniqueIndex != -1 || primary != -1) 201 column = new DatabaseColumn(colName, true); 202 else 203 column = new DatabaseColumn(colName, false); 204 columns.add(new TableColumn(tableName, colName)); 205 } 206 else if (isDrop) 207 { 208 String colName = sql.trim(); 209 column = schema.getTable(tableName).getColumn(colName); 210 columns.add(new TableColumn(tableName, colName)); 211 } 212 } 213 isParsed = true; 214 } 215 216 219 public void cloneParsing(AbstractRequest request) 220 { 221 if (!request.isParsed()) 222 return; 223 AlterRequest alterRequest = (AlterRequest) request; 224 cloneTableNameAndColumns((AbstractWriteRequest) request); 225 table = alterRequest.getDatabaseTable(); 226 column = alterRequest.getColumn(); 227 isParsed = true; 228 } 229 230 235 public DatabaseTable getDatabaseTable() 236 { 237 return table; 238 } 239 240 245 public DatabaseColumn getColumn() 246 { 247 return column; 248 } 249 250 255 public boolean isAdd() 256 { 257 return isAdd; 258 } 259 } | Popular Tags |