Docs/Prog/Manual/ApplicationLibraries/lib825mssql: appdatabase.cpp

File appdatabase.cpp, 8.5 KB (added by Don Wilson, 3 days ago)
Line 
1/*
2 * appmysql.cpp
3 *
4 * Created on: Oct 14, 2025
5 * Author: user
6 */
7
8#include "sqlite3.h"
9
10#include "cardinal825.h"
11
12#include "../appinfo.h"
13#include "../apphelpers.h"
14#include "appdatabase.h"
15
16using namespace std;
17
18enum { DB_CFG_MODE = 21,
19 DB_CFG_SERVER,
20 DB_CFG_SERVER2,
21 DB_CFG_USER,
22 DB_CFG_PASSWORD,
23 DB_CFG_DATABASE
24};
25
26void CAppDatabaseCfg::UpdateConfigForm(CForm *pForm)
27{
28 AppDbType mode = appDataSQLite;
29
30 CFormInput *pMode = dynamic_cast<CFormInput*>(pForm->GetItemByIDVal(DB_CFG_MODE));
31 if(pMode != nullptr) {
32 mode = static_cast<AppDbType>(pMode->GetSelItem());
33 }
34
35 if(mode == appDataMySQL || mode == appDataMSSQL) {
36 pForm->ShowItem(DB_CFG_SERVER);
37 pForm->ShowItem(DB_CFG_SERVER2);
38 pForm->ShowItem(DB_CFG_USER);
39 pForm->ShowItem(DB_CFG_PASSWORD);
40 pForm->ShowItem(DB_CFG_DATABASE);
41 } else {
42 pForm->HideItem(DB_CFG_SERVER);
43 pForm->HideItem(DB_CFG_SERVER2);
44 pForm->HideItem(DB_CFG_USER);
45 pForm->HideItem(DB_CFG_PASSWORD);
46 pForm->HideItem(DB_CFG_DATABASE);
47 }
48}
49
50EVENT(ConfigMySQLShow)
51{
52 ClearScreenAndShowTitle(pForm, "Database Configuration");
53 return 0;
54}
55
56EVENT(DatabaseModeChg)
57{
58 CAppDatabaseCfg* p = static_cast<CAppDatabaseCfg*>(pForm->GetUserStruct());
59 if(p != nullptr) {
60 p->UpdateConfigForm(pForm);
61 }
62
63 return 0;
64}
65
66int show_db_callback(char* response, void* data)
67{
68 DEBUG_MSG2("Res [%s]\n", response);
69 vector<string>* lines = (vector<string>*)data;
70 lines->push_back(response);
71
72 return 0;
73}
74
75int CAppDatabaseCfg::Config(void)
76{
77 int n = 0;
78
79 uint8 mode = static_cast<uint8>(dbType_);
80 string server = server_;
81 string server2 = server2_;
82 string user = user_;
83 string password = password_;
84 string database = database_;
85#if ARM64
86
87 CFormRect rectInpBtn;
88 CFormButton btnOk;
89
90 while(1) {
91 FORM_INIT(MySQL, EventConfigMySQLShow, NULL, NULL, NULL, /*nFormFlgShowHelp |*/ nFormFlgAutoPresentPopup | nFormFlgShowPopupTabKeypad);
92
93 SetupInputFormToUseFontSet(formMySQL, rectInpBtn, 0.40, 1.2);
94 CFormAdvanceY advY;
95 advY.SetBasedOnFont(formFontSet.GetFont(fontIndexPrompt), 1.4);
96
97 FORM_ADD_INPUT2(MySQL, Mode, rectInpBtn, rectKeypad, "Mode: ", 14, 0, 2, EventDatabaseModeChg, mode, FORM_UINT8_SEL | FORM_FLAG_POPUP_LEFT_PROMPT, "SQLite|MySQL|SQL Server", NULL, DB_CFG_MODE);
98 rectInpBtn += advY;
99 FORM_ADD_INPUT2(MySQL, Server, rectInpBtn, rectKeypad, "Server: ", 40, 0, 0, NULL, server, FORM_STR | FORM_FLAG_POPUP_LEFT_PROMPT | FORM_FLAG_DISABLE, NULL, NULL, DB_CFG_SERVER);
100 rectInpBtn += advY;
101 FORM_ADD_INPUT2(MySQL, Server2, rectInpBtn, rectKeypad, " ", 40, 0, 0, NULL, server2, FORM_STR | FORM_FLAG_POPUP_LEFT_PROMPT | FORM_FLAG_DISABLE, NULL, NULL, DB_CFG_SERVER2);
102 rectInpBtn += advY;
103 FORM_ADD_INPUT2(MySQL, User, rectInpBtn, rectKeypad, "User: ", 40, 0, 0, NULL, user, FORM_STR | FORM_FLAG_POPUP_LEFT_PROMPT | FORM_FLAG_DISABLE, NULL, NULL, DB_CFG_USER);
104 rectInpBtn += advY;
105 FORM_ADD_INPUT2(MySQL, Password, rectInpBtn, rectKeypad, "Password: ", 40, 0, 0, NULL, password, FORM_STR | FORM_FLAG_POPUP_LEFT_PROMPT | FORM_FLAG_DISABLE, NULL, NULL, DB_CFG_PASSWORD);
106 rectInpBtn += advY;
107 FORM_ADD_INPUT2(MySQL, Database, rectInpBtn, rectKeypad, "Database: ", 40, 0, 0, NULL, database, FORM_STR | FORM_FLAG_POPUP_LEFT_PROMPT | FORM_FLAG_DISABLE, NULL, NULL, DB_CFG_DATABASE);
108
109 formMySQL.LeftAlignPromptInputs(inpMode, inpDatabase);
110
111 AddFormOkButton(formMySQL, btnOk);
112
113 formMySQL.SetUserStruct(this);
114
115 FORM_SHOW(MySQL);
116 UpdateConfigForm(&formMySQL);
117
118 n = FORM_RUN(MySQL);
119 FORM_HIDE(MySQL);
120
121 if(n == FORM_RESULT_ESC || static_cast<AppDbType>(mode) == appDataSQLite) {
122 break;
123 }
124 if(password.length() < 3) {
125 DrawMessageBox("Password too short", 2);
126 } else {
127 break;
128 }
129
130 }
131
132 if (n != FORM_RESULT_ESC) {
133
134 if(static_cast<AppDbType>(mode) != dbType_ || server_ != server || server2_ != server2 || user_ != user || password_ != password || database_ != database) {
135 if (DrawConfirmBox("Are you sure you want to change database settings?", LANG(STR_YES), LANG(STR_NO))) {
136 dbType_ = static_cast<AppDbType>(mode);
137 server_ = server;
138 server2_ = server2;
139 user_ = user;
140 password_ = password;
141 database_ = database;
142
143 chg_ = true;
144
145 WriteJSON();
146
147 if(dbType_ == appDataMySQL) {
148 if(server_ == "127.0.0.1" || server_ == "localhost") {
149
150 bool checkForDatabase = false;
151 if(IsServiceActive("mysqld") == false) {
152 if (DrawConfirmBox("Enable local MySQL Server?", LANG(STR_YES), LANG(STR_NO))) {
153 SetService("mysqld", true);
154 SleepMilliseconds(1000);
155 DoShellCmd("sudo enablemysql");
156 checkForDatabase = true;
157 }
158
159 } else {
160 checkForDatabase = true;
161 }
162
163 if(checkForDatabase) {
164 // Check if database exists
165 vector<string> lines;
166 bool found = false;
167 const char* cmd = "sudo mysql -e \"show databases;\"";
168 DoShellCmd(cmd, show_db_callback, &lines);
169 for(auto& line : lines) {
170 DEBUG_MSG("response [%s]\n", line.c_str());
171 if(line == database_) {
172 DEBUG_MSG("%s found\n", database_.c_str());
173 found = true;
174 }
175 }
176 if(found == false) {
177 if (DrawConfirmBox("Create database?", LANG(STR_YES), LANG(STR_NO))) {
178 string cmd = "sudo createdb " + database_;
179 DoShellCmd(cmd.c_str());
180 SleepMilliseconds(200);
181 cmd = "sudo createdbuser " + database_ + " " + user_ + " \"" + password_ + "\"";
182 DoShellCmd(cmd.c_str());
183 }
184 }
185 }
186 }
187 }
188
189
190 DrawMessageBox("Restart app to apply database changes", 4);
191 }
192 }
193 }
194
195#else
196
197#endif
198
199 return n;
200}
201
202EVENT(ConfigDBFirstTimeShow)
203{
204 ClearScreenAndShowTitle(pForm, "Database first-time configuration");
205
206 CFormRect rect;
207 CFormAdvanceY advY;
208
209 rect.SetTopAndHeightBasedOnFont(pForm->GetFontSet()->GetFont(fontIndexSmallPrompt), 2.4, 1.0);
210 rect.SetLeftAndWidthBasedOnDspArea(pForm->GetDspArea(), 0.03, 0.92);
211
212 advY.SetBasedOnFont(pForm->GetFontSet()->GetFont(fontIndexSmallPrompt), 1.0);
213
214 const char* lines[] = { "Select \"SQLite\" for legacy 825 style database.", "", "Select \"Local MySQL\" to be able to view reports of the", "data on this indicator's web page.",
215 "", "Select \"Advanced\" for a custom configuration such as using", "a MySQL database on another server."
216 };
217 int n;
218 SetCurColor(COLOR_REQUEST);
219
220 for(n = 0; n < ARRAY_ELEMS(lines); n++) {
221 if(lines[n][0] != '\0') {
222 pForm->GetFontSet()->GetFont(fontIndexSmallPrompt).DisplayTextCairo(rect, lines[n]);
223 }
224 rect += advY;
225 }
226
227 return 0;
228}
229
230enum { FIRSTTIME_DB_SQLITE = 1, FIRSTTIME_DB_LOCAL_MYSQL, FIRSTTIME_DB_ADVANCED };
231
232int CAppDatabaseCfg::FirstTimePrompt(void)
233{
234 CFormRect rect;
235
236 FORM_INIT(FirstTime, EventConfigDBFirstTimeShow, NULL, NULL, NULL, 0);
237
238 SetupMenuFormToUseFontSet(formFirstTime, rect, MENU_BTN_WIDE_WIDTH);
239
240 rect.SetTopBasedOnFont(formFontSet.GetFont(fontIndexPrompt), 9.0);
241
242 FORM_ADD_BUTTON2(FirstTime, SQLite, rect, "SQLite", 'S', nullptr, FORM_BUTTON_SHADOW | FORM_FLAG_BTN_RETURN_ID, "", FIRSTTIME_DB_SQLITE);
243 rect += buttonAdvanceY;
244 FORM_ADD_BUTTON2(FirstTime, MySQL, rect, "Local MySQL - Allows web page reporting", 'M', nullptr, FORM_BUTTON_SHADOW | FORM_FLAG_BTN_RETURN_ID, "", FIRSTTIME_DB_LOCAL_MYSQL);
245 rect += buttonAdvanceY;
246 FORM_ADD_BUTTON2(FirstTime, Advanced, rect, "Advanced", 'M', nullptr, FORM_BUTTON_SHADOW | FORM_FLAG_BTN_RETURN_ID, "", FIRSTTIME_DB_ADVANCED);
247
248 FORM_SHOW(FirstTime);
249 int n = FORM_RUN(FirstTime);
250 FORM_HIDE(FirstTime);
251
252 if(n == FIRSTTIME_DB_LOCAL_MYSQL) {
253 DEBUG_MSG("Create MySQL database\n");
254 dbType_ = appDataMySQL;
255 server_ = "127.0.0.1";
256 user_ = MYSQL_DEFAULT_DBUSER;
257 password_ = "81440";
258 database_ = MYSQL_DEFAULT_DB;
259
260 chg_ = true;
261
262 WriteJSON();
263
264 if(IsServiceActive("mysqld") == false) {
265 SetService("mysqld", true);
266 SleepMilliseconds(1000);
267 DoShellCmd("sudo enablemysql");
268 }
269
270 vector<string> lines;
271 bool found = false;
272 const char* cmd = "sudo mysql -e \"show databases;\"";
273 DoShellCmd(cmd, show_db_callback, &lines);
274 for(auto& line : lines) {
275 DEBUG_MSG("response [%s]\n", line.c_str());
276 if(line == database_) {
277 DEBUG_MSG("%s found\n", database_.c_str());
278 found = true;
279 }
280 }
281 if(found == false) {
282 //if (DrawConfirmBox("Create database?", LANG(STR_YES), LANG(STR_NO))) {
283 string cmd = "sudo createdb " + database_;
284 DoShellCmd(cmd.c_str());
285 SleepMilliseconds(200);
286 cmd = "sudo createdbuser " + database_ + " " + user_ + " \"" + password_ + "\"";
287 DoShellCmd(cmd.c_str());
288 //}
289 }
290
291 } else if(n == FIRSTTIME_DB_ADVANCED) {
292 Config();
293 }
294 DEBUG_MSG("Calling WriteJSON [%s]\n", GetJSONFilepath().c_str());
295 WriteJSON();
296
297 return n;
298}