From 69ec9bd08cd613ede603ada72f8a404ddf456f75 Mon Sep 17 00:00:00 2001 From: Donovan Date: Tue, 3 Dec 2024 16:22:03 -0600 Subject: [PATCH] sqlc generate --- internal/sqlc/db.go | 31 +++ internal/sqlc/models.go | 50 ++++ internal/sqlc/query.sql.go | 478 +++++++++++++++++++++++++++++++++++++ sqlc.yaml | 9 + sqlite/query.sql | 136 +++++++++++ sqlite/schema.sql | 57 +++++ 6 files changed, 761 insertions(+) create mode 100644 internal/sqlc/db.go create mode 100644 internal/sqlc/models.go create mode 100644 internal/sqlc/query.sql.go create mode 100644 sqlc.yaml create mode 100644 sqlite/query.sql create mode 100644 sqlite/schema.sql diff --git a/internal/sqlc/db.go b/internal/sqlc/db.go new file mode 100644 index 0000000..2248616 --- /dev/null +++ b/internal/sqlc/db.go @@ -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, + } +} diff --git a/internal/sqlc/models.go b/internal/sqlc/models.go new file mode 100644 index 0000000..d8f68cf --- /dev/null +++ b/internal/sqlc/models.go @@ -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 +} diff --git a/internal/sqlc/query.sql.go b/internal/sqlc/query.sql.go new file mode 100644 index 0000000..c11071c --- /dev/null +++ b/internal/sqlc/query.sql.go @@ -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 +} diff --git a/sqlc.yaml b/sqlc.yaml new file mode 100644 index 0000000..eda576e --- /dev/null +++ b/sqlc.yaml @@ -0,0 +1,9 @@ +version: "2" +sql: + - engine: "sqlite" + queries: "./sqlite/query.sql" + schema: "./sqlite/schema.sql" + gen: + go: + package: "sqlc" + out: "./internal/sqlc" \ No newline at end of file diff --git a/sqlite/query.sql b/sqlite/query.sql new file mode 100644 index 0000000..b3c50cf --- /dev/null +++ b/sqlite/query.sql @@ -0,0 +1,136 @@ +-- 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 (?,?,?,?,?,?,?,?,?,?,?); + +-- 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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?); + +-- 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 = ?; + +-- name: UpdateUserInterface :exec +UPDATE user SET idx_interface = ?, last_login = ? WHERE id = ?; + +-- name: UpdateUserRefreshToken :exec +UPDATE user SET refresh_token = ? WHERE id = ?; + +-- name: RenewCustomer :exec +UPDATE customer +SET attribute_values = ?, set_values = ? +WHERE id = ?; + +-- name: RenewUser :exec +UPDATE user +SET alpha_key = ?, set_key = ?, renew = ? +WHERE id = ?; + +-- name: RefreshUserPasscode :exec +UPDATE user +SET + renew = ? + ,code = ? + ,mask = ? + ,alpha_key = ? + ,set_key = ? + ,pass_key = ? + ,mask_key = ? + ,salt = ? +WHERE id = ?; + +-- name: GetUserRenew :many +SELECT + id + ,alpha_key + ,set_key + ,attributes_per_key + ,number_of_keys +FROM user +WHERE customer_id = ?; + +-- 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 = ?; + +-- 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 = ?; + +-- name: GetSvgId :one +SELECT svg +FROM svg_icon +WHERE id = ?; + +-- name: GetSvgCount :one +SELECT COUNT(*) as count FROM svg_icon; diff --git a/sqlite/schema.sql b/sqlite/schema.sql new file mode 100644 index 0000000..4e30249 --- /dev/null +++ b/sqlite/schema.sql @@ -0,0 +1,57 @@ +PRAGMA journal_mode=WAL; + + +CREATE TABLE IF NOT EXISTS customer ( + id TEXT NOT NULL PRIMARY KEY + ,max_nkode_len INTEGER NOT NULL + ,min_nkode_len INTEGER NOT NULL + ,distinct_sets INTEGER NOT NULL + ,distinct_attributes INTEGER NOT NULL + ,lock_out INTEGER NOT NULL + ,expiration INTEGER NOT NULL + ,attribute_values BLOB NOT NULL + ,set_values BLOB NOT NULL + ,last_renew TEXT NOT NULL + ,created_at TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS user ( + id TEXT NOT NULL PRIMARY KEY + ,email TEXT NOT NULL +-- first_name TEXT NOT NULL +-- last_name TEXT NOT NULL + ,renew INT NOT NULL + ,refresh_token TEXT + ,customer_id TEXT NOT NULL + +-- Enciphered Passcode + ,code TEXT NOT NULL + ,mask TEXT NOT NULL + +-- Keypad Dimensions + ,attributes_per_key INT NOT NULL + ,number_of_keys INT NOT NULL + +-- User Keys + ,alpha_key BLOB NOT NULL + ,set_key BLOB NOT NULL + ,pass_key BLOB NOT NULL + ,mask_key BLOB NOT NULL + ,salt BLOB NOT NULL + ,max_nkode_len INT NOT NULL + +-- User Interface + ,idx_interface BLOB NOT NULL + ,svg_id_interface BLOB NOT NULL + + ,last_login TEXT NULL + ,created_at TEXT + + ,FOREIGN KEY (customer_id) REFERENCES customer(id) + ,UNIQUE(customer_id, email) +); + +CREATE TABLE IF NOT EXISTS svg_icon ( + id INTEGER PRIMARY KEY AUTOINCREMENT + ,svg TEXT NOT NULL +);