2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
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 {
|
2018-11-05 05:49:08 +00:00
|
|
|
GetDashboardStats *sqlx.Stmt `query:"get-dashboard-stats"`
|
|
|
|
|
2018-12-18 05:24:55 +00:00
|
|
|
InsertSubscriber *sqlx.Stmt `query:"insert-subscriber"`
|
|
|
|
UpsertSubscriber *sqlx.Stmt `query:"upsert-subscriber"`
|
|
|
|
UpsertBlacklistSubscriber *sqlx.Stmt `query:"upsert-blacklist-subscriber"`
|
|
|
|
GetSubscriber *sqlx.Stmt `query:"get-subscriber"`
|
|
|
|
GetSubscribersByEmails *sqlx.Stmt `query:"get-subscribers-by-emails"`
|
|
|
|
GetSubscriberLists *sqlx.Stmt `query:"get-subscriber-lists"`
|
|
|
|
UpdateSubscriber *sqlx.Stmt `query:"update-subscriber"`
|
|
|
|
BlacklistSubscribers *sqlx.Stmt `query:"blacklist-subscribers"`
|
|
|
|
AddSubscribersToLists *sqlx.Stmt `query:"add-subscribers-to-lists"`
|
|
|
|
DeleteSubscriptions *sqlx.Stmt `query:"delete-subscriptions"`
|
|
|
|
UnsubscribeSubscribersFromLists *sqlx.Stmt `query:"unsubscribe-subscribers-from-lists"`
|
|
|
|
DeleteSubscribers *sqlx.Stmt `query:"delete-subscribers"`
|
|
|
|
Unsubscribe *sqlx.Stmt `query:"unsubscribe"`
|
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"`
|
|
|
|
QuerySubscribersTpl string `query:"query-subscribers-template"`
|
|
|
|
DeleteSubscribersByQuery string `query:"delete-subscribers-by-query"`
|
|
|
|
AddSubscribersToListsByQuery string `query:"add-subscribers-to-lists-by-query"`
|
|
|
|
BlacklistSubscribersByQuery string `query:"blacklist-subscribers-by-query"`
|
|
|
|
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"`
|
|
|
|
GetLists *sqlx.Stmt `query:"get-lists"`
|
|
|
|
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
|
|
|
|
|
|
|
CreateCampaign *sqlx.Stmt `query:"create-campaign"`
|
2019-03-30 07:01:24 +00:00
|
|
|
QueryCampaigns *sqlx.Stmt `query:"query-campaigns"`
|
|
|
|
GetCampaign *sqlx.Stmt `query:"get-campaign"`
|
2018-10-26 05:48:17 +00:00
|
|
|
GetCampaignForPreview *sqlx.Stmt `query:"get-campaign-for-preview"`
|
2018-10-25 13:51:47 +00:00
|
|
|
GetCampaignStats *sqlx.Stmt `query:"get-campaign-stats"`
|
2019-04-01 11:37:24 +00:00
|
|
|
GetCampaignStatus *sqlx.Stmt `query:"get-campaign-status"`
|
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"`
|
|
|
|
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"`
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// GetStats *sqlx.Stmt `query:"get-stats"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectDB initializes a database connection.
|
2019-05-27 11:46:46 +00:00
|
|
|
func connectDB(host string, port int, user, pwd, dbName string, sslMode string) (*sqlx.DB, error) {
|
2018-10-25 13:51:47 +00:00
|
|
|
db, err := sqlx.Connect("postgres",
|
2019-05-27 11:46:46 +00:00
|
|
|
fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", host, port, user, pwd, dbName, sslMode))
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
2018-12-18 05:24:55 +00:00
|
|
|
|
|
|
|
// compileSubscriberQueryTpl takes a arbitrary WHERE expressions
|
|
|
|
// 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.
|
|
|
|
func (q *Queries) compileSubscriberQueryTpl(exp string, db *sqlx.DB) (string, error) {
|
|
|
|
tx, err := db.BeginTxx(context.Background(), &sql.TxOptions{ReadOnly: true})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
tx.Rollback()
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stmt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// compileSubscriberQueryTpl takes a arbitrary WHERE expressions and a subscriber
|
|
|
|
// query template that depends on the filter (eg: delete by query, blacklist by query etc.)
|
|
|
|
// combines and executes them.
|
|
|
|
func (q *Queries) execSubscriberQueryTpl(exp, tpl string, listIDs []int64, db *sqlx.DB, args ...interface{}) error {
|
|
|
|
// Perform a dry run.
|
|
|
|
filterExp, err := q.compileSubscriberQueryTpl(exp, db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(listIDs) == 0 {
|
|
|
|
listIDs = pq.Int64Array{}
|
|
|
|
}
|
|
|
|
// First argument is the boolean indicating if the query is a dry run.
|
|
|
|
a := append([]interface{}{false, pq.Int64Array(listIDs)}, args...)
|
|
|
|
if _, err := db.Exec(fmt.Sprintf(tpl, filterExp), a...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|