bool IOMapSerialize::saveHouse(Database* db, House* house) { DBQuery query; query << "UPDATE `houses` SET `owner` = " << house->getOwner() << ", `paid` = " << house->getPaidUntil() << ", `warnings` = " << house->getRentWarnings() << ", `lastwarning` = " << house->getLastWarning() << ", `clear` = 0 WHERE `id` = " << house->getId() << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << db->getUpdateLimiter(); if(!db->executeQuery(query.str())) return false; query.str(""); query << "DELETE FROM `house_lists` WHERE `house_id` = " << house->getId() << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID); if(!db->executeQuery(query.str())) return false; DBInsert queryInsert(db); queryInsert.setQuery("INSERT INTO `house_lists` (`house_id`, `world_id`, `listid`, `list`) VALUES "); std::string listText; if(house->getAccessList(GUEST_LIST, listText) && !listText.empty()) { query.str(""); query << house->getId() << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << GUEST_LIST << ", " << db->escapeString(listText); if(!queryInsert.addRow(query.str())) return false; } if(house->getAccessList(SUBOWNER_LIST, listText) && !listText.empty()) { query.str(""); query << house->getId() << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << SUBOWNER_LIST << ", " << db->escapeString(listText); if(!queryInsert.addRow(query.str())) return false; } for(HouseDoorList::iterator it = house->getHouseDoorBegin(); it != house->getHouseDoorEnd(); ++it) { const Door* door = (*it); if(!door) continue; if(door->getAccessList(listText) && !listText.empty()) { query.str(""); query << house->getId() << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << door->getDoorId() << ", " << db->escapeString(listText); if(!queryInsert.addRow(query.str())) return false; } } return query.str().empty() || queryInsert.execute(); }
/*! * \brief 将连线图翻译成为代码 */ void Translate::translateCode() { qDebug() << "开始翻译"; translateProgress_ = 0; //////////////读取翻译缓冲用数据库//////////// QSqlDatabase dbBuffer=QSqlDatabase::addDatabase("QSQLITE","Buffer"); dbBuffer.setDatabaseName("./resource/Blocks/Buffer.mpb"); dbBuffer.open(); QSqlDatabase dbDict=QSqlDatabase::addDatabase("QSQLITE","Dict"); //读取翻译缓冲用数据库//////////// ///////翻译之前来一腿!!!!/////////////////// QSqlQuery pQuery_(dbBuffer); int counter = 0; qDebug() << "------1-------"; QFile fileLinkage("Linkage.csv"); if(!fileLinkage.open(QFile::ReadOnly)) { qDebug() << "文件打开失败"; qDebug() << fileLinkage.errorString(); } QTextStream in(&fileLinkage); pQuery_.exec("delete From Linkage"); dbBuffer.transaction(); counter = 0; while (!in.atEnd()) { QString str; in >> str; qDebug() << str; QStringList strs = str.split(","); qDebug() << strs.size(); if(counter < strs.size()) { QString cmd = QString("INSERT INTO Linkage VALUES('%1','%2','%3','%4')").arg(strs.at(0)).arg(strs.at(1)).arg(strs.at(2)).arg(strs.at(3)); qDebug() << cmd; pQuery_.exec(cmd); } counter++; } dbBuffer.commit(); fileLinkage.close(); /////////////////////////////////// qDebug() << "------2-------"; QFile fileParameter("Parameter.csv"); if(!fileParameter.open(QFile::ReadOnly)) { qDebug() << "文件打开失败"; qDebug() << fileParameter.errorString(); } QTextStream in1(&fileParameter); pQuery_.exec("delete From Parameter"); dbBuffer.transaction(); counter = 0; while (!in1.atEnd()) { QString str; in1 >> str; qDebug() << str; QStringList strs = str.split(","); qDebug() << strs.size(); if(counter < strs.size()) { QString cmd = QString("INSERT INTO Parameter VALUES('%1','%2','%3','%4','%5','%6','%7')").arg(strs.at(0)).arg(strs.at(1)).arg(strs.at(2)).arg(strs.at(3)).arg(strs.at(4)).arg(strs.at(5)).arg(strs.at(6)); qDebug() << cmd; pQuery_.exec(cmd); } counter++; } dbBuffer.commit(); fileParameter.close(); /////////////////////////////////// qDebug() << "-------3------"; QFile fileBB("BBList.csv"); if(!fileBB.open(QFile::ReadOnly)) { qDebug() << "文件打开失败"; qDebug() << fileBB.errorString(); } QTextStream in2(&fileBB); counter = 0; pQuery_.exec("delete From BBList"); dbBuffer.transaction(); while (!in2.atEnd()) { QString str; in2 >> str; qDebug() << str; QStringList strs = str.split(","); qDebug() << strs.size(); if(counter < strs.size()) { QString cmd = QString("INSERT INTO BBList VALUES('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13','%14','%15')").arg(strs.at(0)).arg(strs.at(1)).arg(strs.at(2)).arg(strs.at(3)).arg(strs.at(4)).arg(strs.at(5)).arg(strs.at(6)).arg(strs.at(7)).arg(strs.at(8)).arg(strs.at(9)).arg(strs.at(10)).arg(strs.at(11)).arg(strs.at(12)).arg(strs.at(13)).arg(strs.at(14)); qDebug() << cmd; pQuery_.exec(cmd); } counter++; } dbBuffer.commit(); fileBB.close(); ///////翻译之后结束的一腿!!!!/////////////////// //////////////清空上次翻译结果//////////// QSqlQuery queryDel(dbBuffer); queryDel.exec("delete from Code where ROM = '0'"); //////////////清空上次翻译结果//////////// QSqlQuery queryInsert(dbBuffer); QString queryInsertStr; //////////////插入参数代码//////////// QSqlQuery queryParameter(dbBuffer); queryParameter.exec("select ID,Type,Value,Postfix from Parameter"); while(queryParameter.next()) { queryInsert.exec("select ID from Code where Mark = 'Variable Parameter' order by ID desc limit 1"); queryInsert.next(); queryInsertStr = "insert into Code values("; queryInsertStr += QString::number(queryInsert.value(0).toInt()+1); queryInsertStr += ",'"+queryParameter.value(1).toString() + " LocalParameter_" + queryParameter.value(0).toString() + queryParameter.value(3).toString() + " = " + queryParameter.value(2).toString() + ";" ; queryInsertStr += "','Variable Parameter',0)"; queryInsert.exec(queryInsertStr); } //////////////插入参数代码//////////// ////////////翻译主体(检索所有BB)//////////// QSqlQuery queryBBList(dbBuffer); queryBBList.exec("select BB,Type from BBList"); float resultCount = 0; queryBBList.last(); resultCount = queryBBList.at()+1;//+1因为index从0开始 queryBBList.first(); //**************Report Process************** //qDebug()<< compileProgress; emit signalAdvanceProgress(++translateProgress_, resultCount+1, 500);//+1是因为前面插入参数也是一个步骤 //**************Report Process************** queryBBList.seek(0); for (int i =0; i < resultCount ; i++) { //检索BB List列表 if (i != 0) queryBBList.next(); QString currentBB = queryBBList.value(0).toString(); QString currentBBType = queryBBList.value(1).toString(); qDebug() << currentBB; ///////////////////清理BPIO////////////// for (int i = 0 ; i < 10 ; i++) { BI[i] = "Null"; BO[i] = "Null"; PI[i] = "Null"; PO[i] = "Null"; } ///////////////////清理BPIO////////////// ///////////////BI//////////////// QString BIBaseString = currentBB + "_"; //dbDict.setDatabaseName(("./resource/Blocks/") + currentBBType + ".mpb"); if(!map_fileName_filePath_.contains(currentBBType)) { qDebug() << "翻译的时候检查模块出错 map_fileName_filePath_没有对应的" << currentBBType; } dbDict.setDatabaseName(map_fileName_filePath_.value(currentBBType)); dbDict.open(); QSqlQuery queryBI(dbDict); QString queryBIStr = "select name from behaviorIn order by id"; queryBI.exec(queryBIStr); for (int i = 0;i < 10;i++) { if (queryBI.next()){ if (queryBI.value(0).toString() != "") { BI[i] = BIBaseString + queryBI.value(0).toString(); //qDebug(BI[i]); } } } ///////////////BI//////////////// //qDebug("==========BO==================="); ///////////////BO/////////////// QSqlQuery queryBO(dbDict); QString queryBOStr = "select name from behaviorOut order by id"; queryBO.exec(queryBOStr); for (int i = 0;i < 10;i++) { if (queryBO.next()){ if (queryBO.value(0).toString() != "") //只运算有输出引脚的部分 { QString fromPin = currentBB + "_"+queryBO.value(0).toString(); //qDebug(fromPin); //获取当前模块的输出引脚名字 QSqlQuery queryLinkage(dbBuffer); QString queryLinkageStr = "select * from Linkage where Come = '"+fromPin+"'"; queryLinkage.exec(queryLinkageStr); while(queryLinkage.next()) { BO[i] = BO[i] + "," + queryLinkage.value(1).toString(); } } } } ///////////////BO/////////////// //qDebug("============PI================="); ///////////////PI/////////////// QString PIBaseString = "LocalParameter_"; QSqlQuery queryPI(dbBuffer); QString queryPIStr = "select pi0,pi1,pi2,pi3,pi4 from BBList where BB = '"+currentBB+"'"; queryPI.exec(queryPIStr); queryPI.next(); for (int i = 0;i < 10;i++) { if (queryPI.value(i).toString() != "") { PI[i] = PIBaseString + queryPI.value(i).toString(); //qDebug(PI[i]); } } ///////////////PI/////////////// //qDebug("==========PO==================="); ///////////////PO/////////////// QSqlQuery queryPO(dbDict); QString queryPOStr = "select name from paramOut order by id"; queryPO.exec(queryPOStr); for (int i = 0;i < 10;i++) { if (queryPO.next()){ if (queryPO.value(0).toString() != "") { QString fromPin = currentBB + "_"+queryPO.value(0).toString(); //qDebug(fromPin); //获取当前模块的输出引脚名字 QSqlQuery queryLinkage(dbBuffer); QString queryLinkageStr = "select * from Linkage where Come = '"+fromPin+"'"; queryLinkage.exec(queryLinkageStr); while(queryLinkage.next()) { PO[i] = PO[i] + "," + queryLinkage.value(1).toString(); } } } } ///////////////PO/////////////// ////////////翻译主体(检索所有BB)//////////// ////////////////生成BB变量/////////////// for (int i =0; i < 10;i++) { if (BI[i] != "Null") { queryInsert.exec("select ID from Code where Mark = 'Variable Beh IO' order by ID desc limit 1"); queryInsert.next(); QSqlQuery queryLinkage(dbBuffer); QString queryLinkageStr = "select * from Linkage where Go = '"+BI[i]+"'"; queryLinkage.exec(queryLinkageStr); int startAtBegin = 0; while(queryLinkage.next()){ if (queryLinkage.value(0).toString() == "Start"){ startAtBegin = 1; } } //用于检测所有相关结果 但只保留一个结果 //检测是否与Start连接 queryInsertStr = "insert into Code values("; queryInsertStr += QString::number(queryInsert.value(0).toInt()+1); if (startAtBegin) { queryInsertStr += ",'boolean " + BI[i] + " = true;"; }else{ queryInsertStr += ",'boolean " + BI[i] + " = false;"; } /////检测是否与Start连接 设置是否自动启动 queryInsertStr += "','Variable Parameter',0)"; if (BI[i] != "" && BI[i] != "Null") queryInsert.exec(queryInsertStr); } } //////////////////生成每个环节的代码///////////////////// QSqlQuery queryLib(dbDict); QString queryLibStr = "select codeInclude,codeClass,codeParameter,codeSetup,codePreWork,codeMainWork,codeLastWork,codeVoid from code"; queryLib.exec(queryLibStr); queryLib.next(); QString currentBBSetup; //////////////获取当前BB的资料////////////// //////////////0插入Include//////////////// currentBBSetup = queryLib.value(0).toString(); //qDebug(currentBBSetup); queryInsert.exec("select ID from Code where Mark = 'Include' order by ID desc limit 1"); queryInsert.next(); currentBBSetup = ReplaceBPIO(currentBBSetup); QSqlQuery queryCheckRepeat(dbBuffer); QString queryCheckRepeatStr = "select Code from Code where Code = '"; queryCheckRepeatStr += currentBBSetup; queryCheckRepeatStr += "'"; queryCheckRepeat.exec(queryCheckRepeatStr); queryCheckRepeat.next(); QString result = queryCheckRepeat.value(0).toString(); if (result == "") { queryInsertStr = "insert into Code values("; queryInsertStr += QString::number(queryInsert.value(0).toInt()+1); queryInsertStr += ",'"+currentBBSetup; queryInsertStr += "','Include',0)"; queryInsert.exec(queryInsertStr); } //qDebug(currentBBSetup); //////////////1插入Class//////////////// currentBBSetup = queryLib.value(1).toString(); //qDebug(currentBBSetup); queryInsert.exec("select ID from Code where Mark = 'Variable Class' order by ID desc limit 1"); queryInsert.next(); currentBBSetup = ReplaceBPIO(currentBBSetup); queryCheckRepeatStr = "select Code from Code where Code = '"; queryCheckRepeatStr += currentBBSetup; queryCheckRepeatStr += "'"; queryCheckRepeat.exec(queryCheckRepeatStr); queryCheckRepeat.next(); result = queryCheckRepeat.value(0).toString(); if (result == "") { queryInsertStr = "insert into Code values("; queryInsertStr += QString::number(queryInsert.value(0).toInt()+1); queryInsertStr += ",'"+currentBBSetup; queryInsertStr += "','Variable Class',0)"; queryInsert.exec(queryInsertStr); } //qDebug(currentBBSetup); //////////////2插入Parameter//////////////// currentBBSetup = queryLib.value(2).toString(); //qDebug(currentBBSetup); queryInsert.exec("select ID from Code where Mark = 'Variable Parameter' order by ID desc limit 1"); queryInsert.next(); currentBBSetup = ReplaceBPIO(currentBBSetup); queryCheckRepeatStr = "select Code from Code where Code = '"; queryCheckRepeatStr += currentBBSetup; queryCheckRepeatStr += "'"; queryCheckRepeat.exec(queryCheckRepeatStr); queryCheckRepeat.next(); result = queryCheckRepeat.value(0).toString(); if (result == "") { queryInsertStr = "insert into Code values("; queryInsertStr += QString::number(queryInsert.value(0).toInt()+1); queryInsertStr += ",'"+currentBBSetup; queryInsertStr += "','Variable Parameter',0)"; queryInsert.exec(queryInsertStr); } ////////////////3插入Setup资料////////////// currentBBSetup = queryLib.value(3).toString(); //qDebug(currentBBSetup); queryInsert.exec("select ID from Code where Mark = 'Pin Setup' order by ID desc limit 1"); queryInsert.next(); //QString queryInsertPos = queryInsert.value(0).toString(); //qDebug(queryInsertPos); //获取插入位置 // currentBBSetup = ReplaceBPIO(currentBBSetup); queryCheckRepeatStr = "select Code from Code where Code = '"; queryCheckRepeatStr += currentBBSetup; queryCheckRepeatStr += "'"; queryCheckRepeat.exec(queryCheckRepeatStr); queryCheckRepeat.next(); result = queryCheckRepeat.value(0).toString(); if (result == "") { queryInsertStr = "insert into Code values("; queryInsertStr += QString::number(queryInsert.value(0).toInt()+1); queryInsertStr += ",'"+currentBBSetup; queryInsertStr += "','Pin Setup',0)"; queryInsert.exec(queryInsertStr); //qDebug(currentBBSetup); } //////////////4插入PreWork//////////////// currentBBSetup = queryLib.value(4).toString(); //qDebug(currentBBSetup); queryInsert.exec("select ID from Code where Mark = 'Pre Work' order by ID desc limit 1"); queryInsert.next(); // currentBBSetup = ReplaceBPIO(currentBBSetup); queryCheckRepeatStr = "select Code from Code where Code = '"; queryCheckRepeatStr += currentBBSetup; queryCheckRepeatStr += "'"; queryCheckRepeat.exec(queryCheckRepeatStr); queryCheckRepeat.next(); result = queryCheckRepeat.value(0).toString(); if (result == "") { queryInsertStr = "insert into Code values("; queryInsertStr += QString::number(queryInsert.value(0).toInt()+1); queryInsertStr += ",'"+currentBBSetup; queryInsertStr += "','Pre Work',0)"; queryInsert.exec(queryInsertStr); } //qDebug(currentBBSetup); //////////////5插入MainWork//////////////// currentBBSetup = queryLib.value(5).toString(); //qDebug(currentBBSetup); queryInsert.exec("select ID from Code where Mark = 'Main Work' order by ID desc limit 1"); queryInsert.next(); currentBBSetup = ReplaceBPIO(currentBBSetup); queryCheckRepeatStr = "select Code from Code where Code = '"; queryCheckRepeatStr += currentBBSetup; queryCheckRepeatStr += "'"; queryCheckRepeat.exec(queryCheckRepeatStr); queryCheckRepeat.next(); result = queryCheckRepeat.value(0).toString(); if (result == "") { queryInsertStr = "insert into Code values("; queryInsertStr += QString::number(queryInsert.value(0).toInt()+1); queryInsertStr += ",'"+currentBBSetup; queryInsertStr += "','Main Work',0)"; queryInsert.exec(queryInsertStr); } //////////////6插入LastWork//////////////// currentBBSetup = queryLib.value(6).toString(); //qDebug(currentBBSetup); queryInsert.exec("select ID from Code where Mark = 'Last Work' order by ID desc limit 1"); queryInsert.next(); currentBBSetup = ReplaceBPIO(currentBBSetup); queryCheckRepeatStr = "select Code from Code where Code = '"; queryCheckRepeatStr += currentBBSetup; queryCheckRepeatStr += "'"; queryCheckRepeat.exec(queryCheckRepeatStr); queryCheckRepeat.next(); result = queryCheckRepeat.value(0).toString(); if (result == "") { if (currentBBSetup != "") { queryInsertStr = "insert into Code values("; queryInsertStr += QString::number(queryInsert.value(0).toInt()+1); queryInsertStr += ",'"+currentBBSetup; queryInsertStr += "','Last Work',0)"; queryInsert.exec(queryInsertStr); } } //////////////7插入VoidWork//////////////// currentBBSetup = queryLib.value(7).toString(); //qDebug(currentBBSetup); queryInsert.exec("select ID from Code where Mark = 'Void Work' order by ID desc limit 1"); queryInsert.next(); // currentBBSetup = ReplaceBPIO(currentBBSetup); queryInsertStr = "insert into Code values("; queryInsertStr += QString::number(queryInsert.value(0).toInt()+1); queryInsertStr += ",'"+currentBBSetup; queryInsertStr += "','Void Work',0)"; queryInsert.exec(queryInsertStr); //**************Report Process************** //qDebug()<< compileProgress; emit signalAdvanceProgress(++translateProgress_, resultCount+1, 500);//+1是因为前面插入参数也是一个步骤 //**************Report Process************** } dbDict.close(); qDebug("Success GenCode !!!"); /////////////////////Save File////////////////// // { // //确保目录存在 // QFileInfo infro(fileSavePath_); // QDir dir(infro.path()); // if(dir.exists()) // { // qDebug() << "The Temp dir not exists, and will be create it"; // if(!dir.mkpath(infro.path())) // { // qDebug() << "create Temp fail"; // } // } // } QFile file(fileSavePath_); if (!file.open(QIODevice::WriteOnly)) { emit signalTranslateError(); emit signalError(FileError); return; } QTextStream stream(&file); //创建文件和文件流 QSqlQuery query(dbBuffer); query.exec("select Code from Code order by ID"); //以下执行相关QSL语句 while(query.next()) { QString string = query.value(0).toString(); //qDebug(qPrintable( string )); //qDebug(string); if (string != "") { stream<<string<<endl; } //将Code的每一行都写入文件中 } /////////////////////Save File////////////////// qDebug("Success Save File !!!"); dbBuffer.close(); file.close(); }
bool UsersGroupsManager::add_elements() { /** METHOD TO LINK GROUPS WITH A STUDENT OR STUDENTS WITH A GROUP **/ //Get current selection QList<QListWidgetItem*> selection = ui->list_no->selectedItems(); bool browseStd = (ui->comboBox_browse->currentData().toInt() == BrowseStudents); //Error if no group selected if(selection.length() <= 0) { QMessageBox::critical(this, "Erreur", "Veuillez sélectionner un item."); return false; } // Prepare the query (to make only one global query) QString queryStr = "INSERT INTO tau_groups_users(id_groups, id_users) VALUES"; QMap<QString, qulonglong> bindValuesMap; /// Get selected elements in linking lists for(int i=0; i<selection.count(); i++) { QListWidgetItem* itemFromNo = selection.at(i); QListWidgetItem* itemFromBrowse = ui->list_browse->currentItem(); // Identify the items according the selected method to link Group* group = NULL; Student* student = NULL; if(browseStd) { group = (Group*) itemFromNo->data(Qt::UserRole).toULongLong(); student = (Student*) itemFromBrowse->data(Qt::UserRole).toULongLong(); } else { group = (Group*) itemFromBrowse->data(Qt::UserRole).toULongLong(); student = (Student*) itemFromNo->data(Qt::UserRole).toULongLong(); } // Add the link in the variables preparing the query if(i) queryStr += ", "; QString varGrp = ":idGroup" + QString::number(i), varStd = ":idUser" + QString::number(i); queryStr += "("+varGrp+", "+varStd+")"; bindValuesMap.insert(varGrp, (qulonglong) group->getId()); bindValuesMap.insert(varStd, (qulonglong) student->getId()); } /// Make the query QSqlQuery queryInsert(*m_db); queryInsert.prepare(queryStr); // Add the bind values QMapIterator<QString, qulonglong> i(bindValuesMap); while (i.hasNext()) { i.next(); queryInsert.bindValue(i.key(), i.value()); } queryInsert.exec(); /// Update the linking lists update_listsYesNo(); // Update the selected item in the browsing list QListWidgetItem* item = ui->list_browse->currentItem(); if(item && !browseStd){ // If the selected item is a group --> change the number of students Group* grp = (Group*) item->data(Qt::UserRole).toLongLong(); item->setText(grp->getName() + " ("+QString::number(nbStudentsInGroup(grp->getId()))+")"); } return true; }