// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.24.0
// source: query.sql

package sqlitedb

import (
	"context"
	"strings"
	"time"
)

const addGraphEncryptKey = `-- name: AddGraphEncryptKey :exec
INSERT INTO graph_encrypt_keys (
  id, graph_id, encrypted_private_key, public_key
) VALUES (
  ?, ?, ?, ?
)
`

type AddGraphEncryptKeyParams struct {
	ID                  string
	GraphID             string
	EncryptedPrivateKey string
	PublicKey           string
}

func (q *Queries) AddGraphEncryptKey(ctx context.Context, arg AddGraphEncryptKeyParams) error {
	_, err := q.db.ExecContext(ctx, addGraphEncryptKey,
		arg.ID,
		arg.GraphID,
		arg.EncryptedPrivateKey,
		arg.PublicKey,
	)
	return err
}

const addGraphSalt = `-- name: AddGraphSalt :exec
INSERT INTO graph_salts (
  id, graph_id, value, expires_at
) VALUES (
  ?, ?, ?, ?
)
`

type AddGraphSaltParams struct {
	ID        string
	GraphID   string
	Value     []byte
	ExpiresAt time.Time
}

func (q *Queries) AddGraphSalt(ctx context.Context, arg AddGraphSaltParams) error {
	_, err := q.db.ExecContext(ctx, addGraphSalt,
		arg.ID,
		arg.GraphID,
		arg.Value,
		arg.ExpiresAt,
	)
	return err
}

const allFileMeta = `-- name: AllFileMeta :many
SELECT id, graph_id, file_id, blob_path, checksum, size, last_modified_at, last_modified_tx
FROM file_metas
WHERE graph_id = ?
`

func (q *Queries) AllFileMeta(ctx context.Context, graphID string) ([]FileMeta, error) {
	rows, err := q.db.QueryContext(ctx, allFileMeta, graphID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []FileMeta
	for rows.Next() {
		var i FileMeta
		if err := rows.Scan(
			&i.ID,
			&i.GraphID,
			&i.FileID,
			&i.BlobPath,
			&i.Checksum,
			&i.Size,
			&i.LastModifiedAt,
			&i.LastModifiedTx,
		); 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 batchFileMetas = `-- name: BatchFileMetas :many
SELECT id, graph_id, file_id, blob_path, checksum, size, last_modified_at, last_modified_tx
FROM file_metas
WHERE graph_id = ?
  AND file_id IN (/*SLICE:file_ids*/?)
`

type BatchFileMetasParams struct {
	GraphID string
	FileIds []string
}

func (q *Queries) BatchFileMetas(ctx context.Context, arg BatchFileMetasParams) ([]FileMeta, error) {
	query := batchFileMetas
	var queryParams []interface{}
	queryParams = append(queryParams, arg.GraphID)
	if len(arg.FileIds) > 0 {
		for _, v := range arg.FileIds {
			queryParams = append(queryParams, v)
		}
		query = strings.Replace(query, "/*SLICE:file_ids*/?", strings.Repeat(",?", len(arg.FileIds))[1:], 1)
	} else {
		query = strings.Replace(query, "/*SLICE:file_ids*/?", "NULL", 1)
	}
	rows, err := q.db.QueryContext(ctx, query, queryParams...)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []FileMeta
	for rows.Next() {
		var i FileMeta
		if err := rows.Scan(
			&i.ID,
			&i.GraphID,
			&i.FileID,
			&i.BlobPath,
			&i.Checksum,
			&i.Size,
			&i.LastModifiedAt,
			&i.LastModifiedTx,
		); 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 createGraph = `-- name: CreateGraph :one
INSERT INTO graphs (
  id, name, current_tx
) VALUES (
  ?, ?, 0
)
RETURNING current_tx
`

type CreateGraphParams struct {
	ID   string
	Name string
}

func (q *Queries) CreateGraph(ctx context.Context, arg CreateGraphParams) (int64, error) {
	row := q.db.QueryRowContext(ctx, createGraph, arg.ID, arg.Name)
	var current_tx int64
	err := row.Scan(&current_tx)
	return current_tx, err
}

const deleteGraph = `-- name: DeleteGraph :exec
DELETE FROM graphs
WHERE id = ?
`

func (q *Queries) DeleteGraph(ctx context.Context, id string) error {
	_, err := q.db.ExecContext(ctx, deleteGraph, id)
	return err
}

const graph = `-- name: Graph :one
SELECT id, name, current_tx
FROM graphs
WHERE id = ?
`

func (q *Queries) Graph(ctx context.Context, id string) (Graph, error) {
	row := q.db.QueryRowContext(ctx, graph, id)
	var i Graph
	err := row.Scan(&i.ID, &i.Name, &i.CurrentTx)
	return i, err
}

const graphEncryptKeys = `-- name: GraphEncryptKeys :many
SELECT id, graph_id, encrypted_private_key, public_key
FROM graph_encrypt_keys
WHERE graph_id = ?
`

func (q *Queries) GraphEncryptKeys(ctx context.Context, graphID string) ([]GraphEncryptKey, error) {
	rows, err := q.db.QueryContext(ctx, graphEncryptKeys, graphID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GraphEncryptKey
	for rows.Next() {
		var i GraphEncryptKey
		if err := rows.Scan(
			&i.ID,
			&i.GraphID,
			&i.EncryptedPrivateKey,
			&i.PublicKey,
		); 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 graphSalts = `-- name: GraphSalts :many
SELECT id, graph_id, value, expires_at
FROM graph_salts
WHERE graph_id = ?
`

func (q *Queries) GraphSalts(ctx context.Context, graphID string) ([]GraphSalt, error) {
	rows, err := q.db.QueryContext(ctx, graphSalts, graphID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GraphSalt
	for rows.Next() {
		var i GraphSalt
		if err := rows.Scan(
			&i.ID,
			&i.GraphID,
			&i.Value,
			&i.ExpiresAt,
		); 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 graphs = `-- name: Graphs :many
SELECT id, name, current_tx
FROM graphs
`

func (q *Queries) Graphs(ctx context.Context) ([]Graph, error) {
	rows, err := q.db.QueryContext(ctx, graphs)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Graph
	for rows.Next() {
		var i Graph
		if err := rows.Scan(&i.ID, &i.Name, &i.CurrentTx); 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 incrementTx = `-- name: IncrementTx :one
UPDATE graphs
SET current_tx = current_tx + 1
WHERE id = ?
RETURNING current_tx
`

func (q *Queries) IncrementTx(ctx context.Context, id string) (int64, error) {
	row := q.db.QueryRowContext(ctx, incrementTx, id)
	var current_tx int64
	err := row.Scan(&current_tx)
	return current_tx, err
}

const setFileMeta = `-- name: SetFileMeta :exec
INSERT INTO file_metas (id, graph_id, file_id, blob_path, checksum, size, last_modified_at, last_modified_tx)
  VALUES(?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?)
`

type SetFileMetaParams struct {
	ID             string
	GraphID        string
	FileID         string
	BlobPath       string
	Checksum       []byte
	Size           int64
	LastModifiedTx int64
}

func (q *Queries) SetFileMeta(ctx context.Context, arg SetFileMetaParams) error {
	_, err := q.db.ExecContext(ctx, setFileMeta,
		arg.ID,
		arg.GraphID,
		arg.FileID,
		arg.BlobPath,
		arg.Checksum,
		arg.Size,
		arg.LastModifiedTx,
	)
	return err
}

const setTx = `-- name: SetTx :exec
UPDATE graphs
SET current_tx = ?
WHERE id = ?
`

type SetTxParams struct {
	CurrentTx int64
	ID        string
}

func (q *Queries) SetTx(ctx context.Context, arg SetTxParams) error {
	_, err := q.db.ExecContext(ctx, setTx, arg.CurrentTx, arg.ID)
	return err
}
