void MongoClient::createFunction(const std::string &dbName, const MongoFunction &fun, const std::string &existingFunctionName /* = QString() */) { MongoNamespace ns(dbName, "system.js"); mongo::BSONObj obj = fun.toBson(); if (existingFunctionName.empty()) { // this is insert _dbclient->insert(ns.toString(), obj); } else { // this is update std::string name = fun.name(); if (existingFunctionName == name) { mongo::BSONObjBuilder builder; builder.append("_id", name); mongo::BSONObj bsonQuery = builder.obj(); mongo::Query query(bsonQuery); _dbclient->update(ns.toString(), query, obj, true, false); } else { _dbclient->insert(ns.toString(), obj); std::string res = _dbclient->getLastError(); // if no errors if (res.empty()) { mongo::BSONObjBuilder builder; builder.append("_id", existingFunctionName); mongo::BSONObj bsonQuery = builder.obj(); mongo::Query query(bsonQuery); _dbclient->remove(ns.toString(), query, true); } } } }
FunctionTextEditor::FunctionTextEditor(const QString &server, const QString &database, const MongoFunction &function, QWidget *parent) : QDialog(parent), _function(function) { setMinimumWidth(700); setMinimumHeight(550); Indicator *databaseIndicator = new Indicator(GuiRegistry::instance().databaseIcon(), database); Indicator *serverIndicator = new Indicator(GuiRegistry::instance().serverIcon(), server); QPushButton *cancel = new QPushButton("Cancel"); connect(cancel, SIGNAL(clicked()), this, SLOT(close())); QPushButton *save = new QPushButton("Save"); save->setIcon(qApp->style()->standardIcon(QStyle::SP_ArrowRight)); connect(save, SIGNAL(clicked()), this, SLOT(accept())); _nameEdit = new QLineEdit(function.name()); _queryText = new RoboScintilla; _textFont = chooseTextFont(); _configureQueryText(); _queryText->setText(_function.code()); QFrame *hline = new QFrame(); hline->setFrameShape(QFrame::HLine); hline->setFrameShadow(QFrame::Sunken); QHBoxLayout *hlayout = new QHBoxLayout(); hlayout->setContentsMargins(2, 0, 5, 1); hlayout->setSpacing(0); hlayout->addWidget(serverIndicator, 0, Qt::AlignLeft); hlayout->addWidget(databaseIndicator, 0, Qt::AlignLeft); hlayout->addStretch(1); QHBoxLayout *bottomlayout = new QHBoxLayout(); bottomlayout->addStretch(1); #if defined(Q_OS_MAC) save->setDefault(true); bottomlayout->addWidget(cancel, 0, Qt::AlignRight); bottomlayout->addWidget(save, 0, Qt::AlignRight); #else bottomlayout->addWidget(save, 0, Qt::AlignRight); bottomlayout->addWidget(cancel, 0, Qt::AlignRight); #endif QVBoxLayout *layout = new QVBoxLayout(); layout->addLayout(hlayout); layout->addWidget(hline); layout->addWidget(new QLabel("Name:")); layout->addWidget(_nameEdit); layout->addWidget(new QLabel("Code:")); layout->addWidget(_queryText); layout->addLayout(bottomlayout); setLayout(layout); }
void ExplorerTreeWidget::ui_dropFunction() { ExplorerFunctionTreeItem *functionItem = selectedFunctionItem(); if (!functionItem) return; MongoFunction function = functionItem->function(); MongoDatabase *database = functionItem->database(); // Ask user int answer = QMessageBox::question(this, "Remove Function", QString("Remove <b>%1</b> function?").arg(function.name()), QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton); if (answer != QMessageBox::Yes) return; database->dropFunction(function.name()); database->loadFunctions(); // refresh list of functions }
QString ExplorerFunctionTreeItem::buildToolTip(const MongoFunction &function) { return QString("%0").arg(QtUtils::toQString(function.name())); }