// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.27.0
// source: databases.sql

package sqlcdb

import (
	"context"
	"database/sql"
)

const countDatabaseServicesByUserAndName = `-- name: CountDatabaseServicesByUserAndName :one
SELECT COUNT(*)
FROM database_services
WHERE user_id = $1 AND LOWER(name) = LOWER($2)
`

type CountDatabaseServicesByUserAndNameParams struct {
	UserID string `json:"user_id"`
	Name   string `json:"name"`
}

func (q *Queries) CountDatabaseServicesByUserAndName(ctx context.Context, arg CountDatabaseServicesByUserAndNameParams) (int64, error) {
	row := q.db.QueryRowContext(ctx, countDatabaseServicesByUserAndName, arg.UserID, arg.Name)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createDatabaseBackup = `-- name: CreateDatabaseBackup :exec
INSERT INTO database_backups (id, database_id, size, status, backup_path, created_at)
VALUES ($1, $2, $3, $4, $5, $6)
`

type CreateDatabaseBackupParams struct {
	ID         string         `json:"id"`
	DatabaseID string         `json:"database_id"`
	Size       string         `json:"size"`
	Status     string         `json:"status"`
	BackupPath sql.NullString `json:"backup_path"`
	CreatedAt  sql.NullTime   `json:"created_at"`
}

func (q *Queries) CreateDatabaseBackup(ctx context.Context, arg CreateDatabaseBackupParams) error {
	_, err := q.db.ExecContext(ctx, createDatabaseBackup,
		arg.ID,
		arg.DatabaseID,
		arg.Size,
		arg.Status,
		arg.BackupPath,
		arg.CreatedAt,
	)
	return err
}

const createDatabaseService = `-- name: CreateDatabaseService :exec
INSERT INTO database_services (id, user_id, name, type, status, version, plan, region, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
`

type CreateDatabaseServiceParams struct {
	ID        string       `json:"id"`
	UserID    string       `json:"user_id"`
	Name      string       `json:"name"`
	Type      string       `json:"type"`
	Status    string       `json:"status"`
	Version   string       `json:"version"`
	Plan      string       `json:"plan"`
	Region    string       `json:"region"`
	CreatedAt sql.NullTime `json:"created_at"`
	UpdatedAt sql.NullTime `json:"updated_at"`
}

func (q *Queries) CreateDatabaseService(ctx context.Context, arg CreateDatabaseServiceParams) error {
	_, err := q.db.ExecContext(ctx, createDatabaseService,
		arg.ID,
		arg.UserID,
		arg.Name,
		arg.Type,
		arg.Status,
		arg.Version,
		arg.Plan,
		arg.Region,
		arg.CreatedAt,
		arg.UpdatedAt,
	)
	return err
}

const databaseServiceExistsByIDAndUser = `-- name: DatabaseServiceExistsByIDAndUser :one
SELECT EXISTS(
  SELECT 1 FROM database_services WHERE id = $1 AND user_id = $2
)
`

type DatabaseServiceExistsByIDAndUserParams struct {
	ID     string `json:"id"`
	UserID string `json:"user_id"`
}

func (q *Queries) DatabaseServiceExistsByIDAndUser(ctx context.Context, arg DatabaseServiceExistsByIDAndUserParams) (bool, error) {
	row := q.db.QueryRowContext(ctx, databaseServiceExistsByIDAndUser, arg.ID, arg.UserID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const deleteDatabaseServiceByIDAndUser = `-- name: DeleteDatabaseServiceByIDAndUser :exec
DELETE FROM database_services
WHERE id = $1 AND user_id = $2
`

type DeleteDatabaseServiceByIDAndUserParams struct {
	ID     string `json:"id"`
	UserID string `json:"user_id"`
}

func (q *Queries) DeleteDatabaseServiceByIDAndUser(ctx context.Context, arg DeleteDatabaseServiceByIDAndUserParams) error {
	_, err := q.db.ExecContext(ctx, deleteDatabaseServiceByIDAndUser, arg.ID, arg.UserID)
	return err
}

const getDatabaseBackupByIDAndDatabaseAndUser = `-- name: GetDatabaseBackupByIDAndDatabaseAndUser :one
SELECT b.id, b.database_id, b.size, b.status, b.backup_path, b.created_at, b.completed_at
FROM database_backups b
JOIN database_services s ON s.id = b.database_id
WHERE b.id = $1 AND b.database_id = $2 AND s.user_id = $3
`

type GetDatabaseBackupByIDAndDatabaseAndUserParams struct {
	ID         string `json:"id"`
	DatabaseID string `json:"database_id"`
	UserID     string `json:"user_id"`
}

func (q *Queries) GetDatabaseBackupByIDAndDatabaseAndUser(ctx context.Context, arg GetDatabaseBackupByIDAndDatabaseAndUserParams) (DatabaseBackup, error) {
	row := q.db.QueryRowContext(ctx, getDatabaseBackupByIDAndDatabaseAndUser, arg.ID, arg.DatabaseID, arg.UserID)
	var i DatabaseBackup
	err := row.Scan(
		&i.ID,
		&i.DatabaseID,
		&i.Size,
		&i.Status,
		&i.BackupPath,
		&i.CreatedAt,
		&i.CompletedAt,
	)
	return i, err
}

const getDatabaseServiceByIDAndUser = `-- name: GetDatabaseServiceByIDAndUser :one
SELECT id, name, type, status, version, plan, region, connection_url, created_at, updated_at
FROM database_services
WHERE id = $1 AND user_id = $2
`

type GetDatabaseServiceByIDAndUserParams struct {
	ID     string `json:"id"`
	UserID string `json:"user_id"`
}

type GetDatabaseServiceByIDAndUserRow struct {
	ID            string         `json:"id"`
	Name          string         `json:"name"`
	Type          string         `json:"type"`
	Status        string         `json:"status"`
	Version       string         `json:"version"`
	Plan          string         `json:"plan"`
	Region        string         `json:"region"`
	ConnectionUrl sql.NullString `json:"connection_url"`
	CreatedAt     sql.NullTime   `json:"created_at"`
	UpdatedAt     sql.NullTime   `json:"updated_at"`
}

func (q *Queries) GetDatabaseServiceByIDAndUser(ctx context.Context, arg GetDatabaseServiceByIDAndUserParams) (GetDatabaseServiceByIDAndUserRow, error) {
	row := q.db.QueryRowContext(ctx, getDatabaseServiceByIDAndUser, arg.ID, arg.UserID)
	var i GetDatabaseServiceByIDAndUserRow
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Type,
		&i.Status,
		&i.Version,
		&i.Plan,
		&i.Region,
		&i.ConnectionUrl,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listDatabaseBackupsByDatabaseAndUser = `-- name: ListDatabaseBackupsByDatabaseAndUser :many
SELECT b.id, b.database_id, b.size, b.status, b.backup_path, b.created_at, b.completed_at
FROM database_backups b
JOIN database_services s ON s.id = b.database_id
WHERE b.database_id = $1 AND s.user_id = $2
ORDER BY b.created_at DESC
LIMIT $3
`

type ListDatabaseBackupsByDatabaseAndUserParams struct {
	DatabaseID string `json:"database_id"`
	UserID     string `json:"user_id"`
	Limit      int32  `json:"limit"`
}

func (q *Queries) ListDatabaseBackupsByDatabaseAndUser(ctx context.Context, arg ListDatabaseBackupsByDatabaseAndUserParams) ([]DatabaseBackup, error) {
	rows, err := q.db.QueryContext(ctx, listDatabaseBackupsByDatabaseAndUser, arg.DatabaseID, arg.UserID, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []DatabaseBackup{}
	for rows.Next() {
		var i DatabaseBackup
		if err := rows.Scan(
			&i.ID,
			&i.DatabaseID,
			&i.Size,
			&i.Status,
			&i.BackupPath,
			&i.CreatedAt,
			&i.CompletedAt,
		); 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 listDatabaseServicesByUser = `-- name: ListDatabaseServicesByUser :many
SELECT id, name, type, status, version, plan, region, connection_url, created_at, updated_at
FROM database_services
WHERE user_id = $1
ORDER BY created_at DESC
`

type ListDatabaseServicesByUserRow struct {
	ID            string         `json:"id"`
	Name          string         `json:"name"`
	Type          string         `json:"type"`
	Status        string         `json:"status"`
	Version       string         `json:"version"`
	Plan          string         `json:"plan"`
	Region        string         `json:"region"`
	ConnectionUrl sql.NullString `json:"connection_url"`
	CreatedAt     sql.NullTime   `json:"created_at"`
	UpdatedAt     sql.NullTime   `json:"updated_at"`
}

func (q *Queries) ListDatabaseServicesByUser(ctx context.Context, userID string) ([]ListDatabaseServicesByUserRow, error) {
	rows, err := q.db.QueryContext(ctx, listDatabaseServicesByUser, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListDatabaseServicesByUserRow{}
	for rows.Next() {
		var i ListDatabaseServicesByUserRow
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Type,
			&i.Status,
			&i.Version,
			&i.Plan,
			&i.Region,
			&i.ConnectionUrl,
			&i.CreatedAt,
			&i.UpdatedAt,
		); 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 setDatabaseBackupStatusByID = `-- name: SetDatabaseBackupStatusByID :exec
UPDATE database_backups
SET status = $1, size = $2, completed_at = $3
WHERE id = $4
`

type SetDatabaseBackupStatusByIDParams struct {
	Status      string       `json:"status"`
	Size        string       `json:"size"`
	CompletedAt sql.NullTime `json:"completed_at"`
	ID          string       `json:"id"`
}

func (q *Queries) SetDatabaseBackupStatusByID(ctx context.Context, arg SetDatabaseBackupStatusByIDParams) error {
	_, err := q.db.ExecContext(ctx, setDatabaseBackupStatusByID,
		arg.Status,
		arg.Size,
		arg.CompletedAt,
		arg.ID,
	)
	return err
}

const setDatabaseServiceStatusAndConnectionByID = `-- name: SetDatabaseServiceStatusAndConnectionByID :exec
UPDATE database_services
SET status = $1, connection_url = $2, updated_at = $3
WHERE id = $4
`

type SetDatabaseServiceStatusAndConnectionByIDParams struct {
	Status        string         `json:"status"`
	ConnectionUrl sql.NullString `json:"connection_url"`
	UpdatedAt     sql.NullTime   `json:"updated_at"`
	ID            string         `json:"id"`
}

func (q *Queries) SetDatabaseServiceStatusAndConnectionByID(ctx context.Context, arg SetDatabaseServiceStatusAndConnectionByIDParams) error {
	_, err := q.db.ExecContext(ctx, setDatabaseServiceStatusAndConnectionByID,
		arg.Status,
		arg.ConnectionUrl,
		arg.UpdatedAt,
		arg.ID,
	)
	return err
}

const setDatabaseServiceStatusByID = `-- name: SetDatabaseServiceStatusByID :exec
UPDATE database_services
SET status = $1, updated_at = $2
WHERE id = $3
`

type SetDatabaseServiceStatusByIDParams struct {
	Status    string       `json:"status"`
	UpdatedAt sql.NullTime `json:"updated_at"`
	ID        string       `json:"id"`
}

func (q *Queries) SetDatabaseServiceStatusByID(ctx context.Context, arg SetDatabaseServiceStatusByIDParams) error {
	_, err := q.db.ExecContext(ctx, setDatabaseServiceStatusByID, arg.Status, arg.UpdatedAt, arg.ID)
	return err
}

const setDatabaseServiceStatusByIDAndUser = `-- name: SetDatabaseServiceStatusByIDAndUser :exec
UPDATE database_services
SET status = $1, updated_at = $2
WHERE id = $3 AND user_id = $4
`

type SetDatabaseServiceStatusByIDAndUserParams struct {
	Status    string       `json:"status"`
	UpdatedAt sql.NullTime `json:"updated_at"`
	ID        string       `json:"id"`
	UserID    string       `json:"user_id"`
}

func (q *Queries) SetDatabaseServiceStatusByIDAndUser(ctx context.Context, arg SetDatabaseServiceStatusByIDAndUserParams) error {
	_, err := q.db.ExecContext(ctx, setDatabaseServiceStatusByIDAndUser,
		arg.Status,
		arg.UpdatedAt,
		arg.ID,
		arg.UserID,
	)
	return err
}

const updateDatabaseServiceNameAndPlanByIDAndUser = `-- name: UpdateDatabaseServiceNameAndPlanByIDAndUser :exec
UPDATE database_services
SET name = $1, plan = $2, updated_at = $3
WHERE id = $4 AND user_id = $5
`

type UpdateDatabaseServiceNameAndPlanByIDAndUserParams struct {
	Name      string       `json:"name"`
	Plan      string       `json:"plan"`
	UpdatedAt sql.NullTime `json:"updated_at"`
	ID        string       `json:"id"`
	UserID    string       `json:"user_id"`
}

func (q *Queries) UpdateDatabaseServiceNameAndPlanByIDAndUser(ctx context.Context, arg UpdateDatabaseServiceNameAndPlanByIDAndUserParams) error {
	_, err := q.db.ExecContext(ctx, updateDatabaseServiceNameAndPlanByIDAndUser,
		arg.Name,
		arg.Plan,
		arg.UpdatedAt,
		arg.ID,
		arg.UserID,
	)
	return err
}

const updateDatabaseServiceNameByIDAndUser = `-- name: UpdateDatabaseServiceNameByIDAndUser :exec
UPDATE database_services
SET name = $1, updated_at = $2
WHERE id = $3 AND user_id = $4
`

type UpdateDatabaseServiceNameByIDAndUserParams struct {
	Name      string       `json:"name"`
	UpdatedAt sql.NullTime `json:"updated_at"`
	ID        string       `json:"id"`
	UserID    string       `json:"user_id"`
}

func (q *Queries) UpdateDatabaseServiceNameByIDAndUser(ctx context.Context, arg UpdateDatabaseServiceNameByIDAndUserParams) error {
	_, err := q.db.ExecContext(ctx, updateDatabaseServiceNameByIDAndUser,
		arg.Name,
		arg.UpdatedAt,
		arg.ID,
		arg.UserID,
	)
	return err
}

const updateDatabaseServicePlanByIDAndUser = `-- name: UpdateDatabaseServicePlanByIDAndUser :exec
UPDATE database_services
SET plan = $1, updated_at = $2
WHERE id = $3 AND user_id = $4
`

type UpdateDatabaseServicePlanByIDAndUserParams struct {
	Plan      string       `json:"plan"`
	UpdatedAt sql.NullTime `json:"updated_at"`
	ID        string       `json:"id"`
	UserID    string       `json:"user_id"`
}

func (q *Queries) UpdateDatabaseServicePlanByIDAndUser(ctx context.Context, arg UpdateDatabaseServicePlanByIDAndUserParams) error {
	_, err := q.db.ExecContext(ctx, updateDatabaseServicePlanByIDAndUser,
		arg.Plan,
		arg.UpdatedAt,
		arg.ID,
		arg.UserID,
	)
	return err
}
