31#include <wx/filename.h>
41static void ReportError(
const std::string zmsg);
43static bool executeSQL(sqlite3* db,
const char* sql) {
44 char* errMsg =
nullptr;
45 if (sqlite3_exec(db, sql,
nullptr,
nullptr, &errMsg) != SQLITE_OK) {
47 wxString::Format(_(
"navobj database error.") +
" %s", errMsg);
49 auto& noteman = NotificationManager::GetInstance();
50 noteman.AddNotification(NotificationSeverity::kWarning, msg.ToStdString());
57static bool executeSQL(sqlite3* db, wxString& sql) {
58 return executeSQL(db, sql.ToStdString().c_str());
61bool CreateTables(sqlite3* db) {
63 const char* create_tables_sql = R
"(
64 CREATE TABLE IF NOT EXISTS tracks (
65 guid TEXT PRIMARY KEY NOT NULL,
74 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
77 CREATE TABLE IF NOT EXISTS trk_points (
78 track_guid TEXT NOT NULL,
79 latitude REAL NOT NULL,
80 longitude REAL NOT NULL,
81 timestamp TEXT NOT NULL,
83 FOREIGN KEY (track_guid) REFERENCES tracks(guid) ON DELETE CASCADE
87 CREATE TABLE IF NOT EXISTS track_html_links (
88 guid TEXT PRIMARY KEY,
89 track_guid TEXT NOT NULL,
91 html_description TEXT,
93 FOREIGN KEY (track_guid) REFERENCES tracks(guid) ON DELETE CASCADE
97 CREATE TABLE IF NOT EXISTS routes (
98 guid TEXT PRIMARY KEY NOT NULL,
103 planned_departure TEXT,
110 shared_wp_viz INTEGER,
111 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
115 CREATE TABLE IF NOT EXISTS routepoints (
116 guid TEXT PRIMARY KEY NOT NULL,
128 RangeRingsNumber INTEGER,
130 RangeRingsStepUnits INTEGER,
131 RangeRingsVisible INTEGER,
132 RangeRingsColour TEXT,
140 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
143 CREATE TABLE IF NOT EXISTS routepoints_link (
147 PRIMARY KEY (route_guid, point_order),
148 FOREIGN KEY (route_guid) REFERENCES routes(guid) ON DELETE CASCADE
151 CREATE TABLE IF NOT EXISTS route_html_links (
152 guid TEXT PRIMARY KEY,
153 route_guid TEXT NOT NULL,
155 html_description TEXT,
157 FOREIGN KEY (route_guid) REFERENCES routes(guid) ON DELETE CASCADE
160 CREATE TABLE IF NOT EXISTS routepoint_html_links (
161 guid TEXT PRIMARY KEY,
162 routepoint_guid TEXT NOT NULL,
164 html_description TEXT,
166 FOREIGN KEY (routepoint_guid) REFERENCES routepoints(guid) ON DELETE CASCADE
169 CREATE INDEX IF NOT EXISTS idx_track_points
170 ON trk_points (track_guid);
174 if (!executeSQL(db, create_tables_sql))
return false;
179bool TrackExists(sqlite3* db,
const std::string& track_guid) {
180 const char* sql =
"SELECT 1 FROM tracks WHERE guid = ? LIMIT 1";
184 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
185 sqlite3_bind_text(stmt, 1, track_guid.c_str(), -1, SQLITE_TRANSIENT);
187 if (sqlite3_step(stmt) == SQLITE_ROW) {
191 sqlite3_finalize(stmt);
193 ReportError(
"TrackExists:prepare");
199bool TrackHtmlLinkExists(sqlite3* db,
const std::string& link_guid) {
200 const char* sql =
"SELECT 1 FROM track_html_links WHERE guid = ? LIMIT 1";
204 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
205 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
207 if (sqlite3_step(stmt) == SQLITE_ROW) {
211 sqlite3_finalize(stmt);
213 ReportError(
"TrackHtmlLinkExists:prepare");
219bool DeleteAllCommentsForTrack(sqlite3* db,
const std::string& track_guid) {
220 const char* sql =
"DELETE FROM track_html_links WHERE track_guid = ?";
223 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
224 sqlite3_bind_text(stmt, 1, track_guid.c_str(), -1, SQLITE_TRANSIENT);
225 if (sqlite3_step(stmt) != SQLITE_DONE) {
226 ReportError(
"DeleteAllCommentsForTrack:step");
230 sqlite3_finalize(stmt);
232 ReportError(
"DeleteAllCommentsForTrack:prepare");
238bool InsertTrackPoint(sqlite3* db,
const std::string& track_guid,
double lat,
239 double lon,
const std::string& timestamp,
int i_point) {
240 const char* sql = R
"(
241 INSERT INTO trk_points (track_guid, latitude, longitude, timestamp, point_order)
242 VALUES (?, ?, ?, ?, ?)
246 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
247 sqlite3_bind_text(stmt, 1, track_guid.c_str(), -1, SQLITE_TRANSIENT);
248 sqlite3_bind_double(stmt, 2, lat);
249 sqlite3_bind_double(stmt, 3, lon);
250 sqlite3_bind_text(stmt, 4, timestamp.c_str(), -1, SQLITE_TRANSIENT);
251 sqlite3_bind_int(stmt, 5, i_point);
252 if (sqlite3_step(stmt) != SQLITE_DONE) {
253 ReportError(
"InsertTrackPoint:step");
254 sqlite3_finalize(stmt);
257 sqlite3_finalize(stmt);
264bool InsertTrackHTML(sqlite3* db,
const std::string& track_guid,
265 const std::string& link_guid,
const std::string& descrText,
266 const std::string& link,
const std::string& ltype) {
267 const char* sql = R
"(
268 INSERT INTO track_html_links (guid, track_guid, html_link, html_description, html_type)
269 VALUES (?, ?, ?, ?, ?)
273 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
274 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
275 sqlite3_bind_text(stmt, 2, track_guid.c_str(), -1, SQLITE_TRANSIENT);
276 sqlite3_bind_text(stmt, 3, link.c_str(), -1, SQLITE_TRANSIENT);
277 sqlite3_bind_text(stmt, 4, descrText.c_str(), -1, SQLITE_TRANSIENT);
278 sqlite3_bind_text(stmt, 5, ltype.c_str(), -1, SQLITE_TRANSIENT);
279 if (sqlite3_step(stmt) != SQLITE_DONE) {
280 ReportError(
"InsertTrackHTML:step");
281 sqlite3_finalize(stmt);
284 sqlite3_finalize(stmt);
293bool DeleteAllCommentsForRoute(sqlite3* db,
const std::string& route_guid) {
294 const char* sql = R
"(
295 DELETE FROM route_html_links WHERE route_guid = ?
298 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
299 sqlite3_bind_text(stmt, 1, route_guid.c_str(), -1, SQLITE_TRANSIENT);
300 if (sqlite3_step(stmt) != SQLITE_DONE) {
301 ReportError(
"DeleteAllCommentsForRoute:step");
302 sqlite3_finalize(stmt);
305 sqlite3_finalize(stmt);
312bool RouteHtmlLinkExists(sqlite3* db,
const std::string& link_guid) {
313 const char* sql =
"SELECT 1 FROM route_html_links WHERE guid = ? LIMIT 1";
317 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
318 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
320 if (sqlite3_step(stmt) == SQLITE_ROW) {
324 sqlite3_finalize(stmt);
331bool RoutePointHtmlLinkExists(sqlite3* db,
const std::string& link_guid) {
333 "SELECT 1 FROM routepoint_html_links WHERE guid = ? LIMIT 1";
337 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
338 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
340 if (sqlite3_step(stmt) == SQLITE_ROW) {
344 sqlite3_finalize(stmt);
351bool RouteExistsDB(sqlite3* db,
const std::string& route_guid) {
352 const char* sql =
"SELECT 1 FROM routes WHERE guid = ? LIMIT 1";
356 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
357 sqlite3_bind_text(stmt, 1, route_guid.c_str(), -1, SQLITE_TRANSIENT);
359 if (sqlite3_step(stmt) == SQLITE_ROW) {
363 sqlite3_finalize(stmt);
370bool InsertRouteHTML(sqlite3* db,
const std::string& route_guid,
371 const std::string& link_guid,
const std::string& descrText,
372 const std::string& link,
const std::string& ltype) {
373 const char* sql = R
"(
374 INSERT INTO route_html_links (guid, route_guid, html_link, html_description, html_type)
375 VALUES (?, ?, ?, ?, ?)
379 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
380 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
381 sqlite3_bind_text(stmt, 2, route_guid.c_str(), -1, SQLITE_TRANSIENT);
382 sqlite3_bind_text(stmt, 3, link.c_str(), -1, SQLITE_TRANSIENT);
383 sqlite3_bind_text(stmt, 4, descrText.c_str(), -1, SQLITE_TRANSIENT);
384 sqlite3_bind_text(stmt, 5, ltype.c_str(), -1, SQLITE_TRANSIENT);
385 if (sqlite3_step(stmt) != SQLITE_DONE) {
386 ReportError(
"InsertRouteHTML:step");
387 sqlite3_finalize(stmt);
390 sqlite3_finalize(stmt);
397bool RoutePointExists(sqlite3* db,
const std::string& routepoint_guid) {
398 const char* sql =
"SELECT 1 FROM routepoints WHERE guid = ? LIMIT 1";
402 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
403 sqlite3_bind_text(stmt, 1, routepoint_guid.c_str(), -1, SQLITE_TRANSIENT);
405 if (sqlite3_step(stmt) == SQLITE_ROW) {
409 sqlite3_finalize(stmt);
416bool InsertRoutePointHTML(sqlite3* db,
const std::string& point_guid,
417 const std::string& link_guid,
418 const std::string& descrText,
const std::string& link,
419 const std::string& ltype) {
420 const char* sql = R
"(
421 INSERT INTO routepoint_html_links (guid, routepoint_guid, html_link, html_description, html_type)
422 VALUES (?, ?, ?, ?, ?)
426 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
427 sqlite3_bind_text(stmt, 1, link_guid.c_str(), -1, SQLITE_TRANSIENT);
428 sqlite3_bind_text(stmt, 2, point_guid.c_str(), -1, SQLITE_TRANSIENT);
429 sqlite3_bind_text(stmt, 3, link.c_str(), -1, SQLITE_TRANSIENT);
430 sqlite3_bind_text(stmt, 4, descrText.c_str(), -1, SQLITE_TRANSIENT);
431 sqlite3_bind_text(stmt, 5, ltype.c_str(), -1, SQLITE_TRANSIENT);
432 if (sqlite3_step(stmt) != SQLITE_DONE) {
433 ReportError(
"InsertRoutePointHTML:step");
434 sqlite3_finalize(stmt);
437 sqlite3_finalize(stmt);
443bool DeleteAllCommentsForRoutePoint(sqlite3* db,
444 const std::string& routepoint_guid) {
446 "DELETE FROM routepoint_html_links WHERE routepoint_guid = ?";
449 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
450 sqlite3_bind_text(stmt, 1, routepoint_guid.c_str(), -1, SQLITE_TRANSIENT);
451 if (sqlite3_step(stmt) != SQLITE_DONE) {
452 ReportError(
"DeleteAllCommentsForRoutepoint:step");
456 sqlite3_finalize(stmt);
458 ReportError(
"DeleteAllCommentsForRoutepoint:prepare");
464bool InsertRoutePointDB(sqlite3* db,
RoutePoint* point) {
465 const char* sql = R
"(
466 INSERT or REPLACE INTO routepoints(guid)
471 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
472 sqlite3_bind_text(stmt, 1, point->
m_GUID.ToStdString().c_str(), -1,
474 if (sqlite3_step(stmt) != SQLITE_DONE) {
475 ReportError(
"InsertRoutePointDB:step");
476 sqlite3_finalize(stmt);
479 sqlite3_finalize(stmt);
488 const char* sql = R
"(
489 INSERT or IGNORE INTO routepoints_link (route_guid, point_guid, point_order)
495 if (sqlite3_prepare_v2(db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
496 sqlite3_bind_text(stmt, 1, route->
m_GUID.ToStdString().c_str(), -1,
498 sqlite3_bind_text(stmt, 2, point->
m_GUID.ToStdString().c_str(), -1,
500 sqlite3_bind_int(stmt, 3, point_order);
501 if (sqlite3_step(stmt) != SQLITE_DONE) {
502 ReportError(
"InsertTrackPointLink:step");
503 sqlite3_finalize(stmt);
506 sqlite3_finalize(stmt);
513void DeleteOrphanedRoutepoint(sqlite3* db) {
514 const char* sql = R
"(
515 DELETE FROM routepoints
516 WHERE guid NOT IN (SELECT point_guid FROM routepoints_link)
518 char* errMsg =
nullptr;
520 if (sqlite3_exec(db, sql,
nullptr,
nullptr, &errMsg) != SQLITE_OK) {
525void errorLogCallback(
void* pArg,
int iErrCode,
const char* zMsg) {
527 wxString::Format(_(
"navobj database error.") +
" %d: %s", iErrCode, zMsg);
529 auto& noteman = NotificationManager::GetInstance();
530 noteman.AddNotification(NotificationSeverity::kWarning, msg.ToStdString());
533static void ReportError(
const std::string zmsg) {
535 wxString::Format(_(
"navobj database error.") +
" %s", zmsg.c_str());
537 auto& noteman = NotificationManager::GetInstance();
538 noteman.AddNotification(NotificationSeverity::kWarning, msg.ToStdString());
546NavObj_dB::NavObj_dB() {
547 m_pImportProgress =
nullptr;
550 int ie = sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback,
nullptr);
554 wxFileName::GetPathSeparator() +
"navobj.db";
555 if (!wxFileExists(db_filename)) {
558 wxFileName::GetPathSeparator() +
"navobj.xml";
559 if (wxFileExists(xml_filename)) {
560 wxCopyFile(xml_filename, xml_filename +
".backup");
564 wxFileName::GetPathSeparator() +
565 "navobj.xml.import_backup";
566 if (!wxFileExists(deep_backup_filename)) {
567 wxCopyFile(xml_filename, deep_backup_filename);
573 int create_result = sqlite3_open_v2(
574 db_filename.ToStdString().c_str(),
576 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
579 if (create_result != SQLITE_OK) {
580 wxLogMessage(
"Cannot create new navobj.db database file");
589 int close_result = sqlite3_close_v2(m_db);
590 if (close_result != SQLITE_OK) {
596 int m_open_result = sqlite3_open_v2(db_filename.ToStdString().c_str(), &m_db,
597 SQLITE_OPEN_READWRITE, NULL);
598 sqlite3_exec(m_db,
"PRAGMA foreign_keys = ON;",
nullptr,
nullptr,
nullptr);
602 sqlite3_close_v2(m_db);
604 m_open_result = sqlite3_open_v2(db_filename.ToStdString().c_str(), &m_db,
605 SQLITE_OPEN_READWRITE, NULL);
606 sqlite3_exec(m_db,
"PRAGMA foreign_keys = ON;",
nullptr,
nullptr,
nullptr);
612NavObj_dB::~NavObj_dB() { sqlite3_close_v2(m_db); }
614void NavObj_dB::Close() {
615 sqlite3_close_v2(m_db);
619bool NavObj_dB::ImportLegacyNavobj(wxFrame* frame) {
621 wxFileName::GetPathSeparator() +
"navobj.xml";
623 if (::wxFileExists(navobj_filename)) {
625 CountImportNavObjects();
626 m_pImportProgress =
new wxProgressDialog(_(
"Importing Navobj database"),
"",
627 m_nImportObjects, frame);
628 m_import_progesscount = 0;
630 rv = ImportLegacyPoints();
631 rv |= ImportLegacyRoutes();
632 rv |= ImportLegacyTracks();
634 m_pImportProgress->Destroy();
638 if (::wxFileExists(navobj_filename)) ::wxRemoveFile(navobj_filename);
643void NavObj_dB::CountImportNavObjects() {
644 m_nImportObjects = 0;
651 wxFileName::GetPathSeparator() +
"navobj.xml";
653 if (::wxFileExists(navobj_filename) &&
654 input_set->load_file(navobj_filename.ToStdString().c_str()).status ==
655 pugi::xml_parse_status::status_ok) {
656 input_set->LoadAllGPXPointObjects();
657 auto pointlist = pWayPointMan->GetWaypointList();
665 input_set->LoadAllGPXRouteObjects();
669 m_nImportObjects += route_import->GetnPoints();
672 input_set->LoadAllGPXTrackObjects();
677 m_nImportObjects += track_import->GetnPoints();
683bool NavObj_dB::ImportLegacyTracks() {
684 std::vector<Track*> tracks_added;
688 if (InsertTrack(track_import)) {
689 tracks_added.push_back(track_import);
692 m_import_progesscount += track_import->GetnPoints() + 1;
693 wxString msg = wxString::Format(
"Tracks %d/%d", ntrack, m_nimportTracks);
694 m_pImportProgress->Update(m_import_progesscount, msg);
695 m_pImportProgress->Show();
699 for (
Track* ptrack : tracks_added) {
700 if (ptrack->m_bIsInLayer)
continue;
707bool NavObj_dB::ImportLegacyRoutes() {
708 std::vector<Route*> routes_added;
712 if (InsertRoute(route_import)) {
713 routes_added.push_back(route_import);
716 m_import_progesscount += route_import->GetnPoints() + 1;
717 wxString msg = wxString::Format(
"Routes %d/%d", nroute, m_nimportRoutes);
718 m_pImportProgress->Update(m_import_progesscount, msg);
719 m_pImportProgress->Show();
723 for (
Route* route : routes_added) {
729 pWayPointMan->DeleteAllWaypoints(
true);
734bool NavObj_dB::ImportLegacyPoints() {
735 std::vector<RoutePoint*> points_added;
739 if (m_nimportPoints > 1000) nmod = 10;
740 if (m_nimportPoints > 10000) nmod = 100;
742 for (
RoutePoint* point : *pWayPointMan->GetWaypointList()) {
744 if (InsertRoutePointDB(m_db, point)) {
745 points_added.push_back(point);
748 UpdateDBRoutePointAttributes(point);
749 m_import_progesscount += 1;
750 if ((npoint % nmod) == 0) {
752 wxString::Format(
"Points %d/%d", npoint, m_nimportPoints);
753 m_pImportProgress->Update(m_import_progesscount, msg);
754 m_pImportProgress->Show();
769void NavObj_dB::LoadNavObjects() {
775bool NavObj_dB::InsertTrack(
Track* track) {
776 if (TrackExists(m_db, track->m_GUID.ToStdString()))
return false;
780 sqlite3_exec(m_db,
"BEGIN TRANSACTION", 0, 0, &errMsg);
782 ReportError(
"InsertTrack:BEGIN TRANSACTION");
787 wxString sql = wxString::Format(
"INSERT INTO tracks (guid) VALUES ('%s')",
788 track->m_GUID.ToStdString().c_str());
789 if (!executeSQL(m_db, sql)) {
790 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
794 UpdateDBTrackAttributes(track);
797 for (
int i = 0; i < track->GetnPoints(); i++) {
798 auto point = track->GetPoint(i);
800 InsertTrackPoint(m_db, track->m_GUID.ToStdString(), point->m_lat,
801 point->m_lon, point->GetTimeString(), i);
805 int NbrOfLinks = track->m_TrackHyperlinkList->size();
806 if (NbrOfLinks > 0) {
807 auto& list = track->m_TrackHyperlinkList;
808 for (
auto it = list->begin(); it != list->end(); ++it) {
810 if (!TrackHtmlLinkExists(m_db, link->GUID)) {
811 InsertTrackHTML(m_db, track->m_GUID.ToStdString(), link->GUID,
812 link->DescrText.ToStdString(), link->Link.ToStdString(),
813 link->LType.ToStdString());
817 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
819 if (errMsg) rv =
false;
824bool NavObj_dB::UpdateTrack(
Track* track) {
828 if (!TrackExists(m_db, track->m_GUID.ToStdString()))
return false;
830 sqlite3_exec(m_db,
"BEGIN TRANSACTION", 0, 0, &errMsg);
832 ReportError(
"UpdateTrack:BEGIN TRANSACTION");
836 UpdateDBTrackAttributes(track);
839 const char* sql =
"DELETE FROM trk_points WHERE track_guid = ?";
841 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
842 sqlite3_bind_text(stmt, 1, track->m_GUID.ToStdString().c_str(), -1,
845 ReportError(
"UpdateTrack:prepare");
846 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
849 if (sqlite3_step(stmt) != SQLITE_DONE) {
850 ReportError(
"UpdateTrack:step");
851 sqlite3_finalize(stmt);
852 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
855 sqlite3_finalize(stmt);
858 for (
int i = 0; i < track->GetnPoints(); i++) {
859 auto point = track->GetPoint(i);
862 InsertTrackPoint(m_db, track->m_GUID.ToStdString(), point->m_lat,
863 point->m_lon, point->GetTimeString(), i);
867 sqlite3_exec(m_db,
"COMMIT", 0, 0,
nullptr);
870 if (errMsg) rv =
false;
874bool NavObj_dB::UpdateDBTrackAttributes(
Track* track) {
888 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
889 sqlite3_bind_text(stmt, 1, track->GetName().ToStdString().c_str(), -1,
891 sqlite3_bind_text(stmt, 2, track->m_TrackDescription.ToStdString().c_str(),
892 -1, SQLITE_TRANSIENT);
893 sqlite3_bind_int(stmt, 3, track->m_bVisible);
894 sqlite3_bind_text(stmt, 4, track->m_TrackStartString.ToStdString().c_str(),
895 -1, SQLITE_TRANSIENT);
896 sqlite3_bind_text(stmt, 5, track->m_TrackEndString.ToStdString().c_str(),
897 -1, SQLITE_TRANSIENT);
898 sqlite3_bind_int(stmt, 6, track->m_width);
899 sqlite3_bind_int(stmt, 7,
900 (
int)(track->m_style));
901 sqlite3_bind_text(stmt, 8, track->m_Colour.ToStdString().c_str(), -1,
903 sqlite3_bind_text(stmt, 9, track->m_GUID.c_str(), track->m_GUID.size(),
909 if (sqlite3_step(stmt) != SQLITE_DONE) {
910 ReportError(
"UpdateDBTrackAttributesA:step");
911 sqlite3_finalize(stmt);
915 sqlite3_finalize(stmt);
920 DeleteAllCommentsForTrack(m_db, track->m_GUID.ToStdString());
923 int NbrOfLinks = track->m_TrackHyperlinkList->size();
924 if (NbrOfLinks > 0) {
925 auto& list = track->m_TrackHyperlinkList;
926 for (
auto it = list->begin(); it != list->end(); ++it) {
929 if (!TrackHtmlLinkExists(m_db, link->GUID)) {
930 InsertTrackHTML(m_db, track->m_GUID.ToStdString(), link->GUID,
931 link->DescrText.ToStdString(), link->Link.ToStdString(),
932 link->LType.ToStdString());
935 "UPDATE track_html_links SET "
937 "html_description = ?, "
941 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
942 sqlite3_bind_text(stmt, 3, link->Link.ToStdString().c_str(), -1,
944 sqlite3_bind_text(stmt, 4, link->DescrText.ToStdString().c_str(), -1,
946 sqlite3_bind_text(stmt, 5, link->LType.ToStdString().c_str(), -1,
949 if (sqlite3_step(stmt) != SQLITE_DONE) {
950 ReportError(
"UpdateDBTRackAttributesB:step");
951 sqlite3_finalize(stmt);
954 sqlite3_finalize(stmt);
964 if (!TrackExists(m_db, track->m_GUID.ToStdString()))
return false;
967 int this_point_index = track->GetnPoints();
970 if (!InsertTrackPoint(m_db, track->m_GUID.ToStdString(), point->m_lat,
971 point->m_lon, point->GetTimeString(),
972 this_point_index - 1))
978bool NavObj_dB::LoadAllTracks() {
979 const char* sql = R
"(
981 description, visibility, start_string, end_string,
985 ORDER BY created_at ASC
989 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) != SQLITE_OK) {
993 while (sqlite3_step(stmt) == SQLITE_ROW) {
995 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 0));
997 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 1));
998 std::string description =
999 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 2));
1000 int visibility = sqlite3_column_int(stmt, 3);
1001 std::string start_string =
1002 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 4));
1003 std::string end_string =
1004 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 5));
1005 int width = sqlite3_column_int(stmt, 6);
1006 int style = sqlite3_column_int(stmt, 7);
1008 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 8));
1009 std::string created =
1010 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 9));
1012 Track* new_trk = NULL;
1015 const char* sql = R
"(
1016 SELECT latitude, longitude, timestamp, point_order
1018 WHERE track_guid = ?
1019 ORDER BY point_order ASC
1022 sqlite3_stmt* stmtp;
1023 if (sqlite3_prepare_v2(m_db, sql, -1, &stmtp,
nullptr) != SQLITE_OK) {
1027 sqlite3_bind_text(stmtp, 1, guid.c_str(), -1, SQLITE_TRANSIENT);
1030 while (sqlite3_step(stmtp) == SQLITE_ROW) {
1032 new_trk =
new Track;
1033 new_trk->m_GUID = guid;
1036 new_trk->SetVisible(visibility == 1);
1037 new_trk->SetName(name.c_str());
1038 new_trk->m_TrackStartString = start_string.c_str();
1039 new_trk->m_TrackEndString = end_string.c_str();
1040 new_trk->m_width = width;
1041 new_trk->m_style = (wxPenStyle)style;
1042 new_trk->m_Colour = color;
1045 double latitude = sqlite3_column_double(stmtp, 0);
1046 double longitude = sqlite3_column_double(stmtp, 1);
1047 std::string timestamp =
1048 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, 2));
1049 int point_order = sqlite3_column_int(stmtp, 3);
1051 auto point =
new TrackPoint(latitude, longitude, timestamp);
1053 point->m_GPXTrkSegNo = GPXTrkSeg;
1054 new_trk->AddPoint(point);
1056 sqlite3_finalize(stmtp);
1059 new_trk->SetCurrentTrackSeg(GPXTrkSeg);
1062 const char* sqlh = R
"(
1063 SELECT guid, html_link, html_description, html_type
1064 FROM track_html_links
1065 WHERE track_guid = ?
1066 ORDER BY html_type ASC
1071 if (sqlite3_prepare_v2(m_db, sqlh, -1, &stmt,
nullptr) == SQLITE_OK) {
1072 sqlite3_bind_text(stmt, 1, new_trk->m_GUID.ToStdString().c_str(), -1,
1075 while (sqlite3_step(stmt) == SQLITE_ROW) {
1076 std::string link_guid =
1077 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 0));
1078 std::string link_link =
1079 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 1));
1080 std::string link_description =
1081 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 2));
1082 std::string link_type =
1083 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 3));
1086 h->DescrText = link_description;
1087 h->Link = link_link;
1088 h->LType = link_type;
1090 new_trk->m_TrackHyperlinkList->push_back(h);
1094 sqlite3_finalize(stmt);
1103 pSelect->AddAllSelectableTrackSegments(new_trk);
1109bool NavObj_dB::DeleteTrack(
Track* track) {
1110 if (!track)
return false;
1111 std::string track_guid = track->m_GUID.ToStdString();
1112 const char* sql =
"DELETE FROM tracks WHERE guid = ?";
1115 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1116 sqlite3_bind_text(stmt, 1, track_guid.c_str(), -1, SQLITE_TRANSIENT);
1117 if (sqlite3_step(stmt) != SQLITE_DONE) {
1118 ReportError(
"DeleteTrack:step");
1119 sqlite3_finalize(stmt);
1123 sqlite3_finalize(stmt);
1132bool NavObj_dB::InsertRoute(
Route* route) {
1136 if (!RouteExistsDB(m_db, route->
m_GUID.ToStdString())) {
1138 wxString sql = wxString::Format(
"INSERT INTO routes (guid) VALUES ('%s')",
1139 route->
m_GUID.ToStdString().c_str());
1140 if (!executeSQL(m_db, sql)) {
1143 UpdateDBRouteAttributes(route);
1146 sqlite3_exec(m_db,
"BEGIN TRANSACTION", 0, 0, &errMsg);
1148 ReportError(
"InsertRoute:BEGIN TRANSACTION");
1153 for (
int i = 0; i < route->GetnPoints(); i++) {
1154 auto point = route->GetPoint(i + 1);
1157 if (!RoutePointExists(m_db, point->m_GUID.ToStdString())) {
1158 InsertRoutePointDB(m_db, point);
1159 UpdateDBRoutePointAttributes(point);
1165 for (
int i = 0; i < route->GetnPoints(); i++) {
1166 auto point = route->GetPoint(i + 1);
1169 InsertRoutePointLink(m_db, route, point, i + 1);
1175 if (NbrOfLinks > 0) {
1177 for (
auto it = list.begin(); it != list.end(); ++it) {
1179 if (!RouteHtmlLinkExists(m_db, link->GUID)) {
1180 InsertRouteHTML(m_db, route->
m_GUID.ToStdString(), link->GUID,
1181 link->DescrText.ToStdString(), link->Link.ToStdString(),
1182 link->LType.ToStdString());
1187 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
1190 ReportError(
"InsertRoute:commit");
1196bool NavObj_dB::UpdateRoute(
Route* route) {
1200 if (!RouteExistsDB(m_db, route->
m_GUID.ToStdString()))
return false;
1202 sqlite3_exec(m_db,
"BEGIN TRANSACTION", 0, 0, &errMsg);
1204 ReportError(
"UpdateRoute:BEGIN TRANSACTION");
1208 UpdateDBRouteAttributes(route);
1211 for (
int i = 0; i < route->GetnPoints(); i++) {
1212 auto point = route->GetPoint(i + 1);
1215 if (!RoutePointExists(m_db, point->m_GUID.ToStdString())) {
1216 InsertRoutePointDB(m_db, point);
1218 UpdateDBRoutePointAttributes(point);
1223 const char* sql =
"DELETE FROM routepoints_link WHERE route_guid = ?";
1225 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1226 sqlite3_bind_text(stmt, 1, route->
m_GUID.ToStdString().c_str(), -1,
1229 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
1232 if (sqlite3_step(stmt) != SQLITE_DONE) {
1233 ReportError(
"UpdateRoute:step");
1234 sqlite3_finalize(stmt);
1235 sqlite3_exec(m_db,
"COMMIT", 0, 0, &errMsg);
1239 sqlite3_finalize(stmt);
1241 for (
int i = 0; i < route->GetnPoints(); i++) {
1242 auto point = route->GetPoint(i + 1);
1244 InsertRoutePointLink(m_db, route, point, i + 1);
1250 if (NbrOfLinks > 0) {
1252 for (
auto it = list.begin(); it != list.end(); ++it) {
1254 if (!RouteHtmlLinkExists(m_db, link->GUID)) {
1255 InsertRouteHTML(m_db, route->
m_GUID.ToStdString(), link->GUID,
1256 link->DescrText.ToStdString(), link->Link.ToStdString(),
1257 link->LType.ToStdString());
1261 sqlite3_exec(m_db,
"COMMIT", 0, 0,
nullptr);
1264 if (errMsg) rv =
false;
1269bool NavObj_dB::UpdateRouteViz(
Route* route) {
1272 if (!RouteExistsDB(m_db, route->
m_GUID.ToStdString()))
return false;
1274 UpdateDBRouteAttributes(route);
1276 for (
int i = 0; i < route->GetnPoints(); i++) {
1277 auto point = route->GetPoint(i + 1);
1280 UpdateDBRoutePointViz(point);
1284 if (errMsg) rv =
false;
1289bool NavObj_dB::UpdateDBRouteAttributes(
Route* route) {
1291 "UPDATE routes SET "
1294 "start_string = ?, "
1297 "shared_wp_viz = ?, "
1298 "planned_departure = ?, "
1307 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1308 sqlite3_bind_text(stmt, 1, route->GetName().ToStdString().c_str(), -1,
1311 -1, SQLITE_TRANSIENT);
1313 -1, SQLITE_TRANSIENT);
1315 -1, SQLITE_TRANSIENT);
1316 sqlite3_bind_int(stmt, 5, route->IsVisible());
1317 sqlite3_bind_int(stmt, 6, route->GetSharedWPViz());
1322 -1, SQLITE_TRANSIENT);
1323 sqlite3_bind_int(stmt, 10, route->
m_width);
1324 sqlite3_bind_int(stmt, 11,
1326 sqlite3_bind_text(stmt, 12, route->
m_Colour.ToStdString().c_str(), -1,
1328 sqlite3_bind_text(stmt, 13, route->
m_GUID.c_str(), route->
m_GUID.size(),
1334 if (sqlite3_step(stmt) != SQLITE_DONE) {
1335 ReportError(
"UpdateDBRouteAttributesA:step");
1336 sqlite3_finalize(stmt);
1340 sqlite3_finalize(stmt);
1345 DeleteAllCommentsForRoute(m_db, route->
m_GUID.ToStdString());
1349 if (NbrOfLinks > 0) {
1351 for (
auto it = list->begin(); it != list->end(); ++it) {
1353 if (!RouteHtmlLinkExists(m_db, link->GUID)) {
1354 InsertRouteHTML(m_db, route->
m_GUID.ToStdString(), link->GUID,
1355 link->DescrText.ToStdString(), link->Link.ToStdString(),
1356 link->LType.ToStdString());
1359 "UPDATE route_html_links SET "
1361 "html_description = ?, "
1365 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1366 sqlite3_bind_text(stmt, 3, link->Link.ToStdString().c_str(), -1,
1368 sqlite3_bind_text(stmt, 4, link->DescrText.ToStdString().c_str(), -1,
1370 sqlite3_bind_text(stmt, 5, link->LType.ToStdString().c_str(), -1,
1373 if (sqlite3_step(stmt) != SQLITE_DONE) {
1376 if (sqlite3_step(stmt) != SQLITE_DONE) {
1377 ReportError(
"UpdateDBRouteAttributesB:step");
1378 sqlite3_finalize(stmt);
1381 sqlite3_finalize(stmt);
1388bool NavObj_dB::UpdateDBRoutePointAttributes(
RoutePoint* point) {
1390 "UPDATE routepoints SET "
1401 "ArrivalRadius = ?, "
1402 "RangeRingsNumber = ?, "
1403 "RangeRingsStep = ?, "
1404 "RangeRingsStepUnits = ?, "
1405 "RangeRingsVisible = ?, "
1406 "RangeRingsColour = ?, "
1417 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1418 sqlite3_bind_double(stmt, 1, point->GetLatitude());
1419 sqlite3_bind_double(stmt, 2, point->GetLongitude());
1420 sqlite3_bind_text(stmt, 3, point->GetIconName().ToStdString().c_str(), -1,
1422 sqlite3_bind_text(stmt, 4, point->GetName().ToStdString().c_str(), -1,
1424 sqlite3_bind_text(stmt, 5, point->GetDescription().ToStdString().c_str(),
1425 -1, SQLITE_TRANSIENT);
1426 sqlite3_bind_text(stmt, 6, point->
m_TideStation.ToStdString().c_str(), -1,
1431 sqlite3_bind_int(stmt, 8, etd);
1432 sqlite3_bind_text(stmt, 9,
"type", -1, SQLITE_TRANSIENT);
1433 std::string timit = point->
m_timestring.ToStdString().c_str();
1434 sqlite3_bind_text(stmt, 10, point->
m_timestring.ToStdString().c_str(), -1,
1447 -1, SQLITE_TRANSIENT);
1449 sqlite3_bind_int(stmt, 17, point->GetScaMin());
1450 sqlite3_bind_int(stmt, 18, point->GetScaMax());
1451 sqlite3_bind_int(stmt, 19, point->GetUseSca());
1453 sqlite3_bind_int(stmt, 20, point->IsVisible());
1454 sqlite3_bind_int(stmt, 21, point->IsNameShown());
1455 sqlite3_bind_int(stmt, 22, point->IsShared());
1457 sqlite3_bind_int(stmt, 23, iso);
1459 sqlite3_bind_text(stmt, 24, point->
m_GUID.ToStdString().c_str(), -1,
1466 if (sqlite3_step(stmt) != SQLITE_DONE) {
1467 ReportError(
"UpdateDBRoutePointAttributesA:step");
1468 sqlite3_finalize(stmt);
1472 sqlite3_finalize(stmt);
1477 DeleteAllCommentsForRoutePoint(m_db, point->
m_GUID.ToStdString());
1481 if (NbrOfLinks > 0) {
1483 for (
auto it = list->begin(); it != list->end(); ++it) {
1485 if (!RoutePointHtmlLinkExists(m_db, link->GUID)) {
1486 InsertRoutePointHTML(m_db, point->
m_GUID.ToStdString(), link->GUID,
1487 link->DescrText.ToStdString(),
1488 link->Link.ToStdString(),
1489 link->LType.ToStdString());
1492 "UPDATE routepoint_html_links SET "
1494 "html_description = ?, "
1498 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1499 sqlite3_bind_text(stmt, 3, link->Link.ToStdString().c_str(), -1,
1501 sqlite3_bind_text(stmt, 4, link->DescrText.ToStdString().c_str(), -1,
1503 sqlite3_bind_text(stmt, 5, link->LType.ToStdString().c_str(), -1,
1506 if (sqlite3_step(stmt) != SQLITE_DONE) {
1509 if (sqlite3_step(stmt) != SQLITE_DONE) {
1510 ReportError(
"UpdateDBRoutePointAttributesB:step-h");
1511 sqlite3_finalize(stmt);
1514 sqlite3_finalize(stmt);
1522bool NavObj_dB::UpdateDBRoutePointViz(
RoutePoint* point) {
1524 "UPDATE routepoints SET "
1529 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1530 sqlite3_bind_int(stmt, 1, point->IsVisible());
1531 sqlite3_bind_text(stmt, 2, point->
m_GUID.ToStdString().c_str(), -1,
1538 if (sqlite3_step(stmt) != SQLITE_DONE) {
1539 ReportError(
"UpdateDBRoutePointVizA:step");
1540 sqlite3_finalize(stmt);
1544 sqlite3_finalize(stmt);
1549bool NavObj_dB::DeleteRoute(
Route* route) {
1550 if (m_importing)
return false;
1551 if (!route)
return false;
1552 std::string route_guid = route->
m_GUID.ToStdString();
1553 const char* sql =
"DELETE FROM routes WHERE guid = ?";
1556 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
1557 sqlite3_bind_text(stmt, 1, route_guid.c_str(), -1, SQLITE_TRANSIENT);
1558 if (sqlite3_step(stmt) != SQLITE_DONE) {
1559 ReportError(
"DeleteRoute:step");
1560 sqlite3_finalize(stmt);
1563 sqlite3_finalize(stmt);
1570bool NavObj_dB::LoadAllRoutes() {
1580 "planned_departure, "
1587 "ORDER BY created_at ASC";
1590 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) != SQLITE_OK) {
1594 int errcode0 = SQLITE_OK;
1595 while ((errcode0 = sqlite3_step(stmt)) == SQLITE_ROW) {
1597 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 0));
1599 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 1));
1600 std::string description =
1601 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 2));
1602 std::string start_string =
1603 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 3));
1604 std::string end_string =
1605 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 4));
1606 int visibility = sqlite3_column_int(stmt, 5);
1607 int sharewp_viz = sqlite3_column_int(stmt, 6);
1608 time_t planned_departure_ticks = sqlite3_column_int(stmt, 7);
1609 double plan_speed = sqlite3_column_double(stmt, 8);
1610 std::string time_format =
1611 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 9));
1613 int width = sqlite3_column_int(stmt, 10);
1614 int style = sqlite3_column_int(stmt, 11);
1616 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 12));
1618 Route* route = NULL;
1621 const char* sql = R
"(
1622 SELECT latitude, longitude, timestamp, point_order
1624 WHERE track_guid = ?
1625 ORDER BY point_order ASC
1641 "p.RangeRingsNumber, "
1642 "p.RangeRingsStep, "
1643 "p.RangeRingsStepUnits, "
1644 "p.RangeRingsVisible, "
1645 "p.RangeRingsColour, "
1654 "FROM routepoints_link tp "
1655 "JOIN routepoints p ON p.guid = tp.point_guid "
1656 "WHERE tp.route_guid = ? "
1657 "ORDER BY tp.point_order ASC";
1659 sqlite3_stmt* stmtp;
1660 if (sqlite3_prepare_v2(m_db, sqlp, -1, &stmtp,
nullptr) != SQLITE_OK) {
1661 ReportError(
"LoadAllRoutes-B:prepare");
1665 sqlite3_bind_text(stmtp, 1, guid.c_str(), -1, SQLITE_TRANSIENT);
1668 int errcode = SQLITE_OK;
1669 while ((errcode = sqlite3_step(stmtp)) == SQLITE_ROW) {
1675 route->SetVisible(visibility == 1);
1680 route->SetVisible(visibility == 1);
1681 route->SetSharedWPViz(sharewp_viz == 1);
1687 route->
m_style = (wxPenStyle)style;
1693 std::string point_guid =
1694 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1695 double latitude = sqlite3_column_double(stmtp, col++);
1696 double longitude = sqlite3_column_double(stmtp, col++);
1697 std::string symbol =
1698 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1700 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1701 std::string description =
1702 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1703 std::string tide_station =
1704 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1705 double plan_speed = sqlite3_column_double(stmtp, col++);
1706 time_t etd_epoch = sqlite3_column_int(stmtp, col++);
1708 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1710 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1711 double arrival_radius = sqlite3_column_double(stmtp, col++);
1713 int range_ring_number = sqlite3_column_int(stmtp, col++);
1714 double range_ring_step = sqlite3_column_double(stmtp, col++);
1715 int range_ring_units = sqlite3_column_int(stmtp, col++);
1716 int range_ring_visible = sqlite3_column_int(stmtp, col++);
1717 std::string range_ring_color =
1718 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1720 int scamin = sqlite3_column_int(stmtp, col++);
1721 int scamax = sqlite3_column_int(stmtp, col++);
1722 int use_scaminmax = sqlite3_column_int(stmtp, col++);
1724 int visibility = sqlite3_column_int(stmtp, col++);
1725 int viz_name = sqlite3_column_int(stmtp, col++);
1726 int shared = sqlite3_column_int(stmtp, col++);
1727 int isolated = sqlite3_column_int(stmtp, col++);
1728 std::string point_created_at =
1729 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1734 auto containing_route =
1735 g_pRouteMan->FindRouteContainingWaypoint(point_guid);
1737 if (containing_route) {
1738 existing_point = containing_route->GetPoint(point_guid);
1741 if (!existing_point) {
1742 existing_point = pWayPointMan->FindRoutePointByGUID(point_guid.c_str());
1745 if (existing_point) {
1746 point = existing_point;
1747 point->SetShared(
true);
1751 new RoutePoint(latitude, longitude, symbol, name, point_guid,
true);
1755 point->SetPlannedSpeed(plan_speed);
1758 etd.Set((time_t)etd_epoch);
1759 if (etd.IsValid()) point->
SetETD(etd);
1766 point->SetShowWaypointRangeRings(range_ring_visible == 1);
1770 point->SetScaMin(scamin);
1771 point->SetScaMax(scamax);
1772 point->SetUseSca(use_scaminmax == 1);
1774 point->SetVisible(visibility == 1);
1775 point->SetNameShown(viz_name == 1);
1776 point->SetShared(shared == 1);
1779 if (point_created_at.size()) {
1783 std::istringstream ss(point_created_at);
1784 ss >> std::get_time(&tm,
"%Y-%m-%d %H:%M:%S");
1785 time_t epoch_time = mktime(&tm);
1790 const char* sqlh = R
"(
1791 SELECT guid, html_link, html_description, html_type
1792 FROM routepoint_html_links
1793 WHERE routepoint_guid = ?
1794 ORDER BY html_type ASC
1799 if (sqlite3_prepare_v2(m_db, sqlh, -1, &stmt,
nullptr) == SQLITE_OK) {
1800 sqlite3_bind_text(stmt, 1, point->
m_GUID.ToStdString().c_str(), -1,
1803 while (sqlite3_step(stmt) == SQLITE_ROW) {
1804 std::string link_guid =
1805 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 0));
1806 std::string link_link =
1807 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 1));
1808 std::string link_description =
1809 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 2));
1810 std::string link_type =
1811 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 3));
1814 h->DescrText = link_description;
1815 h->Link = link_link;
1816 h->LType = link_type;
1823 route->AddPoint(point);
1825 sqlite3_finalize(stmtp);
1826 if (errcode != SQLITE_DONE) {
1827 ReportError(
"LoadAllRoutes-A:step");
1834 const char* sqlh = R
"(
1835 SELECT guid, html_link, html_description, html_type
1836 FROM route_html_links
1837 WHERE route_guid = ?
1838 ORDER BY html_type ASC
1843 if (sqlite3_prepare_v2(m_db, sqlh, -1, &stmt,
nullptr) == SQLITE_OK) {
1844 sqlite3_bind_text(stmt, 1, route->
m_GUID.ToStdString().c_str(), -1,
1847 int errcode2 = SQLITE_OK;
1848 while ((errcode2 = sqlite3_step(stmt)) == SQLITE_ROW) {
1849 std::string link_guid =
1850 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 0));
1851 std::string link_link =
1852 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 1));
1853 std::string link_description =
1854 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 2));
1855 std::string link_type =
1856 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 3));
1859 h->DescrText = link_description;
1860 h->Link = link_link;
1861 h->LType = link_type;
1865 if (errcode != SQLITE_DONE) {
1866 ReportError(
"LoadAllRoutes-B:step");
1870 sqlite3_finalize(stmt);
1873 ReportError(
"LoadAllRoutes-B:prepare");
1884 if (errcode0 != SQLITE_DONE) {
1885 ReportError(
"LoadAllRoutes-C:step");
1892bool NavObj_dB::LoadAllPoints() {
1907 "p.RangeRingsNumber, "
1908 "p.RangeRingsStep, "
1909 "p.RangeRingsStepUnits, "
1910 "p.RangeRingsVisible, "
1911 "p.RangeRingsColour, "
1920 "FROM routepoints p ";
1924 sqlite3_stmt* stmtp;
1925 if (sqlite3_prepare_v2(m_db, sqlp, -1, &stmtp,
nullptr) != SQLITE_OK) {
1929 while (sqlite3_step(stmtp) == SQLITE_ROW) {
1932 std::string point_guid =
1933 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1934 double latitude = sqlite3_column_double(stmtp, col++);
1935 double longitude = sqlite3_column_double(stmtp, col++);
1936 std::string symbol =
1937 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1939 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1940 std::string description =
1941 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1942 std::string tide_station =
1943 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1944 double plan_speed = sqlite3_column_double(stmtp, col++);
1945 time_t etd = sqlite3_column_int(stmtp, col++);
1947 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1948 std::string point_time_string =
1949 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1950 double arrival_radius = sqlite3_column_double(stmtp, col++);
1952 int range_ring_number = sqlite3_column_int(stmtp, col++);
1953 double range_ring_step = sqlite3_column_double(stmtp, col++);
1954 int range_ring_units = sqlite3_column_int(stmtp, col++);
1955 int range_ring_visible = sqlite3_column_int(stmtp, col++);
1956 std::string range_ring_color =
1957 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1959 int scamin = sqlite3_column_int(stmtp, col++);
1960 int scamax = sqlite3_column_int(stmtp, col++);
1961 int use_scaminmax = sqlite3_column_int(stmtp, col++);
1963 int visibility = sqlite3_column_int(stmtp, col++);
1964 int viz_name = sqlite3_column_int(stmtp, col++);
1965 int shared = sqlite3_column_int(stmtp, col++);
1966 int isolated = sqlite3_column_int(stmtp, col++);
1967 std::string point_created_at =
1968 reinterpret_cast<const char*
>(sqlite3_column_text(stmtp, col++));
1972 new RoutePoint(latitude, longitude, symbol, name, point_guid,
false);
1976 point->SetPlannedSpeed(plan_speed);
1982 point->SetShowWaypointRangeRings(range_ring_visible == 1);
1986 point->SetScaMin(scamin);
1987 point->SetScaMax(scamax);
1988 point->SetUseSca(use_scaminmax == 1);
1990 point->SetVisible(visibility == 1);
1991 point->SetNameShown(viz_name == 1);
1992 point->SetShared(shared == 1);
1995 if (point_created_at.size()) {
1999 std::istringstream ss(point_created_at);
2000 ss >> std::get_time(&tm,
"%Y-%m-%d %H:%M:%S");
2001 time_t epoch_time = mktime(&tm);
2007 pSelect->AddSelectableRoutePoint(point->m_lat, point->m_lon, point);
2010 sqlite3_finalize(stmtp);
2014 const char* sqlh = R
"(
2015 SELECT guid, html_link, html_description, html_type
2016 FROM routepoint_html_links
2017 WHERE routepoint_guid = ?
2018 ORDER BY html_type ASC
2023 if (sqlite3_prepare_v2(m_db, sqlh, -1, &stmt,
nullptr) == SQLITE_OK) {
2024 sqlite3_bind_text(stmt, 1, point->
m_GUID.ToStdString().c_str(), -1,
2027 while (sqlite3_step(stmt) == SQLITE_ROW) {
2028 std::string link_guid =
2029 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 0));
2030 std::string link_link =
2031 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 1));
2032 std::string link_description =
2033 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 2));
2034 std::string link_type =
2035 reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 3));
2038 h->DescrText = link_description;
2039 h->Link = link_link;
2040 h->LType = link_type;
2045 sqlite3_finalize(stmt);
2053bool NavObj_dB::InsertRoutePoint(
RoutePoint* point) {
2057 if (!RoutePointExists(m_db, point->
m_GUID.ToStdString())) {
2060 wxString::Format(
"INSERT INTO routepoints (guid) VALUES ('%s')",
2061 point->
m_GUID.ToStdString().c_str());
2062 if (!executeSQL(m_db, sql)) {
2067 UpdateDBRoutePointAttributes(point);
2071 if (NbrOfLinks > 0) {
2073 for (
auto it = list->begin(); it != list->end(); ++it) {
2075 if (!RoutePointHtmlLinkExists(m_db, link->GUID)) {
2076 InsertRoutePointHTML(m_db, point->
m_GUID.ToStdString(), link->GUID,
2077 link->DescrText.ToStdString(),
2078 link->Link.ToStdString(),
2079 link->LType.ToStdString());
2087bool NavObj_dB::DeleteRoutePoint(
RoutePoint* point) {
2088 if (m_importing)
return false;
2089 if (!point)
return false;
2091 std::string point_guid = point->
m_GUID.ToStdString();
2095 const char* sql =
"DELETE FROM routepoints WHERE guid = ?";
2098 if (sqlite3_prepare_v2(m_db, sql, -1, &stmt,
nullptr) == SQLITE_OK) {
2099 sqlite3_bind_text(stmt, 1, point_guid.c_str(), -1, SQLITE_TRANSIENT);
2100 if (sqlite3_step(stmt) != SQLITE_DONE) {
2101 ReportError(
"DeleteRoutePoint:step");
2102 sqlite3_finalize(stmt);
2106 sqlite3_finalize(stmt);
2113bool NavObj_dB::UpdateRoutePoint(
RoutePoint* point) {
2114 if (m_importing)
return false;
2115 if (!RoutePointExists(m_db, point->
m_GUID.ToStdString()))
return false;
2116 UpdateDBRoutePointAttributes(point);
2120bool NavObj_dB::Backup(wxString fileName) {
2121 sqlite3_backup* pBackup;
2122 sqlite3* backupDatabase;
2124 if (sqlite3_open(fileName.c_str(), &backupDatabase) == SQLITE_OK) {
2125 pBackup = sqlite3_backup_init(backupDatabase,
"main", m_db,
"main");
2127 int result = sqlite3_backup_step(pBackup, -1);
2128 if ((result == SQLITE_OK) || (result == SQLITE_DONE)) {
2129 if (sqlite3_backup_finish(pBackup) == SQLITE_OK) {
2130 sqlite3_close_v2(backupDatabase);
2136 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.
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.