From e2315d3a166b0640f8189d3d25e6745b63a7ca37 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 6 May 2022 16:38:33 -0400 Subject: [PATCH] add json and skip regex for ranged sql building --- FL.bas | 6 +++--- TheBigOne.cls | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/FL.bas b/FL.bas index 7caa45c..9c4bb91 100644 --- a/FL.bas +++ b/FL.bas @@ -1560,7 +1560,7 @@ Sub pricegroup_upload() Dim sql As String Selection.CurrentRegion.Select - sql = x.SQLp_build_sql_values(x.ARRAYp_get_range_string(Selection), True, True, PostgreSQL, False, True, "S", "S", "S", "S", "S", "S", "S", "S", "S", "N", "S", "S", "S", "A", "A") + sql = x.SQLp_build_sql_values(x.ARRAYp_get_range_string(Selection), True, True, PostgreSQL, False, True, "S", "S", "S", "S", "S", "S", "S", "S", "S", "N", "S", "S", "S", "A", "A", "J") sql = "BEGIN;" & vbCrLf & "DELETE FROM rlarp.price_map;" & vbCrLf & "INSERT INTO rlarp.price_map" & vbCrLf & sql & ";" & vbCrLf & "COMMIT;" If Not x.ADOp_Exec(0, sql, 1, True, PostgreSQLODBC, "usmidlnx01", False, "ptrowbridge", "qqqx53!030", "Port=5030;Database=ubm") Then @@ -1572,7 +1572,7 @@ Sub pricegroup_upload() Call x.ADOp_CloseCon(0) - sql = x.SQLp_build_sql_values(x.ARRAYp_get_range_string(Selection), True, True, PostgreSQL, False, True, "S", "S", "S", "S", "S", "S", "S", "S", "S", "N", "S", "S", "S", "A", "A") + sql = x.SQLp_build_sql_values(x.ARRAYp_get_range_string(Selection), True, True, PostgreSQL, False, True, "S", "S", "S", "S", "S", "S", "S", "S", "S", "N", "S", "S", "S", "A", "A", "A") sql = "BEGIN" & vbCrLf & "DELETE FROM rlarp.price_map;" & vbCrLf & "INSERT INTO rlarp.price_map" & vbCrLf & sql & ";" & vbCrLf & "END" If Not x.ADOp_Exec(0, sql, 1, True, ADOinterface.SqlServer, "usmidsql01", True) Then @@ -1626,7 +1626,7 @@ Sub pricegroup_upload_db2() inc = 250 Do While i <= UBound(ul, 2) 'sql = x.ADOp_BuildInsertSQL(ul, "rlarp.price_map", True, i, WorksheetFunction.Min(i + inc, UBound(ul, 2)), Array("S", "S", "S", "S", "S", "S", "S", "N", "S", "S")) - sql = x.SQLp_build_sql_values_ranged(ul, True, True, Db2, False, i, i + inc, "S", "S", "S", "S", "S", "S", "S", "S", "S", "N", "S", "S", "S", "A", "A") + sql = x.SQLp_build_sql_values_ranged(ul, True, True, Db2, False, True, i, i + inc, "S", "S", "S", "S", "S", "S", "S", "S", "S", "N", "S", "S", "S", "A", "A", "A") sql = "INSERT INTO rlarp.price_map " & vbCrLf & sql If Not x.ADOp_Exec(0, sql) Then MsgBox (x.ADOo_errstring) diff --git a/TheBigOne.cls b/TheBigOne.cls index ee865d1..c8631d2 100644 --- a/TheBigOne.cls +++ b/TheBigOne.cls @@ -2560,7 +2560,6 @@ Public Function SQLp_build_sql_values(ByRef tbl() As String, trim As Boolean, he rec = rec & "'" & Replace(LTrim(RTrim(rx.Replace(tbl(j, i), ""))), "'", "''") & "'" Else rec = rec & "'" & Replace(rx.Replace(tbl(j, i), ""), "'", "''") & "'" - End If End If Case "A" '-------A = string but dont apply any regex------------------ @@ -2572,7 +2571,17 @@ Public Function SQLp_build_sql_values(ByRef tbl() As String, trim As Boolean, he rec = rec & "'" & Replace(LTrim(RTrim(tbl(j, i))), "'", "''") & "'" Else rec = rec & "'" & Replace(tbl(j, i), "'", "''") & "'" - + End If + End If + Case "J" '-------no regex and cast to jsonb--------------------------- + rx.Pattern = strip_text + If LTrim(RTrim(tbl(j, i))) = "" And empty_as_null Then + rec = rec & "CAST(NULL AS jsonb)" + Else + If trim Then + rec = rec & "'" & Replace(LTrim(RTrim(tbl(j, i))), "'", "''") & "'::jsonb" + Else + rec = rec & "'" & Replace(tbl(j, i), "'", "''") & "'::jsonb" End If End If Case "D" '-------D = date--------------------------------------------- @@ -2615,7 +2624,7 @@ Public Function SQLp_build_sql_values(ByRef tbl() As String, trim As Boolean, he End Function -Public Function SQLp_build_sql_values_ranged(ByRef tbl() As String, trim As Boolean, headers As Boolean, syntax As SQLsyntax, ByRef quote_headers As Boolean, start_row As Long, end_row As Long, ParamArray typeflag()) As String +Public Function SQLp_build_sql_values_ranged(ByRef tbl() As String, trim As Boolean, headers As Boolean, syntax As SQLsyntax, ByRef quote_headers As Boolean, empty_as_null As Boolean, start_row As Long, end_row As Long, ParamArray typeflag()) As String Dim i As Long @@ -2708,14 +2717,35 @@ Public Function SQLp_build_sql_values_ranged(ByRef tbl() As String, trim As Bool End If Case "S" '-------S = string------------------------------------------ rx.Pattern = strip_text - If LTrim(RTrim(tbl(j, i))) = "" Then + If LTrim(RTrim(tbl(j, i))) = "" And empty_as_null Then rec = rec & "CAST(NULL AS " & nullText & ")" Else If trim Then rec = rec & "'" & Replace(LTrim(RTrim(rx.Replace(tbl(j, i), ""))), "'", "''") & "'" Else rec = rec & "'" & Replace(rx.Replace(tbl(j, i), ""), "'", "''") & "'" - + End If + End If + Case "A" '-------A = string but dont apply any regex------------------ + rx.Pattern = strip_text + If LTrim(RTrim(tbl(j, i))) = "" And empty_as_null Then + rec = rec & "CAST(NULL AS " & nullText & ")" + Else + If trim Then + rec = rec & "'" & Replace(LTrim(RTrim(tbl(j, i))), "'", "''") & "'" + Else + rec = rec & "'" & Replace(tbl(j, i), "'", "''") & "'" + End If + End If + Case "J" '-------no regex and cast to jsonb--------------------------- + rx.Pattern = strip_text + If LTrim(RTrim(tbl(j, i))) = "" And empty_as_null Then + rec = rec & "CAST(NULL AS jsonb)" + Else + If trim Then + rec = rec & "'" & Replace(LTrim(RTrim(tbl(j, i))), "'", "''") & "'::jsonb" + Else + rec = rec & "'" & Replace(tbl(j, i), "'", "''") & "'::jsonb" End If End If Case "D" '-------D = date--------------------------------------------- @@ -2727,7 +2757,7 @@ Public Function SQLp_build_sql_values_ranged(ByRef tbl() As String, trim As Bool End If Case Else '-------Assume text------------------------------------------ rx.Pattern = strip_text - If LTrim(RTrim(tbl(j, i))) = "" Then + If LTrim(RTrim(tbl(j, i))) = "" And empty_as_null Then rec = rec & "CAST(NULL AS " & nullText & ")" Else If trim Then