migrate nkode-core
This commit is contained in:
31
sqlc/db.go
Normal file
31
sqlc/db.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
||||
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
||||
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
||||
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
50
sqlc/models.go
Normal file
50
sqlc/models.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Customer struct {
|
||||
ID string
|
||||
MaxNkodeLen int64
|
||||
MinNkodeLen int64
|
||||
DistinctSets int64
|
||||
DistinctAttributes int64
|
||||
LockOut int64
|
||||
Expiration int64
|
||||
AttributeValues []byte
|
||||
SetValues []byte
|
||||
LastRenew string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
type SvgIcon struct {
|
||||
ID int64
|
||||
Svg string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID string
|
||||
Email string
|
||||
Renew int64
|
||||
RefreshToken sql.NullString
|
||||
CustomerID string
|
||||
Code string
|
||||
Mask string
|
||||
AttributesPerKey int64
|
||||
NumberOfKeys int64
|
||||
AlphaKey []byte
|
||||
SetKey []byte
|
||||
PassKey []byte
|
||||
MaskKey []byte
|
||||
Salt []byte
|
||||
MaxNkodeLen int64
|
||||
IdxInterface []byte
|
||||
SvgIDInterface []byte
|
||||
LastLogin interface{}
|
||||
CreatedAt sql.NullString
|
||||
}
|
||||
478
sqlc/query.sql.go
Normal file
478
sqlc/query.sql.go
Normal file
@@ -0,0 +1,478 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// source: query.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const createCustomer = `-- name: CreateCustomer :exec
|
||||
INSERT INTO customer (
|
||||
id
|
||||
,max_nkode_len
|
||||
,min_nkode_len
|
||||
,distinct_sets
|
||||
,distinct_attributes
|
||||
,lock_out
|
||||
,expiration
|
||||
,attribute_values
|
||||
,set_values
|
||||
,last_renew
|
||||
,created_at
|
||||
)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?)
|
||||
`
|
||||
|
||||
type CreateCustomerParams struct {
|
||||
ID string
|
||||
MaxNkodeLen int64
|
||||
MinNkodeLen int64
|
||||
DistinctSets int64
|
||||
DistinctAttributes int64
|
||||
LockOut int64
|
||||
Expiration int64
|
||||
AttributeValues []byte
|
||||
SetValues []byte
|
||||
LastRenew string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateCustomer(ctx context.Context, arg CreateCustomerParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createCustomer,
|
||||
arg.ID,
|
||||
arg.MaxNkodeLen,
|
||||
arg.MinNkodeLen,
|
||||
arg.DistinctSets,
|
||||
arg.DistinctAttributes,
|
||||
arg.LockOut,
|
||||
arg.Expiration,
|
||||
arg.AttributeValues,
|
||||
arg.SetValues,
|
||||
arg.LastRenew,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const createUser = `-- name: CreateUser :exec
|
||||
INSERT INTO user (
|
||||
id
|
||||
,email
|
||||
,renew
|
||||
,refresh_token
|
||||
,customer_id
|
||||
,code
|
||||
,mask
|
||||
,attributes_per_key
|
||||
,number_of_keys
|
||||
,alpha_key
|
||||
,set_key
|
||||
,pass_key
|
||||
,mask_key
|
||||
,salt
|
||||
,max_nkode_len
|
||||
,idx_interface
|
||||
,svg_id_interface
|
||||
,created_at
|
||||
)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
ID string
|
||||
Email string
|
||||
Renew int64
|
||||
RefreshToken sql.NullString
|
||||
CustomerID string
|
||||
Code string
|
||||
Mask string
|
||||
AttributesPerKey int64
|
||||
NumberOfKeys int64
|
||||
AlphaKey []byte
|
||||
SetKey []byte
|
||||
PassKey []byte
|
||||
MaskKey []byte
|
||||
Salt []byte
|
||||
MaxNkodeLen int64
|
||||
IdxInterface []byte
|
||||
SvgIDInterface []byte
|
||||
CreatedAt sql.NullString
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createUser,
|
||||
arg.ID,
|
||||
arg.Email,
|
||||
arg.Renew,
|
||||
arg.RefreshToken,
|
||||
arg.CustomerID,
|
||||
arg.Code,
|
||||
arg.Mask,
|
||||
arg.AttributesPerKey,
|
||||
arg.NumberOfKeys,
|
||||
arg.AlphaKey,
|
||||
arg.SetKey,
|
||||
arg.PassKey,
|
||||
arg.MaskKey,
|
||||
arg.Salt,
|
||||
arg.MaxNkodeLen,
|
||||
arg.IdxInterface,
|
||||
arg.SvgIDInterface,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const getCustomer = `-- name: GetCustomer :one
|
||||
SELECT
|
||||
max_nkode_len
|
||||
,min_nkode_len
|
||||
,distinct_sets
|
||||
,distinct_attributes
|
||||
,lock_out
|
||||
,expiration
|
||||
,attribute_values
|
||||
,set_values
|
||||
FROM customer
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
type GetCustomerRow struct {
|
||||
MaxNkodeLen int64
|
||||
MinNkodeLen int64
|
||||
DistinctSets int64
|
||||
DistinctAttributes int64
|
||||
LockOut int64
|
||||
Expiration int64
|
||||
AttributeValues []byte
|
||||
SetValues []byte
|
||||
}
|
||||
|
||||
func (q *Queries) GetCustomer(ctx context.Context, id string) (GetCustomerRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getCustomer, id)
|
||||
var i GetCustomerRow
|
||||
err := row.Scan(
|
||||
&i.MaxNkodeLen,
|
||||
&i.MinNkodeLen,
|
||||
&i.DistinctSets,
|
||||
&i.DistinctAttributes,
|
||||
&i.LockOut,
|
||||
&i.Expiration,
|
||||
&i.AttributeValues,
|
||||
&i.SetValues,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSvgCount = `-- name: GetSvgCount :one
|
||||
SELECT COUNT(*) as count FROM svg_icon
|
||||
`
|
||||
|
||||
func (q *Queries) GetSvgCount(ctx context.Context) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, getSvgCount)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const getSvgId = `-- name: GetSvgId :one
|
||||
SELECT svg
|
||||
FROM svg_icon
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) GetSvgId(ctx context.Context, id int64) (string, error) {
|
||||
row := q.db.QueryRowContext(ctx, getSvgId, id)
|
||||
var svg string
|
||||
err := row.Scan(&svg)
|
||||
return svg, err
|
||||
}
|
||||
|
||||
const getUser = `-- name: GetUser :one
|
||||
SELECT
|
||||
id
|
||||
,renew
|
||||
,refresh_token
|
||||
,code
|
||||
,mask
|
||||
,attributes_per_key
|
||||
,number_of_keys
|
||||
,alpha_key
|
||||
,set_key
|
||||
,pass_key
|
||||
,mask_key
|
||||
,salt
|
||||
,max_nkode_len
|
||||
,idx_interface
|
||||
,svg_id_interface
|
||||
FROM user
|
||||
WHERE user.email = ? AND user.customer_id = ?
|
||||
`
|
||||
|
||||
type GetUserParams struct {
|
||||
Email string
|
||||
CustomerID string
|
||||
}
|
||||
|
||||
type GetUserRow struct {
|
||||
ID string
|
||||
Renew int64
|
||||
RefreshToken sql.NullString
|
||||
Code string
|
||||
Mask string
|
||||
AttributesPerKey int64
|
||||
NumberOfKeys int64
|
||||
AlphaKey []byte
|
||||
SetKey []byte
|
||||
PassKey []byte
|
||||
MaskKey []byte
|
||||
Salt []byte
|
||||
MaxNkodeLen int64
|
||||
IdxInterface []byte
|
||||
SvgIDInterface []byte
|
||||
}
|
||||
|
||||
func (q *Queries) GetUser(ctx context.Context, arg GetUserParams) (GetUserRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getUser, arg.Email, arg.CustomerID)
|
||||
var i GetUserRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Renew,
|
||||
&i.RefreshToken,
|
||||
&i.Code,
|
||||
&i.Mask,
|
||||
&i.AttributesPerKey,
|
||||
&i.NumberOfKeys,
|
||||
&i.AlphaKey,
|
||||
&i.SetKey,
|
||||
&i.PassKey,
|
||||
&i.MaskKey,
|
||||
&i.Salt,
|
||||
&i.MaxNkodeLen,
|
||||
&i.IdxInterface,
|
||||
&i.SvgIDInterface,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserRenew = `-- name: GetUserRenew :many
|
||||
SELECT
|
||||
id
|
||||
,alpha_key
|
||||
,set_key
|
||||
,attributes_per_key
|
||||
,number_of_keys
|
||||
FROM user
|
||||
WHERE customer_id = ?
|
||||
`
|
||||
|
||||
type GetUserRenewRow struct {
|
||||
ID string
|
||||
AlphaKey []byte
|
||||
SetKey []byte
|
||||
AttributesPerKey int64
|
||||
NumberOfKeys int64
|
||||
}
|
||||
|
||||
func (q *Queries) GetUserRenew(ctx context.Context, customerID string) ([]GetUserRenewRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getUserRenew, customerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetUserRenewRow
|
||||
for rows.Next() {
|
||||
var i GetUserRenewRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.AlphaKey,
|
||||
&i.SetKey,
|
||||
&i.AttributesPerKey,
|
||||
&i.NumberOfKeys,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const refreshUserPasscode = `-- name: RefreshUserPasscode :exec
|
||||
UPDATE user
|
||||
SET
|
||||
renew = ?
|
||||
,code = ?
|
||||
,mask = ?
|
||||
,alpha_key = ?
|
||||
,set_key = ?
|
||||
,pass_key = ?
|
||||
,mask_key = ?
|
||||
,salt = ?
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
type RefreshUserPasscodeParams struct {
|
||||
Renew int64
|
||||
Code string
|
||||
Mask string
|
||||
AlphaKey []byte
|
||||
SetKey []byte
|
||||
PassKey []byte
|
||||
MaskKey []byte
|
||||
Salt []byte
|
||||
ID string
|
||||
}
|
||||
|
||||
func (q *Queries) RefreshUserPasscode(ctx context.Context, arg RefreshUserPasscodeParams) error {
|
||||
_, err := q.db.ExecContext(ctx, refreshUserPasscode,
|
||||
arg.Renew,
|
||||
arg.Code,
|
||||
arg.Mask,
|
||||
arg.AlphaKey,
|
||||
arg.SetKey,
|
||||
arg.PassKey,
|
||||
arg.MaskKey,
|
||||
arg.Salt,
|
||||
arg.ID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const renewCustomer = `-- name: RenewCustomer :exec
|
||||
UPDATE customer
|
||||
SET attribute_values = ?, set_values = ?
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
type RenewCustomerParams struct {
|
||||
AttributeValues []byte
|
||||
SetValues []byte
|
||||
ID string
|
||||
}
|
||||
|
||||
func (q *Queries) RenewCustomer(ctx context.Context, arg RenewCustomerParams) error {
|
||||
_, err := q.db.ExecContext(ctx, renewCustomer, arg.AttributeValues, arg.SetValues, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const renewUser = `-- name: RenewUser :exec
|
||||
UPDATE user
|
||||
SET alpha_key = ?, set_key = ?, renew = ?
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
type RenewUserParams struct {
|
||||
AlphaKey []byte
|
||||
SetKey []byte
|
||||
Renew int64
|
||||
ID string
|
||||
}
|
||||
|
||||
func (q *Queries) RenewUser(ctx context.Context, arg RenewUserParams) error {
|
||||
_, err := q.db.ExecContext(ctx, renewUser,
|
||||
arg.AlphaKey,
|
||||
arg.SetKey,
|
||||
arg.Renew,
|
||||
arg.ID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateUser = `-- name: UpdateUser :exec
|
||||
UPDATE user
|
||||
SET renew = ?
|
||||
,refresh_token = ?
|
||||
,code = ?
|
||||
,mask = ?
|
||||
,attributes_per_key = ?
|
||||
,number_of_keys = ?
|
||||
,alpha_key = ?
|
||||
,set_key = ?
|
||||
,pass_key = ?
|
||||
,mask_key = ?
|
||||
,salt = ?
|
||||
,max_nkode_len = ?
|
||||
,idx_interface = ?
|
||||
,svg_id_interface = ?
|
||||
WHERE email = ? AND customer_id = ?
|
||||
`
|
||||
|
||||
type UpdateUserParams struct {
|
||||
Renew int64
|
||||
RefreshToken sql.NullString
|
||||
Code string
|
||||
Mask string
|
||||
AttributesPerKey int64
|
||||
NumberOfKeys int64
|
||||
AlphaKey []byte
|
||||
SetKey []byte
|
||||
PassKey []byte
|
||||
MaskKey []byte
|
||||
Salt []byte
|
||||
MaxNkodeLen int64
|
||||
IdxInterface []byte
|
||||
SvgIDInterface []byte
|
||||
Email string
|
||||
CustomerID string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateUser,
|
||||
arg.Renew,
|
||||
arg.RefreshToken,
|
||||
arg.Code,
|
||||
arg.Mask,
|
||||
arg.AttributesPerKey,
|
||||
arg.NumberOfKeys,
|
||||
arg.AlphaKey,
|
||||
arg.SetKey,
|
||||
arg.PassKey,
|
||||
arg.MaskKey,
|
||||
arg.Salt,
|
||||
arg.MaxNkodeLen,
|
||||
arg.IdxInterface,
|
||||
arg.SvgIDInterface,
|
||||
arg.Email,
|
||||
arg.CustomerID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateUserInterface = `-- name: UpdateUserInterface :exec
|
||||
UPDATE user SET idx_interface = ?, last_login = ? WHERE id = ?
|
||||
`
|
||||
|
||||
type UpdateUserInterfaceParams struct {
|
||||
IdxInterface []byte
|
||||
LastLogin interface{}
|
||||
ID string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUserInterface(ctx context.Context, arg UpdateUserInterfaceParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateUserInterface, arg.IdxInterface, arg.LastLogin, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateUserRefreshToken = `-- name: UpdateUserRefreshToken :exec
|
||||
UPDATE user SET refresh_token = ? WHERE id = ?
|
||||
`
|
||||
|
||||
type UpdateUserRefreshTokenParams struct {
|
||||
RefreshToken sql.NullString
|
||||
ID string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUserRefreshToken(ctx context.Context, arg UpdateUserRefreshTokenParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateUserRefreshToken, arg.RefreshToken, arg.ID)
|
||||
return err
|
||||
}
|
||||
93
sqlc/sqlite_queue.go
Normal file
93
sqlc/sqlite_queue.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const writeBufferSize = 100
|
||||
|
||||
type SqlcGeneric func(*Queries, context.Context, any) error
|
||||
|
||||
type WriteTx struct {
|
||||
ErrChan chan error
|
||||
Query SqlcGeneric
|
||||
Args interface{}
|
||||
}
|
||||
|
||||
type Queue struct {
|
||||
Queries *Queries
|
||||
Db *sql.DB
|
||||
WriteQueue chan WriteTx
|
||||
wg sync.WaitGroup
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func NewQueue(sqlDb *sql.DB, ctx context.Context) (*Queue, error) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
sqldb := &Queue{
|
||||
Queries: New(sqlDb),
|
||||
Db: sqlDb,
|
||||
WriteQueue: make(chan WriteTx, writeBufferSize),
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
|
||||
return sqldb, nil
|
||||
}
|
||||
|
||||
func (d *Queue) Start() {
|
||||
d.wg.Add(1)
|
||||
defer d.wg.Done()
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-d.ctx.Done():
|
||||
return
|
||||
case writeTx := <-d.WriteQueue:
|
||||
err := writeTx.Query(d.Queries, d.ctx, writeTx.Args)
|
||||
writeTx.ErrChan <- err
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (d *Queue) Stop() error {
|
||||
d.cancel()
|
||||
d.wg.Wait()
|
||||
close(d.WriteQueue)
|
||||
return d.Db.Close()
|
||||
}
|
||||
|
||||
func (d *Queue) EnqueueWriteTx(queryFunc SqlcGeneric, args any) error {
|
||||
select {
|
||||
case <-d.ctx.Done():
|
||||
return errors.New("database is shutting down")
|
||||
default:
|
||||
}
|
||||
|
||||
errChan := make(chan error, 1)
|
||||
writeTx := WriteTx{
|
||||
Query: queryFunc,
|
||||
Args: args,
|
||||
ErrChan: errChan,
|
||||
}
|
||||
d.WriteQueue <- writeTx
|
||||
return <-errChan
|
||||
}
|
||||
|
||||
func OpenSqliteDb(dbPath string) (*sql.DB, error) {
|
||||
sqliteDb, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open database: %w", err)
|
||||
}
|
||||
|
||||
if err := sqliteDb.Ping(); err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
||||
}
|
||||
return sqliteDb, nil
|
||||
}
|
||||
Reference in New Issue
Block a user