updates from lab pc during ecoc sprint

This commit is contained in:
Silas Labor Zizou
2025-04-16 09:18:18 +02:00
parent 0236103b13
commit 943677ee40
9 changed files with 324 additions and 136 deletions

View File

@@ -9,6 +9,7 @@ classdef DBHandler < handle
tableNames % Cell array containing names of all tables in the database
tables = struct(); % Structure containing MATLAB tables for each database table
distinctValues
type
end
methods
@@ -21,6 +22,7 @@ classdef DBHandler < handle
arguments
options.pathToDB = ""; % Default value for pathToDB if not provided
options.type = "mysql";
end
% Assign values to class properties based on input arguments
@@ -33,7 +35,22 @@ classdef DBHandler < handle
% Establish a connection to the SQLite database
try
obj.conn = sqlite(obj.pathToDB);
if options.type == "sqlite"
obj.conn = sqlite(obj.pathToDB);
elseif options.type == "mysql"
datasource = "jdbc:mysql://134.245.243.254:3306/labor";
obj.conn = database( ...
"labor", ... % Database name
"silas", ... % Username
"silas", ... % Password (or getSecret)
"Vendor", "MySQL", ...
"Server", "134.245.243.254", ...
"PortNumber", 3306, ...
"JDBCDriverLocation", "C:\Users\sioe\Documents\mysql-connector-j-9.3.0\mysql-connector-j-9.3.0.jar");
end
catch e
error('Failed to connect to the database: %s', e.message);
end
@@ -59,8 +76,14 @@ classdef DBHandler < handle
function obj = getTableNames(obj)
% Get all table names from the database
try
result = fetch(obj.conn, 'SELECT name FROM sqlite_master WHERE type="table"');
obj.tableNames = result.name;
if obj.type == "mysql"
result = fetch(obj.conn, 'SHOW TABLES;');
obj.tableNames = result.Variables;
elseif obj.type == "sqlite"
result = fetch(obj.conn, 'SELECT name FROM sqlite_master WHERE type="table"');
obj.tableNames = result.name;
end
catch e
error('Failed to retrieve table names: %s', e.message);
end
@@ -213,12 +236,16 @@ classdef DBHandler < handle
elseif ischar(value)
newRow.(colName{1}) = string(value);
elseif isdatetime(value)
% newRow.(colName{1}) = string(value);
end
end
% Parameters for retry logic
maxRetries = 5;
maxRetries = 50;
basePause = 0.05; % seconds
attempt = 0;
@@ -226,6 +253,9 @@ classdef DBHandler < handle
while ~success && attempt <= maxRetries
try
if obj.type == "mysql"
newRow_ = obj.convertTableToCellStrings(newRow);
end
sqlwrite(obj.conn, tableName, newRow);
success = true; % Write successful
catch e
@@ -245,7 +275,14 @@ classdef DBHandler < handle
end
% Retrieve the measurement_id of the newly inserted row for linking other tables
result = fetch(obj.conn, 'SELECT last_insert_rowid()');
if obj.type == "mysql"
queue = "SELECT LAST_INSERT_ID()";
elseif obj.type == "sqlite"
queue = "SELECT last_insert_rowid()";
end
result = fetch(obj.conn, queue);
lastID = result{1, 1}; % Access the value directly from the table
end
@@ -502,8 +539,87 @@ classdef DBHandler < handle
% Step 4: Execute the query and handle results
result = obj.fetch(query);
result = obj.normalizeMySQLTable(result);
end
function cleanedTable = normalizeMySQLTable(~,result)
cleanedTable = result;
varNames = result.Properties.VariableNames;
for i = 1:numel(varNames)
col = result.(varNames{i});
% Only process if column is a cell array of strings or chars
if iscell(col) && all(cellfun(@(x) ischar(x) || isstring(x), col))
% Try converting to numeric if possible
numCol = str2double(col);
if all(~isnan(numCol) | strcmpi(col, 'NaN'))
% It's numeric (with possible NaNs)
cleanedTable.(varNames{i}) = numCol;
else
% Clean double-quoted SQL literals (e.g., ""no_db"")
cleanedTable.(varNames{i}) = strrep(string(col), '""', '"');
end
end
end
end
function cellTable = convertTableToCellStrings(~,tbl)
% Converts all variables in a MATLAB table to cell arrays of strings (like JDBC fetch from MySQL)
%
% Example:
% jdbcFormatted = convertTableToCellStrings(myTable);
cellTable = tbl;
varNames = tbl.Properties.VariableNames;
for i = 1:numel(varNames)
col = tbl.(varNames{i});
if isnumeric(col)
% Convert numeric values to strings
cellTable.(varNames{i}) = arrayfun(@(x) num2str(x, '%.15g'), col, 'UniformOutput', false);
elseif isstring(col) || ischar(col)
% Ensure cell array of strings
cellTable.(varNames{i}) = cellstr(col);
elseif iscell(col)
% Convert each cell entry to string
cellTable.(varNames{i}) = cellfun(@convertToString, col, 'UniformOutput', false);
elseif islogical(col)
% Convert logicals to '0' or '1'
cellTable.(varNames{i}) = cellstr(string(double(col)));
elseif isdatetime(col)
% Convert datetimes to formatted string
cellTable.(varNames{i}) = cellstr(string(col));
else
warning('Column "%s" has unsupported type. Converting using string().', varNames{i});
cellTable.(varNames{i}) = cellstr(string(col));
end
end
end
function out = convertToString(~,val)
if ischar(val)
out = val;
elseif isstring(val)
out = char(val);
elseif isnumeric(val)
out = num2str(val, '%.15g');
elseif islogical(val)
out = num2str(double(val));
elseif isdatetime(val)
out = datestr(val, 'yyyy-mm-dd HH:MM:SS');
else
out = char(string(val)); % fallback
end
end
function query = constructSQLQuery(obj, filterParams, selectedFields)
% constructSQLQuery Constructs the SQL query based on filter parameters and selected fields.
%