#19-dynamic-table-database #460

Merged
b24johka merged 24 commits from #19-dynamic-table-database into team_1_week_2 2026-04-22 06:53:17 +00:00
Collaborator
  • Updated middlewae to dynamicaly create tables with the structure of the data given
  • Updated roombooking to use this new middleware
  • all other services does currently not work, need to create new issues to fix this
  • Guide on how to use this new middleware is in the README file of middleware PLEASE READ THIS
  • No error handling and test is currently implementded, (readroombookingmockup has outcomented code that i used for testing)
  • to properly test everything we need to do it by using the services/mockup
- Updated middlewae to dynamicaly create tables with the structure of the data given - Updated roombooking to use this new middleware - all other services does currently not work, need to create new issues to fix this - Guide on how to use this new middleware is in the README file of middleware PLEASE READ THIS - No error handling and test is currently implementded, (readroombookingmockup has outcomented code that i used for testing) - to properly test everything we need to do it by using the services/mockup
Trying to control how an SQL-table is created/structured based on the data from the objects it will store inside it by having the SQL-table data types be specified in the object itself and from there having the middleware automatically figure out how to create the associated tables.

If this doesn't prove to be a good solution, I have also made the payloads easier to read in the middleware container's log, as well as display the name+datatype of each of the payload's properties. This should make it easier to get a better grasp of what's actually being sent.
(I also fixed some spelling errors that were irking me)
Managed to solve the issue of sending an array in the payload, so now an array of strings defining the SQL-datatypes(char, varchar, DATETIME, etc.) on each object can be used to structure the SQL-query for our database inserts. Only implemented in the Room Service so far.

It doesn't do anything yet except send an additional piece of data in the room service payload, but we can use this piece of data to "dynamically" construct our SQL-queries in the middleware.

(See the test SQL-query string print-out on line 47-58 in roomBookingApiMockup/Program.cs for an example of how the query string could be assembled in the middleware.)
Create table and insert works as intended
can also get data from database, with some modification in the service
added commented out tests to make shure that delete and update function works in middleware
a24sakma left a comment
Collaborator

Looks good with the dynamic table, it makes life easy for re usability later.

However I added a comment about the better error handling and we will have a meeting for clarifying the side effects of this pr and what needs to be done later.

Don't merge this pr until the update after the meeting.

Looks good with the dynamic table, it makes life easy for re usability later. However I added a comment about the better error handling and we will have a meeting for clarifying the side effects of this pr and what needs to be done later. Don't merge this pr until the update after the meeting.
@ -123,1 +169,4 @@
string tablename = Sanitize(topic);
var jpayload = JsonObject.Parse(payload);
string dirtySqlDataTypes = jpayload[0].ToString();
Collaborator

I would add try catch for InsertMessage, DeleteMessage and updateMessage for better error handling with relevent message like
`
try{
your logic here
}catch(){

Console.WriteLine($"Operation InserMessage from middleware, it failed with error message : {e.Message}");
throw;
}
`

`
try{

your logic here

}catch(){

Console.WriteLine($"Operation DeleteMessage from middleware, it failed with error message : {e.Message}");
throw;
}

`
try{
your logic here

}catch(){

Console.WriteLine($"Operation Update Message from middleware, it failed with error message : {e.Message}");
throw;
}

I would add try catch for InsertMessage, DeleteMessage and updateMessage for better error handling with relevent message like ` try{ your logic here }catch(){ Console.WriteLine($"Operation InserMessage from middleware, it failed with error message : {e.Message}"); throw; } ` ` try{ your logic here }catch(){ Console.WriteLine($"Operation DeleteMessage from middleware, it failed with error message : {e.Message}"); throw; } ` try{ your logic here }catch(){ Console.WriteLine($"Operation Update Message from middleware, it failed with error message : {e.Message}"); throw; }
a24sakma approved these changes 2026-04-21 11:44:03 +00:00
a24sakma left a comment
Collaborator

Done review from me! 👍

Done review from me! 👍
Collaborator

Just a questions for my own curiosity: What is the intent of the UpdateMessage and DeleteMessage? As far as I know, all current mockups only insert into their tables to keep history of values. But what purpose would an update and delete function serve in such a database?

Just a questions for my own curiosity: What is the intent of the UpdateMessage and DeleteMessage? As far as I know, all current mockups only insert into their tables to keep history of values. But what purpose would an update and delete function serve in such a database?
Author
Collaborator

like in roombooking you may want to change the date on an existing booking or delete some bookings.

like in roombooking you may want to change the date on an existing booking or delete some bookings.
Collaborator

So the idea is to simulate the booking process entirely. Got it. Just wondering if this is related to #43, as it only asks for visualization of booked rooms. Now it might be that Andras is a bit ambiguous in his wording and also means to add the ability to add, change and delete bookings. I assume that all mockup data is to be replaced with real data in the future (hopefully) and if that is the case we might put a lot of time on something that essentially will be replaced with the TimeEdit API.

So the idea is to simulate the booking process entirely. Got it. Just wondering if this is related to #43, as it only asks for visualization of booked rooms. Now it might be that Andras is a bit ambiguous in his wording and also means to add the ability to add, change and delete bookings. I assume that all mockup data is to be replaced with real data in the future (hopefully) and if that is the case we might put a lot of time on something that essentially will be replaced with the TimeEdit API.
Removed some old commented-out code and made the middleware log more readable in the console by segmenting each message. (Just prints a "-----------------------" to make each message distinct)
Collaborator

I have spent some time trying to get the tempsensor to work with the new middleware took some time but got it to work.
The problem i had was i tried to use DUBLE(5,2) as a datatype but in the middleware it became an index problem as it saw the comma as the start of a new datatype.
I havent checked it completly as i was only working on getting the tempSensor to work but will try to look through the rest after a break.

I have spent some time trying to get the tempsensor to work with the new middleware took some time but got it to work. The problem i had was i tried to use DUBLE(5,2) as a datatype but in the middleware it became an index problem as it saw the comma as the start of a new datatype. I havent checked it completly as i was only working on getting the tempSensor to work but will try to look through the rest after a break.
Collaborator

@c24danli wrote in #460 (comment):

I have spent some time trying to get the tempsensor to work with the new middleware took some time but got it to work. The problem i had was i tried to use DUBLE(5,2) as a datatype but in the middleware it became an index problem as it saw the comma as the start of a new datatype. I havent checked it completly as i was only working on getting the tempSensor to work but will try to look through the rest after a break.

We had some work earlier that used an array to store the different SQL datatypes, but there are issues with trying to send an array through MQTT as the result is still just a string arriving at the destination.
I suppose changing the split function to use another character than ',' to split the datatype string could solve this issue, but we'll have to discuss which character would work best for this. I suspect a character not used by SQL-queries would be best.

@c24danli wrote in https://git.webug.se/Andras/BoundlessFlowCampus2K/pulls/460#issuecomment-4844: > I have spent some time trying to get the tempsensor to work with the new middleware took some time but got it to work. The problem i had was i tried to use DUBLE(5,2) as a datatype but in the middleware it became an index problem as it saw the comma as the start of a new datatype. I havent checked it completly as i was only working on getting the tempSensor to work but will try to look through the rest after a break. We had some work earlier that used an array to store the different SQL datatypes, but there are issues with trying to send an array through MQTT as the result is still just a string arriving at the destination. I suppose changing the split function to use another character than ',' to split the datatype string could solve this issue, but we'll have to discuss which character would work best for this. I suspect a character not used by SQL-queries would be best.
Collaborator

A bit tierd so might be some mistakes but its more of list to talk about tomorrow

Rewiew

Strong points

  • Makes it a lot easier to querry the db
  • Really good Read me file(was needed)
  • More options for later in terms of delete,uppdate

Fixes/uncertainties

middleware\Program.cs

  • don’t mind it but wasn’t switch statements one of the nono’s that we had in software engenering?
    switch (action)
    {
    case "insert":
    await CreateTable(topic, dataBase, payload);
    await InsertMessage(topic, dataBase, payload);
    break;
    case "update":
    await UpdateMessage(topic, dataBase, payload);
    break;
    case "delete":
    await DeleteMessage(topic, dataBase, payload);
    break;
    }

  • Old code left

    • line 206
      // Old sql string
      string sql = $"INSERT INTO {tablename} (payload) VALUES (@payload)";

    • line 212
      cmd.Parameters.AddWithValue("@payload", payload);

    • line 243
      cmd.Parameters.AddWithValue("@payload", payload);

    • line 292

      cmd.Parameters.AddWithValue("@payload", payload);

  • Safety
    not safe for sql injections not a big deal for the data from mockup as we control it, not sure how it will work in parking/bookingroom but if the user can input values say regnumber for car harmful sql injections could be a thing.
    example code:
    secondhalfsql += $"'{jpayload[i + 2]}'";
    if example regnr is inserted by the user ABC123'; DROP TABLE t_sensors_... could be a thing. Probably could be handled in frontend but should it?

  • Uppdate
    Later problem but updates becomes a bit more complex as we will need more data then normally, normally primarykey, tablename and column to chage with value is needed but now we will need sqlDataTypes with matching amount to the columns that are changed or send a complete row.

  • Redability/
    because it uses index/positions instead of names it can become hard to understand intuativly ex:
    string secondhalfsql = $" WHERE {jpayload[1].GetPropertyName()} = '{jpayload[1]}';"
    Or when looping through “Jpayload” could not

foreach (var property in jpayload) 
{ string columnName = property.Key; 
var value = property.Value;

be used instead of
for (int i = 0; i < sqlDataTypes.Length; i++)
As its an Json object with keys and values?
If it works then the problem I had with inserting DECIMAL(5,2) would not be a problem as there would be no need for the string.Split(",").

A bit tierd so might be some mistakes but its more of list to talk about tomorrow ### Rewiew ### Strong points - Makes it a lot easier to querry the db - Really good Read me file(was needed) - More options for later in terms of delete,uppdate ### Fixes/uncertainties _middleware\Program.cs_ - don’t mind it but wasn’t switch statements one of the nono’s that we had in software engenering? switch (action) { case "insert": await CreateTable(topic, dataBase, payload); await InsertMessage(topic, dataBase, payload); break; case "update": await UpdateMessage(topic, dataBase, payload); break; case "delete": await DeleteMessage(topic, dataBase, payload); break; } - Old code left - line 206 ` // Old sql string` `string sql = $"INSERT INTO {tablename} (payload) VALUES (@payload)";` - line 212 ` cmd.Parameters.AddWithValue("@payload", payload);` - line 243 `cmd.Parameters.AddWithValue("@payload", payload);` - line 292 `cmd.Parameters.AddWithValue("@payload", payload);` - Safety not safe for sql injections not a big deal for the data from mockup as we control it, not sure how it will work in parking/bookingroom but if the user can input values say regnumber for car harmful sql injections could be a thing. example code: `secondhalfsql += $"'{jpayload[i + 2]}'";` if example regnr is inserted by the user `ABC123'; DROP TABLE t_sensors_...` could be a thing. Probably could be handled in frontend but should it? - Uppdate Later problem but updates becomes a bit more complex as we will need more data then normally, normally primarykey, tablename and column to chage with value is needed but now we will need sqlDataTypes with matching amount to the columns that are changed or send a complete row. - Redability/ because it uses index/positions instead of names it can become hard to understand intuativly ex: ` string secondhalfsql = $" WHERE {jpayload[1].GetPropertyName()} = '{jpayload[1]}';"` Or when looping through “Jpayload” could not ``` foreach (var property in jpayload) { string columnName = property.Key; var value = property.Value; ``` be used instead of `for (int i = 0; i < sqlDataTypes.Length; i++)` As its an Json object with keys and values? If it works then the problem I had with inserting DECIMAL(5,2) would not be a problem as there would be no need for the `string.Split(",").`
added sql_datatypers and id to fit middleware
Collaborator

Very detailed review, this will be merged into the team branch.

Very detailed review, this will be merged into the team branch.
b24johka merged commit 98a307ff2c into team_1_week_2 2026-04-22 06:53:17 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
6 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Andras/BoundlessFlowCampus2K!460
No description provided.