/** * Method declaration * * * @return * * @throws Exception */ public Result processUpdate() { string token = tTokenizer.getstring(); cChannel.checkReadWrite(); cChannel.check(token, Access.UPDATE); Table table = dDatabase.getTable(token, cChannel); TableFilter filter = new TableFilter(table, null, false); tTokenizer.getThis("SET"); ArrayList vColumn = new ArrayList(); ArrayList eColumn = new ArrayList(); int len = 0; token = null; do { len++; int i = table.getColumnNr(tTokenizer.getstring()); vColumn.Add(i); tTokenizer.getThis("="); Expression e = parseExpression(); e.resolve(filter); eColumn.Add(e); token = tTokenizer.getstring(); } while (token.Equals(",")); Expression eCondition = null; if (token.Equals("WHERE")) { eCondition = parseExpression(); eCondition.resolve(filter); filter.setCondition(eCondition); } else { tTokenizer.back(); } // do the update Expression[] exp = new Expression[len]; eColumn.CopyTo(exp); int[] col = new int[len]; int[] type = new int[len]; for (int i = 0; i < len; i++) { col[i] = ((int) vColumn[i]); type[i] = table.getType(col[i]); } int count = 0; if (filter.findFirst()) { Result del = new Result(); // don't need column count and so on Result ins = new Result(); int size = table.getColumnCount(); do { if (eCondition == null || eCondition.test()) { object[] nd = filter.oCurrentData; del.add(nd); object[] ni = table.getNewRow(); for (int i = 0; i < size; i++) { ni[i] = nd[i]; } for (int i = 0; i < len; i++) { ni[col[i]] = exp[i].getValue(type[i]); } ins.add(ni); } } while (filter.next()); cChannel.beginNestedTransaction(); try { Record nd = del.rRoot; while (nd != null) { table.deleteNoCheck(nd.data, cChannel); nd = nd.next; } Record ni = ins.rRoot; while (ni != null) { table.insertNoCheck(ni.data, cChannel); ni = ni.next; count++; } table.checkUpdate(col, del, ins); ni = ins.rRoot; while (ni != null) { ni = ni.next; } cChannel.endNestedTransaction(false); } catch (Exception e) { // update failed (violation of primary key / referential integrity) cChannel.endNestedTransaction(true); throw e; } } Result r = new Result(); r.iUpdateCount = count; return r; }
// [email protected] end changes from 1.50 // [email protected] begin changes from 1.50 public Result getResult(int start, int cnt) { int maxrows = start + cnt; //<-new, cut definitly // [email protected] begin changes from 1.50 resolve(); checkResolved(); if (sUnion != null && sUnion.iResultLen != iResultLen) { throw Trace.error(Trace.COLUMN_COUNT_DOES_NOT_MATCH); } int len = eColumn.Length; Result r = new Result(len); bool aggregated = false; bool grouped = false; for (int i = 0; i < len; i++) { Expression e = eColumn[i]; r.iType[i] = e.getDataType(); if (e.isAggregate()) { aggregated = true; } } object[] agg = null; if (aggregated) { agg = new object[len]; } if (iGroupLen > 0) { // has been set in Parser grouped = true; } bool simple_maxrows = false; if (maxrows != 0 && grouped == false && sUnion == null && iOrderLen == 0) { simple_maxrows = true; } else { simple_maxrows = false; } int count = 0; int filter = tFilter.Length; bool[] first = new bool[filter]; int level = 0; while (level >= 0) { TableFilter t = tFilter[level]; bool found; if (!first[level]) { found = t.findFirst(); first[level] = found; } else { found = t.next(); first[level] = found; } if (!found) { level--; continue; } if (level < filter - 1) { level++; continue; } if (eCondition == null || eCondition.test()) { object[] row = new object[len]; for (int i = 0; i < len; i++) { row[i] = eColumn[i].getValue(); } count++; if (aggregated) { updateAggregateRow(agg, row, len); } else { r.add(row); if (simple_maxrows && count >= maxrows) { break; } } } } if (aggregated && !grouped) { addAggregateRow(r, agg, len, count); } else if (grouped) { int[] order = new int[iGroupLen]; int[] way = new int[iGroupLen]; for (int i = iResultLen, j = 0; j < iGroupLen; i++, j++) { order[j] = i; way[j] = 1; } r = sortResult(r, order, way); Record n = r.rRoot; Result x = new Result(len); for (int i = 0; i < len; i++) { x.iType[i] = r.iType[i]; } do { object[] row = new object[len]; count = 0; bool newgroup = false; while (n != null && newgroup == false) { count++; for (int i = 0; i < iGroupLen; i++) { if (n.next == null) { newgroup = true; } else if (Column.compare(n.data[i], n.next.data[i], r.iType[i]) != 0) { // can't use .Equals because 'null' is also one group newgroup = true; } } updateAggregateRow(row, n.data, len); n = n.next; } addAggregateRow(x, row, len, count); } while (n != null); r = x; } if (iOrderLen != 0) { int[] order = new int[iOrderLen]; int[] way = new int[iOrderLen]; for (int i = iResultLen, j = 0; j < iOrderLen; i++, j++) { order[j] = i; way[j] = eColumn[i].isDescending() ? -1 : 1; } r = sortResult(r, order, way); } // the result is maybe bigger (due to group and order by) // but don't tell this anybody else r.setColumnCount(iResultLen); if (bDistinct) { r = removeDuplicates(r); } for (int i = 0; i < iResultLen; i++) { Expression e = eColumn[i]; r.sLabel[i] = e.getAlias(); r.sTable[i] = e.getTableName(); r.sName[i] = e.getColumnName(); } if (sUnion != null) { Result x = sUnion.getResult(0); if (iUnionType == UNION) { r.append(x); r = removeDuplicates(r); } else if (iUnionType == UNIONALL) { r.append(x); } else if (iUnionType == INTERSECT) { r = removeDuplicates(r); x = removeDuplicates(x); r = removeDifferent(r, x); } else if (iUnionType == EXCEPT) { r = removeDuplicates(r); x = removeDuplicates(x); r = removeSecond(r, x); } } if (maxrows > 0 && !simple_maxrows) { trimResult(r, maxrows); } // [email protected] begin changes from 1.50 if (start > 0) { //then cut the first 'start' elements trimResultFront(r, start); } // [email protected] end changes from 1.50 return(r); }
/** * Method declaration * * * @return * * @throws Exception */ public Result processDelete() { tTokenizer.getThis("FROM"); string token = tTokenizer.getstring(); cChannel.checkReadWrite(); cChannel.check(token, Access.DELETE); Table table = dDatabase.getTable(token, cChannel); TableFilter filter = new TableFilter(table, null, false); token = tTokenizer.getstring(); Expression eCondition = null; if (token.Equals("WHERE")) { eCondition = parseExpression(); eCondition.resolve(filter); filter.setCondition(eCondition); } else { tTokenizer.back(); } int count = 0; if (filter.findFirst()) { Result del = new Result(); // don't need column count and so on do { if (eCondition == null || eCondition.test()) { del.add(filter.oCurrentData); } } while (filter.next()); Record n = del.rRoot; while (n != null) { table.delete(n.data, cChannel); count++; n = n.next; } } Result r = new Result(); r.iUpdateCount = count; return r; }