From 8c9067ed4a9b786882a3247221de4bbb20847693 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Mon, 15 Aug 2022 16:28:06 -0700 Subject: [PATCH] SQLite fixes and addition to config.json schema. --- db.js | 44 ++++++++++++++++++++++------------ meshcentral-config-schema.json | 13 +++++----- meshuser.js | 2 +- sample-config-advanced.json | 1 + 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/db.js b/db.js index 7941c983..56e42eab 100644 --- a/db.js +++ b/db.js @@ -1190,12 +1190,20 @@ module.exports.CreateDB = function (parent, func) { } // Query the database - function sqlDbQuery(query, args, func) { + function sqlDbQuery(query, args, func, debug) { if (obj.databaseType == 8) { // SQLite if (args == null) { args = []; } obj.file.all(query, args, function (err, docs) { if (err != null) { console.log(query, args, err, docs); } - if (docs != null) { for (var i in docs) { if (typeof docs[i].doc == 'string') { docs[i] = JSON.parse(docs[i].doc); } } } + if (docs != null) { + for (var i in docs) { + if (typeof docs[i].doc == 'string') { + try { docs[i] = JSON.parse(docs[i].doc); } catch (ex) { + console.log(query, args, docs[i]); + } + } + } + } if (func) { func(err, docs); } }); } else if (obj.databaseType == 4) { // MariaDB @@ -1323,18 +1331,18 @@ module.exports.CreateDB = function (parent, func) { }; obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, func) { if (id && (id != '')) { - sqlDbQuery('SELECT doc FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra IN ($4))', [id, type, domain, meshes], function (err, docs) { + sqlDbQuery('SELECT doc FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra IN (' + dbMergeSqlArray(meshes) + '))', [id, type, domain], function (err, docs) { if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } } func(err, performTypedRecordDecrypt(docs)); }); } else { if (extrasids == null) { - sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra IN ($3))', [type, domain, meshes], function (err, docs) { + sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra IN (' + dbMergeSqlArray(meshes) + '))', [type, domain], function (err, docs) { if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } } func(err, performTypedRecordDecrypt(docs)); - }, true); + }); } else { - sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND ((extra IN ($3)) OR (id IN ($4)))', [type, domain, meshes, extrasids], function (err, docs) { + sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND ((extra IN (' + dbMergeSqlArray(meshes) + ')) OR (id IN (' + dbMergeSqlArray(extrasids) + ')))', [type, domain], function (err, docs) { if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } } func(err, performTypedRecordDecrypt(docs)); }); @@ -1343,12 +1351,12 @@ module.exports.CreateDB = function (parent, func) { }; obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) { if (id && (id != '')) { - sqlDbQuery('SELECT doc FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra IN ($4))', [id, type, domain, nodes], function (err, docs) { + sqlDbQuery('SELECT doc FROM main WHERE (id = $1) AND (type = $2) AND (domain = $3) AND (extra IN (' + dbMergeSqlArray(nodes) + '))', [id, type, domain], function (err, docs) { if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } } func(err, performTypedRecordDecrypt(docs)); }); } else { - sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra IN ($3))', [type, domain, nodes], function (err, docs) { + sqlDbQuery('SELECT doc FROM main WHERE (type = $1) AND (domain = $2) AND (extra IN (' + dbMergeSqlArray(nodes) + '))', [type, domain], function (err, docs) { if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } } func(err, performTypedRecordDecrypt(docs)); }); @@ -1361,7 +1369,7 @@ module.exports.CreateDB = function (parent, func) { }); } obj.GetAllIdsOfType = function (ids, domain, type, func) { - sqlDbQuery('SELECT doc FROM main WHERE (id IN ($1)) AND domain = $2 AND type = $3', [ids, domain, type], function (err, docs) { + sqlDbQuery('SELECT doc FROM main WHERE (id IN (' + dbMergeSqlArray(ids) + ')) AND domain = $1 AND type = $2', [domain, type], function (err, docs) { if (docs != null) { for (var i in docs) { delete docs[i].type; if (docs[i].links != null) { docs[i] = common.unEscapeLinksFieldName(docs[i]); } } } func(err, performTypedRecordDecrypt(docs)); }); @@ -1415,35 +1423,35 @@ module.exports.CreateDB = function (parent, func) { if (ids.indexOf('*') >= 0) { sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC', [domain], func); } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN ($2))) GROUP BY id ORDER BY time DESC', [domain, ids], func); + sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC', [domain], func); } }; obj.GetEventsWithLimit = function (ids, domain, limit, func) { if (ids.indexOf('*') >= 0) { sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC LIMIT $2', [domain, limit], func); } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN ($2))) GROUP BY id ORDER BY time DESC LIMIT $3', [domain, ids, limit], func); + sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC LIMIT $2', [domain, limit], func); } }; obj.GetUserEvents = function (ids, domain, userid, func) { if (ids.indexOf('*') >= 0) { sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC', [domain, userid], func); } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN ($3))) GROUP BY id ORDER BY time DESC', [domain, userid, ids], func); + sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC', [domain, userid], func); } }; obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) { if (ids.indexOf('*') >= 0) { sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC LIMIT $3', [domain, userid, limit], func); } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN ($3))) GROUP BY id ORDER BY time DESC LIMIT $4', [domain, userid, ids, limit], func); + sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC LIMIT $3', [domain, userid, limit], func); } }; obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) { if (ids.indexOf('*') >= 0) { sqlDbQuery('SELECT doc FROM events WHERE ((domain = $1) AND (time BETWEEN $2 AND $3)) ORDER BY time', [domain, start, end], func); } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE ((domain = $1) AND (target IN ($2)) AND (time BETWEEN $3 AND $4)) GROUP BY id ORDER BY time', [domain, ids, start, end], func); + sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE ((domain = $1) AND (target IN (' + dbMergeSqlArray(ids) + ')) AND (time BETWEEN $2 AND $3)) GROUP BY id ORDER BY time', [domain, start, end], func); } }; //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO @@ -1456,7 +1464,7 @@ module.exports.CreateDB = function (parent, func) { // Database actions on the power collection obj.getAllPower = function (func) { sqlDbQuery('SELECT doc FROM power', null, func); }; - obj.storePowerEvent = function (event, multiServer, func) { obj.dbCounters.powerSet++; if (multiServer != null) { event.server = multiServer.serverid; } sqlDbQuery('INSERT INTO power VALUES (NULL, $1, $2, $3)', [event.time, event.nodeid ? event.nodeid : null, event], func); }; + obj.storePowerEvent = function (event, multiServer, func) { obj.dbCounters.powerSet++; if (multiServer != null) { event.server = multiServer.serverid; } sqlDbQuery('INSERT INTO power VALUES (NULL, $1, $2, $3)', [event.time, event.nodeid ? event.nodeid : null, JSON.stringify(event)], func); }; obj.getPowerTimeline = function (nodeid, func) { sqlDbQuery('SELECT doc FROM power WHERE ((nodeid = $1) OR (nodeid = \'*\')) ORDER BY time ASC', [nodeid], func); }; obj.removeAllPowerEvents = function () { sqlDbQuery('DELETE FROM power', null, function (err, docs) { }); }; obj.removeAllPowerEventsForNode = function (nodeid) { if (nodeid == null) return; sqlDbQuery('DELETE FROM power WHERE nodeid = $1', [nodeid], function (err, docs) { }); }; @@ -3280,5 +3288,11 @@ module.exports.CreateDB = function (parent, func) { parent.DispatchEvent(['*', usergroup.ugrpid], obj, usergroup); } + function dbMergeSqlArray(arr) { + var x = ''; + for (var i in arr) { if (x != '') { x += ','; } x += '"' + arr[i] + '"'; } + return x; + } + return obj; }; diff --git a/meshcentral-config-schema.json b/meshcentral-config-schema.json index 8e650d31..9a12ff20 100644 --- a/meshcentral-config-schema.json +++ b/meshcentral-config-schema.json @@ -35,13 +35,7 @@ } } }, - "acebase": { - "type": "object", - "description": "Add this section to enable AceBase database support, this is a local database system much like NeDB.", - "properties": { - "sponsor": { "type": "boolean", "default": false, "description": "Set true to remove the AceBase banner on startup." }, - } - }, + "sqlite3": { "type": "boolean", "default": false, "description": "Set true to use SQLite3 as a local MeshCentral database." }, "mySQL": { "type": "object", "description": "Add this section to connect MeshCentral to a MySQL database instance.", @@ -75,6 +69,11 @@ "database": { "type": "string", "default": "meshcentral", "description": "Name of PostgreSQL database used" } } }, + "acebase": { + "type": "object", + "description": "Add this section to enable AceBase database support, this is a local database system much like NeDB.", + "properties": { "sponsor": { "type": "boolean", "default": false, "description": "Set true to remove the AceBase banner on startup." } } + }, "WANonly": { "type": "boolean", "default": false, "description": "When enabled, only MeshCentral WAN features are enabled and agents will connect to the server using a well known DNS name." }, "LANonly": { "type": "boolean", "default": false, "description": "When enabled, only MeshCentral LAN features are enabled and agents will find the server using multicast LAN packets." }, "maintenanceMode": { "type": "boolean", "default": false, "description": "When enabled the server is in maintenance mode, only administrators can login. Use the maintenance command in server console to change." }, diff --git a/meshuser.js b/meshuser.js index 9fb3b686..96db7aad 100644 --- a/meshuser.js +++ b/meshuser.js @@ -696,7 +696,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use // Request a list of all nodes db.GetAllTypeNoTypeFieldMeshFiltered(links, extraids, domain.id, 'node', command.id, function (err, docs) { - //console.log(err, docs); + //console.log(err, docs, links, extraids, domain.id, 'node', command.id); if (docs == null) { docs = []; } parent.common.unEscapeAllLinksFieldName(docs); diff --git a/sample-config-advanced.json b/sample-config-advanced.json index e694d275..f1dbf89e 100644 --- a/sample-config-advanced.json +++ b/sample-config-advanced.json @@ -3,6 +3,7 @@ "__comment__": "This is a sample configuration file, all values and sections that start with underscore (_) are ignored. Edit a section and remove the _ in front of the name. Refer to the user's guide for details.", "settings": { "_cert": "myserver.mydomain.com", + "_SQLite3": true, "_acebase": { "_sponsor": true }, "_mongoDb": "mongodb://127.0.0.1:27017", "_mongoDbName": "meshcentral",