const string DBMS::Delete (const string& tbname, Poliz& whereCl) { int affR = 0; if (getTableStruct(tbname).numRecords == 0) return "OK. \n0 affected rows.\n\n"; lockTable(tbname); moveToStart(tbname); Lex res; IntWrap ind = nameMap[tbname]; if (!ind || deleted[ind]) throw TableNotFound(); while (!afterTableEnd(tbname)) { evaluateExpr(tbname, whereCl); res = pst[0]; if (res.get_type() != Lex_Bool) { throw TypeMismatch("WHERE-clause expected a boolean expression as it's argument."); } if (res.get_value()) // подходит под where-клаузу { tables[ind]->removeCurrentRecord(); ++affR; } else moveNext(tbname); } moveToStart(tbname); unlockTable(tbname); stringstream s; s << "OK.\n" << affR << " affected rows.\n\n"; return s.str(); }
const string DBMS::Update(const string& tbname, const string& fdname, Poliz& expr, Poliz& whereCl) { int affR = 0; lockTable(tbname); if (getTableStruct(tbname).numRecords == 0) return "OK.\n0 affected rows.\n\n"; moveToStart(tbname); Lex res; stringstream buf; ITBField* fdp; IntWrap ind = nameMap[tbname]; if (!ind || deleted[ind]) throw TableNotFound(); while (!afterTableEnd(tbname)) { evaluateExpr(tbname, whereCl); res = pst[0]; if (res.get_type() != Lex_Bool) { throw TypeMismatch("WHERE-clause expected a boolean expression as it's argument."); } if (res.get_value()) // подходит под where-клаузу { ++affR; pst.clear(); evaluateExpr(tbname, expr); res = pst[0]; fdp = getField(tbname, fdname); if (getFieldType(tbname, fdname) == Long) { if (res.get_type() != Lex_Num) { throw TypeMismatch("Type mismatch in UPDATE: LONG expression expected."); } buf << res.get_value() << endl; fdp->read(buf); } else { if (res.get_type() != Lex_Str) { throw TypeMismatch("Type mismatch in UPDATE: LONG expression expected."); } buf << res.get_str_value() << endl; fdp->read(buf); } } moveNext(tbname); } moveToStart(tbname); unlockTable(tbname); stringstream s; s << "OK.\n" << affR << " affected rows.\n\n"; return s.str(); }
const string DBMS::Select(const string& tbname, vector<string>& fdList, Poliz& whereCl) { stringstream result; if (fdList.size() == 1 && fdList[0] == "*") { fdList = getTableStruct(tbname).names; } Table* tab = new Table("from "+tbname); tab->tbstr.names = fdList; tab->tbstr.numFields = fdList.size(); tab->tbstr.numRecords = 0; if (getTableStruct(tbname).numRecords == 0) { result << *tab << endl << "OK." << endl; return result.str(); } moveToStart(tbname); IntWrap ind; for (int i = 0; i < tab->tbstr.numFields; ++i) { tab->tbstr.types.push_back( getFieldType(tbname, fdList[i]) ); tab->tbstr.nameMap[fdList[i]] = i; } Lex res; stringstream buf; ITBField* buf_fd; while (!afterTableEnd(tbname)) { evaluateExpr(tbname, whereCl); res = pst[0]; if (res.get_type() != Lex_Bool) { throw TypeMismatch("WHERE-clause expected a boolean expression as it's argument."); } if (res.get_value()) // подходит под where-клаузу { tab->records.push_back( new ITBField* [tab->tbstr.numFields] ); for (int k = 0; k < tab->tbstr.numFields; ++k) { if (tab->tbstr.types[k] == Long) { (*(tab->records.rbegin()))[k] = new TBFieldLong(); } else { (*(tab->records.rbegin()))[k] = new TBFieldText(); } IntWrap ii = tab->tbstr.nameMap[fdList[k]]; if (!ii) { throw FieldNotFound(); } buf_fd = getField(tbname, tab->tbstr.names[ii]); buf_fd->write(buf, false); buf << endl; (*(tab->records.rbegin()))[k]->read(buf); } ++(tab->tbstr.numRecords); if (tab->currentId == -1) { tab->currentId = 0; tab->currentRecord = tab->records.begin(); } } moveNext(tbname); } result << *tab << endl << "OK.\n\n" << endl; moveToStart(tbname); return result.str(); }