/** * Method declaration * * ALTER TABLE tableName ADD COLUMN columnName columnType; * ALTER TABLE tableName DELETE COLUMN columnName; * * <B>Note: </B>The only change I've made to Sergio's original code was * changing the insert's to call insertNoCheck to bypass the trigger * mechanism that is a part of hsqldb 1.60 and beyond. - Mark Tutt * * @return * * @throws Exception */ public Result processAlter() { tTokenizer.getThis("TABLE"); string token = tTokenizer.getstring(); cChannel.checkReadWrite(); // cChannel.check(token,Access.ALTER); --> Accessul nu-l inca controleaza... string tName = token; string swap = tName + "SWAP"; // nimicirea swapului... dDatabase.execute("DROP TABLE " + swap, cChannel); Table initialTable = dDatabase.getTable(token, cChannel); int count = 0; token = tTokenizer.getstring(); if (token.Equals("ADD")) { token = tTokenizer.getstring(); if (token.Equals("COLUMN")) { Table swapTable = new Table(dDatabase, true, swap, initialTable.isCached()); // copiem coloanele (fara date) din tabelul initial in swap for (int i = 0; i < initialTable.getColumnCount(); i++) { Column aColumn = initialTable.getColumn(i); swapTable.addColumn(aColumn); } // end Of copiem coloanele... // aflam daca are PrimaryKey & o cream... string cName = tTokenizer.getstring(); string cType = tTokenizer.getstring(); int iType = Column.getTypeNr(cType); string sToken = cType; // int primarykeycolumn = -1; bool identity = false; int column = initialTable.getColumnCount() + 1; // !-- // stolen from CREATE TABLE... string sColumn = cName; if (iType == Column.VARCHAR && dDatabase.isIgnoreCase()) { iType = Column.VARCHAR_IGNORECASE; } sToken = tTokenizer.getstring(); if (iType == Column.DOUBLE && sToken.Equals("PRECISION")) { sToken = tTokenizer.getstring(); } if (sToken.Equals("(")) { // overread length do { sToken = tTokenizer.getstring(); } while (!sToken.Equals(")")); sToken = tTokenizer.getstring(); } // !-- bool nullable = true; if (sToken.Equals("NULL")) { sToken = tTokenizer.getstring(); } else if (sToken.Equals("NOT")) { tTokenizer.getThis("NULL"); nullable = false; sToken = tTokenizer.getstring(); } /* * if(sToken.Equals("IDENTITY")) { * identity=true; * Trace.check(primarykeycolumn==-1,Trace.SECOND_PRIMARY_KEY,sColumn); * sToken=tTokenizer.getstring(); * primarykeycolumn=column; * } * * if(sToken.Equals("PRIMARY")) { * tTokenizer.getThis("KEY"); * Trace.check(identity || primarykeycolumn==-1, * Trace.SECOND_PRIMARY_KEY,sColumn); * primarykeycolumn=column; * //sToken=tTokenizer.getstring(); * } * //end of STOLEN... */ swapTable.addColumn(cName, iType, nullable, identity); // under construction... if (initialTable.getColumnCount() < initialTable.getInternalColumnCount()) { swapTable.createPrimaryKey(); } else { swapTable.createPrimaryKey(initialTable.getPrimaryIndex().getColumns()[0]); } // endof PrimaryKey... // sa ne farimam cu indicii... ;-(( Index idx = null; while (true) { idx = initialTable.getNextIndex(idx); if (idx == null) { break; } if (idx == initialTable.getPrimaryIndex()) { continue; } swapTable.createIndex(idx); } // end of Index... cChannel.commit(); dDatabase.linkTable(swapTable); Tokenizer tmpTokenizer = new Tokenizer("SELECT * FROM " + tName); Parser pp = new Parser(dDatabase, tmpTokenizer, cChannel); string ff = tmpTokenizer.getstring(); if (!initialTable.isEmpty()) { Record n = ((Result) pp.processSelect()).rRoot; do { object[] row = swapTable.getNewRow(); object[] row1 = n.data; for (int i = 0; i < initialTable.getColumnCount(); i++) { row[i] = row1[i]; } swapTable.insertNoCheck(row, cChannel); n = n.next; } while (n != null); } dDatabase.execute("DROP TABLE " + tName, cChannel); // cream tabelul vechi cu proprietatile celui nou... initialTable = new Table(dDatabase, true, tName, swapTable.isCached()); for (int i = 0; i < swapTable.getColumnCount(); i++) { Column aColumn = swapTable.getColumn(i); initialTable.addColumn(aColumn); } if (swapTable.getColumnCount() < swapTable.getInternalColumnCount()) { initialTable.createPrimaryKey(); } else { initialTable.createPrimaryKey(swapTable.getPrimaryIndex().getColumns()[0]); } // endof PrimaryKey... // sa ne farimam cu indicii... ;-(( idx = null; while (true) { idx = swapTable.getNextIndex(idx); if (idx == null) { break; } if (idx == swapTable.getPrimaryIndex()) { continue; } initialTable.createIndex(idx); } // end of Index... cChannel.commit(); dDatabase.linkTable(initialTable); // end of cream... // copiem datele din swap in tabel... tmpTokenizer = new Tokenizer("SELECT * FROM " + swap); pp = new Parser(dDatabase, tmpTokenizer, cChannel); ff = tmpTokenizer.getstring(); if (!swapTable.isEmpty()) { Record n = ((Result) pp.processSelect()).rRoot; do { object[] row = initialTable.getNewRow(); object[] row1 = n.data; for (int i = 0; i < swapTable.getColumnCount(); i++) { row[i] = row1[i]; } initialTable.insertNoCheck(row, cChannel); n = n.next; } while (n != null); // end of copiem... } dDatabase.execute("DROP TABLE " + swap, cChannel); count = 4; } else { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } } else if (token.Equals("DELETE")) { token = tTokenizer.getstring(); if (token.Equals("COLUMN")) { Table swapTable = new Table(dDatabase, true, swap, initialTable.isCached()); string cName = tTokenizer.getstring(); int undesired = initialTable.getColumnNr(cName); for (int i = 0; i < initialTable.getColumnCount(); i++) { Column aColumn = initialTable.getColumn(i); if (i != undesired) { swapTable.addColumn(aColumn); } } int pKey = -1; // !-- if (initialTable.getColumnCount() < initialTable.getInternalColumnCount()) { swapTable.createPrimaryKey(); } else { int[] cols = initialTable.getPrimaryIndex().getColumns(); pKey = cols[0]; if ((cols[0] > undesired) || (cols[0] + cols.Length < undesired)) { if (undesired < initialTable.getPrimaryIndex().getColumns()[0]) { // reindexarea... for (int i = 0; i < cols.Length; i++) { cols[i]--; } // endOf reindexarea... } // MT: This initially wouldn't compile, missing the array index on cols[] swapTable.createPrimaryKey(cols[0]); } else { swapTable.createPrimaryKey(); } } // endof PrimaryKey... // sa ne farimam cu indicii... ;-(( Index idx = null; while (true) { idx = initialTable.getNextIndex(idx); if (idx == null) { break; } if (idx == initialTable.getPrimaryIndex()) { continue; } bool flag = true; int[] cols = idx.getColumns(); for (int i = 0; i < cols.Length; i++) { if (cols[i] == undesired) { flag = false; } } if (flag) { Index tIdx; for (int i = 0; i < cols.Length; i++) { if (cols[i] > undesired) { cols[i]--; } } tIdx = new Index(idx.getName(), idx.getColumns(), idx.getType(), idx.isUnique()); swapTable.createIndex(tIdx); } } // !-- cChannel.commit(); dDatabase.linkTable(swapTable); Tokenizer tmpTokenizer = new Tokenizer("SELECT * FROM " + tName); Parser pp = new Parser(dDatabase, tmpTokenizer, cChannel); string ff = tmpTokenizer.getstring(); if (!initialTable.isEmpty()) { Record n = ((Result) pp.processSelect()).rRoot; do { object[] row = swapTable.getNewRow(); object[] row1 = n.data; int j = 0; for (int i = 0; i < initialTable.getColumnCount(); i++) { if (i != undesired) { row[j] = row1[i]; j++; } } swapTable.insertNoCheck(row, cChannel); n = n.next; } while (n != null); } dDatabase.execute("DROP TABLE " + tName, cChannel); // cream tabelul vechi cu proprietatile celui nou... initialTable = new Table(dDatabase, true, tName, swapTable.isCached()); for (int i = 0; i < swapTable.getColumnCount(); i++) { Column aColumn = swapTable.getColumn(i); initialTable.addColumn(aColumn); } // !-- if (swapTable.getColumnCount() < swapTable.getInternalColumnCount()) { initialTable.createPrimaryKey(); } else { initialTable.createPrimaryKey(swapTable.getPrimaryIndex().getColumns()[0]); } // endof PrimaryKey... // sa ne farimam cu indicii... ;-(( idx = null; while (true) { idx = swapTable.getNextIndex(idx); if (idx == null) { break; } if (idx == swapTable.getPrimaryIndex()) { continue; } initialTable.createIndex(idx); } // end of Index... // !-- cChannel.commit(); dDatabase.linkTable(initialTable); // end of cream... // copiem datele din swap in tabel... tmpTokenizer = new Tokenizer("SELECT * FROM " + swap); pp = new Parser(dDatabase, tmpTokenizer, cChannel); ff = tmpTokenizer.getstring(); if (!swapTable.isEmpty()) { Record n = ((Result) pp.processSelect()).rRoot; do { object[] row = initialTable.getNewRow(); object[] row1 = n.data; for (int i = 0; i < swapTable.getColumnCount(); i++) { row[i] = row1[i]; } initialTable.insertNoCheck(row, cChannel); n = n.next; } while (n != null); // end of copiem... } dDatabase.execute("DROP TABLE " + swap, cChannel); count = 3; } else { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } count = 3; } Result r = new Result(); r.iUpdateCount = count; // --> nu tre inca...?? return r; }
/** * Constructor declaration * * * @param db * @param t * @param channel */ public Parser(Database db, Tokenizer t, Channel channel) { dDatabase = db; tTokenizer = t; cChannel = channel; }