Skip to content

Repeated named parameters not working correctly with sqlite #4110

@r-vdp

Description

@r-vdp

Version

1.30.0

What happened?

When using the same named parameter twice, the second instance of sqlc.arg is not properly replaced in the generated query.

I get the following go code:

const getEntity = `-- name: GetEntity :one
SELECT e1.expires_at, e1.payload, e1.owner_address, e1.created_at_block, e1.last_modified_at_block
FROM entities AS e1
WHERE e1.key = ?1
AND e1.deleted == FALSE
AND e1.last_modified_at_block <= ?2
AND NOT EXISTS (
  SELECT 1
  FROM entities AS e2
  WHERE e2.key = e1.key
  AND e2.last_modified_at_block > e1.last_modified_at_block
  AND e2.last_modified_at_block <= sqlc.arg(block)
)
`

type GetEntityParams struct {
	Key   string
	Block int64
}

type GetEntityRow struct {
	ExpiresAt           int64
	Payload             []byte
	OwnerAddress        string
	CreatedAtBlock      int64
	LastModifiedAtBlock int64
}

func (q *Queries) GetEntity(ctx context.Context, arg GetEntityParams) (GetEntityRow, error) {
	row := q.db.QueryRowContext(ctx, getEntity, arg.Key, arg.Block)
	var i GetEntityRow
	err := row.Scan(
		&i.ExpiresAt,
		&i.Payload,
		&i.OwnerAddress,
		&i.CreatedAtBlock,
		&i.LastModifiedAtBlock,
	)
	return i, err
}

Relevant log output

Database schema

CREATE TABLE entities (
  key TEXT NOT NULL,
  expires_at INTEGER NOT NULL,
  payload BLOB NOT NULL,
  created_at_block INTEGER NOT NULL,
  last_modified_at_block INTEGER NOT NULL,
  deleted INTEGER NOT NULL,
  transaction_index_in_block INTEGER NOT NULL,
  operation_index_in_transaction INTEGER NOT NULL,
  owner_address TEXT NOT NULL,
  PRIMARY KEY (key, last_modified_at_block)
);

SQL queries

-- name: GetEntity :one
SELECT e1.expires_at, e1.payload, e1.owner_address, e1.created_at_block, e1.last_modified_at_block
FROM entities AS e1
WHERE e1.key = sqlc.arg(key)
AND e1.deleted == FALSE
AND e1.last_modified_at_block <= sqlc.arg(block)
AND NOT EXISTS (
  SELECT 1
  FROM entities AS e2
  WHERE e2.key = e1.key
  AND e2.last_modified_at_block > e1.last_modified_at_block
  AND e2.last_modified_at_block <= sqlc.arg(block)
);

Configuration

version: "2"
sql:
  - engine: "sqlite"
    queries: "query.sql"
    schema: "schema.sql"
    gen:
      go:
        package: "foo"
        out: "."

Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

SQLite

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions