// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.21.0
// source: person.sql

package db

import (
	"context"
	"database/sql"
	"time"
)

const createPerson = `-- name: CreatePerson :one
INSERT INTO persons (
    "customerID",
    firstname,
    lastname,
    birthday,
    city,
    zip,
    street,
    country,
    creator,
    changer
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10
) RETURNING "ID", "customerID", firstname, lastname, birthday, city, zip, street, country, creator, created, changer, changed
`

type CreatePersonParams struct {
	CustomerID int64     `json:"customerID"`
	Firstname  string    `json:"firstname"`
	Lastname   string    `json:"lastname"`
	Birthday   time.Time `json:"birthday"`
	City       string    `json:"city"`
	Zip        string    `json:"zip"`
	Street     string    `json:"street"`
	Country    string    `json:"country"`
	Creator    string    `json:"creator"`
	Changer    string    `json:"changer"`
}

func (q *Queries) CreatePerson(ctx context.Context, arg CreatePersonParams) (Person, error) {
	row := q.db.QueryRowContext(ctx, createPerson,
		arg.CustomerID,
		arg.Firstname,
		arg.Lastname,
		arg.Birthday,
		arg.City,
		arg.Zip,
		arg.Street,
		arg.Country,
		arg.Creator,
		arg.Changer,
	)
	var i Person
	err := row.Scan(
		&i.ID,
		&i.CustomerID,
		&i.Firstname,
		&i.Lastname,
		&i.Birthday,
		&i.City,
		&i.Zip,
		&i.Street,
		&i.Country,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const deletePerson = `-- name: DeletePerson :exec
DELETE FROM persons
WHERE "ID" = $1
`

func (q *Queries) DeletePerson(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, deletePerson, id)
	return err
}

const getPerson = `-- name: GetPerson :one
SELECT "ID", "customerID", firstname, lastname, birthday, city, zip, street, country, creator, created, changer, changed FROM persons
WHERE "ID" = $1 LIMIT 1
`

func (q *Queries) GetPerson(ctx context.Context, id int64) (Person, error) {
	row := q.db.QueryRowContext(ctx, getPerson, id)
	var i Person
	err := row.Scan(
		&i.ID,
		&i.CustomerID,
		&i.Firstname,
		&i.Lastname,
		&i.Birthday,
		&i.City,
		&i.Zip,
		&i.Street,
		&i.Country,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const listPersons = `-- name: ListPersons :many
SELECT "ID", "customerID", firstname, lastname, birthday, city, zip, street, country, creator, created, changer, changed FROM persons
ORDER BY lastname, firstname
LIMIT $1
OFFSET $2
`

type ListPersonsParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

func (q *Queries) ListPersons(ctx context.Context, arg ListPersonsParams) ([]Person, error) {
	rows, err := q.db.QueryContext(ctx, listPersons, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Person{}
	for rows.Next() {
		var i Person
		if err := rows.Scan(
			&i.ID,
			&i.CustomerID,
			&i.Firstname,
			&i.Lastname,
			&i.Birthday,
			&i.City,
			&i.Zip,
			&i.Street,
			&i.Country,
			&i.Creator,
			&i.Created,
			&i.Changer,
			&i.Changed,
		); 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 updatePerson = `-- name: UpdatePerson :one
UPDATE persons
SET
    "customerID" = COALESCE($3, "customerID"),
    firstname = COALESCE($4, firstname),
    lastname = COALESCE($5, lastname),
    birthday = COALESCE($6, birthday),
    city = COALESCE($7, city),
    zip = COALESCE($8, zip),
    street = COALESCE($9, street),
    country = COALESCE($10, country),
    changer = $2,
    changed = now()
WHERE "ID" = $1
RETURNING "ID", "customerID", firstname, lastname, birthday, city, zip, street, country, creator, created, changer, changed
`

type UpdatePersonParams struct {
	ID         int64          `json:"ID"`
	Changer    string         `json:"changer"`
	Customerid sql.NullInt64  `json:"customerid"`
	Firstname  sql.NullString `json:"firstname"`
	Lastname   sql.NullString `json:"lastname"`
	Birthday   sql.NullTime   `json:"birthday"`
	City       sql.NullString `json:"city"`
	Zip        sql.NullString `json:"zip"`
	Street     sql.NullString `json:"street"`
	Country    sql.NullString `json:"country"`
}

func (q *Queries) UpdatePerson(ctx context.Context, arg UpdatePersonParams) (Person, error) {
	row := q.db.QueryRowContext(ctx, updatePerson,
		arg.ID,
		arg.Changer,
		arg.Customerid,
		arg.Firstname,
		arg.Lastname,
		arg.Birthday,
		arg.City,
		arg.Zip,
		arg.Street,
		arg.Country,
	)
	var i Person
	err := row.Scan(
		&i.ID,
		&i.CustomerID,
		&i.Firstname,
		&i.Lastname,
		&i.Birthday,
		&i.City,
		&i.Zip,
		&i.Street,
		&i.Country,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}