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");
972 long long lNbOfDuplicatedPKs = 0;
974 <<
"select count(*) from "
975 "(select pk from optd_por group by pk having count(*) > 1) "
976 "as duplicated_pk;", soci::into (lNbOfDuplicatedPKs);
977 if (lNbOfDuplicatedPKs > 0) {
978 std::ostringstream warningStr;
979 warningStr << lNbOfDuplicatedPKs <<
" duplicated primary key(s) "
980 <<
"have been found in the 'optd_por' table (the "
981 <<
"upstream OPTD POR data file contains a few rows "
982 <<
"sharing the same key). For each duplicated key, "
983 <<
"all but the last-inserted row will be removed "
984 <<
"before creating the unique index.";
988 <<
"delete from optd_por a using optd_por b "
989 "where a.pk = b.pk and a.ctid < b.ctid;";
993 <<
"create unique index optd_por_pk on optd_por (pk);";
995 <<
"create index optd_por_iata_code on optd_por (iata_code asc);";
997 <<
"create index optd_por_iata_date on optd_por (iata_code asc, date_from asc, date_until asc);";
999 <<
"create index optd_por_icao_code on optd_por (icao_code asc);";
1001 <<
"create index optd_por_geonameid on optd_por (geoname_id asc);";
1003 <<
"create index optd_por_unlocode_code on optd_por (unlocode_code asc);";
1005 <<
"create index optd_por_uic_code on optd_por (uic_code asc);";
1007 }
catch (std::exception
const& lException) {
1008 std::ostringstream errorStr;
1009 errorStr <<
"Error when trying to create PostgreSQL indices: "
1010 << lException.what();
1017 "for the PostgreSQL database");
1041 <<
"alter table optd_por add unique index optd_por_pk (pk asc);";
1043 <<
"alter table optd_por add index optd_por_iata_code (iata_code asc);";
1045 <<
"alter table optd_por add index optd_por_iata_date (iata_code asc, date_from asc, date_until asc);";
1047 <<
"alter table optd_por add index optd_por_icao_code (icao_code asc);";
1049 <<
"alter table optd_por add index optd_por_geonameid (geoname_id asc);";
1051 <<
"alter table optd_por add index optd_por_unlocode_code (unlocode_code asc);";
1053 <<
"alter table optd_por add index optd_por_uic_code (uic_code asc);";
1055 }
catch (std::exception
const& lException) {
1056 std::ostringstream errorStr;
1057 errorStr <<
"Error when trying to create MySQL/MariaDB indices: "
1058 << lException.what();
1065 "for the MySQL/MariaDB database");
1076 std::ostringstream errorStr;
1077 errorStr <<
"Error: the '" << lDBName
1078 <<
"' SQL database type is not supported";
1089 soci::statement& ioSelectStatement) {
1090 std::string oSerialisedPlaceStr;
1099 ioSelectStatement = (ioSociSession.prepare
1100 <<
"select serialised_place from optd_por",
1101 soci::into (oSerialisedPlaceStr));
1104 ioSelectStatement.execute();
1106 }
catch (std::exception
const& lException) {
1107 std::ostringstream errorStr;
1109 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1110 << lException.what();
1116 return oSerialisedPlaceStr;
1121 prepareSelectBlobOnIataCodeStatement (soci::session& ioSociSession,
1122 soci::statement& ioSelectStatement,
1123 const std::string& iIataCode,
1124 std::string& ioSerialisedPlaceStr) {
1132 ioSelectStatement = (ioSociSession.prepare
1133 <<
"select serialised_place from optd_por "
1134 <<
"where iata_code = :place_iata_code",
1135 soci::use (iIataCode),
1136 soci::into (ioSerialisedPlaceStr));
1139 ioSelectStatement.execute();
1141 }
catch (std::exception
const& lException) {
1142 std::ostringstream errorStr;
1144 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1145 << lException.what();
1147 throw SQLDatabaseException (errorStr.str());
1153 prepareSelectBlobOnIcaoCodeStatement (soci::session& ioSociSession,
1154 soci::statement& ioSelectStatement,
1155 const std::string& iIcaoCode,
1156 std::string& ioSerialisedPlaceStr) {
1164 ioSelectStatement = (ioSociSession.prepare
1165 <<
"select serialised_place from optd_por "
1166 <<
"where icao_code = :place_icao_code",
1167 soci::use (iIcaoCode),
1168 soci::into (ioSerialisedPlaceStr));
1171 ioSelectStatement.execute();
1173 }
catch (std::exception
const& lException) {
1174 std::ostringstream errorStr;
1176 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1177 << lException.what();
1179 throw SQLDatabaseException (errorStr.str());
1185 prepareSelectBlobOnFaaCodeStatement (soci::session& ioSociSession,
1186 soci::statement& ioSelectStatement,
1187 const std::string& iFaaCode,
1188 std::string& ioSerialisedPlaceStr) {
1196 ioSelectStatement = (ioSociSession.prepare
1197 <<
"select serialised_place from optd_por "
1198 <<
"where faa_code = :place_faa_code",
1199 soci::use (iFaaCode),
1200 soci::into (ioSerialisedPlaceStr));
1203 ioSelectStatement.execute();
1205 }
catch (std::exception
const& lException) {
1206 std::ostringstream errorStr;
1208 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1209 << lException.what();
1211 throw SQLDatabaseException (errorStr.str());
1217 prepareSelectBlobOnUNLOCodeStatement (soci::session& ioSociSession,
1218 soci::statement& ioSelectStatement,
1219 const std::string& iUNLOCode,
1220 std::string& ioSerialisedPlaceStr) {
1228 ioSelectStatement = (ioSociSession.prepare
1229 <<
"select serialised_place from optd_por "
1230 <<
"where unlocode_code = :place_unlocode_code",
1231 soci::use (iUNLOCode),
1232 soci::into (ioSerialisedPlaceStr));
1235 ioSelectStatement.execute();
1237 }
catch (std::exception
const& lException) {
1238 std::ostringstream errorStr;
1240 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1241 << lException.what();
1243 throw SQLDatabaseException (errorStr.str());
1249 prepareSelectBlobOnUICCodeStatement (soci::session& ioSociSession,
1250 soci::statement& ioSelectStatement,
1252 std::string& ioSerialisedPlaceStr) {
1260 ioSelectStatement = (ioSociSession.prepare
1261 <<
"select serialised_place from optd_por "
1262 <<
"where uic_code = :place_uic_code",
1263 soci::use (iUICCode),
1264 soci::into (ioSerialisedPlaceStr));
1267 ioSelectStatement.execute();
1269 }
catch (std::exception
const& lException) {
1270 std::ostringstream errorStr;
1272 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1273 << lException.what();
1275 throw SQLDatabaseException (errorStr.str());
1281 prepareSelectBlobOnPlaceGeoIDStatement (soci::session& ioSociSession,
1282 soci::statement& ioSelectStatement,
1284 std::string& ioSerialisedPlaceStr) {
1292 ioSelectStatement = (ioSociSession.prepare
1293 <<
"select serialised_place from optd_por "
1294 <<
"where geoname_id = :place_geoname_id",
1295 soci::use (iGeonameID),
1296 soci::into (ioSerialisedPlaceStr));
1299 ioSelectStatement.execute();
1301 }
catch (std::exception
const& lException) {
1302 std::ostringstream errorStr;
1304 <<
"Error in the 'select serialised_place from optd_por' SQL request: "
1305 << lException.what();
1307 throw SQLDatabaseException (errorStr.str());
1313 const std::string& iSerialisedPlaceStr) {
1314 bool hasStillData =
false;
1319 hasStillData = ioStatement.fetch();
1321 }
catch (std::exception
const& lException) {
1322 std::ostringstream errorStr;
1323 errorStr <<
"Error when iterating on the SQL fetch: " << lException.what();
1324 errorStr <<
". The current place is: " << iSerialisedPlaceStr;
1329 return hasStillData;
1334 const Place& iPlace) {
1339 ioSociSession.begin();
1343 const std::string lPK (lLocationKey.
toString());
1346 const std::string lIataCode (iPlace.
getIataCode());
1347 const std::string lIcaoCode (iPlace.
getIcaoCode());
1348 const std::string lFaaCode (iPlace.
getFaaCode());
1349 const std::string lIsGeonames ((iPlace.
isGeonames())?
"Y":
"N");
1350 const std::string lGeonameID =
1352 const std::string lEnvID =
1354 const std::string lDateFrom =
1355 boost::gregorian::to_iso_extended_string (iPlace.
getDateFrom());
1356 const std::string lDateEnd =
1357 boost::gregorian::to_iso_extended_string (iPlace.
getDateEnd());
1385 std::string lUNLOCodeStr (
"");
1386 if (lUNLOCodeList.empty() ==
false) {
1387 const UNLOCode_T& lUNLOCode = lUNLOCodeList.front();
1388 lUNLOCodeStr =
static_cast<const std::string
> (lUNLOCode);
1397 if (lUICCodeList.empty() ==
false) {
1398 const UICCode_T& lUICCode = lUICCodeList.front();
1399 lUICCodeInt =
static_cast<const UICCode_T> (lUICCode);
1417 ioSociSession <<
"insert into optd_por values (:pk, "
1418 <<
":location_type, :iata_code, :icao_code, :faa_code, "
1419 <<
":unlocode_code, :uic_code, "
1420 <<
":is_geonames, :geoname_id, "
1421 <<
":envelope_id, :date_from, :date_until, "
1422 <<
":serialised_place)",
1423 soci::use (lPK), soci::use (lLocationType), soci::use (lIataCode),
1424 soci::use (lIcaoCode), soci::use (lFaaCode),
1425 soci::use (lUNLOCodeStr), soci::use (lUICCodeInt),
1426 soci::use (lIsGeonames), soci::use (lGeonameID),
1427 soci::use (lEnvID), soci::use (lDateFrom), soci::use (lDateEnd),
1428 soci::use (lRawDataString);
1431 ioSociSession.commit();
1436 }
catch (std::exception
const& lException) {
1437 std::ostringstream errorStr;
1438 errorStr <<
"Error when updating " << iPlace.
toString() <<
": "
1439 << lException.what();
1447 const Place& iPlace) {
1452 ioSociSession.begin();
1456 std::string lIataCode;
1457 soci::statement lUpdateStatement =
1458 (ioSociSession.prepare
1459 <<
"update place_details "
1460 <<
"set xapian_docid = :xapian_docid "
1461 <<
"where iata_code = :iata_code",
1462 soci::use (lDocID), soci::use (lIataCode));
1467 lUpdateStatement.execute (
true);
1470 ioSociSession.commit();
1475 }
catch (std::exception
const& lException) {
1476 std::ostringstream errorStr;
1477 errorStr <<
"Error when updating " << iPlace.
toString() <<
": "
1478 << lException.what();
1496 ioSociSession <<
"select count(1) from optd_por;", soci::into(oNbOfEntries);
1498 }
catch (std::exception
const& lException) {
1499 std::ostringstream errorStr;
1501 <<
"Error when trying to count the number of rows in the optd_por table: "
1502 << lException.what();
1507 return oNbOfEntries;
1517 soci::statement lSelectStatement (ioSociSession);
1518 std::string lPlace =
1526 bool hasStillData =
true;
1527 while (hasStillData ==
true) {
1531 if (hasStillData ==
true) {
1539 }
catch (std::exception
const& lException) {
1540 std::ostringstream errorStr;
1541 errorStr <<
"Error when trying to retrieve " << oNbOfEntries
1542 <<
"-th row from the SQL database: " << lException.what();
1547 return oNbOfEntries;
1554 const bool iUniqueEntry) {
1563 const std::string& lCode =
static_cast<const std::string&
> (iIataCode);
1564 const std::string lCodeUpper = boost::algorithm::to_upper_copy (lCode);
1567 soci::statement lSelectStatement (ioSociSession);
1568 std::string lPlaceRawDataString;
1569 DBManager::prepareSelectBlobOnIataCodeStatement (ioSociSession,
1572 lPlaceRawDataString);
1578 bool hasStillData =
true;
1579 while (hasStillData ==
true) {
1581 lPlaceRawDataString);
1584 const std::string lFoundStr = hasStillData?
"more; see below":
"no more";
1586 <<
"corresponding to '" << iIataCode
1587 <<
"' IATA code. Result: " << lFoundStr);
1589 if (hasStillData ==
true) {
1600 lLocationList.push_back (lLocation);
1607 }
catch (std::exception
const& lException) {
1608 std::ostringstream errorStr;
1609 errorStr <<
"Error when trying to retrieve a POR for " << iIataCode
1610 <<
" from the SQL database: " << lException.what();
1617 const Location* lHighestPRLocation_ptr = NULL;
1619 for (LocationList_T::const_iterator itLoc = lLocationList.begin();
1620 itLoc != lLocationList.end(); ++itLoc) {
1621 const Location& lLocation = *itLoc;
1631 if (lPRValue >= lHighestPRValue) {
1632 lHighestPRLocation_ptr = &lLocation;
1633 lHighestPRValue = lPRValue;
1638 if (iUniqueEntry ==
false) {
1639 ioLocationList.push_back (lLocation);
1644 if (iUniqueEntry ==
true && lHighestPRLocation_ptr != NULL) {
1645 assert (lHighestPRLocation_ptr != NULL);
1646 ioLocationList.push_back (*lHighestPRLocation_ptr);
1650 << lHighestPRValue <<
") for '" << iIataCode
1651 <<
"' IATA code: " << lHighestPRLocation_ptr->
getKey());
1656 if (oNbOfEntries > 0 && iUniqueEntry ==
true) {
1661 return oNbOfEntries;
1675 const std::string& lCode =
static_cast<const std::string&
> (iIcaoCode);
1676 const std::string lCodeUpper = boost::algorithm::to_upper_copy (lCode);
1679 soci::statement lSelectStatement (ioSociSession);
1680 std::string lPlaceRawDataString;
1681 DBManager::prepareSelectBlobOnIcaoCodeStatement (ioSociSession,
1684 lPlaceRawDataString);
1690 bool hasStillData =
true;
1691 while (hasStillData ==
true) {
1693 lPlaceRawDataString);
1696 const std::string lFoundStr = hasStillData?
"Yes":
"No";
1698 << iIcaoCode <<
" ICAO code. Found: " << lFoundStr);
1700 if (hasStillData ==
true) {
1711 ioLocationList.push_back (lLocation);
1718 }
catch (std::exception
const& lException) {
1719 std::ostringstream errorStr;
1720 errorStr <<
"Error when trying to retrieve a POR for " << iIcaoCode
1721 <<
" from the SQL database: " << lException.what();
1727 return oNbOfEntries;
1741 const std::string& lCode =
static_cast<const std::string&
> (iFaaCode);
1742 const std::string lCodeUpper = boost::algorithm::to_upper_copy (lCode);
1745 soci::statement lSelectStatement (ioSociSession);
1746 std::string lPlaceRawDataString;
1747 DBManager::prepareSelectBlobOnFaaCodeStatement (ioSociSession,
1750 lPlaceRawDataString);
1756 bool hasStillData =
true;
1757 while (hasStillData ==
true) {
1759 lPlaceRawDataString);
1762 const std::string lFoundStr = hasStillData?
"Yes":
"No";
1764 << iFaaCode <<
" FAA code. Found: " << lFoundStr);
1766 if (hasStillData ==
true) {
1777 ioLocationList.push_back (lLocation);
1784 }
catch (std::exception
const& lException) {
1785 std::ostringstream errorStr;
1786 errorStr <<
"Error when trying to retrieve a POR for " << iFaaCode
1787 <<
" from the SQL database: " << lException.what();
1793 return oNbOfEntries;
1800 const bool iUniqueEntry) {
1809 const std::string& lCode =
static_cast<const std::string&
> (iUNLOCode);
1810 const std::string lCodeUpper = boost::algorithm::to_upper_copy (lCode);
1813 soci::statement lSelectStatement (ioSociSession);
1814 std::string lPlaceRawDataString;
1815 DBManager::prepareSelectBlobOnUNLOCodeStatement (ioSociSession,
1818 lPlaceRawDataString);
1824 bool hasStillData =
true;
1825 while (hasStillData ==
true) {
1827 lPlaceRawDataString);
1830 const std::string lFoundStr = hasStillData?
"Yes":
"No";
1832 << iUNLOCode <<
" UN/LOCODE code. Found: "
1835 if (hasStillData ==
true) {
1846 lLocationList.push_back (lLocation);
1853 }
catch (std::exception
const& lException) {
1854 std::ostringstream errorStr;
1855 errorStr <<
"Error when trying to retrieve a POR for " << iUNLOCode
1856 <<
" from the SQL database: " << lException.what();
1863 const Location* lHighestPRLocation_ptr = NULL;
1865 for (LocationList_T::const_iterator itLoc = lLocationList.begin();
1866 itLoc != lLocationList.end(); ++itLoc) {
1867 const Location& lLocation = *itLoc;
1871 if (lPRValue > lHighestPRValue) {
1872 lHighestPRLocation_ptr = &lLocation;
1873 lHighestPRValue = lPRValue;
1877 if (iUniqueEntry ==
false) {
1878 ioLocationList.push_back (lLocation);
1883 if (iUniqueEntry ==
true && lHighestPRLocation_ptr != NULL) {
1884 assert (lHighestPRLocation_ptr != NULL);
1885 ioLocationList.push_back (*lHighestPRLocation_ptr);
1889 << lHighestPRValue <<
") for '" << iUNLOCode
1890 <<
"' IATA code: " << lHighestPRLocation_ptr->
getKey());
1895 if (oNbOfEntries > 0 && iUniqueEntry ==
true) {
1900 return oNbOfEntries;
1912 soci::statement lSelectStatement (ioSociSession);
1913 std::string lPlaceRawDataString;
1914 DBManager::prepareSelectBlobOnUICCodeStatement (ioSociSession,
1917 lPlaceRawDataString);
1923 bool hasStillData =
true;
1924 while (hasStillData ==
true) {
1926 lPlaceRawDataString);
1929 const std::string lFoundStr = hasStillData?
"Yes":
"No";
1931 << iUICCode <<
" UIC code. Found: "
1934 if (hasStillData ==
true) {
1942 const std::string lUICCodeStr =
1943 boost::lexical_cast<std::string> (iUICCode);
1947 ioLocationList.push_back (lLocation);
1954 }
catch (std::exception
const& lException) {
1955 std::ostringstream errorStr;
1956 errorStr <<
"Error when trying to retrieve a POR for " << iUICCode
1957 <<
" from the SQL database: " << lException.what();
1963 return oNbOfEntries;
1975 soci::statement lSelectStatement (ioSociSession);
1976 std::string lPlaceRawDataString;
1977 DBManager::prepareSelectBlobOnPlaceGeoIDStatement (ioSociSession,
1980 lPlaceRawDataString);
1986 bool hasStillData =
true;
1987 while (hasStillData ==
true) {
1989 lPlaceRawDataString);
1992 const std::string lFoundStr = hasStillData?
"Yes":
"No";
1994 << iGeonameID<<
" Geonames ID. Found: "<< lFoundStr);
1996 if (hasStillData ==
true) {
2004 const std::string lGeonamesIDStr =
2005 boost::lexical_cast<std::string> (iGeonameID);
2009 ioLocationList.push_back (lLocation);
2016 }
catch (std::exception
const& lException) {
2017 std::ostringstream errorStr;
2018 errorStr <<
"Error when trying to retrieve a POR for " << iGeonameID
2019 <<
" from the SQL database: " << lException.what();
2025 return oNbOfEntries;
#define OPENTREP_LOG_ERROR(iToBeLogged)
#define OPENTREP_LOG_DEBUG(iToBeLogged)
#define OPENTREP_LOG_WARNING(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)