2022-04-03 15:24:40 +00:00
|
|
|
package models
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
import (
|
2018-12-18 05:24:55 +00:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
2018-10-25 13:51:47 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2019-03-09 07:46:47 +00:00
|
|
|
"github.com/lib/pq"
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Queries contains all prepared SQL queries.
|
|
|
|
type Queries struct {
|
2020-07-04 16:55:02 +00:00
|
|
|
GetDashboardCharts *sqlx.Stmt `query:"get-dashboard-charts"`
|
|
|
|
GetDashboardCounts *sqlx.Stmt `query:"get-dashboard-counts"`
|
2018-11-05 05:49:08 +00:00
|
|
|
|
2018-12-18 05:24:55 +00:00
|
|
|
InsertSubscriber *sqlx.Stmt `query:"insert-subscriber"`
|
|
|
|
UpsertSubscriber *sqlx.Stmt `query:"upsert-subscriber"`
|
2020-08-01 11:15:29 +00:00
|
|
|
UpsertBlocklistSubscriber *sqlx.Stmt `query:"upsert-blocklist-subscriber"`
|
2018-12-18 05:24:55 +00:00
|
|
|
GetSubscriber *sqlx.Stmt `query:"get-subscriber"`
|
|
|
|
GetSubscribersByEmails *sqlx.Stmt `query:"get-subscribers-by-emails"`
|
|
|
|
GetSubscriberLists *sqlx.Stmt `query:"get-subscriber-lists"`
|
2019-12-01 12:18:36 +00:00
|
|
|
GetSubscriberListsLazy *sqlx.Stmt `query:"get-subscriber-lists-lazy"`
|
2018-12-18 05:24:55 +00:00
|
|
|
UpdateSubscriber *sqlx.Stmt `query:"update-subscriber"`
|
2020-08-01 11:15:29 +00:00
|
|
|
BlocklistSubscribers *sqlx.Stmt `query:"blocklist-subscribers"`
|
2018-12-18 05:24:55 +00:00
|
|
|
AddSubscribersToLists *sqlx.Stmt `query:"add-subscribers-to-lists"`
|
|
|
|
DeleteSubscriptions *sqlx.Stmt `query:"delete-subscriptions"`
|
2019-12-01 12:18:36 +00:00
|
|
|
ConfirmSubscriptionOptin *sqlx.Stmt `query:"confirm-subscription-optin"`
|
2018-12-18 05:24:55 +00:00
|
|
|
UnsubscribeSubscribersFromLists *sqlx.Stmt `query:"unsubscribe-subscribers-from-lists"`
|
|
|
|
DeleteSubscribers *sqlx.Stmt `query:"delete-subscribers"`
|
2022-05-08 09:15:45 +00:00
|
|
|
UnsubscribeByCampaign *sqlx.Stmt `query:"unsubscribe-by-campaign"`
|
2019-07-21 13:48:41 +00:00
|
|
|
ExportSubscriberData *sqlx.Stmt `query:"export-subscriber-data"`
|
2018-12-18 05:24:55 +00:00
|
|
|
|
|
|
|
// Non-prepared arbitrary subscriber queries.
|
|
|
|
QuerySubscribers string `query:"query-subscribers"`
|
2021-09-18 10:16:22 +00:00
|
|
|
QuerySubscribersCount string `query:"query-subscribers-count"`
|
2021-01-23 12:53:29 +00:00
|
|
|
QuerySubscribersForExport string `query:"query-subscribers-for-export"`
|
2018-12-18 05:24:55 +00:00
|
|
|
QuerySubscribersTpl string `query:"query-subscribers-template"`
|
|
|
|
DeleteSubscribersByQuery string `query:"delete-subscribers-by-query"`
|
|
|
|
AddSubscribersToListsByQuery string `query:"add-subscribers-to-lists-by-query"`
|
2020-08-01 11:15:29 +00:00
|
|
|
BlocklistSubscribersByQuery string `query:"blocklist-subscribers-by-query"`
|
2018-12-18 05:24:55 +00:00
|
|
|
DeleteSubscriptionsByQuery string `query:"delete-subscriptions-by-query"`
|
|
|
|
UnsubscribeSubscribersFromListsByQuery string `query:"unsubscribe-subscribers-from-lists-by-query"`
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-05-14 16:36:14 +00:00
|
|
|
CreateList *sqlx.Stmt `query:"create-list"`
|
2021-01-31 10:49:39 +00:00
|
|
|
QueryLists string `query:"query-lists"`
|
|
|
|
GetLists *sqlx.Stmt `query:"get-lists"`
|
2019-12-01 12:18:36 +00:00
|
|
|
GetListsByOptin *sqlx.Stmt `query:"get-lists-by-optin"`
|
2019-05-14 16:36:14 +00:00
|
|
|
UpdateList *sqlx.Stmt `query:"update-list"`
|
|
|
|
UpdateListsDate *sqlx.Stmt `query:"update-lists-date"`
|
|
|
|
DeleteLists *sqlx.Stmt `query:"delete-lists"`
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2022-07-30 13:31:20 +00:00
|
|
|
CreateCampaign *sqlx.Stmt `query:"create-campaign"`
|
|
|
|
QueryCampaigns string `query:"query-campaigns"`
|
|
|
|
GetCampaign *sqlx.Stmt `query:"get-campaign"`
|
|
|
|
GetCampaignForPreview *sqlx.Stmt `query:"get-campaign-for-preview"`
|
|
|
|
GetCampaignStats *sqlx.Stmt `query:"get-campaign-stats"`
|
|
|
|
GetCampaignStatus *sqlx.Stmt `query:"get-campaign-status"`
|
|
|
|
|
|
|
|
// These two queries are read as strings and based on settings.individual_tracking=on/off,
|
|
|
|
// are interpolated and copied to view and click counts. Same query, different tables.
|
|
|
|
GetCampaignAnalyticsCounts string `query:"get-campaign-analytics-counts"`
|
|
|
|
GetCampaignAnalyticsCountsUnique string `query:"get-campaign-analytics-unique-counts"`
|
|
|
|
GetCampaignViewCounts *sqlx.Stmt `query:"get-campaign-view-counts"`
|
|
|
|
GetCampaignClickCounts *sqlx.Stmt `query:"get-campaign-click-counts"`
|
|
|
|
GetCampaignLinkCounts *sqlx.Stmt `query:"get-campaign-link-counts"`
|
|
|
|
GetCampaignBounceCounts *sqlx.Stmt `query:"get-campaign-bounce-counts"`
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
NextCampaigns *sqlx.Stmt `query:"next-campaigns"`
|
|
|
|
NextCampaignSubscribers *sqlx.Stmt `query:"next-campaign-subscribers"`
|
|
|
|
GetOneCampaignSubscriber *sqlx.Stmt `query:"get-one-campaign-subscriber"`
|
|
|
|
UpdateCampaign *sqlx.Stmt `query:"update-campaign"`
|
|
|
|
UpdateCampaignStatus *sqlx.Stmt `query:"update-campaign-status"`
|
|
|
|
UpdateCampaignCounts *sqlx.Stmt `query:"update-campaign-counts"`
|
2018-11-02 07:50:32 +00:00
|
|
|
RegisterCampaignView *sqlx.Stmt `query:"register-campaign-view"`
|
2018-10-25 13:51:47 +00:00
|
|
|
DeleteCampaign *sqlx.Stmt `query:"delete-campaign"`
|
|
|
|
|
|
|
|
InsertMedia *sqlx.Stmt `query:"insert-media"`
|
2022-04-03 15:24:40 +00:00
|
|
|
GetAllMedia *sqlx.Stmt `query:"get-all-media"`
|
2018-10-25 13:51:47 +00:00
|
|
|
GetMedia *sqlx.Stmt `query:"get-media"`
|
|
|
|
DeleteMedia *sqlx.Stmt `query:"delete-media"`
|
|
|
|
|
|
|
|
CreateTemplate *sqlx.Stmt `query:"create-template"`
|
|
|
|
GetTemplates *sqlx.Stmt `query:"get-templates"`
|
|
|
|
UpdateTemplate *sqlx.Stmt `query:"update-template"`
|
|
|
|
SetDefaultTemplate *sqlx.Stmt `query:"set-default-template"`
|
|
|
|
DeleteTemplate *sqlx.Stmt `query:"delete-template"`
|
|
|
|
|
2018-10-31 12:54:21 +00:00
|
|
|
CreateLink *sqlx.Stmt `query:"create-link"`
|
|
|
|
RegisterLinkClick *sqlx.Stmt `query:"register-link-click"`
|
|
|
|
|
2020-07-08 11:00:14 +00:00
|
|
|
GetSettings *sqlx.Stmt `query:"get-settings"`
|
|
|
|
UpdateSettings *sqlx.Stmt `query:"update-settings"`
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// GetStats *sqlx.Stmt `query:"get-stats"`
|
2021-05-24 17:11:48 +00:00
|
|
|
RecordBounce *sqlx.Stmt `query:"record-bounce"`
|
|
|
|
QueryBounces string `query:"query-bounces"`
|
|
|
|
DeleteBounces *sqlx.Stmt `query:"delete-bounces"`
|
|
|
|
DeleteBouncesBySubscriber *sqlx.Stmt `query:"delete-bounces-by-subscriber"`
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
// CompileSubscriberQueryTpl takes an arbitrary WHERE expressions
|
2018-12-18 05:24:55 +00:00
|
|
|
// to filter subscribers from the subscribers table and prepares a query
|
|
|
|
// out of it using the raw `query-subscribers-template` query template.
|
|
|
|
// While doing this, a readonly transaction is created and the query is
|
|
|
|
// dry run on it to ensure that it is indeed readonly.
|
2022-04-03 15:24:40 +00:00
|
|
|
func (q *Queries) CompileSubscriberQueryTpl(exp string, db *sqlx.DB) (string, error) {
|
2018-12-18 05:24:55 +00:00
|
|
|
tx, err := db.BeginTxx(context.Background(), &sql.TxOptions{ReadOnly: true})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-12-05 15:57:31 +00:00
|
|
|
defer tx.Rollback()
|
2018-12-18 05:24:55 +00:00
|
|
|
|
|
|
|
// Perform the dry run.
|
|
|
|
if exp != "" {
|
|
|
|
exp = " AND " + exp
|
|
|
|
}
|
|
|
|
stmt := fmt.Sprintf(q.QuerySubscribersTpl, exp)
|
|
|
|
if _, err := tx.Exec(stmt, true, pq.Int64Array{}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stmt, nil
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:19:50 +00:00
|
|
|
// compileSubscriberQueryTpl takes an arbitrary WHERE expressions and a subscriber
|
2020-08-01 11:15:29 +00:00
|
|
|
// query template that depends on the filter (eg: delete by query, blocklist by query etc.)
|
2018-12-18 05:24:55 +00:00
|
|
|
// combines and executes them.
|
2022-04-03 15:24:40 +00:00
|
|
|
func (q *Queries) ExecSubscriberQueryTpl(exp, tpl string, listIDs []int, db *sqlx.DB, args ...interface{}) error {
|
2018-12-18 05:24:55 +00:00
|
|
|
// Perform a dry run.
|
2022-04-03 15:24:40 +00:00
|
|
|
filterExp, err := q.CompileSubscriberQueryTpl(exp, db)
|
2018-12-18 05:24:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(listIDs) == 0 {
|
2022-04-03 15:24:40 +00:00
|
|
|
listIDs = []int{}
|
2018-12-18 05:24:55 +00:00
|
|
|
}
|
2022-03-20 05:32:43 +00:00
|
|
|
|
2018-12-18 05:24:55 +00:00
|
|
|
// First argument is the boolean indicating if the query is a dry run.
|
2022-04-03 15:24:40 +00:00
|
|
|
a := append([]interface{}{false, pq.Array(listIDs)}, args...)
|
2018-12-18 05:24:55 +00:00
|
|
|
if _, err := db.Exec(fmt.Sprintf(tpl, filterExp), a...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|