sqlstore

package
v0.4.0-rc.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sqlstore is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package sqlstore is a wechatpay.DeliveryStore over database/sql, written once for SQLite and Postgres and opened through a driver package: sqlitestore (with the pure-Go modernc.org/sqlite driver) or pgstore (with github.com/jackc/pgx/v5). It imports no driver itself, so New works with any *sql.DB of a supported dialect.

The schema is one table, created by New when missing and versioned in wechatpay_schema:

  • wechatpay_deliveries: out_trade_no (the key), the push's raw Payload in payload, and delivered_at in Unix milliseconds

Put inserts with ON CONFLICT DO NOTHING, so the first record of an order stays. On Postgres the store is also a wechatpay.DeliveryLocker: Lock takes a transaction-level advisory lock on the order, so a Handler in each of several processes delivers an order once.

Example
package main

import (
	"fmt"

	"github.com/nsgo-dev/game/pkg/online/storeverify/wechatpay/sqlstore"
)

func main() {
	fmt.Println(sqlstore.SchemaVersion)
}
Output:
1

Index

Examples

Constants

View Source
const SchemaVersion = 1

SchemaVersion is the schema version New creates and accepts.

Variables

View Source
var Postgres = Dialect{
	Name:        "postgres",
	Numbered:    true,
	Blob:        "BYTEA",
	MigrateLock: `SELECT pg_advisory_xact_lock(2004381817)`,
	LockOrder:   `SELECT pg_advisory_xact_lock(2004381817, hashtext(?))`,
}

Postgres is Postgres's dialect. Its locks use the arbitrary class key 2004381817 ("wxpy" in ASCII).

View Source
var SQLite = Dialect{Name: "sqlite", Blob: "BLOB"}

SQLite is SQLite's dialect. A SQLite store serves one process (sqlitestore.Open), whose Handler already serializes the handling of each order, so its Lock does not lock.

Functions

This section is empty.

Types

type Dialect

type Dialect struct {
	// Name names the database in errors.
	Name string
	// Numbered is true for "$1" placeholders, false for "?".
	Numbered bool
	// Blob is the column type of a byte string, such as BLOB or BYTEA.
	Blob string
	// MigrateLock, when set, runs first in the migration's transaction to serialize migrations of several processes.
	MigrateLock string
	// LockOrder, when set, runs in the transaction Lock opens with the OutTradeNo as its one parameter and holds a lock on the order until the transaction ends. Without it Lock only returns, which suits a database one process uses.
	LockOrder string
}

Dialect is what differs between SQL databases.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a wechatpay.DeliveryStore in a SQL database.

func New

func New(ctx context.Context, db *sql.DB, d Dialect) (*Store, error)

New returns a store on db, creating the schema when it is missing. It refuses a database whose schema is newer than SchemaVersion.

func (*Store) Close

func (s *Store) Close() error

Close closes the store's database.

func (*Store) DB

func (s *Store) DB() *sql.DB

DB returns the store's database.

func (*Store) Get

func (s *Store) Get(ctx context.Context, outTradeNo string) ([]byte, bool, error)

Get implements wechatpay.DeliveryStore.

func (*Store) Lock

func (s *Store) Lock(ctx context.Context, outTradeNo string) (func(), error)

Lock implements wechatpay.DeliveryLocker. With the dialect's LockOrder it opens a transaction holding the order's lock, which unlock rolls back; the transaction holds one of the database's connections until then, and ends by itself when ctx does. Get and Put take a second connection meanwhile, so a pool capped with SetMaxOpenConns must allow two connections for each order handled at once: with every connection holding a lock, Get waits until ctx ends. Without LockOrder it returns at once.

func (*Store) Put

func (s *Store) Put(ctx context.Context, outTradeNo string, payload []byte) error

Put implements wechatpay.DeliveryStore: it records outTradeNo as delivered now, and keeps the first record of an order already recorded.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL