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

package db

import (
	"context"
	"database/sql"
)

const allCountries = `-- name: AllCountries :many
SELECT id, countrycode, countryname, created_at, dialcodes FROM countries
`

func (q *Queries) AllCountries(ctx context.Context) ([]Country, error) {
	rows, err := q.db.QueryContext(ctx, allCountries)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Country{}
	for rows.Next() {
		var i Country
		if err := rows.Scan(
			&i.ID,
			&i.Countrycode,
			&i.Countryname,
			&i.CreatedAt,
			&i.Dialcodes,
		); 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 createCountry = `-- name: CreateCountry :one
INSERT INTO countries (
    countrycode,
    countryname
 ) VALUES (
    $1, $2
 ) RETURNING id, countrycode, countryname, created_at, dialcodes
`

type CreateCountryParams struct {
	Countrycode string `json:"countrycode"`
	Countryname string `json:"countryname"`
}

func (q *Queries) CreateCountry(ctx context.Context, arg CreateCountryParams) (Country, error) {
	row := q.db.QueryRowContext(ctx, createCountry, arg.Countrycode, arg.Countryname)
	var i Country
	err := row.Scan(
		&i.ID,
		&i.Countrycode,
		&i.Countryname,
		&i.CreatedAt,
		&i.Dialcodes,
	)
	return i, err
}

const deleteCountry = `-- name: DeleteCountry :exec
DELETE FROM countries WHERE id= $1
`

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

const getCountry = `-- name: GetCountry :one
SELECT id, countrycode, countryname, created_at, dialcodes FROM countries
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetCountry(ctx context.Context, id int64) (Country, error) {
	row := q.db.QueryRowContext(ctx, getCountry, id)
	var i Country
	err := row.Scan(
		&i.ID,
		&i.Countrycode,
		&i.Countryname,
		&i.CreatedAt,
		&i.Dialcodes,
	)
	return i, err
}

const importDialCodes = `-- name: ImportDialCodes :exec
UPDATE countries
  set 
    dialcodes= $2
WHERE countrycode = $1
RETURNING id, countrycode, countryname, created_at, dialcodes
`

type ImportDialCodesParams struct {
	Countrycode string         `json:"countrycode"`
	Dialcodes   sql.NullString `json:"dialcodes"`
}

func (q *Queries) ImportDialCodes(ctx context.Context, arg ImportDialCodesParams) error {
	_, err := q.db.ExecContext(ctx, importDialCodes, arg.Countrycode, arg.Dialcodes)
	return err
}

const listCountries = `-- name: ListCountries :many
SELECT id, countrycode, countryname, created_at, dialcodes FROM countries
ORDER BY id LIMIT $1
OFFSET $2
`

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

func (q *Queries) ListCountries(ctx context.Context, arg ListCountriesParams) ([]Country, error) {
	rows, err := q.db.QueryContext(ctx, listCountries, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Country{}
	for rows.Next() {
		var i Country
		if err := rows.Scan(
			&i.ID,
			&i.Countrycode,
			&i.Countryname,
			&i.CreatedAt,
			&i.Dialcodes,
		); 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 updateCountry = `-- name: UpdateCountry :one
UPDATE countries
  set 
    countrycode= $2,
    countryname= $3
WHERE id = $1
RETURNING id, countrycode, countryname, created_at, dialcodes
`

type UpdateCountryParams struct {
	ID          int64  `json:"id"`
	Countrycode string `json:"countrycode"`
	Countryname string `json:"countryname"`
}

func (q *Queries) UpdateCountry(ctx context.Context, arg UpdateCountryParams) (Country, error) {
	row := q.db.QueryRowContext(ctx, updateCountry, arg.ID, arg.Countrycode, arg.Countryname)
	var i Country
	err := row.Scan(
		&i.ID,
		&i.Countrycode,
		&i.Countryname,
		&i.CreatedAt,
		&i.Dialcodes,
	)
	return i, err
}
