crawshaw / sqlite

Go SQLite3 driver
ISC License
561 stars 67 forks source link

sqlitex.ExecTransient and sqlitex.Exec not executing statements #144

Open Dilemma1980 opened 1 year ago

Dilemma1980 commented 1 year ago

I've been at this for days and can't seem to figure out what I'm doing wrong, so I'm hoping someone can help.

I have a series of sql statements in my app that I previously used the mattn/go-sqlite3 to execute so I know they work syntax-wise. I decided to go with this library for concurrency purposes. The Update statements run without error but the database doesn't populate with any records as expected. They are left blank. The only clue I have is a WAL file that's left behind during the process that has a pretty big file size. Am I missing a step?

This is the function that executes the statements:

func Execute_sql(ctx context.Context, statement string, parameters []any) errormgmt.Error_Package {

    var (
        Err error
        Err_pkg errormgmt.Error_Package
        conn *sqlite.Conn
    )

    conn, Err_pkg = Get_conn_from_pool(ctx)
    if Err_pkg.Err != nil {
        return Err_pkg
    }
    defer func() {
        if conn != nil {
            Return_conn_to_pool(conn)
        }

    }()

    if parameters == nil {
        Err = sqlitex.ExecTransient(conn, statement, nil)
    } else {
        Err = sqlitex.ExecTransient(conn, statement, nil, parameters...)
    }

    select {
    case <-ctx.Done():
        return errormgmt.Process_Error(errors.New("User cancelled"))
    default:
        if Err != nil {
            return errormgmt.Process_Error(Err)
        }
    }

    return errormgmt.Process_Error(nil)
}

The function calls another function that pulls the connection from the pool and I check if the connection is nil. Then it executes sqlitex.ExecTransient.

This is how I initially open the pool:

func Open_db_connect() errormgmt.Error_Package {

    var Err error
    // Dbpoolsize = runtime.NumCPU() - 1
    Dbpoolsize = 1
    Dbpool, Err = sqlitex.Open(DATABASE_PATH, 0, Dbpoolsize)
    if Err != nil {
        return errormgmt.Process_Error(Err)
    }

    return errormgmt.Process_Error(nil)
}

The pool size is just one connection for now.

This is an example of statement I'm trying to execute:

sql_statement = "WITH ClosedMarketCoreServices ([SID], [Vendor]) AS " +
                        "(" +
                            "SELECT s.[SID], s.[Vendor] " +
                            "FROM [service_detail] AS s " +
                            "WHERE s.[sCode] IN ('CAM', 'CTY', 'EXCT', 'FRCH', 'LL', 'RCPT', 'SPL', 'UTL') " +
                                "AND s.[Extra Pickup] == 'N' " +
                                "AND s.[End Date] IS NULL " +
                                "AND s.[Service Type] IN ('CSC', 'R', 'LR', 'CTF', 'H', 'DSP', 'HANDPICKUP', 'PCK', 'KT', 'PMPOT') " +
                                "AND GETVENDORTYPE(s.[PVID], s.[Vendor]) != 'SBS Fees' " +
                        "), " +
                    "ClosedMarketFeesNoCore ([SID], [Vendor]) AS " +
                        "(" +
                            "SELECT s2.[SID], s2.[Vendor] " +
                            "FROM [service_detail] AS s2 " +
                            "WHERE s2.[sCode] IN ('CAM', 'CTY', 'EXCT', 'FRCH', 'LL', 'RCPT', 'SPL', 'UTL') " +
                                "AND s2.[Extra Pickup] == 'N' " +
                                "AND s2.[End Date] IS NULL " +
                                "AND s2.[Service Type] NOT IN ('CSC', 'R', 'LR', 'CTF', 'H', 'DSP', 'HANDPICKUP', 'PCK', 'KT', 'PMPOT') " +
                                "AND GETVENDORTYPE(s2.[PVID], s2.[Vendor]) != 'SBS Fees' " +
                                "AND NOT EXISTS " +
                                    "(" +
                                        "SELECT s3.[SID], s3.[Vendor] " +
                                        "FROM [service_detail] AS s3 " +
                                        "WHERE s3.[Service Type] IN ('CSC', 'R', 'LR', 'CTF', 'H', 'DSP', 'HANDPICKUP', 'PCK', 'KT', 'PMPOT') " +
                                            "AND s3.[Extra Pickup] == 'N' " +
                                            "AND s3.[End Date] IS NULL " +
                                            "AND s2.[SID] == s3.[SID] " +
                                            "AND s2.[Vendor] == s3.[Vendor] " +
                                    ")" +
                        ") " +
                    "INSERT INTO [cancellation_upload] ([LocationCode], [ServiceBaselineId], [PriceModelTypeCode], [CostAmount], " +
                        "[Vendor], [EndDate], [CommandCode], [CreateNotif], [NotifReasonCode], [CustomMessage], [ExceptionType], " +
                        "[ExceptionReasonCode], [ExceptionComments], [ExceptionEffectiveDate], [ResourceName], [BypassMASServiceChange], " +
                        "[VCRCode], [MASOpsRouting], [OverrideApprover]) " +
                    "SELECT [Location Code], [Service Baseline Id], GETPRICEMODELTYPE([price Model Type Name]), [Cost Amount], " +
                        "[Vendor], '" + cancel_date + "', 'STOP', IIF(GETVENDORTYPE([PVID], [Vendor]) NOT IN ('SBS Fees', 'WM'), '1', '0'), " +
                        "IIF(GETVENDORTYPE([PVID], [Vendor]) NOT IN ('SBS Fees', 'WM'), '" + notif_reason_closed_market + "', NULL), " +
                        "IIF(GETVENDORTYPE([PVID], [Vendor]) NOT IN ('SBS Fees', 'WM') AND '" + cancel_reason + "' != 'Store Closing', " +
                        "'" + single_quote_escape(tpv_vendor_msg_billing_change) + "', " +
                        "IIF(GETVENDORTYPE([PVID], [Vendor]) NOT IN ('SBS Fees', 'WM') AND '" + cancel_reason + "' == 'Store Closing', " +
                        "'" + single_quote_escape(tpv_vendor_msg_removal) + "', NULL)), " +
                        "'CLTINITCHG', 'PERMEND_CANSER', " +
                        "'" + single_quote_escape(final_exception_comments) + "', " +
                        "'" + exception_date + "', '" + single_quote_escape(userid) + "', IIF(GETVENDORTYPE([PVID], [Vendor]) == 'WM', '1', NULL), " +
                        "IIF(GETVENDORTYPE([PVID], [Vendor]) == 'WM', 'MSC', NULL), IIF(GETVENDORTYPE([PVID], [Vendor]) == 'WM', '0', NULL), " +
                        "'" + single_quote_escape(pm_userid) + "' " +
                    "FROM [service_detail] " +
                    "WHERE " +
                        "CASE '" + cancel_by_setting + "' " +
                            "WHEN 'By Location' " +
                            "THEN [Location Code] IN " +
                                "(" +
                                    "SELECT [Location Code] " +
                                    "FROM [cancel_list] " +
                                ") " +
                            "WHEN 'By SID' " +
                            "THEN [SID] IN " +
                                "(" +
                                    "SELECT [SID] " +
                                    "FROM [cancel_list] " +
                                ") " +
                            "ELSE 1 " +
                        "END " +
                        "AND [Extra Pickup] == 'N' " +
                        "AND [End Date] IS NULL " +
                        "AND (EXISTS " +
                            "(" +
                                "SELECT [SID], [Vendor] " +
                                "FROM ClosedMarketCoreServices " +
                                "WHERE [service_detail].[SID] == ClosedMarketCoreServices.[SID] " +
                                    "AND [service_detail].[Vendor] == ClosedMarketCoreServices.[Vendor] " +
                            ") " +
                            "OR " +
                            "EXISTS " +
                            "(" +
                                "SELECT [SID], [Vendor] " +
                                "FROM ClosedMarketFeesNoCore " +
                                "WHERE [service_detail].[SID] == ClosedMarketFeesNoCore.[SID] " +
                                    "AND [service_detail].[Vendor] == ClosedMarketFeesNoCore.[Vendor] " +
                            ") " +
                            ");"

Like I said, this statement works in the mattn library but I feel like I'm missing something.