Many changes towards simulation of JLT and once again the evaluation of the Highspeed data from Lab experiments 2024
This commit is contained in:
@@ -5,7 +5,7 @@ classdef DBHandler < handle
|
||||
|
||||
properties
|
||||
conn % Database connection object
|
||||
pathToDB % Path to the SQLite database
|
||||
dataBase % Path to the SQLite database
|
||||
tableNames % Cell array containing names of all tables in the database
|
||||
tables = struct(); % Structure containing MATLAB tables for each database table
|
||||
distinctValues
|
||||
@@ -21,7 +21,7 @@ classdef DBHandler < handle
|
||||
% obj = DBHandler('pathToDB', 'path/to/database.db');
|
||||
|
||||
arguments
|
||||
options.pathToDB = ""; % Default value for pathToDB if not provided
|
||||
options.dataBase = ""; % Default value for pathToDB if not provided
|
||||
options.type = "mysql";
|
||||
end
|
||||
|
||||
@@ -37,12 +37,17 @@ classdef DBHandler < handle
|
||||
try
|
||||
|
||||
if options.type == "sqlite"
|
||||
obj.conn = sqlite(obj.pathToDB);
|
||||
|
||||
obj.conn = sqlite(obj.dataBase);
|
||||
|
||||
elseif options.type == "mysql"
|
||||
datasource = "jdbc:mysql://134.245.243.254:3306/labor";
|
||||
|
||||
% datasource = "jdbc:mysql://134.245.243.254:3306/labor";
|
||||
|
||||
|
||||
|
||||
obj.conn = database( ...
|
||||
"labor", ... % Database name
|
||||
string(obj.dataBase), ... % Database name
|
||||
"silas", ... % Username
|
||||
"silas", ... % Password (or getSecret)
|
||||
"Vendor", "MySQL", ...
|
||||
@@ -68,8 +73,10 @@ classdef DBHandler < handle
|
||||
|
||||
function obj = refresh(obj)
|
||||
% Get table names and the first rows of each table to understand the structure
|
||||
warning off
|
||||
obj.getTableNames();
|
||||
obj.getTables();
|
||||
warning on
|
||||
% obj.getDistinctValues();
|
||||
end
|
||||
|
||||
@@ -201,20 +208,24 @@ classdef DBHandler < handle
|
||||
if isstruct(newRow)
|
||||
fields = fieldnames(newRow);
|
||||
|
||||
% Handle empty fields
|
||||
emptyFields = structfun(@isempty, newRow);
|
||||
if sum(emptyFields) > 0
|
||||
emptyFieldNames = fields(emptyFields);
|
||||
for idx = 1:numel(emptyFieldNames)
|
||||
newRow.(emptyFieldNames{idx}) = NaN;
|
||||
end
|
||||
end
|
||||
% % Handle empty fields
|
||||
% emptyFields = structfun(@isempty, newRow);
|
||||
% if sum(emptyFields) > 0
|
||||
% emptyFieldNames = fields(emptyFields);
|
||||
% for idx = 1:numel(emptyFieldNames)
|
||||
% newRow.(emptyFieldNames{idx}) = NaN;
|
||||
% end
|
||||
% end
|
||||
|
||||
% Convert arrays to JSON strings
|
||||
for i = 1:length(fields)
|
||||
fieldValue = newRow.(fields{i});
|
||||
if isnumeric(fieldValue) && length(fieldValue) > 1
|
||||
newRow.(fields{i}) = jsonencode(fieldValue);
|
||||
% Convert any non-scalar numeric (including [] and vectors) into JSON
|
||||
for i = 1:numel(fields)
|
||||
name = fields{i};
|
||||
value = newRow.(name);
|
||||
|
||||
if isnumeric(value) && ~isscalar(value)
|
||||
% jsonencode([]) -> "[]"
|
||||
% jsonencode([a,b,c]) -> "[a,b,c]"
|
||||
newRow.(name) = jsonencode(value);
|
||||
end
|
||||
end
|
||||
|
||||
@@ -238,6 +249,7 @@ classdef DBHandler < handle
|
||||
elseif ischar(value)
|
||||
newRow.(colName{1}) = string(value);
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
% Parameters for retry logic
|
||||
@@ -251,7 +263,7 @@ classdef DBHandler < handle
|
||||
if obj.type == "mysql"
|
||||
newRow_ = obj.convertTableToCellStrings(newRow);
|
||||
end
|
||||
sqlwrite(obj.conn, tableName, newRow);
|
||||
sqlwrite(obj.conn, tableName, newRow,"Catalog",obj.dataBase);
|
||||
success = true;
|
||||
catch e
|
||||
if contains(e.message, 'database is locked') || contains(e.message, 'cannot rollback transaction')
|
||||
|
||||
Reference in New Issue
Block a user