Currently, I am working on node with express library using mysql connector. So most likely I am working with string literal SQL Query.
Now, I have implemented a design wherein I do have a constant formula on query.
Table Structure:
╔═══╦═════════════════════╦═══════════════╗
║ ║ Field Name ║ Type ║
╠═══╬═════════════════════╬═══════════════╣
║ 1 ║ groupLevel ║ varchar ║
║ 2 ║ insideBranchAccess ║ boolean ║
║ 3 ║ outsideBranchAccess ║ boolean ║
║ 4 ║ ipadFunctionID ║ varchar ║
║ 5 ║ createdBy ║ varchar ║
║ 6 ║ approvedBy ║ varchar ║
╚═══╩═════════════════════╩═══════════════╝
SQL Query:
INSERT INTO iPadFunctionAccess
(groupLevel, insideBranchAccess, outsideBranchAccess, ipadFunctionID, createdBy, approvedBy)
VALUES
?
ON DUPLICATE KEY UPDATE
insideBranchAccess = VALUES(insideBranchAccess),
outsideBranchAccess = VALUES(outsideBranchAccess),
updatedBy = VALUES(createdBy);
So the ? is where I insert my data. The sample data that I insert is this
(5, false, true, 'IIU.A05', 'SYSTEM', 'SYSTEM')
The sample data that I am getting from the application is this.
[
{
"id": "DEP.A03",
"groupLevel": "5",
"insideBranchAccess": true,
"outsideBranchAccess": true
},
{
"id": "DEP.S02",
"groupLevel": "5",
"insideBranchAccess": true
},
{
"id": "DEP.S04",
"groupLevel": "5",
"outsideBranchAccess": true
}
]
So as you can see, the insideBranchAccess and outsideBranchAccess is kinda optional.
I have seen this answer so I have come up with this kind of query. So incase that I only got insideBranchAccess my query should be like this
(5, true, outsideBranchAccess, 'IIU.A05', 'SYSTEM', 'SYSTEM')
and vice versa.
However, when I do this, the outsideBranchAccess always return to false even though the previous data from the database is true. Any thoughts?
MySQL Fiddle: HERE

