27 #include "../../../common/StringUtils.h" 28 #include "../../../dataaccess/dataset/DataSet.h" 29 #include "../../../dataaccess/dataset/DataSetAdapter.h" 30 #include "../../../dataaccess/dataset/DataSetTypeConverter.h" 31 #include "../../../dataaccess/dataset/DataSetType.h" 32 #include "../../../dataaccess/dataset/ObjectIdSet.h" 33 #include "../../../dataaccess/datasource/DataSourceCapabilities.h" 34 #include "../../../dataaccess/datasource/DataSourceInfoManager.h" 35 #include "../../../dataaccess/datasource/DataSourceInfo.h" 36 #include "../../../dataaccess/datasource/DataSourceManager.h" 37 #include "../../../dataaccess/query/SQLDialect.h" 38 #include "../../../dataaccess/query/SQLFunctionEncoder.h" 39 #include "../../../dataaccess/utils/Utils.h" 40 #include "../../../datatype/NumericProperty.h" 41 #include "../../../datatype/SimpleProperty.h" 42 #include "../../../datatype/StringProperty.h" 43 #include "../../../geometry/GeometryProperty.h" 44 #include "../../../maptools/DataSetLayer.h" 45 #include "../datasource/selector/DataSourceSelectorDialog.h" 46 #include "../layer/utils/DataSet2Layer.h" 47 #include "../utils/ScopedCursor.h" 48 #include "../ceditor/ScriptWidget.h" 50 #include "ui_QueryDataSourceDialogForm.h" 54 #include <QFileDialog> 55 #include <QGridLayout> 56 #include <QMessageBox> 57 #include <QTextStream> 58 #include <QListWidgetItem> 65 #include <boost/filesystem.hpp> 66 #include <boost/uuid/random_generator.hpp> 67 #include <boost/uuid/uuid_io.hpp> 71 te::qt::widgets::QueryDataSourceDialog::QueryDataSourceDialog(
QWidget* parent, Qt::WindowFlags f)
73 m_ui(new
Ui::QueryDataSourceDialogForm)
77 m_ui->m_applyToolButton->setIcon(QIcon::fromTheme(
"media-playback-start-green"));
78 m_ui->m_executeToolButton->setIcon(QIcon::fromTheme(
"media-playback-start-green-execute"));
79 m_ui->m_clearToolButton->setIcon(QIcon::fromTheme(
"edit-clear"));
80 m_ui->m_applySelToolButton->setIcon(QIcon::fromTheme(
"pointer-selection"));
81 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
82 m_ui->m_createLayerlToolButton->setIcon(QIcon::fromTheme(
"layer-new"));
85 m_ui->m_saveSqlToolButton->setIcon(QIcon::fromTheme(
"document-save-as"));
86 m_ui->m_openSqlToolButton->setIcon(QIcon::fromTheme(
"document-open"));
90 m_ui->m_tableView->setModel(m_tableModel);
95 m_appMapDisplay =
nullptr;
97 QGridLayout* displayGridLayout =
new QGridLayout(m_ui->m_displayWidget);
98 displayGridLayout->setContentsMargins(0, 0, 0, 0);
99 displayGridLayout->addWidget(m_dataSetDisplay);
101 m_ui->m_sqlEditor->setHighlightMode(
"sql");
104 connect(m_ui->m_dataSourceComboBox, SIGNAL(activated(
int)),
this, SLOT(onDataSourceSelected(
int)));
105 connect(m_ui->m_baseDataSetComboBox, SIGNAL(activated(
int)),
this, SLOT(onBaseDataSetSelected(
int)));
106 connect(m_ui->m_dataSetListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
this, SLOT(onDataSetItemClicked(QListWidgetItem*)));
107 connect(m_ui->m_applyToolButton, SIGNAL(clicked()),
this, SLOT(onApplyPushButtonClicked()));
108 connect(m_ui->m_executeToolButton, SIGNAL(clicked()),
this, SLOT(onExecutePushButtonClicked()));
109 connect(m_ui->m_clearToolButton, SIGNAL(clicked()),
this, SLOT(onClearPushButtonClicked()));
110 connect(m_ui->m_saveSqlToolButton, SIGNAL(clicked()),
this, SLOT(onSaveSqlToolButtonClicked()));
111 connect(m_ui->m_openSqlToolButton, SIGNAL(clicked()),
this, SLOT(onOpenSqlToolButtonClicked()));
112 connect(m_ui->m_applySelToolButton, SIGNAL(clicked()),
this, SLOT(onApplySelToolButtonClicked()));
113 connect(m_ui->m_createLayerlToolButton, SIGNAL(pressed()),
this, SLOT(onCreateLayerToolButtonClicked()));
114 connect(m_ui->m_targetDatasourceToolButton, SIGNAL(pressed()),
this, SLOT(onTargetDatasourceToolButtonPressed()));
115 connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()),
this, SLOT(onTargetFileToolButtonPressed()));
116 connect(m_ui->m_pkTableComboBox, SIGNAL(activated(
int)),
this, SLOT(onPkTableComboBoxSelected(
int)));
117 connect(m_ui->m_attrDataSetListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
this, SLOT(onListWidgetDoubleClicked(QListWidgetItem*)));
118 connect(m_ui->m_dataSetListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
this, SLOT(onListWidgetDoubleClicked(QListWidgetItem*)));
121 loadDataSourcesInformation();
124 m_ui->m_helpPushButton->setPageReference(
"widgets/query/query_datasource.html");
136 if(
m_ui->m_baseDataSetComboBox->count() > 0)
147 m_ui->m_dataSourceComboBox->clear();
161 m_ui->m_dataSourceComboBox->addItem(it->second->getTitle().c_str(), QVariant(it->second->getId().c_str()));
172 if(
m_ui->m_dataSourceComboBox->count() != 0)
181 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"SELECT", Qt::blue));
182 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"FROM", Qt::blue));
183 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"WHERE", Qt::blue));
184 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"JOIN", Qt::blue));
185 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"INNER", Qt::blue));
186 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"LEFT", Qt::blue));
187 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"RIGHT", Qt::blue));
188 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"AS", Qt::blue));
189 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"GROUP", Qt::blue));
190 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"ORDER", Qt::blue));
191 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(
"BY", Qt::blue));
194 std::string dataSourceId =
m_ui->m_dataSourceComboBox->itemData(
m_ui->m_dataSourceComboBox->currentIndex()).toString().toUtf8().data();
202 for(std::size_t t = 0; t < names.size(); ++t)
204 QString s = names[t].c_str();
205 QString sUpper = s.toUpper();
207 m_keyWords.insert(std::map<std::string, Qt::GlobalColor>::value_type(sUpper.toUtf8().data(), Qt::red));
214 m_ui->m_baseDataSetComboBox->clear();
215 m_ui->m_dataSetListWidget->clear();
216 m_ui->m_attrDataSetListWidget->clear();
217 m_ui->m_pkTableComboBox->clear();
218 m_ui->m_pkAttrComboBox->clear();
220 std::string dataSourceId =
m_ui->m_dataSourceComboBox->itemData(index).toString().toUtf8().data();
227 std::vector<std::string> dataSetNames;
230 for(std::size_t t = 0; t < dataSetNames.size(); ++t)
232 m_ui->m_baseDataSetComboBox->addItem(dataSetNames[t].c_str());
233 m_ui->m_dataSetListWidget->addItem(dataSetNames[t].c_str());
234 m_ui->m_pkTableComboBox->addItem(dataSetNames[t].c_str());
237 if(
m_ui->m_baseDataSetComboBox->count() > 0)
240 if (
m_ui->m_pkTableComboBox->count() > 0)
248 std::string dataSet =
m_ui->m_baseDataSetComboBox->itemText(index).toUtf8().data();
250 m_ui->m_layerComboBox->clear();
252 std::list<te::map::AbstractLayerPtr>::iterator it =
m_layerList.begin();
258 std::unique_ptr<te::da::DataSetType> dsType = l->getSchema();
262 if(dsLayer && dsType->getName() == dataSet)
263 m_ui->m_layerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
268 m_ui->m_pkTableComboBox->setCurrentIndex(index);
274 m_ui->m_attrDataSetListWidget->clear();
279 std::string dataSourceId =
m_ui->m_dataSourceComboBox->itemData(
m_ui->m_dataSourceComboBox->currentIndex()).toString().toUtf8().data();
281 std::string dataSetName = item->text().toUtf8().data();
285 std::vector<te::dt::Property*> propVec = dsType->
getProperties();
287 for(std::size_t t = 0; t < propVec.size(); ++t)
289 m_ui->m_attrDataSetListWidget->addItem(propVec[t]->getName().c_str());
297 std::string dataSetName =
m_ui->m_pkTableComboBox->itemText(index).toUtf8().data();
299 std::string dataSourceId =
m_ui->m_dataSourceComboBox->itemData(
m_ui->m_dataSourceComboBox->currentIndex()).toString().toUtf8().data();
303 std::vector<te::dt::Property*> propVec = dsType->
getProperties();
305 for (std::size_t t = 0; t < propVec.size(); ++t)
307 m_ui->m_pkAttrComboBox->addItem(propVec[t]->getName().c_str());
317 m_ui->m_sqlEditor->setFocus();
319 if(
m_ui->m_sqlEditor->text().isEmpty())
326 if (
m_ui->m_sqlEditor->selectedText().isEmpty())
327 sql =
m_ui->m_sqlEditor->text().toUtf8().data();
329 sql =
m_ui->m_sqlEditor->selectedText().toUtf8().data();
331 std::vector<std::string> tokens;
336 QString errorMessage =
"SQL Error: ";
337 errorMessage += tr(
"Use the \"Execute\" option to execute a instruction.");
338 errorMessage +=
"\n";
339 errorMessage +=
"-------------------------------------------------------------------------\n";
341 m_ui->m_logPlainTextEdit->appendPlainText(errorMessage);
343 m_ui->m_tabWidget->setCurrentIndex(1);
348 std::string dataSourceId =
m_ui->m_dataSourceComboBox->itemData(
m_ui->m_dataSourceComboBox->currentIndex()).toString().toUtf8().data();
352 std::string dataSetName =
m_ui->m_baseDataSetComboBox->currentText().toUtf8().data();
357 std::unique_ptr<te::da::DataSet> dataSet;
361 dataSet = ds->query(sql);
363 catch(
const std::exception& e)
368 std::string errorMessage =
"SQL Error: ";
369 errorMessage += e.what();
370 errorMessage +=
"\n";
371 errorMessage +=
"-------------------------------------------------------------------------\n";
373 m_ui->m_logPlainTextEdit->appendPlainText(errorMessage.c_str());
375 m_ui->m_tabWidget->setCurrentIndex(1);
380 std::string message =
"SQL Done: ";
383 message +=
"-------------------------------------------------------------------------\n";
385 m_ui->m_logPlainTextEdit->appendPlainText(message.c_str());
391 for(std::size_t t = 0; t < dataSet->getNumProperties(); ++t)
393 int type = dataSet->getPropertyDataType(t);
410 m_ui->m_tabWidget->setCurrentIndex(0);
417 m_ui->m_sqlEditor->setFocus();
419 if (
m_ui->m_sqlEditor->text().isEmpty())
426 if (
m_ui->m_sqlEditor->selectedText().isEmpty())
427 sql =
m_ui->m_sqlEditor->text().toUtf8().data();
429 sql =
m_ui->m_sqlEditor->selectedText().toUtf8().data();
431 std::vector<std::string> tokens;
436 QString errorMessage =
"SQL Error: ";
437 errorMessage += tr(
"Use the \"Query\" option to execute a selection.");
438 errorMessage +=
"\n";
439 errorMessage +=
"-------------------------------------------------------------------------\n";
441 m_ui->m_logPlainTextEdit->appendPlainText(errorMessage);
443 m_ui->m_tabWidget->setCurrentIndex(1);
448 std::string dataSourceId =
m_ui->m_dataSourceComboBox->itemData(
m_ui->m_dataSourceComboBox->currentIndex()).toString().toUtf8().data();
452 std::string dataSetName =
m_ui->m_baseDataSetComboBox->currentText().toUtf8().data();
460 catch (
const std::exception& e)
465 std::string errorMessage =
"SQL Error: ";
466 errorMessage += e.what();
467 errorMessage +=
"\n";
468 errorMessage +=
"-------------------------------------------------------------------------\n";
470 m_ui->m_logPlainTextEdit->appendPlainText(errorMessage.c_str());
472 m_ui->m_tabWidget->setCurrentIndex(1);
477 std::string message =
"SQL Done: ";
480 message +=
"-------------------------------------------------------------------------\n";
482 m_ui->m_logPlainTextEdit->appendPlainText(message.c_str());
484 m_ui->m_tabWidget->setCurrentIndex(1);
489 m_ui->m_sqlEditor->setText(
"");
503 QString path = QFileDialog::getSaveFileName(
this, tr(
"Set a SQL file..."),
"", tr(
"SQL File *.sql"));
508 if (!path.endsWith(
".sql"))
514 if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
516 QMessageBox::warning(
this, tr(
"Query DataSource"), file.errorString());
521 QTextStream out(&file);
523 out <<
m_ui->m_sqlEditor->text();
531 QString path = QFileDialog::getOpenFileName(
this, tr(
"Select a SQL file..."),
"", tr(
"SQL File *.sql"));
536 m_ui->m_sqlEditor->open(path);
541 if(
m_ui->m_sqlEditor->text().isEmpty())
543 QMessageBox::information(
this, tr(
"Warning"), tr(
"SQL not defined."));
547 QVariant varLayer =
m_ui->m_layerComboBox->itemData(
m_ui->m_layerComboBox->currentIndex(), Qt::UserRole);
552 QMessageBox::warning(
this, tr(
"Query DataSource"), tr(
"No layer selected."));
558 std::string dataSourceId =
m_ui->m_dataSourceComboBox->itemData(
m_ui->m_dataSourceComboBox->currentIndex()).toString().toUtf8().data();
562 std::string dataSetName =
m_ui->m_baseDataSetComboBox->currentText().toUtf8().data();
568 if (
m_ui->m_sqlEditor->selectedText().isEmpty())
569 sql =
m_ui->m_sqlEditor->text().toUtf8().data();
571 sql =
m_ui->m_sqlEditor->selectedText().toUtf8().data();
574 std::unique_ptr<te::da::DataSet> dataSet;
578 dataSet = ds->query(sql);
582 QMessageBox::warning(
this, tr(
"Query DataSource"), tr(
"Error executing SQL."));
588 if(
m_ui->m_newSelRadioButton->isChecked())
591 dataSet->moveBeforeFirst();
596 layer->clearSelected();
599 else if(
m_ui->m_addSelRadioButton->isChecked())
602 dataSet->moveBeforeFirst();
617 QMessageBox::warning(
this, tr(
"Query DataSource"), tr(
"Error selecting objects: ") + e.
what());
620 QMessageBox::information(
this, tr(
"Query DataSource"), tr(
"Selection done."));
627 if(
m_ui->m_sqlEditor->text().isEmpty())
629 QMessageBox::information(
this, tr(
"Warning"), tr(
"SQL not defined."));
633 if(
m_ui->m_repositoryLineEdit->text().isEmpty())
635 QMessageBox::information(
this, tr(
"Warning"), tr(
"Define a repository for the result."));
639 if(
m_ui->m_newLayerNameLineEdit->text().isEmpty())
641 QMessageBox::information(
this, tr(
"Warning"), tr(
"Define a name for the resulting layer."));
648 std::string dataSourceId =
m_ui->m_dataSourceComboBox->itemData(
m_ui->m_dataSourceComboBox->currentIndex()).toString().toUtf8().data();
652 std::string inputDataSetName =
m_ui->m_baseDataSetComboBox->currentText().toUtf8().data();
658 if(
m_ui->m_sqlEditor->selectedText().isEmpty())
659 sql =
m_ui->m_sqlEditor->text().toUtf8().data();
661 sql =
m_ui->m_sqlEditor->selectedText().toUtf8().data();
663 std::unique_ptr<te::da::DataSet> dataSet;
667 dataSet = ds->query(sql);
671 QMessageBox::warning(
this, tr(
"Query DataSource"), tr(
"Error executing SQL."));
675 if (dataSet->size() == 0)
677 QMessageBox::warning(
this, tr(
"Query DataSource"), tr(
"Query result is empty."));
687 boost::filesystem::path uri(
m_ui->m_repositoryLineEdit->text().toUtf8().data());
689 const std::string connInfo(
"file://" + uri.string());
691 boost::uuids::basic_random_generator<boost::mt19937> gen;
692 boost::uuids::uuid u = gen();
693 std::string id_ds = boost::uuids::to_string(u);
696 dsInfoPtr->setConnInfo(connInfo);
697 dsInfoPtr->setTitle(uri.stem().string());
698 dsInfoPtr->setAccessDriver(
"OGR");
699 dsInfoPtr->setType(
"OGR");
700 dsInfoPtr->setDescription(uri.string());
701 dsInfoPtr->setId(id_ds);
713 std::string dataSetName =
m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
715 std::size_t idx = dataSetName.find(
".");
716 if (idx != std::string::npos)
717 dataSetName=dataSetName.substr(0,idx);
722 dataSet->moveFirst();
724 std::set<std::string> names;
728 for(std::size_t t = 0; t < dataSet->getNumProperties(); ++t)
731 std::string propName = dataSet->getPropertyName(t);
734 while(names.find(propName) != names.end())
740 names.insert(propName);
759 std::unique_ptr<te::gm::Geometry> geom = dataSet->getGeometry(t);
761 srid = geom->getSRID();
772 if (
m_ui->m_pkCheckBox->isChecked())
774 std::string pkAttrName =
m_ui->m_pkAttrComboBox->itemText(
m_ui->m_pkAttrComboBox->currentIndex()).toUtf8().data();
778 std::string pkName = dataSetName +
"_" + p->
getName() +
"_pk";
786 QMessageBox::warning(
this, tr(
"Query DataSource"), tr(
"Error creating output dataset."));
791 dataSet->moveBeforeFirst();
801 std::map<std::string, std::string> options;
803 outputDataSource->createDataSet(dsType.get(), options);
805 outputDataSource->add(dataSetName, dsAdapter.get(), options);
820 QMessageBox::warning(
this, tr(
"Query DataSource"), tr(
"Error creating layer. ") + e.
what());
823 QMessageBox::information(
this, tr(
"Query DataSource"), tr(
"Layer created."));
828 m_ui->m_newLayerNameLineEdit->clear();
829 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
834 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
836 if(dsPtrList.empty())
839 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
841 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
850 m_ui->m_newLayerNameLineEdit->clear();
851 m_ui->m_repositoryLineEdit->clear();
853 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."), QString(), tr(
"Shapefile (*.shp *.SHP);;"),
nullptr, QFileDialog::DontConfirmOverwrite);
855 if (fileName.isEmpty())
858 boost::filesystem::path outfile(fileName.toUtf8().data());
860 m_ui->m_repositoryLineEdit->setText(outfile.string().c_str());
862 m_ui->m_newLayerNameLineEdit->setText(outfile.leaf().string().c_str());
864 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
871 m_ui->m_sqlEditor->insert(item->text());
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
te::da::SQLDialect * dialect
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
boost::shared_ptr< DataSetType > DataSetTypePtr
An atomic property like an integer or double.
boost::shared_ptr< DataSource > DataSourcePtr
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
A class that models the description of a dataset.
virtual const char * what() const
It outputs the exception message.
It represents the SQL query dialect accepted by a given data source.
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
static te::dt::Date ds(2010, 01, 01)
virtual Property * clone() const =0
It returns a clone of the object.
It models a property definition.
An converter for DataSetType.
The type for arbitrary precison numbers, like numeric(p, q).
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
void setExpressionByInClause(const std::string source="")
static te::dt::TimeDuration dt(20, 30, 50, 11)
The type for string types: FIXED_STRING, VAR_STRING or STRING.
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
It describes a primary key (pk) constraint.
A class that represents a data source component.
std::vector< std::string > getRegisteredNames() const
It gets the all registered names from registed functions.
TEDATAACCESSEXPORT DataSetType * GetDataSetType(const std::string &name, const std::string &datasourceId)
A layer with reference to a dataset.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
This file defines a class for a Query DataSource Dialog Dialog.
file(WRITE ${CMAKE_BINARY_DIR}/config_qhelp.cmake"configure_file (${TERRALIB_ABSOLUTE_ROOT_DIR}/doc/qhelp/help.qhcp.in ${CMAKE_BINARY_DIR}/share/terraview/help/help.qhcp @ONLY)") add_custom_command(OUTPUT del_dir COMMAND $
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
const std::string & getName() const
It returns the property name.