27 #include "../../../common/STLUtils.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/dataset/DataSetType.h"
30 #include "../../../dataaccess/dataset/ObjectIdSet.h"
31 #include "../../../dataaccess/datasource/DataSourceCatalog.h"
32 #include "../../../dataaccess/query/BinaryFunction.h"
33 #include "../../../dataaccess/query/DataSetName.h"
34 #include "../../../dataaccess/query/Distinct.h"
35 #include "../../../dataaccess/query/Field.h"
36 #include "../../../dataaccess/query/Fields.h"
37 #include "../../../dataaccess/query/From.h"
38 #include "../../../dataaccess/query/LiteralDouble.h"
39 #include "../../../dataaccess/query/LiteralGeom.h"
40 #include "../../../dataaccess/query/LiteralInt16.h"
41 #include "../../../dataaccess/query/LiteralInt32.h"
42 #include "../../../dataaccess/query/LiteralString.h"
43 #include "../../../dataaccess/query/OrderByItem.h"
44 #include "../../../dataaccess/query/PropertyName.h"
45 #include "../../../dataaccess/query/Select.h"
46 #include "../../../dataaccess/query/SQLVisitor.h"
47 #include "../../../dataaccess/utils/Utils.h"
48 #include "../../../geometry/Geometry.h"
49 #include "../../../geometry/GeometryCollection.h"
50 #include "../../../geometry/GeometryProperty.h"
51 #include "ui_WhereClauseWidgetForm.h"
56 #include <QMessageBox>
57 #include <QToolButton>
64 m_ui(new Ui::WhereClauseWidgetForm)
71 m_ui->m_addWhereClausePushButton->setIcon(QIcon::fromTheme(
"list-add"));
72 m_ui->m_clearAllPushButton->setIcon(QIcon::fromTheme(
"edit-clear"));
96 int row = m_ui->m_whereClauseTableWidget->rowCount();
102 std::string lastConnectorName =
"";
104 for(
int i = 0; i < row; ++i)
107 std::string propName =
"";
108 QTableWidgetItem* itemPropName = m_ui->m_whereClauseTableWidget->item(i, 1);
110 propName = itemPropName->text().toStdString();
113 QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 1);
114 QComboBox* cmbBox =
dynamic_cast<QComboBox*
>(w);
116 propName = cmbBox->currentText().toStdString();
119 std::string funcName =
"";
120 QTableWidgetItem* itemFuncName = m_ui->m_whereClauseTableWidget->item(i, 2);
122 funcName = itemFuncName->text().toStdString();
125 QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 2);
126 QComboBox* cmbBox =
dynamic_cast<QComboBox*
>(w);
128 funcName = cmbBox->currentText().toStdString();
132 QTableWidgetItem* itemValue = m_ui->m_whereClauseTableWidget->item(i, 3);
135 expIdx = itemValue->data(Qt::UserRole).toInt();
139 QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 3);
140 QComboBox* cmbBox =
dynamic_cast<QComboBox*
>(w);
142 expIdx = m_comboMap[cmbBox].first;
154 if(leftSide != 0 && lastConnectorName.empty() ==
false)
157 leftSide = connectFunc;
165 QTableWidgetItem* itemConnectorName = m_ui->m_whereClauseTableWidget->item(i, 4);
166 if(itemConnectorName)
168 if(itemPropName->text().isEmpty() ==
false)
169 lastConnectorName = itemConnectorName->text().toStdString();
171 lastConnectorName =
"";
175 QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 4);
176 QComboBox* cmbBox =
dynamic_cast<QComboBox*
>(w);
178 lastConnectorName = cmbBox->currentText().toStdString();
189 std::string sql =
"";
221 m_ui->m_layerComboBox->clear();
223 std::list<te::map::AbstractLayerPtr>::iterator it = layerList.begin();
225 while(it != layerList.end())
229 std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
233 if(dsType->hasGeom())
234 m_ui->m_layerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
247 m_ui->m_restrictValueComboBox->clear();
248 m_ui->m_valuePropertyComboBox->clear();
250 for(
size_t t = 0; t <vec.size(); ++t)
252 m_ui->m_restrictValueComboBox->addItem(vec[t].c_str());
253 m_ui->m_valuePropertyComboBox->addItem(vec[t].c_str());
261 m_ui->m_geomAttrComboBox->clear();
263 for(
size_t t = 0; t <vec.size(); ++t)
265 m_ui->m_geomAttrComboBox->addItem(vec[t].c_str());
271 m_ui->m_OperatorComboBox->clear();
273 for(
size_t t = 0; t <vec.size(); ++t)
275 m_ui->m_OperatorComboBox->addItem(vec[t].c_str());
281 m_ui->m_SpatialOperatorComboBox->clear();
283 for(
size_t t = 0; t <vec.size(); ++t)
285 m_ui->m_SpatialOperatorComboBox->addItem(vec[t].c_str());
291 m_connectorsList.clear();
293 m_connectorsList.append(
"");
295 for(
size_t t = 0; t <vec.size(); ++t)
297 m_connectorsList.append(vec[t].c_str());
309 m_ui->m_whereClauseTableWidget->setRowCount(0);
321 m_ui->m_restrictValueComboBox->clear();
322 m_ui->m_valuePropertyComboBox->clear();
323 m_ui->m_valueValueComboBox->clear();
324 m_ui->m_geomAttrComboBox->clear();
325 m_ui->m_OperatorComboBox->clear();
326 m_ui->m_SpatialOperatorComboBox->clear();
327 m_ui->m_sqlTextEdit->clear();
328 m_ui->m_tabWidget->setCurrentIndex(0);
333 std::string restrictValue =
"";
334 std::string operatorStr =
"";
335 std::string valueStr =
"";
337 if(m_ui->m_criteriaTabWidget->currentIndex() == 0)
339 int expId = ++m_count;
341 if(m_ui->m_restrictValueComboBox->currentText().isEmpty())
343 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Restrict value not defined."));
347 if(m_ui->m_valuePropertyRadioButton->isChecked() ==
false &&
348 m_ui->m_valueValueRadioButton->isChecked() ==
false)
350 if(m_ui->m_valueValueComboBox->currentText().isEmpty())
352 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Value not defined."));
357 if(m_ui->m_valueValueRadioButton->isChecked())
359 if(m_ui->m_valueValueComboBox->currentText().isEmpty())
361 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Value not defined."));
366 restrictValue = m_ui->m_restrictValueComboBox->currentText().toStdString();
368 if(m_ui->m_OperatorComboBox->currentText().isEmpty())
370 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Operator not defined."));
373 operatorStr = m_ui->m_OperatorComboBox->currentText().toStdString();
380 if(m_ui->m_valuePropertyRadioButton->isChecked())
382 valueStr = m_ui->m_valuePropertyComboBox->currentText().toStdString();
390 m_mapExp.insert(std::map<int, ExpressionProperty*>::value_type(expId, ep));
392 else if(m_ui->m_valueValueRadioButton->isChecked())
394 valueStr = m_ui->m_valueValueComboBox->currentText().toStdString();
396 te::da::Expression* exp = getExpression(m_ui->m_valueValueComboBox->currentText(), restrictValue);
402 m_mapExp.insert(std::map<int, ExpressionProperty*>::value_type(expId, ep));
405 std::string connector = tr(
"and").toStdString();
408 int newrow = m_ui->m_whereClauseTableWidget->rowCount();
410 m_ui->m_whereClauseTableWidget->insertRow(newrow);
413 QToolButton* removeBtn =
new QToolButton(m_ui->m_whereClauseTableWidget);
414 removeBtn->setIcon(QIcon::fromTheme(
"list-remove"));
415 connect(removeBtn, SIGNAL(clicked()),
this, SLOT(onRemoveWhereClausePushButtonClicked()));
416 m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 0, removeBtn);
419 QComboBox* cmbProperty =
new QComboBox(m_ui->m_whereClauseTableWidget);
420 connect(cmbProperty, SIGNAL(activated(QString)),
this, SLOT(onComboBoxActivated(QString)));
421 std::pair<int, int> pairProperty(expId, 1);
422 m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(cmbProperty, pairProperty));
423 copyCombo(m_ui->m_restrictValueComboBox, cmbProperty, restrictValue);
424 m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 1, cmbProperty);
427 QComboBox* cmbOperator =
new QComboBox(m_ui->m_whereClauseTableWidget);
428 connect(cmbOperator, SIGNAL(activated(QString)),
this, SLOT(onComboBoxActivated(QString)));
429 std::pair<int, int> pairOperator(expId, 2);
430 m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(cmbOperator, pairOperator));
431 copyCombo(m_ui->m_OperatorComboBox, cmbOperator, operatorStr);
432 m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 2, cmbOperator);
435 QComboBox* cmbValue =
new QComboBox(m_ui->m_whereClauseTableWidget);
436 connect(cmbValue, SIGNAL(activated(QString)),
this, SLOT(onComboBoxActivated(QString)));
437 connect(cmbValue, SIGNAL(editTextChanged(QString)),
this, SLOT(onComboBoxActivated(QString)));
438 std::pair<int, int> pairValue(expId, 3);
439 m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(cmbValue, pairValue));
440 if(m_ui->m_valuePropertyRadioButton->isChecked())
441 copyCombo(m_ui->m_valuePropertyComboBox, cmbValue, valueStr);
444 cmbValue->setEditable(
true);
445 copyCombo(m_ui->m_valueValueComboBox, cmbValue, valueStr);
447 m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 3, cmbValue);
451 QComboBox* connectorCmbBox =
new QComboBox(m_ui->m_whereClauseTableWidget);
452 connect(connectorCmbBox, SIGNAL(activated(QString)),
this, SLOT(onComboBoxActivated(QString)));
453 std::pair<int, int> pairConnector(expId, 4);
454 m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(connectorCmbBox, pairConnector));
455 connectorCmbBox->addItems(m_connectorsList);
456 m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 4, connectorCmbBox);
458 for(
int i = 0; i < connectorCmbBox->count(); ++i)
460 if(connectorCmbBox->itemText(i).toStdString() == connector)
462 connectorCmbBox->setCurrentIndex(i);
469 if(m_ui->m_SpatialOperatorComboBox->currentText().isEmpty())
471 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Operator not defined."));
474 operatorStr = m_ui->m_SpatialOperatorComboBox->currentText().toStdString();
476 restrictValue = m_ui->m_geomAttrComboBox->currentText().toStdString();
479 int layerIdx = m_ui->m_layerComboBox->currentIndex();
480 QVariant varLayer = m_ui->m_layerComboBox->itemData(layerIdx, Qt::UserRole);
483 std::auto_ptr<const te::map::LayerSchema> schema(layer->getSchema());
489 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Selected layer has no geometry property."));
493 std::auto_ptr<te::da::DataSet> ds;
495 if(m_ui->m_selectedObjCheckBox->isChecked())
499 if(!selecteds || selecteds->
size() == 0)
501 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Selected layer has no selected geometries."));
505 ds = layer->getData(selecteds);
509 ds = layer->getData();
512 ds->moveBeforeFirst();
514 size_t dsSize = ds->size();
518 while(ds->moveNext())
521 int expId = ++m_count;
525 geom->
setSRID(layer->getSRID());
528 if(layer->getSRID() != m_srid)
534 valueStr = tr(
"Geometry Value").toStdString();
546 m_mapExp.insert(std::map<int, ExpressionProperty*>::value_type(expId, ep));
549 std::string connector =
"";
551 if(count < dsSize - 1)
552 connector = tr(
"or").toStdString();
557 int newrow = m_ui->m_whereClauseTableWidget->rowCount();
559 m_ui->m_whereClauseTableWidget->insertRow(newrow);
562 QToolButton* removeBtn =
new QToolButton(m_ui->m_whereClauseTableWidget);
563 removeBtn->setIcon(QIcon::fromTheme(
"list-remove"));
564 connect(removeBtn, SIGNAL(clicked()),
this, SLOT(onRemoveWhereClausePushButtonClicked()));
565 m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 0, removeBtn);
568 QComboBox* cmbProperty =
new QComboBox(m_ui->m_whereClauseTableWidget);
569 connect(cmbProperty, SIGNAL(activated(QString)),
this, SLOT(onComboBoxActivated(QString)));
570 std::pair<int, int> pairProperty(expId, 1);
571 m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(cmbProperty, pairProperty));
572 copyCombo(m_ui->m_geomAttrComboBox, cmbProperty, restrictValue);
573 m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 1, cmbProperty);
576 QComboBox* cmbOperator =
new QComboBox(m_ui->m_whereClauseTableWidget);
577 connect(cmbOperator, SIGNAL(activated(QString)),
this, SLOT(onComboBoxActivated(QString)));
578 std::pair<int, int> pairOperator(expId, 2);
579 m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(cmbOperator, pairOperator));
580 copyCombo(m_ui->m_SpatialOperatorComboBox, cmbOperator, operatorStr);
581 m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 2, cmbOperator);
584 QTableWidgetItem* itemValue =
new QTableWidgetItem(valueStr.c_str());
585 itemValue->setData(Qt::UserRole, QVariant(expId));
586 m_ui->m_whereClauseTableWidget->setItem(newrow, 3, itemValue);
589 QComboBox* connectorCmbBox =
new QComboBox(m_ui->m_whereClauseTableWidget);
590 connect(connectorCmbBox, SIGNAL(activated(QString)),
this, SLOT(onComboBoxActivated(QString)));
591 std::pair<int, int> pairConnector(expId, 4);
592 m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(connectorCmbBox, pairConnector));
593 connectorCmbBox->addItems(m_connectorsList);
594 m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 4, connectorCmbBox);
596 for(
int i = 0; i < connectorCmbBox->count(); ++i)
598 if(connectorCmbBox->itemText(i).toStdString() == connector)
600 connectorCmbBox->setCurrentIndex(i);
607 m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
610 std::string sql = getWhereString();
612 m_ui->m_sqlTextEdit->setText(sql.c_str());
617 QToolButton* button =
dynamic_cast<QToolButton*
>(sender());
622 for(
int i = 0; i < m_ui->m_whereClauseTableWidget->rowCount(); ++i)
624 QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 0);
625 QToolButton* btn =
dynamic_cast<QToolButton*
>(w);
634 m_ui->m_whereClauseTableWidget->removeRow(row);
636 m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
640 std::string sql = getWhereString();
642 m_ui->m_sqlTextEdit->setText(sql.c_str());
647 std::string propertyName = m_ui->m_restrictValueComboBox->currentText().toStdString();
649 m_ui->m_valueValueComboBox->clear();
650 m_ui->m_valueValueComboBox->addItems(getPropertyValues(propertyName));
655 m_ui->m_whereClauseTableWidget->setRowCount(0);
656 m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
661 if(value.isEmpty() || !m_ui->m_valueValueRadioButton->isChecked())
664 onValuePropertyRadioButtonClicked();
669 QComboBox* cmbBox =
dynamic_cast<QComboBox*
>(sender());
673 int expId = m_comboMap[cmbBox].first;
674 int column = m_comboMap[cmbBox].second;
701 ep->
m_value = value.toStdString();
723 std::string sql = getWhereString();
725 m_ui->m_sqlTextEdit->setText(sql.c_str());
730 std::string dataSetName;
731 std::string dataSetAliasName;
732 std::string propertyName = propName;
734 std::size_t pos = propName.find(
".");
735 if(pos != std::string::npos)
737 dataSetAliasName = propName.substr(0, pos);
738 propertyName = propName.substr(pos + 1, propName.size() - 1);
742 if(m_fromItems.size() == 1)
744 dataSetName = m_fromItems[0].first;
748 for(
size_t t = 0; t < m_fromItems.size(); ++t)
750 if(m_fromItems[t].second == dataSetAliasName)
752 dataSetName = m_fromItems[t].first;
758 if(dataSetName.empty())
762 std::auto_ptr<te::da::DataSetType> dsType = m_ds->getDataSetType(dataSetName);
800 for(
int i = 0; i < input->count(); ++i)
802 output->addItem(input->itemText(i));
804 if(!curValue.empty() && input->itemText(i).toStdString() == curValue)
808 output->setCurrentIndex(idx);
820 fields->push_back(f);
826 for(
size_t t = 0; t < m_fromItems.size(); ++t)
838 std::auto_ptr<te::da::DataSet> dataset;
842 dataset = m_ds->query(*select);
844 catch(
const std::exception& e)
846 std::string msg =
"An exception has occurred: ";
849 QMessageBox::warning(0,
"Query Error: ", msg.c_str());
855 std::string msg =
"An unexpected exception has occurred!";
857 QMessageBox::warning(0,
"Query Error: ", msg.c_str());
862 std::size_t pos = propertyName.find(
".");
863 if(pos != std::string::npos)
865 std::string dataSetAliasName = propertyName.substr(0, pos);
866 propertyName = propertyName.substr(pos + 1, propertyName.size() - 1);
869 std::set<std::string> valuesStr;
870 std::set<double> valuesDouble;
871 std::set<int> valuesInt;
873 std::size_t propertyPos;
874 for(std::size_t t = 0; t < dataset->getNumProperties(); ++t)
876 if(dataset->getPropertyName(t) == propertyName)
883 int propertyType = dataset->getPropertyDataType(propertyPos);
887 while(dataset->moveNext())
889 if(!dataset->isNull(propertyName))
893 int value = dataset->getInt32(propertyName);
895 std::set<int>::iterator it = valuesInt.find(value);
897 if(it == valuesInt.end())
898 valuesInt.insert(value);
902 double value = dataset->getDouble(propertyName);
904 std::set<double>::iterator it = valuesDouble.find(value);
906 if(it == valuesDouble.end())
907 valuesDouble.insert(value);
911 std::string value = dataset->getAsString(propertyName);
913 std::set<std::string>::iterator it = valuesStr.find(value);
915 if(it == valuesStr.end())
916 valuesStr.insert(value);
924 std::set<int>::iterator it = valuesInt.begin();
926 while(it != valuesInt.end())
928 list.append(QString::number(*it));
935 std::set<double>::iterator it = valuesDouble.begin();
937 while(it != valuesDouble.end())
939 list.append(QString::number(*it,
'f', 5));
946 std::set<std::string>::iterator it = valuesStr.begin();
948 while(it != valuesStr.end())
950 list.append((*it).c_str());
An abstract class that models a source of data in a query.
The Field class can be used to model an expression that takes part of the output items of a SELECT...
A class that models the name of a dataset used in a From clause.
boost::shared_ptr< DataSource > DataSourcePtr
A class that models the name of any property of an object.
virtual void transform(int srid)=0
It converts the coordinate values of the geometry to the new spatial reference system.
It models a property definition.
This is an abstract class that models a query expression.
This class models a literal value.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
std::size_t size() const
It returns the object id set size.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
A class that can be used to model a filter expression that can be applied to a query.
Expression * getExp() const
A class that models a literal for double values.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
A Select models a query to be used when retrieving data from a DataSource.
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
int getType() const
It returns the property data type.
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
A visitor for building an SQL statement from a given Query hierarchy.
virtual Expression * clone() const =0
It creates a new copy of this expression.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
A base class for binary functions.
void setFields(Fields *f)
It sets the list of output expressions used to form the result set.
virtual void setSRID(int srid)=0
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
A class that models a literal for Geometry values.
void setFrom(From *f)
It sets the list of source information.
This class models a string Literal value.