int dmModule::modifyRegDoc(TDocumentItem item,QString op){ QString string_query = "execute procedure regdoc_modify(:did,:wid,:gid,:smid,:direct,:gcount,:operid)"; QSqlQuery *query = new QSqlQuery(dmModule::db); query->prepare(string_query); query->bindValue(":did", item.id); query->bindValue(":wid",wid); query->bindValue(":gid",item.getGoodId()); query->bindValue(":smid",item.getMetricId()); query->bindValue(":direct",item.getDirect()); query->bindValue(":gcount",item.getCount()); query->bindValue(":operid",op); query->exec(); if(query->lastError().isValid()){ QMessageBox::about(0,"errror",query->lastError().text()+query->executedQuery()); delete query; return 0; } delete query; return 1;}
int dmModule::modifyRegInventory(const int gid, const int smid, const int wid,const int amid, const double count){ QString query_str = "execute procedure reginventory_modify(?,?,?,?,?)"; QSqlQuery *query = new QSqlQuery(dmModule::db); query->prepare(query_str); query->addBindValue(gid); query->addBindValue(smid); query->addBindValue(wid); query->addBindValue(-1); query->addBindValue(count); query->setForwardOnly(true); query->exec(); if(query->lastError().isValid()){ QMessageBox::about(0,"errror",query->lastError().text()+query->executedQuery()); delete query; return 0; } delete query; return 1;}
int dmModule::modifyReceived(TDocument doc, QTableWidget *tb){ for(int i = 0; i < tb->rowCount();i++){//тут создан объект - возможна ошибка TDocumentItem item(tb,doc.id,i); item.setDirect(doc.getDirect()); if(!modifyRegDoc(item,QString("new"))){//проблемы тут - нет роллбека - откатывается только последняя строка - а не все dmBaseModule::db.rollback(); return 0; } if(!modifyRegInventory(item.getGoodId(), item.getMetricId(), doc.getWereHouse(), doc.id, item.getCount())){ dmBaseModule::db.rollback(); return 0; } } QSqlQuery *query = new QSqlQuery(dmModule::db); QString query_str = "execute procedure isactive_modify(?,?)"; query->prepare(query_str); query->addBindValue(doc.id); query->addBindValue(1); query->setForwardOnly(true); query->exec(); if(query->lastError().isValid()){ QMessageBox::about(0,"errror",query->lastError().text()+query->executedQuery()); dmBaseModule::db.rollback(); delete query; return 0; }//по идее только здесь должно все за комиитится dmBaseModule::db.commit(); delete query;}
C++ (Qt)int dmModule::modifyReceived(TDocument doc, QTableWidget *tb) { //QSqlDatabase::transaction(); dmBaseModule::db.transaction(); ... dmBaseModule::db.commit(); delete query;}
int dmModule::modifyReceived(TDocument doc, QTableWidget *tb){//транзакцию начали dmBaseModule::db.transaction(); for(int i = 0; i < tb->rowCount();i++){ TDocumentItem item(tb,doc.id,i); item.setDirect(doc.getDirect());//тут в одной из итераций есть проблема if(!modifyRegDoc(item,QString("new"))){ dmBaseModule::db.rollback(); return 0; } if(!modifyRegInventory(item.getGoodId(), item.getMetricId(), doc.getWereHouse(), doc.id, item.getCount())){ dmBaseModule::db.rollback(); return 0; } } QSqlQuery *query = new QSqlQuery(dmModule::db); QString query_str = "execute procedure isactive_modify(?,?)"; query->prepare(query_str); query->addBindValue(doc.id); query->addBindValue(1); query->setForwardOnly(true); query->exec(); if(query->lastError().isValid()){ QMessageBox::about(0,"errror",query->lastError().text()+query->executedQuery()); dmBaseModule::db.rollback(); delete query; return 0; } dmBaseModule::db.commit(); delete query;}
class dmBaseModule : public QObject{Q_OBJECTpublic: explicit dmBaseModule(QObject *parent = 0); static void setDatabase(QSqlDatabase &in){dmBaseModule::db = in;}; static QSqlDatabase& getDatabase(){return dmBaseModule::db;} virtual int loadTableListGoods(QObject *sender,const int &id =-1,const int &parentid =-1,const int &isgroup=0,const int &iscomplect=0)=0;signals:public slots:protected: static QSqlDatabase db;};
class dmModule : public dmBaseModule{Q_OBJECTpublic: explicit dmModule(QObject *parent = 0); virtual int loadTableListGoods(QObject *sender,const int &id =-1,const int &parentid =-1,const int &isgroup=0,const int &iscomplect=0);<skip>...</skip>