// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.28.0
// source: subcounty.sql

package db

import (
	"context"
	"database/sql"
)

const allSubcounties = `-- name: AllSubcounties :many
SELECT subcounties.name subcounty_name,subcounties.id as subcounty_id,
subcounties.district_id, districts.name district_name,
subcounties.code from subcounties
JOIN   districts on districts.id = subcounties.district_id
`

type AllSubcountiesRow struct {
	SubcountyName string `json:"subcounty_name"`
	SubcountyID   int64  `json:"subcounty_id"`
	DistrictID    int32  `json:"district_id"`
	DistrictName  string `json:"district_name"`
	Code          string `json:"code"`
}

func (q *Queries) AllSubcounties(ctx context.Context) ([]AllSubcountiesRow, error) {
	rows, err := q.db.QueryContext(ctx, allSubcounties)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []AllSubcountiesRow{}
	for rows.Next() {
		var i AllSubcountiesRow
		if err := rows.Scan(
			&i.SubcountyName,
			&i.SubcountyID,
			&i.DistrictID,
			&i.DistrictName,
			&i.Code,
		); 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 allSubcountiesForDistrict = `-- name: AllSubcountiesForDistrict :many
SELECT id, code, name, district_id, created_at FROM subcounties
WHERE district_id = $1
`

func (q *Queries) AllSubcountiesForDistrict(ctx context.Context, districtID int32) ([]Subcounty, error) {
	rows, err := q.db.QueryContext(ctx, allSubcountiesForDistrict, districtID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Subcounty{}
	for rows.Next() {
		var i Subcounty
		if err := rows.Scan(
			&i.ID,
			&i.Code,
			&i.Name,
			&i.DistrictID,
			&i.CreatedAt,
		); 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 createSubCounty = `-- name: CreateSubCounty :one
INSERT INTO subcounties (
     name,
     district_id,
     code
 ) VALUES (
    $1, $2, $3
)
RETURNING id, code, name, district_id, created_at
`

type CreateSubCountyParams struct {
	Name       string `json:"name"`
	DistrictID int32  `json:"district_id"`
	Code       string `json:"code"`
}

func (q *Queries) CreateSubCounty(ctx context.Context, arg CreateSubCountyParams) (Subcounty, error) {
	row := q.db.QueryRowContext(ctx, createSubCounty, arg.Name, arg.DistrictID, arg.Code)
	var i Subcounty
	err := row.Scan(
		&i.ID,
		&i.Code,
		&i.Name,
		&i.DistrictID,
		&i.CreatedAt,
	)
	return i, err
}

const getSubCounty = `-- name: GetSubCounty :one
SELECT id, code, name, district_id, created_at FROM subcounties
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetSubCounty(ctx context.Context, id int64) (Subcounty, error) {
	row := q.db.QueryRowContext(ctx, getSubCounty, id)
	var i Subcounty
	err := row.Scan(
		&i.ID,
		&i.Code,
		&i.Name,
		&i.DistrictID,
		&i.CreatedAt,
	)
	return i, err
}

const getSubCountyByName = `-- name: GetSubCountyByName :one
SELECT id, code, name, district_id, created_at FROM subcounties
WHERE name = $1
`

func (q *Queries) GetSubCountyByName(ctx context.Context, name string) (Subcounty, error) {
	row := q.db.QueryRowContext(ctx, getSubCountyByName, name)
	var i Subcounty
	err := row.Scan(
		&i.ID,
		&i.Code,
		&i.Name,
		&i.DistrictID,
		&i.CreatedAt,
	)
	return i, err
}

const getSubcountyIdByName = `-- name: GetSubcountyIdByName :one
SELECT id FROM subcounties
WHERE name = $1
`

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

const getSubcountyPerDistrict = `-- name: GetSubcountyPerDistrict :many
SELECT id, code, name, district_id, created_at FROM subcounties
WHERE district_id = $1
`

func (q *Queries) GetSubcountyPerDistrict(ctx context.Context, districtID int32) ([]Subcounty, error) {
	rows, err := q.db.QueryContext(ctx, getSubcountyPerDistrict, districtID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Subcounty{}
	for rows.Next() {
		var i Subcounty
		if err := rows.Scan(
			&i.ID,
			&i.Code,
			&i.Name,
			&i.DistrictID,
			&i.CreatedAt,
		); 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 listSubCounties = `-- name: ListSubCounties :many
SELECT subcounties.name,subcounties.id as subcounty_id,districts.id as district_id, parishes.code, parishes.parish FROM subcounties
JOIN   parishes  ON parishes.subcounty_id = subcounties.id
JOIN   districts ON districts.id          = subcounties.district_id
LIMIT $1 OFFSET $2
`

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

type ListSubCountiesRow struct {
	Name        string `json:"name"`
	SubcountyID int64  `json:"subcounty_id"`
	DistrictID  int64  `json:"district_id"`
	Code        string `json:"code"`
	Parish      string `json:"parish"`
}

func (q *Queries) ListSubCounties(ctx context.Context, arg ListSubCountiesParams) ([]ListSubCountiesRow, error) {
	rows, err := q.db.QueryContext(ctx, listSubCounties, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListSubCountiesRow{}
	for rows.Next() {
		var i ListSubCountiesRow
		if err := rows.Scan(
			&i.Name,
			&i.SubcountyID,
			&i.DistrictID,
			&i.Code,
			&i.Parish,
		); 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 updateSubCounty = `-- name: UpdateSubCounty :one
UPDATE subcounties
  set
     name         = COALESCE($1, name),
     district_id    = COALESCE($2, district_id),
     code         = COALESCE($3, code)

WHERE id = $4
RETURNING id, code, name, district_id, created_at
`

type UpdateSubCountyParams struct {
	Name       sql.NullString `json:"name"`
	DistrictID sql.NullInt32  `json:"district_id"`
	Code       sql.NullString `json:"code"`
	ID         int64          `json:"id"`
}

func (q *Queries) UpdateSubCounty(ctx context.Context, arg UpdateSubCountyParams) (Subcounty, error) {
	row := q.db.QueryRowContext(ctx, updateSubCounty,
		arg.Name,
		arg.DistrictID,
		arg.Code,
		arg.ID,
	)
	var i Subcounty
	err := row.Scan(
		&i.ID,
		&i.Code,
		&i.Name,
		&i.DistrictID,
		&i.CreatedAt,
	)
	return i, err
}
