31#include <wx/filename.h>
42static void ReportError(
const std::string zmsg);
44static bool executeSQL(sqlite3* db,
const char* sql) {
45 char* errMsg =
nullptr;
46 if (sqlite3_exec(db, sql,
nullptr,
nullptr, &errMsg) != SQLITE_OK) {
48 wxString::Format(_(
"navobj database error.") +
" %s", errMsg);
50 auto& noteman = NotificationManager::GetInstance();
51 noteman.AddNotification(NotificationSeverity::kWarning, msg.ToStdString());
58static bool executeSQL(sqlite3* db, wxString& sql) {
59 return executeSQL(db, sql.ToStdString().c_str());
62bool CreateTables(sqlite3* db) {
64 const char* create_tables_sql = R
"(
65 CREATE TABLE IF NOT EXISTS tracks (
66 guid TEXT PRIMARY KEY NOT NULL,
75 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
78 CREATE TABLE IF NOT EXISTS trk_points (
79 track_guid TEXT NOT NULL,
80 latitude REAL NOT NULL,
81 longitude REAL NOT NULL,
82 timestamp TEXT NOT NULL,
84 FOREIGN KEY (track_guid) REFERENCES tracks(guid) ON DELETE CASCADE
88 CREATE TABLE IF NOT EXISTS track_html_links (
89 guid TEXT PRIMARY KEY,
90 track_guid TEXT NOT NULL,
92 html_description TEXT,
94 FOREIGN KEY (track_guid) REFERENCES tracks(guid) ON DELETE CASCADE
98 CREATE TABLE IF NOT EXISTS routes (
99 guid TEXT PRIMARY KEY NOT NULL,
104 planned_departure TEXT,
111 shared_wp_viz INTEGER,
112 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
116 CREATE TABLE IF NOT EXISTS routepoints (
117 guid TEXT PRIMARY KEY NOT NULL,
129 RangeRingsNumber INTEGER,
131 RangeRingsStepUnits INTEGER,
132 RangeRingsVisible INTEGER,
133 RangeRingsColour TEXT,
141 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
144 CREATE TABLE IF NOT EXISTS routepoints_link (
148 PRIMARY KEY (route_guid, point_order),
149 FOREIGN KEY (route_guid) REFERENCES routes(guid) ON DELETE CASCADE
152 CREATE TABLE IF NOT EXISTS route_html_links (
153 guid TEXT PRIMARY KEY,
154 route_guid TEXT NOT NULL,
156 html_description TEXT,
158 FOREIGN KEY (route_guid) REFERENCES routes(guid) ON DELETE CASCADE
161 CREATE TABLE IF NOT EXISTS routepoint_html_links (
162 guid TEXT PRIMARY KEY,
163 routepoint_guid TEXT NOT NULL,
165 html_description TEXT,
167 FOREIGN KEY (routepoint_guid) REFERENCES routepoints(guid) ON DELETE CASCADE
170 CREATE INDEX IF NOT EXISTS idx_track_points
171 ON trk_points (track_guid);
175 if (!executeSQL(db, create_tables_sql))
return false;
180bool TrackExists(sqlite3* db,
const std::string& track_guid) {
181 const char* sql =
"SELECT 1 FROM tracks WHERE guid = ? LIMIT 1";
185 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
186 sqlite3_bind_text(stmt, 1, track_guid.c_str(), -1, SQLITE_TRANSIENT);
188 if (sqlite3_step(stmt) == SQLITE_ROW) {
192 sqlite3_finalize(stmt);
194 ReportError(
"TrackExists:prepare");
200bool TrackHtmlLinkExists(sqlite3* db,
const std::string& link_guid) {
201 const char* sql =
"SELECT 1 FROM track_html_links WHERE guid = ? LIMIT 1";
205 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
206 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
208 if (sqlite3_step(stmt) == SQLITE_ROW) {
212 sqlite3_finalize(stmt);
214 ReportError(
"TrackHtmlLinkExists:prepare");
220bool DeleteAllCommentsForTrack(sqlite3* db,
const std::string& track_guid) {
221 const char* sql =
"DELETE FROM track_html_links WHERE track_guid = ?";
224 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
225 sqlite3_bind_text(stmt, 1, track_guid.c_str(), -1, SQLITE_TRANSIENT);
226 if (sqlite3_step(stmt) != SQLITE_DONE) {
227 ReportError(
"DeleteAllCommentsForTrack:step");
231 sqlite3_finalize(stmt);
233 ReportError(
"DeleteAllCommentsForTrack:prepare");
239bool InsertTrackPoint(sqlite3* db,
const std::string& track_guid,
double lat,
240 double lon,
const std::string& timestamp,
int i_point) {
241 const char* sql = R
"(
242 INSERT INTO trk_points (track_guid, latitude, longitude, timestamp, point_order)
243 VALUES (?, ?, ?, ?, ?)
247 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
248 sqlite3_bind_text(stmt, 1, track_guid.c_str(), -1, SQLITE_TRANSIENT);
249 sqlite3_bind_double(stmt, 2, lat);
250 sqlite3_bind_double(stmt, 3, lon);
251 sqlite3_bind_text(stmt, 4, timestamp.c_str(), -1, SQLITE_TRANSIENT);
252 sqlite3_bind_int(stmt, 5, i_point);
253 if (sqlite3_step(stmt) != SQLITE_DONE) {
254 ReportError(
"InsertTrackPoint:step");
255 sqlite3_finalize(stmt);
258 sqlite3_finalize(stmt);
265bool InsertTrackHTML(sqlite3* db,
const std::string& track_guid,
266 const std::string& link_guid,
const std::string& descrText,
267 const std::string& link,
const std::string& ltype) {
268 const char* sql = R
"(
269 INSERT INTO track_html_links (guid, track_guid, html_link, html_description, html_type)
270 VALUES (?, ?, ?, ?, ?)
274 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
275 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
276 sqlite3_bind_text(stmt, 2, track_guid.c_str(), -1, SQLITE_TRANSIENT);
277 sqlite3_bind_text(stmt, 3, link.c_str(), -1, SQLITE_TRANSIENT);
278 sqlite3_bind_text(stmt, 4, descrText.c_str(), -1, SQLITE_TRANSIENT);
279 sqlite3_bind_text(stmt, 5, ltype.c_str(), -1, SQLITE_TRANSIENT);
280 if (sqlite3_step(stmt) != SQLITE_DONE) {
281 ReportError(
"InsertTrackHTML:step");
282 sqlite3_finalize(stmt);
285 sqlite3_finalize(stmt);
294bool DeleteAllCommentsForRoute(sqlite3* db,
const std::string& route_guid) {
295 const char* sql = R
"(
296 DELETE FROM route_html_links WHERE route_guid = ?
299 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
300 sqlite3_bind_text(stmt, 1, route_guid.c_str(), -1, SQLITE_TRANSIENT);
301 if (sqlite3_step(stmt) != SQLITE_DONE) {
302 ReportError(
"DeleteAllCommentsForRoute:step");
303 sqlite3_finalize(stmt);
306 sqlite3_finalize(stmt);
313bool RouteHtmlLinkExists(sqlite3* db,
const std::string& link_guid) {
314 const char* sql =
"SELECT 1 FROM route_html_links WHERE guid = ? LIMIT 1";
318 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
319 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
321 if (sqlite3_step(stmt) == SQLITE_ROW) {
325 sqlite3_finalize(stmt);
332bool RoutePointHtmlLinkExists(sqlite3* db,
const std::string& link_guid) {
334 "SELECT 1 FROM routepoint_html_links WHERE guid = ? LIMIT 1";
338 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
339 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
341 if (sqlite3_step(stmt) == SQLITE_ROW) {
345 sqlite3_finalize(stmt);
352bool RouteExistsDB(sqlite3* db,
const std::string& route_guid) {
353 const char* sql =
"SELECT 1 FROM routes WHERE guid = ? LIMIT 1";
357 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
358 sqlite3_bind_text(stmt, 1, route_guid.c_str(), -1, SQLITE_TRANSIENT);
360 if (sqlite3_step(stmt) == SQLITE_ROW) {
364 sqlite3_finalize(stmt);
371bool InsertRouteHTML(sqlite3* db,
const std::string& route_guid,
372 const std::string& link_guid,
const std::string& descrText,
373 const std::string& link,
const std::string& ltype) {
374 const char* sql = R
"(
375 INSERT INTO route_html_links (guid, route_guid, html_link, html_description, html_type)
376 VALUES (?, ?, ?, ?, ?)
380 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
381 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
382 sqlite3_bind_text(stmt, 2, route_guid.c_str(), -1, SQLITE_TRANSIENT);
383 sqlite3_bind_text(stmt, 3, link.c_str(), -1, SQLITE_TRANSIENT);
384 sqlite3_bind_text(stmt, 4, descrText.c_str(), -1, SQLITE_TRANSIENT);
385 sqlite3_bind_text(stmt, 5, ltype.c_str(), -1, SQLITE_TRANSIENT);
386 if (sqlite3_step(stmt) != SQLITE_DONE) {
387 ReportError(
"InsertRouteHTML:step");
388 sqlite3_finalize(stmt);
391 sqlite3_finalize(stmt);
398bool RoutePointExists(sqlite3* db,
const std::string& routepoint_guid) {
399 const char* sql =
"SELECT 1 FROM routepoints WHERE guid = ? LIMIT 1";
403 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
404 sqlite3_bind_text(stmt, 1, routepoint_guid.c_str(), -1, SQLITE_TRANSIENT);
406 if (sqlite3_step(stmt) == SQLITE_ROW) {
410 sqlite3_finalize(stmt);
417bool InsertRoutePointHTML(sqlite3* db,
const std::string& point_guid,
418 const std::string& link_guid,
419 const std::string& descrText,
const std::string& link,
420 const std::string& ltype) {
421 const char* sql = R
"(
422 INSERT INTO routepoint_html_links (guid, routepoint_guid, html_link, html_description, html_type)
423 VALUES (?, ?, ?, ?, ?)
427 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
428 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
429 sqlite3_bind_text(stmt, 2, point_guid.c_str(), -1, SQLITE_TRANSIENT);
430 sqlite3_bind_text(stmt, 3, link.c_str(), -1, SQLITE_TRANSIENT);
431 sqlite3_bind_text(stmt, 4, descrText.c_str(), -1, SQLITE_TRANSIENT);
432 sqlite3_bind_text(stmt, 5, ltype.c_str(), -1, SQLITE_TRANSIENT);
433 if (sqlite3_step(stmt) != SQLITE_DONE) {
434 ReportError(
"InsertRoutePointHTML:step");
435 sqlite3_finalize(stmt);
438 sqlite3_finalize(stmt);
444bool DeleteAllCommentsForRoutePoint(sqlite3* db,
445 const std::string& routepoint_guid) {
447 "DELETE FROM routepoint_html_links WHERE routepoint_guid = ?";
450 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
451 sqlite3_bind_text(stmt, 1, routepoint_guid.c_str(), -1, SQLITE_TRANSIENT);
452 if (sqlite3_step(stmt) != SQLITE_DONE) {
453 ReportError(
"DeleteAllCommentsForRoutepoint:step");
457 sqlite3_finalize(stmt);
459 ReportError(
"DeleteAllCommentsForRoutepoint:prepare");
465bool InsertRoutePointDB(sqlite3* db,
RoutePoint* point) {
466 const char* sql = R
"(
467 INSERT or REPLACE INTO routepoints(guid)
472 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
473 sqlite3_bind_text(stmt, 1, point->
m_GUID.ToStdString().c_str(), -1,
475 if (sqlite3_step(stmt) != SQLITE_DONE) {
476 ReportError(
"InsertRoutePointDB:step");
477 sqlite3_finalize(stmt);
480 sqlite3_finalize(stmt);
489 const char* sql = R
"(
490 INSERT or IGNORE INTO routepoints_link (route_guid, point_guid, point_order)
496 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
497 sqlite3_bind_text(stmt, 1, route->
m_GUID.ToStdString().c_str(), -1,
499 sqlite3_bind_text(stmt, 2, point->
m_GUID.ToStdString().c_str(), -1,
501 sqlite3_bind_int(stmt, 3, point_order);
502 if (sqlite3_step(stmt) != SQLITE_DONE) {
503 ReportError(
"InsertTrackPointLink:step");
504 sqlite3_finalize(stmt);
507 sqlite3_finalize(stmt);
514void DeleteOrphanedRoutepoint(sqlite3* db) {
515 const char* sql = R
"(
516 DELETE FROM routepoints
517 WHERE guid NOT IN (SELECT point_guid FROM routepoints_link)
519 char* errMsg =
nullptr;
521 if (sqlite3_exec(db, sql,
nullptr,
nullptr, &errMsg) != SQLITE_OK) {
526void errorLogCallback(
void* pArg,
int iErrCode,
const char* zMsg) {
528 wxString::Format(_(
"navobj database error.") +
" %d: %s", iErrCode, zMsg);
530 auto& noteman = NotificationManager::GetInstance();
531 noteman.AddNotification(NotificationSeverity::kWarning, msg.ToStdString());
534static void ReportError(
const std::string zmsg) {
536 wxString::Format(_(
"navobj database error.") +
" %s", zmsg.c_str());
538 auto& noteman = NotificationManager::GetInstance();
539 noteman.AddNotification(NotificationSeverity::kWarning, msg.ToStdString());
547NavObj_dB::NavObj_dB() {
548 m_pImportProgress =
nullptr;
551 int ie = sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback,
nullptr);
555 wxFileName::GetPathSeparator() +
"navobj.db";
556 if (!wxFileExists(db_filename)) {
559 wxFileName::GetPathSeparator() +
"navobj.xml";
560 if (wxFileExists(xml_filename)) {
561 wxCopyFile(xml_filename, xml_filename +
".backup");
565 wxFileName::GetPathSeparator() +
566 "navobj.xml.import_backup";
567 if (!wxFileExists(deep_backup_filename)) {
568 wxCopyFile(xml_filename, deep_backup_filename);
574 int create_result = sqlite3_open_v2(
575 db_filename.ToStdString().c_str(),
577 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
580 if (create_result != SQLITE_OK) {
581 wxLogMessage(
"Cannot create new navobj.db database file");
590 int close_result = sqlite3_close_v2(m_db);
591 if (close_result != SQLITE_OK) {
597 int m_open_result = sqlite3_open_v2(db_filename.ToStdString().c_str(), &m_db,
598 SQLITE_OPEN_READWRITE, NULL);
599 sqlite3_exec(m_db,
"PRAGMA foreign_keys = ON;",
nullptr,
nullptr,
nullptr);
603 sqlite3_close_v2(m_db);
605 m_open_result = sqlite3_open_v2(db_filename.ToStdString().c_str(), &m_db,
606 SQLITE_OPEN_READWRITE, NULL);
607 sqlite3_exec(m_db,
"PRAGMA foreign_keys = ON;",
nullptr,
nullptr,
nullptr);
613NavObj_dB::~NavObj_dB() { sqlite3_close_v2(m_db); }
615void NavObj_dB::Close() {
616 sqlite3_close_v2(m_db);
620bool NavObj_dB::FullSchemaMigrate(wxFrame* frame) {
622 if (needsMigration_0_1(m_db)) {
623 std::string rs = SchemaUpdate_0_1(m_db, frame);
625 wxLogMessage(
"Error on: Schema update and migration 0->1");
626 wxLogMessage(wxString(rs.c_str()));
629 wxLogMessage(
"Schema update and migration 0->1 successful");
635 }
catch (
const std::runtime_error& e) {
637 wxLogMessage(
"Error on: Schema update and migration 0->1, setUserVersion");
638 wxLogMessage(wxString(std::string(e.what())).c_str());
647bool NavObj_dB::ImportLegacyNavobj(wxFrame* frame) {
649 wxFileName::GetPathSeparator() +
"navobj.xml";
651 if (::wxFileExists(navobj_filename)) {
653 CountImportNavObjects();
654 m_pImportProgress =
new wxProgressDialog(_(
"Importing Navobj database"),
"",
655 m_nImportObjects, frame);
656 m_import_progesscount = 0;
658 rv = ImportLegacyPoints();
659 rv |= ImportLegacyRoutes();
660 rv |= ImportLegacyTracks();
662 m_pImportProgress->Destroy();
666 if (::wxFileExists(navobj_filename)) ::wxRemoveFile(navobj_filename);
671void NavObj_dB::CountImportNavObjects() {
672 m_nImportObjects = 0;
679 wxFileName::GetPathSeparator() +
"navobj.xml";
681 if (::wxFileExists(navobj_filename) &&
682 input_set->load_file(navobj_filename.ToStdString().c_str()).status ==
683 pugi::xml_parse_status::status_ok) {
684 input_set->LoadAllGPXPointObjects();
685 auto pointlist = pWayPointMan->GetWaypointList();
693 input_set->LoadAllGPXRouteObjects();
697 m_nImportObjects += route_import->GetnPoints();
700 input_set->LoadAllGPXTrackObjects();
705 m_nImportObjects += track_import->GetnPoints();
711bool NavObj_dB::ImportLegacyTracks() {
712 std::vector<Track*> tracks_added;
716 if (InsertTrack(track_import)) {
717 tracks_added.push_back(track_import);
720 m_import_progesscount += track_import->GetnPoints() + 1;
721 wxString msg = wxString::Format(
"Tracks %d/%d", ntrack, m_nimportTracks);
722 m_pImportProgress->Update(m_import_progesscount, msg);
723 m_pImportProgress->Show();
727 for (
Track* ptrack : tracks_added) {
728 if (ptrack->m_bIsInLayer)
continue;
735bool NavObj_dB::ImportLegacyRoutes() {
736 std::vector<Route*> routes_added;
740 if (InsertRoute(route_import)) {
741 routes_added.push_back(route_import);
744 m_import_progesscount += route_import->GetnPoints() + 1;
745 wxString msg = wxString::Format(
"Routes %d/%d", nroute, m_nimportRoutes);
746 m_pImportProgress->Update(m_import_progesscount, msg);
747 m_pImportProgress->Show();
751 for (
Route* route : routes_added) {
757 pWayPointMan->DeleteAllWaypoints(
true);
762bool NavObj_dB::ImportLegacyPoints() {
763 std::vector<RoutePoint*> points_added;
767 if (m_nimportPoints > 1000) nmod = 10;
768 if (m_nimportPoints > 10000) nmod = 100;
770 for (
RoutePoint* point : *pWayPointMan->GetWaypointList()) {
772 if (InsertRoutePointDB(m_db, point)) {
773 points_added.push_back(point);
776 UpdateDBRoutePointAttributes(point);
777 m_import_progesscount += 1;
778 if ((npoint % nmod) == 0) {
780 wxString::Format(
"Points %d/%d", npoint, m_nimportPoints);
781 m_pImportProgress->Update(m_import_progesscount, msg);
782 m_pImportProgress->Show();
797void NavObj_dB::LoadNavObjects() {
803bool NavObj_dB::InsertTrack(
Track* track) {
804 if (TrackExists(m_db, track->m_GUID.ToStdString()))
return false;
808 sqlite3_exec(m_db,
"BEGIN TRANSACTION", 0, 0, &errMsg);
810 ReportError(
"InsertTrack:BEGIN TRANSACTION");
815 wxString sql = wxString::Format(
"INSERT INTO tracks (guid) VALUES ('%s')",
816 track->m_GUID.ToStdString().c_str());
817 if (!executeSQL(m_db, sql)) {
818 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
822 UpdateDBTrackAttributes(track);
825 for (
int i = 0; i < track->GetnPoints(); i++) {
826 auto point = track->GetPoint(i);
828 InsertTrackPoint(m_db, track->m_GUID.ToStdString(), point->m_lat,
829 point->m_lon, point->GetTimeString(), i);
833 int NbrOfLinks = track->m_TrackHyperlinkList->size();
834 if (NbrOfLinks > 0) {
835 auto& list = track->m_TrackHyperlinkList;
836 for (
auto it = list->begin(); it != list->end(); ++it) {
838 if (!TrackHtmlLinkExists(m_db, link->GUID)) {
839 InsertTrackHTML(m_db, track->m_GUID.ToStdString(), link->GUID,
840 link->DescrText.ToStdString(), link->Link.ToStdString(),
841 link->LType.ToStdString());
845 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
847 if (errMsg) rv =
false;
852bool NavObj_dB::UpdateTrack(
Track* track) {
856 if (!TrackExists(m_db, track->m_GUID.ToStdString()))
return false;
858 sqlite3_exec(m_db,
"BEGIN TRANSACTION", 0, 0, &errMsg);
860 ReportError(
"UpdateTrack:BEGIN TRANSACTION");
864 UpdateDBTrackAttributes(track);
867 const char* sql =
"DELETE FROM trk_points WHERE track_guid = ?";
869 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
870 sqlite3_bind_text(stmt, 1, track->m_GUID.ToStdString().c_str(), -1,
873 ReportError(
"UpdateTrack:prepare");
874 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
877 if (sqlite3_step(stmt) != SQLITE_DONE) {
878 ReportError(
"UpdateTrack:step");
879 sqlite3_finalize(stmt);
880 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
883 sqlite3_finalize(stmt);
886 for (
int i = 0; i < track->GetnPoints(); i++) {
887 auto point = track->GetPoint(i);
890 InsertTrackPoint(m_db, track->m_GUID.ToStdString(), point->m_lat,
891 point->m_lon, point->GetTimeString(), i);
895 sqlite3_exec(m_db,
"COMMIT", 0, 0,
nullptr);
898 if (errMsg) rv =
false;
902bool NavObj_dB::UpdateDBTrackAttributes(
Track* track) {
916 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
917 sqlite3_bind_text(stmt, 1, track->GetName().ToStdString().c_str(), -1,
919 sqlite3_bind_text(stmt, 2, track->m_TrackDescription.ToStdString().c_str(),
920 -1, SQLITE_TRANSIENT);
921 sqlite3_bind_int(stmt, 3, track->m_bVisible);
922 sqlite3_bind_text(stmt, 4, track->m_TrackStartString.ToStdString().c_str(),
923 -1, SQLITE_TRANSIENT);
924 sqlite3_bind_text(stmt, 5, track->m_TrackEndString.ToStdString().c_str(),
925 -1, SQLITE_TRANSIENT);
926 sqlite3_bind_int(stmt, 6, track->m_width);
927 sqlite3_bind_int(stmt, 7,
928 (
int)(track->m_style));
929 sqlite3_bind_text(stmt, 8, track->m_Colour.ToStdString().c_str(), -1,
931 sqlite3_bind_text(stmt, 9, track->m_GUID.c_str(), track->m_GUID.size(),
937 if (sqlite3_step(stmt) != SQLITE_DONE) {
938 ReportError(
"UpdateDBTrackAttributesA:step");
939 sqlite3_finalize(stmt);
943 sqlite3_finalize(stmt);
948 DeleteAllCommentsForTrack(m_db, track->m_GUID.ToStdString());
951 int NbrOfLinks = track->m_TrackHyperlinkList->size();
952 if (NbrOfLinks > 0) {
953 auto& list = track->m_TrackHyperlinkList;
954 for (
auto it = list->begin(); it != list->end(); ++it) {
957 if (!TrackHtmlLinkExists(m_db, link->GUID)) {
958 InsertTrackHTML(m_db, track->m_GUID.ToStdString(), link->GUID,
959 link->DescrText.ToStdString(), link->Link.ToStdString(),
960 link->LType.ToStdString());
963 "UPDATE track_html_links SET "
965 "html_description = ?, "
969 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
970 sqlite3_bind_text(stmt, 3, link->Link.ToStdString().c_str(), -1,
972 sqlite3_bind_text(stmt, 4, link->DescrText.ToStdString().c_str(), -1,
974 sqlite3_bind_text(stmt, 5, link->LType.ToStdString().c_str(), -1,
977 if (sqlite3_step(stmt) != SQLITE_DONE) {
978 ReportError(
"UpdateDBTRackAttributesB:step");
979 sqlite3_finalize(stmt);
982 sqlite3_finalize(stmt);
992 if (!TrackExists(m_db, track->m_GUID.ToStdString()))
return false;
995 int this_point_index = track->GetnPoints();
998 if (!InsertTrackPoint(m_db, track->m_GUID.ToStdString(), point->m_lat,
999 point->m_lon, point->GetTimeString(),
1000 this_point_index - 1))
1006bool NavObj_dB::LoadAllTracks() {
1007 const char* sql = R
"(
1009 description, visibility, start_string, end_string,
1010 width, style, color,
1013 ORDER BY created_at ASC
1017 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) != SQLITE_OK) {
1021 while (sqlite3_step(stmt) == SQLITE_ROW) {
1023 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 0));
1025 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 1));
1026 std::string description =
1027 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 2));
1028 int visibility = sqlite3_column_int(stmt, 3);
1029 std::string start_string =
1030 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 4));
1031 std::string end_string =
1032 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 5));
1033 int width = sqlite3_column_int(stmt, 6);
1034 int style = sqlite3_column_int(stmt, 7);
1036 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 8));
1037 std::string created =
1038 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 9));
1040 Track* new_trk = NULL;
1043 const char* sql = R
"(
1044 SELECT latitude, longitude, timestamp, point_order
1046 WHERE track_guid = ?
1047 ORDER BY point_order ASC
1050 sqlite3_stmt* stmtp;
1051 if (sqlite3_prepare_v2(m_db, sql, -1, &stmtp,
nullptr) != SQLITE_OK) {
1055 sqlite3_bind_text(stmtp, 1, guid.c_str(), -1, SQLITE_TRANSIENT);
1058 while (sqlite3_step(stmtp) == SQLITE_ROW) {
1060 new_trk =
new Track;
1061 new_trk->m_GUID = guid;
1064 new_trk->SetVisible(visibility == 1);
1065 new_trk->SetName(name.c_str());
1066 new_trk->m_TrackStartString = start_string.c_str();
1067 new_trk->m_TrackEndString = end_string.c_str();
1068 new_trk->m_width = width;
1069 new_trk->m_style = (wxPenStyle)style;
1070 new_trk->m_Colour = color;
1073 double latitude = sqlite3_column_double(stmtp, 0);
1074 double longitude = sqlite3_column_double(stmtp, 1);
1075 std::string timestamp =
1076 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, 2));
1077 int point_order = sqlite3_column_int(stmtp, 3);
1079 auto point =
new TrackPoint(latitude, longitude, timestamp);
1081 point->m_GPXTrkSegNo = GPXTrkSeg;
1082 new_trk->AddPoint(point);
1084 sqlite3_finalize(stmtp);
1087 new_trk->SetCurrentTrackSeg(GPXTrkSeg);
1090 const char* sqlh = R
"(
1091 SELECT guid, html_link, html_description, html_type
1092 FROM track_html_links
1093 WHERE track_guid = ?
1094 ORDER BY html_type ASC
1099 if (sqlite3_prepare_v2(m_db, sqlh, -1, &stmt,
nullptr) == SQLITE_OK) {
1100 sqlite3_bind_text(stmt, 1, new_trk->m_GUID.ToStdString().c_str(), -1,
1103 while (sqlite3_step(stmt) == SQLITE_ROW) {
1104 std::string link_guid =
1105 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 0));
1106 std::string link_link =
1107 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 1));
1108 std::string link_description =
1109 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 2));
1110 std::string link_type =
1111 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 3));
1114 h->DescrText = link_description;
1115 h->Link = link_link;
1116 h->LType = link_type;
1118 new_trk->m_TrackHyperlinkList->push_back(h);
1122 sqlite3_finalize(stmt);
1131 pSelect->AddAllSelectableTrackSegments(new_trk);
1137bool NavObj_dB::DeleteTrack(
Track* track) {
1138 if (!track)
return false;
1139 std::string track_guid = track->m_GUID.ToStdString();
1140 const char* sql =
"DELETE FROM tracks WHERE guid = ?";
1143 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1144 sqlite3_bind_text(stmt, 1, track_guid.c_str(), -1, SQLITE_TRANSIENT);
1145 if (sqlite3_step(stmt) != SQLITE_DONE) {
1146 ReportError(
"DeleteTrack:step");
1147 sqlite3_finalize(stmt);
1151 sqlite3_finalize(stmt);
1160bool NavObj_dB::InsertRoute(
Route* route) {
1164 if (!RouteExistsDB(m_db, route->
m_GUID.ToStdString())) {
1166 wxString sql = wxString::Format(
"INSERT INTO routes (guid) VALUES ('%s')",
1167 route->
m_GUID.ToStdString().c_str());
1168 if (!executeSQL(m_db, sql)) {
1171 UpdateDBRouteAttributes(route);
1174 sqlite3_exec(m_db,
"BEGIN TRANSACTION", 0, 0, &errMsg);
1176 ReportError(
"InsertRoute:BEGIN TRANSACTION");
1181 for (
int i = 0; i < route->GetnPoints(); i++) {
1182 auto point = route->GetPoint(i + 1);
1185 if (!RoutePointExists(m_db, point->m_GUID.ToStdString())) {
1186 InsertRoutePointDB(m_db, point);
1187 UpdateDBRoutePointAttributes(point);
1193 for (
int i = 0; i < route->GetnPoints(); i++) {
1194 auto point = route->GetPoint(i + 1);
1197 InsertRoutePointLink(m_db, route, point, i + 1);
1203 if (NbrOfLinks > 0) {
1205 for (
auto it = list.begin(); it != list.end(); ++it) {
1207 if (!RouteHtmlLinkExists(m_db, link->GUID)) {
1208 InsertRouteHTML(m_db, route->
m_GUID.ToStdString(), link->GUID,
1209 link->DescrText.ToStdString(), link->Link.ToStdString(),
1210 link->LType.ToStdString());
1215 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
1218 ReportError(
"InsertRoute:commit");
1224bool NavObj_dB::UpdateRoute(
Route* route) {
1228 if (!RouteExistsDB(m_db, route->
m_GUID.ToStdString()))
return false;
1230 sqlite3_exec(m_db,
"BEGIN TRANSACTION", 0, 0, &errMsg);
1232 ReportError(
"UpdateRoute:BEGIN TRANSACTION");
1236 UpdateDBRouteAttributes(route);
1239 for (
int i = 0; i < route->GetnPoints(); i++) {
1240 auto point = route->GetPoint(i + 1);
1243 if (!RoutePointExists(m_db, point->m_GUID.ToStdString())) {
1244 InsertRoutePointDB(m_db, point);
1246 UpdateDBRoutePointAttributes(point);
1251 const char* sql =
"DELETE FROM routepoints_link WHERE route_guid = ?";
1253 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1254 sqlite3_bind_text(stmt, 1, route->
m_GUID.ToStdString().c_str(), -1,
1257 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
1260 if (sqlite3_step(stmt) != SQLITE_DONE) {
1261 ReportError(
"UpdateRoute:step");
1262 sqlite3_finalize(stmt);
1263 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
1267 sqlite3_finalize(stmt);
1269 for (
int i = 0; i < route->GetnPoints(); i++) {
1270 auto point = route->GetPoint(i + 1);
1272 InsertRoutePointLink(m_db, route, point, i + 1);
1278 if (NbrOfLinks > 0) {
1280 for (
auto it = list.begin(); it != list.end(); ++it) {
1282 if (!RouteHtmlLinkExists(m_db, link->GUID)) {
1283 InsertRouteHTML(m_db, route->
m_GUID.ToStdString(), link->GUID,
1284 link->DescrText.ToStdString(), link->Link.ToStdString(),
1285 link->LType.ToStdString());
1289 sqlite3_exec(m_db,
"COMMIT", 0, 0,
nullptr);
1292 if (errMsg) rv =
false;
1297bool NavObj_dB::UpdateRouteViz(
Route* route) {
1300 if (!RouteExistsDB(m_db, route->
m_GUID.ToStdString()))
return false;
1302 UpdateDBRouteAttributes(route);
1304 for (
int i = 0; i < route->GetnPoints(); i++) {
1305 auto point = route->GetPoint(i + 1);
1308 UpdateDBRoutePointViz(point);
1312 if (errMsg) rv =
false;
1317bool NavObj_dB::UpdateDBRouteAttributes(
Route* route) {
1319 "UPDATE routes SET "
1322 "start_string = ?, "
1325 "shared_wp_viz = ?, "
1326 "planned_departure = ?, "
1335 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1336 sqlite3_bind_text(stmt, 1, route->GetName().ToStdString().c_str(), -1,
1339 -1, SQLITE_TRANSIENT);
1341 -1, SQLITE_TRANSIENT);
1343 -1, SQLITE_TRANSIENT);
1344 sqlite3_bind_int(stmt, 5, route->IsVisible());
1345 sqlite3_bind_int(stmt, 6, route->GetSharedWPViz());
1350 -1, SQLITE_TRANSIENT);
1351 sqlite3_bind_int(stmt, 10, route->
m_width);
1352 sqlite3_bind_int(stmt, 11,
1354 sqlite3_bind_text(stmt, 12, route->
m_Colour.ToStdString().c_str(), -1,
1356 sqlite3_bind_text(stmt, 13, route->
m_GUID.c_str(), route->
m_GUID.size(),
1362 if (sqlite3_step(stmt) != SQLITE_DONE) {
1363 ReportError(
"UpdateDBRouteAttributesA:step");
1364 sqlite3_finalize(stmt);
1368 sqlite3_finalize(stmt);
1373 DeleteAllCommentsForRoute(m_db, route->
m_GUID.ToStdString());
1377 if (NbrOfLinks > 0) {
1379 for (
auto it = list->begin(); it != list->end(); ++it) {
1381 if (!RouteHtmlLinkExists(m_db, link->GUID)) {
1382 InsertRouteHTML(m_db, route->
m_GUID.ToStdString(), link->GUID,
1383 link->DescrText.ToStdString(), link->Link.ToStdString(),
1384 link->LType.ToStdString());
1387 "UPDATE route_html_links SET "
1389 "html_description = ?, "
1393 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1394 sqlite3_bind_text(stmt, 3, link->Link.ToStdString().c_str(), -1,
1396 sqlite3_bind_text(stmt, 4, link->DescrText.ToStdString().c_str(), -1,
1398 sqlite3_bind_text(stmt, 5, link->LType.ToStdString().c_str(), -1,
1401 if (sqlite3_step(stmt) != SQLITE_DONE) {
1404 if (sqlite3_step(stmt) != SQLITE_DONE) {
1405 ReportError(
"UpdateDBRouteAttributesB:step");
1406 sqlite3_finalize(stmt);
1409 sqlite3_finalize(stmt);
1416bool NavObj_dB::UpdateDBRoutePointAttributes(
RoutePoint* point) {
1418 "UPDATE routepoints SET "
1429 "ArrivalRadius = ?, "
1430 "RangeRingsNumber = ?, "
1431 "RangeRingsStep = ?, "
1432 "RangeRingsStepUnits = ?, "
1433 "RangeRingsVisible = ?, "
1434 "RangeRingsColour = ?, "
1445 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1446 sqlite3_bind_double(stmt, 1, point->GetLatitude());
1447 sqlite3_bind_double(stmt, 2, point->GetLongitude());
1448 sqlite3_bind_text(stmt, 3, point->GetIconName().ToStdString().c_str(), -1,
1450 sqlite3_bind_text(stmt, 4, point->GetName().ToStdString().c_str(), -1,
1452 sqlite3_bind_text(stmt, 5, point->GetDescription().ToStdString().c_str(),
1453 -1, SQLITE_TRANSIENT);
1454 sqlite3_bind_text(stmt, 6, point->
m_TideStation.ToStdString().c_str(), -1,
1459 sqlite3_bind_int(stmt, 8, etd);
1460 sqlite3_bind_text(stmt, 9,
"type", -1, SQLITE_TRANSIENT);
1461 std::string timit = point->
m_timestring.ToStdString().c_str();
1462 sqlite3_bind_text(stmt, 10, point->
m_timestring.ToStdString().c_str(), -1,
1475 -1, SQLITE_TRANSIENT);
1477 sqlite3_bind_int(stmt, 17, point->GetScaMin());
1478 sqlite3_bind_int(stmt, 18, point->GetScaMax());
1479 sqlite3_bind_int(stmt, 19, point->GetUseSca());
1481 sqlite3_bind_int(stmt, 20, point->IsVisible());
1482 sqlite3_bind_int(stmt, 21, point->IsNameShown());
1483 sqlite3_bind_int(stmt, 22, point->IsShared());
1485 sqlite3_bind_int(stmt, 23, iso);
1487 sqlite3_bind_text(stmt, 24, point->
m_GUID.ToStdString().c_str(), -1,
1494 if (sqlite3_step(stmt) != SQLITE_DONE) {
1495 ReportError(
"UpdateDBRoutePointAttributesA:step");
1496 sqlite3_finalize(stmt);
1500 sqlite3_finalize(stmt);
1505 DeleteAllCommentsForRoutePoint(m_db, point->
m_GUID.ToStdString());
1509 if (NbrOfLinks > 0) {
1511 for (
auto it = list->begin(); it != list->end(); ++it) {
1513 if (!RoutePointHtmlLinkExists(m_db, link->GUID)) {
1514 InsertRoutePointHTML(m_db, point->
m_GUID.ToStdString(), link->GUID,
1515 link->DescrText.ToStdString(),
1516 link->Link.ToStdString(),
1517 link->LType.ToStdString());
1520 "UPDATE routepoint_html_links SET "
1522 "html_description = ?, "
1526 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1527 sqlite3_bind_text(stmt, 3, link->Link.ToStdString().c_str(), -1,
1529 sqlite3_bind_text(stmt, 4, link->DescrText.ToStdString().c_str(), -1,
1531 sqlite3_bind_text(stmt, 5, link->LType.ToStdString().c_str(), -1,
1534 if (sqlite3_step(stmt) != SQLITE_DONE) {
1537 if (sqlite3_step(stmt) != SQLITE_DONE) {
1538 ReportError(
"UpdateDBRoutePointAttributesB:step-h");
1539 sqlite3_finalize(stmt);
1542 sqlite3_finalize(stmt);
1550bool NavObj_dB::UpdateDBRoutePointViz(
RoutePoint* point) {
1552 "UPDATE routepoints SET "
1557 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1558 sqlite3_bind_int(stmt, 1, point->IsVisible());
1559 sqlite3_bind_text(stmt, 2, point->
m_GUID.ToStdString().c_str(), -1,
1566 if (sqlite3_step(stmt) != SQLITE_DONE) {
1567 ReportError(
"UpdateDBRoutePointVizA:step");
1568 sqlite3_finalize(stmt);
1572 sqlite3_finalize(stmt);
1577bool NavObj_dB::DeleteRoute(
Route* route) {
1578 if (m_importing)
return false;
1579 if (!route)
return false;
1580 std::string route_guid = route->
m_GUID.ToStdString();
1581 const char* sql =
"DELETE FROM routes WHERE guid = ?";
1584 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1585 sqlite3_bind_text(stmt, 1, route_guid.c_str(), -1, SQLITE_TRANSIENT);
1586 if (sqlite3_step(stmt) != SQLITE_DONE) {
1587 ReportError(
"DeleteRoute:step");
1588 sqlite3_finalize(stmt);
1591 sqlite3_finalize(stmt);
1598bool NavObj_dB::LoadAllRoutes() {
1608 "planned_departure, "
1615 "ORDER BY created_at ASC";
1617 sqlite3_stmt* stmt_routes;
1618 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt_routes,
nullptr) != SQLITE_OK) {
1622 int errcode0 = SQLITE_OK;
1623 while ((errcode0 = sqlite3_step(stmt_routes)) == SQLITE_ROW) {
1625 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_routes, 0));
1627 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_routes, 1));
1628 std::string description =
1629 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_routes, 2));
1630 std::string start_string =
1631 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_routes, 3));
1632 std::string end_string =
1633 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_routes, 4));
1634 int visibility = sqlite3_column_int(stmt_routes, 5);
1635 int sharewp_viz = sqlite3_column_int(stmt_routes, 6);
1636 time_t planned_departure_ticks = sqlite3_column_int(stmt_routes, 7);
1637 double plan_speed = sqlite3_column_double(stmt_routes, 8);
1638 std::string time_format =
1639 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_routes, 9));
1641 int width = sqlite3_column_int(stmt_routes, 10);
1642 int style = sqlite3_column_int(stmt_routes, 11);
1644 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_routes, 12));
1646 Route* route = NULL;
1649 const char* sql = R
"(
1650 SELECT latitude, longitude, timestamp, point_order
1652 WHERE track_guid = ?
1653 ORDER BY point_order ASC
1669 "p.RangeRingsNumber, "
1670 "p.RangeRingsStep, "
1671 "p.RangeRingsStepUnits, "
1672 "p.RangeRingsVisible, "
1673 "p.RangeRingsColour, "
1682 "FROM routepoints_link tp "
1683 "JOIN routepoints p ON p.guid = tp.point_guid "
1684 "WHERE tp.route_guid = ? "
1685 "ORDER BY tp.point_order ASC";
1687 sqlite3_stmt* stmt_rp;
1688 if (sqlite3_prepare_v2(m_db, sqlp, -1, &stmt_rp,
nullptr) != SQLITE_OK) {
1689 ReportError(
"LoadAllRoutes-B:prepare");
1693 sqlite3_bind_text(stmt_rp, 1, guid.c_str(), -1, SQLITE_TRANSIENT);
1696 int errcode = SQLITE_OK;
1697 while ((errcode = sqlite3_step(stmt_rp)) == SQLITE_ROW) {
1703 route->SetVisible(visibility == 1);
1708 route->SetVisible(visibility == 1);
1709 route->SetSharedWPViz(sharewp_viz == 1);
1715 route->
m_style = (wxPenStyle)style;
1721 std::string point_guid =
1722 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_rp, col++));
1723 double latitude = sqlite3_column_double(stmt_rp, col++);
1724 double longitude = sqlite3_column_double(stmt_rp, col++);
1725 std::string symbol =
1726 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_rp, col++));
1728 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_rp, col++));
1729 std::string description =
1730 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_rp, col++));
1731 std::string tide_station =
1732 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_rp, col++));
1733 double plan_speed = sqlite3_column_double(stmt_rp, col++);
1734 time_t etd_epoch = sqlite3_column_int(stmt_rp, col++);
1736 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_rp, col++));
1738 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_rp, col++));
1739 double arrival_radius = sqlite3_column_double(stmt_rp, col++);
1741 int range_ring_number = sqlite3_column_int(stmt_rp, col++);
1742 double range_ring_step = sqlite3_column_double(stmt_rp, col++);
1743 int range_ring_units = sqlite3_column_int(stmt_rp, col++);
1744 int range_ring_visible = sqlite3_column_int(stmt_rp, col++);
1745 std::string range_ring_color =
1746 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_rp, col++));
1748 int scamin = sqlite3_column_int(stmt_rp, col++);
1749 int scamax = sqlite3_column_int(stmt_rp, col++);
1750 int use_scaminmax = sqlite3_column_int(stmt_rp, col++);
1752 int visibility = sqlite3_column_int(stmt_rp, col++);
1753 int viz_name = sqlite3_column_int(stmt_rp, col++);
1754 int shared = sqlite3_column_int(stmt_rp, col++);
1755 int isolated = sqlite3_column_int(stmt_rp, col++);
1756 std::string point_created_at =
1757 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_rp, col++));
1762 auto containing_route =
1763 g_pRouteMan->FindRouteContainingWaypoint(point_guid);
1768 bool b_closed_route =
false;
1769 if (!containing_route) {
1770 RoutePoint* close_point = route->GetPoint(point_guid);
1771 b_closed_route = close_point !=
nullptr;
1772 existing_point = close_point;
1775 if (containing_route) {
1776 existing_point = containing_route->GetPoint(point_guid);
1779 if (!existing_point) {
1780 existing_point = pWayPointMan->FindRoutePointByGUID(point_guid.c_str());
1783 if (existing_point) {
1784 point = existing_point;
1785 if (!b_closed_route) {
1786 point->SetShared(
true);
1791 new RoutePoint(latitude, longitude, symbol, name, point_guid,
true);
1795 point->SetPlannedSpeed(plan_speed);
1798 etd.Set((time_t)etd_epoch);
1799 if (etd.IsValid()) point->
SetETD(etd);
1806 point->SetShowWaypointRangeRings(range_ring_visible == 1);
1810 point->SetScaMin(scamin);
1811 point->SetScaMax(scamax);
1812 point->SetUseSca(use_scaminmax == 1);
1814 point->SetVisible(visibility == 1);
1815 point->SetNameShown(viz_name == 1);
1816 point->SetShared(shared == 1);
1819 if (point_created_at.size()) {
1823 std::istringstream ss(point_created_at);
1824 ss >> std::get_time(&tm,
"%Y-%m-%d %H:%M:%S");
1825 time_t epoch_time = mktime(&tm);
1830 const char* sqlh = R
"(
1831 SELECT guid, html_link, html_description, html_type
1832 FROM routepoint_html_links
1833 WHERE routepoint_guid = ?
1834 ORDER BY html_type ASC
1837 sqlite3_stmt* stmt_point_link;
1839 if (sqlite3_prepare_v2(m_db, sqlh, -1, &stmt_point_link,
nullptr) ==
1841 sqlite3_bind_text(stmt_point_link, 1,
1842 point->
m_GUID.ToStdString().c_str(), -1,
1845 while (sqlite3_step(stmt_point_link) == SQLITE_ROW) {
1846 std::string link_guid =
reinterpret_cast<const char*
>(
1847 sqlite3_column_text(stmt_point_link, 0));
1848 std::string link_link =
reinterpret_cast<const char*
>(
1849 sqlite3_column_text(stmt_point_link, 1));
1850 std::string link_description =
reinterpret_cast<const char*
>(
1851 sqlite3_column_text(stmt_point_link, 2));
1852 std::string link_type =
reinterpret_cast<const char*
>(
1853 sqlite3_column_text(stmt_point_link, 3));
1856 h->DescrText = link_description;
1857 h->Link = link_link;
1858 h->LType = link_type;
1862 sqlite3_finalize(stmt_point_link);
1866 route->AddPoint(point);
1868 sqlite3_finalize(stmt_rp);
1869 if (errcode != SQLITE_DONE) {
1870 ReportError(
"LoadAllRoutes-A:step");
1877 const char* sqlh = R
"(
1878 SELECT guid, html_link, html_description, html_type
1879 FROM route_html_links
1880 WHERE route_guid = ?
1881 ORDER BY html_type ASC
1884 sqlite3_stmt* stmt_route_links;
1886 if (sqlite3_prepare_v2(m_db, sqlh, -1, &stmt_route_links,
nullptr) ==
1888 sqlite3_bind_text(stmt_route_links, 1,
1889 route->
m_GUID.ToStdString().c_str(), -1,
1892 int errcode2 = SQLITE_OK;
1893 while ((errcode2 = sqlite3_step(stmt_route_links)) == SQLITE_ROW) {
1894 std::string link_guid =
reinterpret_cast<const char*
>(
1895 sqlite3_column_text(stmt_route_links, 0));
1896 std::string link_link =
reinterpret_cast<const char*
>(
1897 sqlite3_column_text(stmt_route_links, 1));
1898 std::string link_description =
reinterpret_cast<const char*
>(
1899 sqlite3_column_text(stmt_route_links, 2));
1900 std::string link_type =
reinterpret_cast<const char*
>(
1901 sqlite3_column_text(stmt_route_links, 3));
1904 h->DescrText = link_description;
1905 h->Link = link_link;
1906 h->LType = link_type;
1910 if (errcode != SQLITE_DONE) {
1911 ReportError(
"LoadAllRoutes-B:step");
1915 sqlite3_finalize(stmt_route_links);
1918 ReportError(
"LoadAllRoutes-B:prepare");
1929 if (errcode0 != SQLITE_DONE) {
1930 ReportError(
"LoadAllRoutes-C:step");
1937bool NavObj_dB::LoadAllPoints() {
1952 "p.RangeRingsNumber, "
1953 "p.RangeRingsStep, "
1954 "p.RangeRingsStepUnits, "
1955 "p.RangeRingsVisible, "
1956 "p.RangeRingsColour, "
1965 "FROM routepoints p ";
1969 sqlite3_stmt* stmt_point;
1970 if (sqlite3_prepare_v2(m_db, sqlp, -1, &stmt_point,
nullptr) != SQLITE_OK) {
1974 while (sqlite3_step(stmt_point) == SQLITE_ROW) {
1977 std::string point_guid =
1978 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_point, col++));
1979 double latitude = sqlite3_column_double(stmt_point, col++);
1980 double longitude = sqlite3_column_double(stmt_point, col++);
1981 std::string symbol =
1982 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_point, col++));
1984 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_point, col++));
1985 std::string description =
1986 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_point, col++));
1987 std::string tide_station =
1988 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_point, col++));
1989 double plan_speed = sqlite3_column_double(stmt_point, col++);
1990 time_t etd = sqlite3_column_int(stmt_point, col++);
1992 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_point, col++));
1993 std::string point_time_string =
1994 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_point, col++));
1995 double arrival_radius = sqlite3_column_double(stmt_point, col++);
1997 int range_ring_number = sqlite3_column_int(stmt_point, col++);
1998 double range_ring_step = sqlite3_column_double(stmt_point, col++);
1999 int range_ring_units = sqlite3_column_int(stmt_point, col++);
2000 int range_ring_visible = sqlite3_column_int(stmt_point, col++);
2001 std::string range_ring_color =
2002 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_point, col++));
2004 int scamin = sqlite3_column_int(stmt_point, col++);
2005 int scamax = sqlite3_column_int(stmt_point, col++);
2006 int use_scaminmax = sqlite3_column_int(stmt_point, col++);
2008 int visibility = sqlite3_column_int(stmt_point, col++);
2009 int viz_name = sqlite3_column_int(stmt_point, col++);
2010 int shared = sqlite3_column_int(stmt_point, col++);
2011 int isolated = sqlite3_column_int(stmt_point, col++);
2012 std::string point_created_at =
2013 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_point, col++));
2017 new RoutePoint(latitude, longitude, symbol, name, point_guid,
false);
2021 point->SetPlannedSpeed(plan_speed);
2027 point->SetShowWaypointRangeRings(range_ring_visible == 1);
2031 point->SetScaMin(scamin);
2032 point->SetScaMax(scamax);
2033 point->SetUseSca(use_scaminmax == 1);
2035 point->SetVisible(visibility == 1);
2036 point->SetNameShown(viz_name == 1);
2037 point->SetShared(shared == 1);
2040 if (point_created_at.size()) {
2044 std::istringstream ss(point_created_at);
2045 ss >> std::get_time(&tm,
"%Y-%m-%d %H:%M:%S");
2046 time_t epoch_time = mktime(&tm);
2052 pSelect->AddSelectableRoutePoint(point->m_lat, point->m_lon, point);
2055 const char* sqlh = R
"(
2056 SELECT guid, html_link, html_description, html_type
2057 FROM routepoint_html_links
2058 WHERE routepoint_guid = ?
2059 ORDER BY html_type ASC
2062 sqlite3_stmt* stmt_links;
2064 if (sqlite3_prepare_v2(m_db, sqlh, -1, &stmt_links,
nullptr) ==
2066 sqlite3_bind_text(stmt_links, 1, point->
m_GUID.ToStdString().c_str(),
2067 -1, SQLITE_TRANSIENT);
2069 while (sqlite3_step(stmt_links) == SQLITE_ROW) {
2070 std::string link_guid =
2071 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_links, 0));
2072 std::string link_link =
2073 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_links, 1));
2074 std::string link_description =
2075 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_links, 2));
2076 std::string link_type =
2077 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_links, 3));
2080 h->DescrText = link_description;
2081 h->Link = link_link;
2082 h->LType = link_type;
2086 sqlite3_finalize(stmt_links);
2090 sqlite3_finalize(stmt_point);
2094bool NavObj_dB::InsertRoutePoint(
RoutePoint* point) {
2098 if (!RoutePointExists(m_db, point->
m_GUID.ToStdString())) {
2101 wxString::Format(
"INSERT INTO routepoints (guid) VALUES ('%s')",
2102 point->
m_GUID.ToStdString().c_str());
2103 if (!executeSQL(m_db, sql)) {
2108 UpdateDBRoutePointAttributes(point);
2112 if (NbrOfLinks > 0) {
2114 for (
auto it = list->begin(); it != list->end(); ++it) {
2116 if (!RoutePointHtmlLinkExists(m_db, link->GUID)) {
2117 InsertRoutePointHTML(m_db, point->
m_GUID.ToStdString(), link->GUID,
2118 link->DescrText.ToStdString(),
2119 link->Link.ToStdString(),
2120 link->LType.ToStdString());
2128bool NavObj_dB::DeleteRoutePoint(
RoutePoint* point) {
2129 if (m_importing)
return false;
2130 if (!point)
return false;
2132 std::string point_guid = point->
m_GUID.ToStdString();
2136 const char* sql =
"DELETE FROM routepoints WHERE guid = ?";
2139 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
2140 sqlite3_bind_text(stmt, 1, point_guid.c_str(), -1, SQLITE_TRANSIENT);
2141 if (sqlite3_step(stmt) != SQLITE_DONE) {
2142 ReportError(
"DeleteRoutePoint:step");
2143 sqlite3_finalize(stmt);
2147 sqlite3_finalize(stmt);
2154bool NavObj_dB::UpdateRoutePoint(
RoutePoint* point) {
2155 if (m_importing)
return false;
2156 if (!RoutePointExists(m_db, point->
m_GUID.ToStdString()))
return false;
2157 UpdateDBRoutePointAttributes(point);
2161bool NavObj_dB::Backup(wxString fileName) {
2162 sqlite3_backup* pBackup;
2163 sqlite3* backupDatabase;
2165 if (sqlite3_open(fileName.c_str(), &backupDatabase) == SQLITE_OK) {
2166 pBackup = sqlite3_backup_init(backupDatabase,
"main", m_db,
"main");
2168 int result = sqlite3_backup_step(pBackup, -1);
2169 if ((result == SQLITE_OK) || (result == SQLITE_DONE)) {
2170 if (sqlite3_backup_finish(pBackup) == SQLITE_OK) {
2171 sqlite3_close_v2(backupDatabase);
2177 wxLogMessage(
"navobj database backup error: %s", sqlite3_errmsg(m_db));
The navobj SQLite container object, a singleton.
Represents a waypoint or mark within the navigation system.
HyperlinkList * m_HyperlinkList
List of hyperlinks associated with this waypoint.
wxColour m_wxcWaypointRangeRingsColour
Color for the range rings display.
wxString m_MarkDescription
Description text for the waypoint.
int m_iWaypointRangeRingsNumber
Number of range rings to display around the waypoint.
wxString m_GUID
Globally Unique Identifier for the waypoint.
wxDateTime m_CreateTimeX
Creation timestamp for the waypoint, in UTC.
bool m_bIsolatedMark
Flag indicating if the waypoint is a standalone mark.
wxDateTime GetManualETD()
Retrieves the manually set Estimated Time of Departure for this waypoint, in UTC.
wxString m_timestring
String representation of the waypoint creation time.
double GetPlannedSpeed()
Return the planned speed associated with this waypoint.
double m_WaypointArrivalRadius
Arrival radius in nautical miles.
int m_iWaypointRangeRingsStepUnits
Units for the range rings step (0=nm, 1=km).
float m_fWaypointRangeRingsStep
Distance between consecutive range rings.
wxString m_TideStation
Associated tide station identifier.
bool m_bShowWaypointRangeRings
Flag indicating if range rings should be shown around the waypoint.
void SetETD(const wxDateTime &etd)
Sets the Estimated Time of Departure for this waypoint, in UTC.
Represents a navigational route in the navigation system.
double m_PlannedSpeed
Default planned speed for the route in knots.
wxString m_RouteStartString
Name or description of the route's starting point.
wxString m_RouteDescription
Additional descriptive information about the route.
wxString m_Colour
Color name for rendering the route on the chart.
wxString m_RouteEndString
Name or description of the route's ending point.
wxPenStyle m_style
Style of the route line when rendered on the chart.
wxString m_TimeDisplayFormat
Format for displaying times in the UI.
int m_width
Width of the route line in pixels when rendered on the chart.
wxString m_RouteNameString
User-assigned name for the route.
wxString m_GUID
Globally unique identifier for this route.
wxDateTime m_PlannedDeparture
Planned departure time for the route, in UTC.
HyperlinkList * m_HyperlinkList
List of hyperlinks associated with this route.
bool DeleteRoute(Route *pRoute)
Represents a single point in a track.
Represents a track, which is a series of connected track points.
bool AddRoutePoint(RoutePoint *prp)
Add a point to list which owns it.
bool RemoveRoutePoint(RoutePoint *prp)
Remove a routepoint from list if present, deallocate it all cases.
Decoded messages send/receive support.
bool exists(const std::string &name)
MySQL based storage for routes, tracks, etc.
navobj_db_util.h – MySQL support utilities
Navigation Utility Functions without GUI dependencies.
User notification container.
User notifications manager.
Routeman * g_pRouteMan
Global instance.
RouteList * pRouteList
Global instance.
Select * pSelect
Global instance.
std::vector< Track * > g_TrackList
Global instance.