Skip to content

Commit 09845d0

Browse files
committed
Refactor error handling in database connection and insert functions; add initial VSCode settings file
1 parent aa8f2ee commit 09845d0

3 files changed

Lines changed: 10 additions & 6 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

src/Configuration.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ export fetch
368368

369369
function is_connection_error(e, connection::PormGPostgres)
370370
msg = lowercase(string(e))
371-
return (e isa LibPQ.Errors.UnknownError && string(e) == "") ||
371+
return (e isa LibPQ.Errors.UnknownError && string(e) == "LibPQ.Errors.UnknownError(\"\")") ||
372372
occursin("server closed the connection", msg) ||
373373
occursin("connection not open", msg)
374374
# occursin("connection refused", msg) ||
@@ -390,13 +390,13 @@ function fetch(connection::PormGPostgres, sql::String;
390390
conn === nothing && (conn = acquire_connection(connection))
391391
try
392392
return libpq_execute(conn, sql, params)
393-
catch e
394-
@infiltrate false
393+
catch e
395394
if is_connection_error(e, connection)
396395
@warn "Lost connection to database. Attempting to reconnect..."
397396
conn = reconnect_db(connection, conn)
398397
return libpq_execute(conn, sql, params)
399398
end
399+
@infiltrate
400400
@error "Failed to execute SQL query: $e"
401401
throw(e)
402402
finally

src/QueryBuilder.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,8 +1953,11 @@ function insert(objct::SQLObject; table_alias::Union{Nothing, SQLTableAlias} = n
19531953
# check if the field has max_digits and validate
19541954
if hasfield(typeof(model.fields[field]), :max_digits)
19551955
value_str = string(objct.insert[field])
1956-
integer_part, fractional_part = split(value_str, ".")
1957-
total_digits = length(replace(integer_part, "-" => "")) + length(fractional_part)
1956+
# @infiltrate
1957+
parts = split(value_str, ".")
1958+
integer_part = parts[1]
1959+
fractional_part = length(parts) > 1 ? parts[2] : ""
1960+
total_digits = length(replace(integer_part, "-" => "")) + (fractional_part == "" ? 0 : length(fractional_part))
19581961
if total_digits > model.fields[field].max_digits
19591962
error("""Error in insert, the field \e[4m\e[31m$(field)\e[0m has a max_digits of \e[4m\e[32m$(model.fields[field].max_digits)\e[0m, but the value has \e[4m\e[31m$(total_digits)\e[0m""")
19601963
end
@@ -2018,7 +2021,7 @@ function _update_sequence(model::PormGModel, connection::PormGPostgres, pk_field
20182021
end
20192022
end
20202023
elseif settings.django_prefix !== nothing
2021-
@infiltrate
2024+
@infiltrate false
20222025
try
20232026
# For Django prefixed tables, try with django prefix pattern
20242027
sequence_name = "$(model.name)_$(field)_seq"

0 commit comments

Comments
 (0)