void SQLCode::_BuildSQLStatement(SQLVarParms& varParms, SQLStatement& rSqlStatement) { BOOST_LOG_FUNCTION(); std::string strSQLKey; SQLStatement SQLFormats; int nDbType = 0; size_t uiTotalSQLSize = 0; try { size_t uVarCount = varParms.size(); if (uVarCount < 1) { throw DataAccessException(ER_DB_ERR_SQLCode, "the PrepareStatement parameter count error!"); } _GetDbTypeAndSQLKey(varParms, strSQLKey);//strSQLID=strSQLKey=varParms[0] _GetSQLFormat(strSQLKey, SQLFormats); _BuildNormalSQL(varParms, SQLFormats, rSqlStatement); rSqlStatement.logInfo(); } catch (DataAccessException& e) { throw e; } catch (...) { throw DataAccessException(ER_DB_ERR_UNKNOWN, "buildSQLStatement error!"); } }
terrama2::core::DataSetSeries terrama2::core::DataAccessorDcpToa5::getSeries(const std::string& uri, const terrama2::core::Filter& filter, terrama2::core::DataSetPtr dataSet) const { std::string mask = getMask(dataSet); std::string folder = getFolder(dataSet); QTemporaryDir tempBaseDir; if(!tempBaseDir.isValid()) { QString errMsg = QObject::tr("Can't create temporary folder."); TERRAMA2_LOG_ERROR() << errMsg; throw DataAccessException() << ErrorDescription(errMsg); } QDir tempDir(tempBaseDir.path()); tempDir.mkdir(QString::fromStdString(folder)); tempDir.cd(QString::fromStdString(folder)); QUrl url((uri+"/"+folder+"/"+mask).c_str()); QFileInfo originalInfo(url.path()); QFile file(url.path()); QFile tempFile(tempDir.path()+"/"+originalInfo.fileName()); if(!file.open(QIODevice::ReadOnly)) { QString errMsg = QObject::tr("Can't open file: dataset %1.").arg(dataSet->id); TERRAMA2_LOG_ERROR() << errMsg; throw DataAccessException() << ErrorDescription(errMsg); } if(!tempFile.open(QIODevice::ReadWrite)) { QString errMsg = QObject::tr("Can't open temporary file: dataset %1.").arg(dataSet->id); TERRAMA2_LOG_ERROR() << errMsg; throw DataAccessException() << ErrorDescription(errMsg); } file.readLine();//ignore first line tempFile.write(file.readLine()); //headers line //ignore third and fourth lines file.readLine(); file.readLine(); //read all file tempFile.write(file.readAll()); //headers line //update file path std::string tempUri = "file://"+tempBaseDir.path().toStdString(); file.close(); tempFile.close(); auto dataSeries = terrama2::core::DataAccessorFile::getSeries(tempUri, filter, dataSet); return dataSeries; }
void SQLCode::_BuildNormalSQL(const SQLVarParms& varParms, SQLStatement& rSQLFormats, SQLStatement& rSqlStatement) { BOOST_LOG_FUNCTION(); if (!rSQLFormats.strCommonSQL.empty()) { _BuildSQL(varParms, rSQLFormats.strCommonSQL, rSqlStatement.strCommonSQL); } else { if (rSQLFormats.strOracleSQL.empty() || rSQLFormats.strMySQLSQL.empty() || rSQLFormats.strSQLiteSQL.empty()) { throw DataAccessException(ER_DB_ERR_SQLCode, "The MySQL or Oracle SQL or SQLite Format is empty"); } // build Oracle SQL Statement _BuildSQL(varParms, rSQLFormats.strOracleSQL, rSqlStatement.strOracleSQL, enumOracleDb); // build MySQL SQL statement _BuildSQL(varParms, rSQLFormats.strMySQLSQL, rSqlStatement.strMySQLSQL, enumMysqlDb); // build SqliteDb SQL Statement _BuildSQL(varParms, rSQLFormats.strSQLiteSQL, rSqlStatement.strSQLiteSQL, enumSqliteDb); } }
char SQLTypeAdapter::at(size_t i, size_t uIndex) const { if (i < length(uIndex)) { return m_strParams[uIndex].at(i); } else { throw DataAccessException(ER_DB_ERR_SQLTypeAdapter, "Not enough chars in SQLTypeAdapter"); } }
void SQLCode::_GetSQLFormat(const std::string& strSQLKey, SQLStatement& strSQLFormats) { BOOST_LOG_FUNCTION(); boost::mutex::scoped_lock lock(m_mutexSQLFileHelper); m_pSqlFileHelper->getSQLString(strSQLKey, strSQLFormats); if (strSQLFormats.strCommonSQL.empty() && strSQLFormats.strOracleSQL.empty() && strSQLFormats.strMySQLSQL.empty() && strSQLFormats.strSQLiteSQL.empty()) { throw DataAccessException(ER_DB_ERR_SQLCode, "Cannot find the SQL statement in the hash-table"); } }
void SQLCode::prepareSQLStatement(SQLStatement& rSqlStatement, SQLVarParms& varParms) { BOOST_LOG_FUNCTION(); try { LOG_DEBUG<< "Begin prepare SQL statement..."; rSqlStatement.clear(); _BuildSQLStatement(varParms, rSqlStatement); LOG_DEBUG<< "End prepare SQL statement..."; } catch (DataAccessException& e) { e.logInfo(); throw e; } catch (...) { throw DataAccessException(ER_DB_ERR_UNKNOWN, "unknow buildSQLStatement erro!"); } }