8#include <boost/lexical_cast.hpp>
9#include <boost/date_time/gregorian/gregorian.hpp>
10#include <boost/filesystem.hpp>
11#include <boost/algorithm/string.hpp>
14#include <soci/sqlite3/soci-sqlite3.h>
15#include <soci/mysql/soci-mysql.h>
16#include <soci/postgresql/soci-postgresql.h>
37 bool oCreationSuccessful =
true;
41 boost::filesystem::path lSQLiteDBFullPath (iSQLDBConnStr.begin(),
44 boost::filesystem::path lSQLiteDBParentPath =
45 lSQLiteDBFullPath.parent_path();
49 <<
"') will be cleared and re-created");
52 boost::filesystem::remove_all (lSQLiteDBFullPath);
55 boost::filesystem::create_directories (lSQLiteDBParentPath);
59 if (!(boost::filesystem::exists (lSQLiteDBParentPath)
60 && boost::filesystem::is_directory (lSQLiteDBParentPath))) {
61 std::ostringstream oStr;
62 oStr <<
"Error. The path to the SQLite3 database directory ('"
63 << lSQLiteDBParentPath
64 <<
"') does not exist or is not a directory.";
69 }
catch (std::exception
const& lException) {
70 std::ostringstream errorStr;
71 errorStr <<
"Error when trying to create " << iSQLDBConnStr
72 <<
" SQLite3 database file: " << lException.what();
73 errorStr <<
". Check that the program has got write permission on the "
74 <<
"corresponding parent directories.";
81 <<
"') has been cleared and re-created");
84 return oCreationSuccessful;
90 bool oCreationSuccessful =
true;
97 <<
"' database in MySQL/MariaDB ('" << iSQLDBConnStr
101 soci::session* lSociSession_ptr = NULL;
107 if (lSociSession_ptr == NULL) {
108 oCreationSuccessful =
false;
109 return oCreationSuccessful;
112 }
catch (soci::mysql_soci_error
const& lSociException) {
113 std::ostringstream errorStr;
114 errorStr <<
"SOCI-related error when trying to connect to the "
115 <<
"MySQL/MariaDB database ('" << iSQLDBConnStr
116 <<
"'). SOCI error message: " << lSociException.what();
118 std::cerr << errorStr.str() << std::endl;
119 oCreationSuccessful =
false;
120 return oCreationSuccessful;
122 assert (lSociSession_ptr != NULL);
123 soci::session& lSociSession = *lSociSession_ptr;
155 std::ostringstream lSQLDropTrepLocalStr;
156 lSQLDropTrepLocalStr <<
"drop user '"
159 lSociSession << lSQLDropTrepLocalStr.str();
162 std::ostringstream lSQLDropTrepAllStr;
163 lSQLDropTrepAllStr <<
"drop user '"
165 lSociSession << lSQLDropTrepAllStr.str();
167 }
catch (soci::mysql_soci_error
const& lSociException) {
168 std::ostringstream issueStr;
169 issueStr <<
"Issue when trying to drop MySQL/MariaDB '"
171 <<
"Most probably the user did not exist before. " << std::endl
172 <<
"SOCI error message: " << lSociException.what() << std::endl
173 <<
"The database users should however be created without "
176 std::cout << issueStr.str() << std::endl;
181 std::ostringstream lSQLCreateTrepLocalStr;
182 lSQLCreateTrepLocalStr <<
"create user '"
185 lSQLCreateTrepLocalStr <<
"identified by '"
187 lSociSession << lSQLCreateTrepLocalStr.str();
190 std::ostringstream lSQLGrantTrepLocalStr;
191 lSQLGrantTrepLocalStr <<
"grant SELECT, INSERT, UPDATE, DELETE, ";
192 lSQLGrantTrepLocalStr <<
"CREATE, DROP, FILE, INDEX, ALTER, ";
193 lSQLGrantTrepLocalStr <<
"CREATE TEMPORARY TABLES, CREATE VIEW, EVENT, ";
194 lSQLGrantTrepLocalStr <<
"TRIGGER, SHOW VIEW, CREATE ROUTINE, ";
195 lSQLGrantTrepLocalStr <<
"ALTER ROUTINE, EXECUTE ON *.*";
198 lSociSession << lSQLGrantTrepLocalStr.str();
201 std::ostringstream lSQLCreateTrepAllStr;
202 lSQLCreateTrepAllStr <<
"create user '"
204 <<
"'@'%' identified by '"
206 lSociSession << lSQLCreateTrepAllStr.str();
209 std::ostringstream lSQLGrantTrepAllStr;
210 lSQLGrantTrepAllStr <<
"grant SELECT, INSERT, UPDATE, DELETE, ";
211 lSQLGrantTrepAllStr <<
"CREATE, DROP, FILE, INDEX, ALTER, ";
212 lSQLGrantTrepAllStr <<
"CREATE TEMPORARY TABLES, CREATE VIEW, EVENT, ";
213 lSQLGrantTrepAllStr <<
"TRIGGER, SHOW VIEW, CREATE ROUTINE, ";
214 lSQLGrantTrepAllStr <<
"ALTER ROUTINE, EXECUTE ON *.*";
217 lSociSession << lSQLGrantTrepAllStr.str();
220 std::ostringstream lSQLFlushPrivilegesStr;
221 lSQLFlushPrivilegesStr <<
"flush privileges;";
222 lSociSession << lSQLFlushPrivilegesStr.str();
224 }
catch (soci::mysql_soci_error
const& lSociException) {
225 oCreationSuccessful =
false;
226 std::ostringstream errorStr;
227 errorStr <<
"SOCI-related error when trying to create MySQL/MariaDB "
229 <<
"' user. Error message: " << lSociException.what();
231 std::cerr << errorStr.str() << std::endl;
232 oCreationSuccessful =
false;
233 return oCreationSuccessful;
246 std::ostringstream lSQLDropDBStr;
247 lSQLDropDBStr <<
"drop database if exists "
250 lSociSession << lSQLDropDBStr.str();
253 std::ostringstream lSQLCreateDBStr;
254 lSQLCreateDBStr <<
"create database if not exists "
256 lSQLCreateDBStr <<
" default character set utf8mb4";
257 lSQLCreateDBStr <<
" collate utf8mb4_unicode_ci;";
258 lSociSession << lSQLCreateDBStr.str();
260 }
catch (soci::mysql_soci_error
const& lSociException) {
261 oCreationSuccessful =
false;
262 std::ostringstream errorStr;
263 errorStr <<
"SOCI-related error when trying to create MySQL/MariaDB "
265 <<
"' database with 'utf8mb4' as character set. "
266 <<
"Error message: " << lSociException.what();
268 std::cerr << errorStr.str() << std::endl;
270 if (oCreationSuccessful ==
false) {
273 std::ostringstream lSQLDropDBStr;
274 lSQLDropDBStr <<
"drop database if exists "
277 lSociSession << lSQLDropDBStr.str();
280 std::ostringstream lSQLCreateDBStr;
281 lSQLCreateDBStr <<
"create database if not exists "
283 << iDeploymentNumber;
284 lSQLCreateDBStr <<
" default character set utf8";
285 lSQLCreateDBStr <<
" collate utf8_unicode_ci;";
286 lSociSession << lSQLCreateDBStr.str();
288 }
catch (soci::mysql_soci_error
const& lSociException) {
289 oCreationSuccessful =
false;
290 std::ostringstream errorStr;
291 errorStr <<
"SOCI-related error when trying to create MySQL/MariaDB "
294 <<
"' database. Error message: " << lSociException.what();
296 std::cerr << errorStr.str() << std::endl;
297 oCreationSuccessful =
false;
298 return oCreationSuccessful;
306 <<
"' database have been created in MySQL/MariaDB ('"
307 << iSQLDBConnStr <<
"')");
309 return oCreationSuccessful;
315 bool oCreationSuccessful =
true;
323 <<
"' database in PostgreSQL ('" << iSQLDBConnStr
327 soci::session* lSociSession_ptr = NULL;
333 if (lSociSession_ptr == NULL) {
334 oCreationSuccessful =
false;
335 return oCreationSuccessful;
338 }
catch (soci::postgresql_soci_error
const& lSociException) {
339 std::ostringstream errorStr;
340 errorStr <<
"SOCI-related error when trying to connect to the "
341 <<
"PostgreSQL database ('" << iSQLDBConnStr
342 <<
"'). SOCI error message: " << lSociException.what();
344 std::cerr << errorStr.str() << std::endl;
345 oCreationSuccessful =
false;
346 return oCreationSuccessful;
348 assert (lSociSession_ptr != NULL);
349 soci::session& lSociSession = *lSociSession_ptr;
369 std::ostringstream lSQLDropRoleStr;
370 lSQLDropRoleStr <<
"DROP ROLE IF EXISTS "
372 lSociSession << lSQLDropRoleStr.str();
375 std::ostringstream lSQLCreateRoleStr;
377 <<
" WITH LOGIN ENCRYPTED PASSWORD '"
379 lSociSession << lSQLCreateRoleStr.str();
381 }
catch (soci::postgresql_soci_error
const& lSociException) {
382 oCreationSuccessful =
false;
383 std::ostringstream errorStr;
384 errorStr <<
"SOCI-related error when trying to create PostgreSQL role '"
386 <<
"'. Error message: " << lSociException.what();
388 std::cerr << errorStr.str() << std::endl;
389 return oCreationSuccessful;
394 std::ostringstream lSQLDropDBStr;
395 lSQLDropDBStr <<
"DROP DATABASE IF EXISTS "
398 lSociSession << lSQLDropDBStr.str();
401 std::ostringstream lSQLCreateDBStr;
402 lSQLCreateDBStr <<
"CREATE DATABASE "
405 <<
" ENCODING 'UTF8' TEMPLATE template0;";
406 lSociSession << lSQLCreateDBStr.str();
408 }
catch (soci::postgresql_soci_error
const& lSociException) {
409 oCreationSuccessful =
false;
410 std::ostringstream errorStr;
411 errorStr <<
"SOCI-related error when trying to create PostgreSQL database '"
413 <<
"'. Error message: " << lSociException.what();
415 std::cerr << errorStr.str() << std::endl;
416 return oCreationSuccessful;
423 <<
"' database have been created in PostgreSQL ('"
424 << iSQLDBConnStr <<
"')");
426 return oCreationSuccessful;
434 bool oCreationSuccessful =
true;
465 return oCreationSuccessful;
472 soci::session* oSociSession_ptr = NULL;
480 <<
" SQL database/file ('" << iSQLDBConnStr <<
"')");
486 const bool existSQLDBDir =
488 if (existSQLDBDir ==
false) {
489 std::ostringstream errorStr;
490 errorStr <<
"Error when trying to connect to the '" << iSQLDBConnStr
491 <<
"' SQLite3 database; the directory hosting that "
492 <<
"database does not exist or is not readable";
500 oSociSession_ptr =
new soci::session();
501 assert (oSociSession_ptr != NULL);
502 soci::session& lSociSession = *oSociSession_ptr;
503 lSociSession.open (soci::sqlite3, iSQLDBConnStr);
507 <<
"') has been checked and opened");
509 }
catch (std::exception
const& lException) {
510 std::ostringstream errorStr;
511 errorStr <<
"Error when trying to connect to the '" << iSQLDBConnStr
512 <<
"' SQLite3 database: " << lException.what();
518 assert (oSociSession_ptr != NULL);
527 oSociSession_ptr =
new soci::session();
528 assert (oSociSession_ptr != NULL);
529 soci::session& lSociSession = *oSociSession_ptr;
530 lSociSession.open (soci::mysql, iSQLDBConnStr);
534 << iSQLDBConnStr <<
") is accessible");
536 }
catch (std::exception
const& lException) {
537 std::ostringstream errorStr;
538 errorStr <<
"Error when trying to connect to the '" << iSQLDBConnStr
539 <<
"' MySQL/MariaDB database: " << lException.what();
545 assert (oSociSession_ptr != NULL);
554 oSociSession_ptr =
new soci::session();
555 assert (oSociSession_ptr != NULL);
556 soci::session& lSociSession = *oSociSession_ptr;
557 lSociSession.open (soci::postgresql, iSQLDBConnStr);
561 << iSQLDBConnStr <<
") is accessible");
563 }
catch (std::exception
const& lException) {
564 std::ostringstream errorStr;
565 errorStr <<
"Error when trying to connect to the '" << iSQLDBConnStr
566 <<
"' PostgreSQL database: " << lException.what();
572 assert (oSociSession_ptr != NULL);
584 std::ostringstream errorStr;
585 errorStr <<
"Error: the '" << iDBType.
describe()
586 <<
"' SQL database type is not supported";
593 return oSociSession_ptr;
600 soci::session& ioSociSession) {
607 <<
" SQL database/file ('" << iSQLDBConnStr <<
"')");
615 ioSociSession.close();
617 }
catch (std::exception
const& lException) {
618 std::ostringstream errorStr;
619 errorStr <<
"Error when trying to release the connection ('"
621 <<
"') to the SQLite3 database: " << lException.what();
633 ioSociSession.close();
635 }
catch (std::exception
const& lException) {
636 std::ostringstream errorStr;
637 errorStr <<
"Error when trying to release the connection ('"
639 <<
"') to the PostgreSQL database: " << lException.what();
651 ioSociSession.close();
653 }
catch (std::exception
const& lException) {
654 std::ostringstream errorStr;
655 errorStr <<
"Error when trying to release the connection ('"
657 <<
"') to the MySQL/MariaDB database: " << lException.what();
671 std::ostringstream errorStr;
672 errorStr <<
"Error: the '" << iDBType.
describe()
673 <<
"' SQL database type is not supported";
682 const std::string& lDBName = ioSociSession.get_backend_name();
683 const DBType lDBType (lDBName);
691 <<
" SQL database/file will be created/reset");
721 ioSociSession <<
"drop table if exists optd_por;";
722 std::ostringstream lSQLTableCreationStr;
723 lSQLTableCreationStr <<
"create table optd_por (";
724 lSQLTableCreationStr <<
"pk varchar(20) NOT NULL, ";
725 lSQLTableCreationStr <<
"location_type varchar(4) default NULL, ";
726 lSQLTableCreationStr <<
"iata_code varchar(3) default NULL, ";
727 lSQLTableCreationStr <<
"icao_code varchar(4) default NULL, ";
728 lSQLTableCreationStr <<
"faa_code varchar(4) default NULL, ";
729 lSQLTableCreationStr <<
"unlocode_code varchar(5) default NULL, ";
730 lSQLTableCreationStr <<
"uic_code int(11) default NULL, ";
731 lSQLTableCreationStr <<
"is_geonames varchar(1) default NULL, ";
732 lSQLTableCreationStr <<
"geoname_id int(11) default NULL, ";
733 lSQLTableCreationStr <<
"envelope_id int(11) default NULL, ";
734 lSQLTableCreationStr <<
"date_from date default NULL, ";
735 lSQLTableCreationStr <<
"date_until date default NULL, ";
736 lSQLTableCreationStr <<
"serialised_place varchar(12000) default NULL);";
737 ioSociSession << lSQLTableCreationStr.str();
739 }
catch (std::exception
const& lException) {
740 std::ostringstream errorStr;
741 errorStr <<
"Error when trying to create SQLite3 tables: "
742 << lException.what();
778 ioSociSession <<
"drop table if exists optd_por;";
779 std::ostringstream lSQLTableCreationStr;
780 lSQLTableCreationStr <<
"create table optd_por (";
781 lSQLTableCreationStr <<
"pk varchar(20) NOT NULL, ";
782 lSQLTableCreationStr <<
"location_type varchar(4) default NULL, ";
783 lSQLTableCreationStr <<
"iata_code varchar(3) default NULL, ";
784 lSQLTableCreationStr <<
"icao_code varchar(4) default NULL, ";
785 lSQLTableCreationStr <<
"faa_code varchar(4) default NULL, ";
786 lSQLTableCreationStr <<
"unlocode_code varchar(5) default NULL, ";
787 lSQLTableCreationStr <<
"uic_code integer default NULL, ";
788 lSQLTableCreationStr <<
"is_geonames varchar(1) default NULL, ";
789 lSQLTableCreationStr <<
"geoname_id integer default NULL, ";
790 lSQLTableCreationStr <<
"envelope_id integer default NULL, ";
791 lSQLTableCreationStr <<
"date_from date default NULL, ";
792 lSQLTableCreationStr <<
"date_until date default NULL, ";
793 lSQLTableCreationStr <<
"serialised_place varchar(12000) default NULL);";
794 ioSociSession << lSQLTableCreationStr.str();
796 }
catch (std::exception
const& lException) {
797 std::ostringstream errorStr;
798 errorStr <<
"Error when trying to create PostgreSQL tables: "
799 << lException.what();
806 "PostgreSQL database");
836 ioSociSession <<
"drop table if exists optd_por;";
837 std::ostringstream lSQLTableCreationStr;
838 lSQLTableCreationStr <<
"create table optd_por (";
839 lSQLTableCreationStr <<
"pk varchar(20) NOT NULL, ";
840 lSQLTableCreationStr <<
"location_type varchar(4) default NULL, ";
841 lSQLTableCreationStr <<
"iata_code varchar(3) default NULL, ";
842 lSQLTableCreationStr <<
"icao_code varchar(4) default NULL, ";
843 lSQLTableCreationStr <<
"faa_code varchar(4) default NULL, ";
844 lSQLTableCreationStr <<
"unlocode_code varchar(5) default NULL, ";
845 lSQLTableCreationStr <<
"uic_code int(11) default NULL, ";
846 lSQLTableCreationStr <<
"is_geonames varchar(1) default NULL, ";
847 lSQLTableCreationStr <<
"geoname_id int(11) default NULL, ";
848 lSQLTableCreationStr <<
"envelope_id int(11) default NULL, ";
849 lSQLTableCreationStr <<
"date_from date default NULL, ";
850 lSQLTableCreationStr <<
"date_until date default NULL, ";
851 lSQLTableCreationStr <<
"serialised_place varchar(12000) default NULL); ";
852 ioSociSession << lSQLTableCreationStr.str();
854 }
catch (std::exception
const& lException) {
855 std::ostringstream errorStr;
856 errorStr <<
"Error when trying to create MySQL/MariaDB tables: "
857 << lException.what();
875 std::ostringstream errorStr;
876 errorStr <<
"Error: the '" << lDBName
877 <<
"' SQL database type is not supported";
887 const std::string& lDBName = ioSociSession.get_backend_name();
888 const DBType lDBType (lDBName);
896 <<
" SQL database/file will be created/reset");
918 <<
"create index optd_por_iata_code on optd_por (iata_code);";
920 <<
"create index optd_por_iata_date on optd_por (iata_code, date_from, date_until);";
922 <<
"create index optd_por_icao_code on optd_por (icao_code);";
924 <<
"create index optd_por_geonameid on optd_por (geoname_id);";
926 <<
"create index optd_por_unlocode_code on optd_por (unlocode_code);";
928 <<
"create index optd_por_uic_code on optd_por (uic_code);";
930 }
catch (std::exception
const& lException) {
931 std::ostringstream errorStr;
932 errorStr <<
"Error when trying to create SQLite3 indexes: "
933 << lException.what();
940 "for the SQLite3 database");
963 <<
"create unique index optd_por_pk on optd_por (pk);";
965 <<
"create index optd_por_iata_code on optd_por (iata_code asc);";
967 <<
"create index optd_por_iata_date on optd_por (iata_code asc, date_from asc, date_until asc);";
969 <<
"create index optd_por_icao_code on optd_por (icao_code asc);";
971 <<
"create index optd_por_geonameid on optd_por (geoname_id asc);";
973 <<
"create index optd_por_unlocode_code on optd_por (unlocode_code asc);";
975 <<
"create index optd_por_uic_code on optd_por (uic_code asc);";
977 }
catch (std::exception
const& lException) {
978 std::ostringstream errorStr;
979 errorStr <<
"Error when trying to create PostgreSQL indices: "
980 << lException.what();
987 "for the PostgreSQL database");
1011 <<
"alter table optd_por add unique index optd_por_pk (pk asc);";
1013 <<
"alter table optd_por add index optd_por_iata_code (iata_code asc);";
1015 <<
"alter table optd_por add index optd_por_iata_date (iata_code asc, date_from asc, date_until asc);";
1017 <<
"alter table optd_por add index optd_por_icao_code (icao_code asc);";
1019 <<
"alter table optd_por add index optd_por_geonameid (geoname_id asc);";
1021 <<
"alter table optd_por add index optd_por_unlocode_code (unlocode_code asc);";
1023 <<
"alter table optd_por add index optd_por_uic_code (uic_code asc);";
1025 }
catch (std::exception
const& lException) {
1026 std::ostringstream errorStr;
1027 errorStr <<
"Error when trying to create MySQL/MariaDB indices: "
1028 << lException.what();
1035 "for the MySQL/MariaDB database");
1046 std::ostringstream errorStr;
1047 errorStr <<
"Error: the '" << lDBName
1048 <<
"' SQL database type is not supported";
1059 soci::statement& ioSelectStatement) {
1060 std::string oSerialisedPlaceStr;
1069 ioSelectStatement = (ioSociSession.prepare
1070 <<
"select serialised_place from optd_por",
1071 soci::into (oSerialisedPlaceStr));
1074 ioSelectStatement.execute();
1076 }
catch (std::exception
const& lException) {
1077 std::ostringstream errorStr;
1079 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1080 << lException.what();
1086 return oSerialisedPlaceStr;
1091 prepareSelectBlobOnIataCodeStatement (soci::session& ioSociSession,
1092 soci::statement& ioSelectStatement,
1093 const std::string& iIataCode,
1094 std::string& ioSerialisedPlaceStr) {
1102 ioSelectStatement = (ioSociSession.prepare
1103 <<
"select serialised_place from optd_por "
1104 <<
"where iata_code = :place_iata_code",
1105 soci::use (iIataCode),
1106 soci::into (ioSerialisedPlaceStr));
1109 ioSelectStatement.execute();
1111 }
catch (std::exception
const& lException) {
1112 std::ostringstream errorStr;
1114 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1115 << lException.what();
1117 throw SQLDatabaseException (errorStr.str());
1123 prepareSelectBlobOnIcaoCodeStatement (soci::session& ioSociSession,
1124 soci::statement& ioSelectStatement,
1125 const std::string& iIcaoCode,
1126 std::string& ioSerialisedPlaceStr) {
1134 ioSelectStatement = (ioSociSession.prepare
1135 <<
"select serialised_place from optd_por "
1136 <<
"where icao_code = :place_icao_code",
1137 soci::use (iIcaoCode),
1138 soci::into (ioSerialisedPlaceStr));
1141 ioSelectStatement.execute();
1143 }
catch (std::exception
const& lException) {
1144 std::ostringstream errorStr;
1146 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1147 << lException.what();
1149 throw SQLDatabaseException (errorStr.str());
1155 prepareSelectBlobOnFaaCodeStatement (soci::session& ioSociSession,
1156 soci::statement& ioSelectStatement,
1157 const std::string& iFaaCode,
1158 std::string& ioSerialisedPlaceStr) {
1166 ioSelectStatement = (ioSociSession.prepare
1167 <<
"select serialised_place from optd_por "
1168 <<
"where faa_code = :place_faa_code",
1169 soci::use (iFaaCode),
1170 soci::into (ioSerialisedPlaceStr));
1173 ioSelectStatement.execute();
1175 }
catch (std::exception
const& lException) {
1176 std::ostringstream errorStr;
1178 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1179 << lException.what();
1181 throw SQLDatabaseException (errorStr.str());
1187 prepareSelectBlobOnUNLOCodeStatement (soci::session& ioSociSession,
1188 soci::statement& ioSelectStatement,
1189 const std::string& iUNLOCode,
1190 std::string& ioSerialisedPlaceStr) {
1198 ioSelectStatement = (ioSociSession.prepare
1199 <<
"select serialised_place from optd_por "
1200 <<
"where unlocode_code = :place_unlocode_code",
1201 soci::use (iUNLOCode),
1202 soci::into (ioSerialisedPlaceStr));
1205 ioSelectStatement.execute();
1207 }
catch (std::exception
const& lException) {
1208 std::ostringstream errorStr;
1210 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1211 << lException.what();
1213 throw SQLDatabaseException (errorStr.str());
1219 prepareSelectBlobOnUICCodeStatement (soci::session& ioSociSession,
1220 soci::statement& ioSelectStatement,
1222 std::string& ioSerialisedPlaceStr) {
1230 ioSelectStatement = (ioSociSession.prepare
1231 <<
"select serialised_place from optd_por "
1232 <<
"where uic_code = :place_uic_code",
1233 soci::use (iUICCode),
1234 soci::into (ioSerialisedPlaceStr));
1237 ioSelectStatement.execute();
1239 }
catch (std::exception
const& lException) {
1240 std::ostringstream errorStr;
1242 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1243 << lException.what();
1245 throw SQLDatabaseException (errorStr.str());
1251 prepareSelectBlobOnPlaceGeoIDStatement (soci::session& ioSociSession,
1252 soci::statement& ioSelectStatement,
1254 std::string& ioSerialisedPlaceStr) {
1262 ioSelectStatement = (ioSociSession.prepare
1263 <<
"select serialised_place from optd_por "
1264 <<
"where geoname_id = :place_geoname_id",
1265 soci::use (iGeonameID),
1266 soci::into (ioSerialisedPlaceStr));
1269 ioSelectStatement.execute();
1271 }
catch (std::exception
const& lException) {
1272 std::ostringstream errorStr;
1274 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1275 << lException.what();
1277 throw SQLDatabaseException (errorStr.str());
1283 const std::string& iSerialisedPlaceStr) {
1284 bool hasStillData =
false;
1289 hasStillData = ioStatement.fetch();
1291 }
catch (std::exception
const& lException) {
1292 std::ostringstream errorStr;
1293 errorStr <<
"Error when iterating on the SQL fetch: " << lException.what();
1294 errorStr <<
". The current place is: " << iSerialisedPlaceStr;
1299 return hasStillData;
1304 const Place& iPlace) {
1309 ioSociSession.begin();
1313 const std::string lPK (lLocationKey.
toString());
1316 const std::string lIataCode (iPlace.
getIataCode());
1317 const std::string lIcaoCode (iPlace.
getIcaoCode());
1318 const std::string lFaaCode (iPlace.
getFaaCode());
1319 const std::string lIsGeonames ((iPlace.
isGeonames())?
"Y":
"N");
1320 const std::string lGeonameID =
1322 const std::string lEnvID =
1324 const std::string lDateFrom =
1325 boost::gregorian::to_iso_extended_string (iPlace.
getDateFrom());
1326 const std::string lDateEnd =
1327 boost::gregorian::to_iso_extended_string (iPlace.
getDateEnd());
1355 std::string lUNLOCodeStr (
"");
1356 if (lUNLOCodeList.empty() ==
false) {
1357 const UNLOCode_T& lUNLOCode = lUNLOCodeList.front();
1358 lUNLOCodeStr =
static_cast<const std::string
> (lUNLOCode);
1367 if (lUICCodeList.empty() ==
false) {
1368 const UICCode_T& lUICCode = lUICCodeList.front();
1369 lUICCodeInt =
static_cast<const UICCode_T> (lUICCode);
1387 ioSociSession <<
"insert into optd_por values (:pk, "
1388 <<
":location_type, :iata_code, :icao_code, :faa_code, "
1389 <<
":unlocode_code, :uic_code, "
1390 <<
":is_geonames, :geoname_id, "
1391 <<
":envelope_id, :date_from, :date_until, "
1392 <<
":serialised_place)",
1393 soci::use (lPK), soci::use (lLocationType), soci::use (lIataCode),
1394 soci::use (lIcaoCode), soci::use (lFaaCode),
1395 soci::use (lUNLOCodeStr), soci::use (lUICCodeInt),
1396 soci::use (lIsGeonames), soci::use (lGeonameID),
1397 soci::use (lEnvID), soci::use (lDateFrom), soci::use (lDateEnd),
1398 soci::use (lRawDataString);
1401 ioSociSession.commit();
1406 }
catch (std::exception
const& lException) {
1407 std::ostringstream errorStr;
1408 errorStr <<
"Error when updating " << iPlace.
toString() <<
": "
1409 << lException.what();
1417 const Place& iPlace) {
1422 ioSociSession.begin();
1426 std::string lIataCode;
1427 soci::statement lUpdateStatement =
1428 (ioSociSession.prepare
1429 <<
"update place_details "
1430 <<
"set xapian_docid = :xapian_docid "
1431 <<
"where iata_code = :iata_code",
1432 soci::use (lDocID), soci::use (lIataCode));
1437 lUpdateStatement.execute (
true);
1440 ioSociSession.commit();
1445 }
catch (std::exception
const& lException) {
1446 std::ostringstream errorStr;
1447 errorStr <<
"Error when updating " << iPlace.
toString() <<
": "
1448 << lException.what();
1466 ioSociSession <<
"select count(1) from optd_por;", soci::into(oNbOfEntries);
1468 }
catch (std::exception
const& lException) {
1469 std::ostringstream errorStr;
1471 <<
"Error when trying to count the number of rows in the optd_por table: "
1472 << lException.what();
1477 return oNbOfEntries;
1487 soci::statement lSelectStatement (ioSociSession);
1488 std::string lPlace =
1496 bool hasStillData =
true;
1497 while (hasStillData ==
true) {
1501 if (hasStillData ==
true) {
1509 }
catch (std::exception
const& lException) {
1510 std::ostringstream errorStr;
1511 errorStr <<
"Error when trying to retrieve " << oNbOfEntries
1512 <<
"-th row from the SQL database: " << lException.what();
1517 return oNbOfEntries;
1524 const bool iUniqueEntry) {
1533 const std::string& lCode =
static_cast<const std::string&
> (iIataCode);
1534 const std::string lCodeUpper = boost::algorithm::to_upper_copy (lCode);
1537 soci::statement lSelectStatement (ioSociSession);
1538 std::string lPlaceRawDataString;
1539 DBManager::prepareSelectBlobOnIataCodeStatement (ioSociSession,
1542 lPlaceRawDataString);
1548 bool hasStillData =
true;
1549 while (hasStillData ==
true) {
1551 lPlaceRawDataString);
1554 const std::string lFoundStr = hasStillData?
"more; see below":
"no more";
1556 <<
"corresponding to '" << iIataCode
1557 <<
"' IATA code. Result: " << lFoundStr);
1559 if (hasStillData ==
true) {
1570 lLocationList.push_back (lLocation);
1577 }
catch (std::exception
const& lException) {
1578 std::ostringstream errorStr;
1579 errorStr <<
"Error when trying to retrieve a POR for " << iIataCode
1580 <<
" from the SQL database: " << lException.what();
1587 const Location* lHighestPRLocation_ptr = NULL;
1589 for (LocationList_T::const_iterator itLoc = lLocationList.begin();
1590 itLoc != lLocationList.end(); ++itLoc) {
1591 const Location& lLocation = *itLoc;
1601 if (lPRValue >= lHighestPRValue) {
1602 lHighestPRLocation_ptr = &lLocation;
1603 lHighestPRValue = lPRValue;
1608 if (iUniqueEntry ==
false) {
1609 ioLocationList.push_back (lLocation);
1614 if (iUniqueEntry ==
true && lHighestPRLocation_ptr != NULL) {
1615 assert (lHighestPRLocation_ptr != NULL);
1616 ioLocationList.push_back (*lHighestPRLocation_ptr);
1620 << lHighestPRValue <<
") for '" << iIataCode
1621 <<
"' IATA code: " << lHighestPRLocation_ptr->
getKey());
1626 if (oNbOfEntries > 0 && iUniqueEntry ==
true) {
1631 return oNbOfEntries;
1645 const std::string& lCode =
static_cast<const std::string&
> (iIcaoCode);
1646 const std::string lCodeUpper = boost::algorithm::to_upper_copy (lCode);
1649 soci::statement lSelectStatement (ioSociSession);
1650 std::string lPlaceRawDataString;
1651 DBManager::prepareSelectBlobOnIcaoCodeStatement (ioSociSession,
1654 lPlaceRawDataString);
1660 bool hasStillData =
true;
1661 while (hasStillData ==
true) {
1663 lPlaceRawDataString);
1666 const std::string lFoundStr = hasStillData?
"Yes":
"No";
1668 << iIcaoCode <<
" ICAO code. Found: " << lFoundStr);
1670 if (hasStillData ==
true) {
1681 ioLocationList.push_back (lLocation);
1688 }
catch (std::exception
const& lException) {
1689 std::ostringstream errorStr;
1690 errorStr <<
"Error when trying to retrieve a POR for " << iIcaoCode
1691 <<
" from the SQL database: " << lException.what();
1697 return oNbOfEntries;
1711 const std::string& lCode =
static_cast<const std::string&
> (iFaaCode);
1712 const std::string lCodeUpper = boost::algorithm::to_upper_copy (lCode);
1715 soci::statement lSelectStatement (ioSociSession);
1716 std::string lPlaceRawDataString;
1717 DBManager::prepareSelectBlobOnFaaCodeStatement (ioSociSession,
1720 lPlaceRawDataString);
1726 bool hasStillData =
true;
1727 while (hasStillData ==
true) {
1729 lPlaceRawDataString);
1732 const std::string lFoundStr = hasStillData?
"Yes":
"No";
1734 << iFaaCode <<
" FAA code. Found: " << lFoundStr);
1736 if (hasStillData ==
true) {
1747 ioLocationList.push_back (lLocation);
1754 }
catch (std::exception
const& lException) {
1755 std::ostringstream errorStr;
1756 errorStr <<
"Error when trying to retrieve a POR for " << iFaaCode
1757 <<
" from the SQL database: " << lException.what();
1763 return oNbOfEntries;
1770 const bool iUniqueEntry) {
1779 const std::string& lCode =
static_cast<const std::string&
> (iUNLOCode);
1780 const std::string lCodeUpper = boost::algorithm::to_upper_copy (lCode);
1783 soci::statement lSelectStatement (ioSociSession);
1784 std::string lPlaceRawDataString;
1785 DBManager::prepareSelectBlobOnUNLOCodeStatement (ioSociSession,
1788 lPlaceRawDataString);
1794 bool hasStillData =
true;
1795 while (hasStillData ==
true) {
1797 lPlaceRawDataString);
1800 const std::string lFoundStr = hasStillData?
"Yes":
"No";
1802 << iUNLOCode <<
" UN/LOCODE code. Found: "
1805 if (hasStillData ==
true) {
1816 lLocationList.push_back (lLocation);
1823 }
catch (std::exception
const& lException) {
1824 std::ostringstream errorStr;
1825 errorStr <<
"Error when trying to retrieve a POR for " << iUNLOCode
1826 <<
" from the SQL database: " << lException.what();
1833 const Location* lHighestPRLocation_ptr = NULL;
1835 for (LocationList_T::const_iterator itLoc = lLocationList.begin();
1836 itLoc != lLocationList.end(); ++itLoc) {
1837 const Location& lLocation = *itLoc;
1841 if (lPRValue > lHighestPRValue) {
1842 lHighestPRLocation_ptr = &lLocation;
1843 lHighestPRValue = lPRValue;
1847 if (iUniqueEntry ==
false) {
1848 ioLocationList.push_back (lLocation);
1853 if (iUniqueEntry ==
true && lHighestPRLocation_ptr != NULL) {
1854 assert (lHighestPRLocation_ptr != NULL);
1855 ioLocationList.push_back (*lHighestPRLocation_ptr);
1859 << lHighestPRValue <<
") for '" << iUNLOCode
1860 <<
"' IATA code: " << lHighestPRLocation_ptr->
getKey());
1865 if (oNbOfEntries > 0 && iUniqueEntry ==
true) {
1870 return oNbOfEntries;
1882 soci::statement lSelectStatement (ioSociSession);
1883 std::string lPlaceRawDataString;
1884 DBManager::prepareSelectBlobOnUICCodeStatement (ioSociSession,
1887 lPlaceRawDataString);
1893 bool hasStillData =
true;
1894 while (hasStillData ==
true) {
1896 lPlaceRawDataString);
1899 const std::string lFoundStr = hasStillData?
"Yes":
"No";
1901 << iUICCode <<
" UIC code. Found: "
1904 if (hasStillData ==
true) {
1912 const std::string lUICCodeStr =
1913 boost::lexical_cast<std::string> (iUICCode);
1917 ioLocationList.push_back (lLocation);
1924 }
catch (std::exception
const& lException) {
1925 std::ostringstream errorStr;
1926 errorStr <<
"Error when trying to retrieve a POR for " << iUICCode
1927 <<
" from the SQL database: " << lException.what();
1933 return oNbOfEntries;
1945 soci::statement lSelectStatement (ioSociSession);
1946 std::string lPlaceRawDataString;
1947 DBManager::prepareSelectBlobOnPlaceGeoIDStatement (ioSociSession,
1950 lPlaceRawDataString);
1956 bool hasStillData =
true;
1957 while (hasStillData ==
true) {
1959 lPlaceRawDataString);
1962 const std::string lFoundStr = hasStillData?
"Yes":
"No";
1964 << iGeonameID<<
" Geonames ID. Found: "<< lFoundStr);
1966 if (hasStillData ==
true) {
1974 const std::string lGeonamesIDStr =
1975 boost::lexical_cast<std::string> (iGeonameID);
1979 ioLocationList.push_back (lLocation);
1986 }
catch (std::exception
const& lException) {
1987 std::ostringstream errorStr;
1988 errorStr <<
"Error when trying to retrieve a POR for " << iGeonameID
1989 <<
" from the SQL database: " << lException.what();
1995 return oNbOfEntries;
#define OPENTREP_LOG_ERROR(iToBeLogged)
#define OPENTREP_LOG_DEBUG(iToBeLogged)
static void terminateSQLDBSession(const DBType &, const SQLDBConnectionString_T &, soci::session &)
static void createSQLDBTables(soci::session &)
static std::string prepareSelectAllBlobStatement(soci::session &, soci::statement &)
static NbOfDBEntries_T getPORByUICCode(soci::session &, const UICCode_T &, LocationList_T &)
static NbOfDBEntries_T getPORByICAOCode(soci::session &, const ICAOCode_T &, LocationList_T &)
static soci::session * initSQLDBSession(const DBType &, const SQLDBConnectionString_T &)
static NbOfDBEntries_T getPORByFAACode(soci::session &, const FAACode_T &, LocationList_T &)
static void createSQLDBIndexes(soci::session &)
static NbOfDBEntries_T displayCount(soci::session &)
static NbOfDBEntries_T displayAll(soci::session &)
static NbOfDBEntries_T getPORByUNLOCode(soci::session &, const UNLOCode_T &, LocationList_T &, const bool iUniqueEntry)
static NbOfDBEntries_T getPORByGeonameID(soci::session &, const GeonamesID_T &, LocationList_T &)
static NbOfDBEntries_T getPORByIATACode(soci::session &, const IATACode_T &, LocationList_T &, const bool iUniqueEntry)
static void updatePlaceInDB(soci::session &, const Place &)
static bool iterateOnStatement(soci::statement &, const std::string &)
static bool createSQLDBUser(const DBType &, const SQLDBConnectionString_T &, const DeploymentNumber_T &)
static void insertPlaceInDB(soci::session &, const Place &)
static bool checkSQLiteDirectory(const std::string &iSQLDBConnStr)
Class modelling a place/POR (point of reference).
const IsGeonames_T & isGeonames() const
const ICAOCode_T & getIcaoCode() const
const RawDataString_T & getRawDataString() const
const FAACode_T & getFaaCode() const
const Date_T & getDateEnd() const
const GeonamesID_T & getGeonamesID() const
const XapianDocID_T & getDocID() const
const IATACode_T & getIataCode() const
const Date_T & getDateFrom() const
const EnvelopeID_T & getEnvelopeID() const
const UNLOCodeList_T & getUNLOCodeList() const
std::string toString() const
const IATAType & getIataType() const
const LocationKey & getKey() const
const UICCodeList_T & getUICCodeList() const
static Location retrieveLocation(const Xapian::Document &)
bool createSQLDBUserOnPG(const SQLDBConnectionString_T &iSQLDBConnStr, const DeploymentNumber_T &iDeploymentNumber)
bool createSQLDBUserOnMySQL(const SQLDBConnectionString_T &iSQLDBConnStr, const DeploymentNumber_T &iDeploymentNumber)
unsigned int NbOfDBEntries_T
const std::string DEFAULT_OPENTREP_MYSQL_DB_DBNAME
const std::string DEFAULT_OPENTREP_MYSQL_DB_USER
const std::string DEFAULT_OPENTREP_MYSQL_DB_PASSWD
std::list< Location > LocationList_T
std::list< UICCode_T > UICCodeList_T
const std::string DEFAULT_OPENTREP_MYSQL_DB_HOST
std::list< UNLOCode_T > UNLOCodeList_T
const std::string DEFAULT_OPENTREP_PG_DB_USER
bool createSQLDBUserOnSQLite(const SQLDBConnectionString_T &iSQLDBConnStr, const DeploymentNumber_T &iDeploymentNumber)
const std::string DEFAULT_OPENTREP_PG_DB_PASSWD
unsigned short DeploymentNumber_T
unsigned int GeonamesID_T
const std::string DEFAULT_OPENTREP_PG_DB_DBNAME
unsigned int XapianDocID_T
Enumeration of database types.
const std::string describe() const
static EN_DBType getType(const char)
Enumeration of place/location types with respect to their use for transportation purposes.
std::string getTypeAsString() const
Class modelling the primary key of a location/POR (point of reference).
std::string toString() const
Structure modelling a (geographical) location.
const LocationKey & getKey() const
const PageRank_T & getPageRank() const
void setCorrectedKeywords(const std::string &iCorrectedKeywords)