Skip to content

query_parameter_limit configured to be greater than 1 will generate an error for statements containing sqlc.slice(). #2268

Description

@J404J

Version

v1.18.0

What happened?

DB: MySQL

query_parameter_limit configured to be greater than 1 will generate an error for statements containing sqlc.slice().

for example this code, arg input not exist , but generate code still use arg

func (q *Queries) Test(ctx context.Context, iD []int64, exported TestExported) ([]int64, error) {
	sql := test
	var queryParams []interface{}
	if len(arg.ID) > 0 {
		for _, v := range arg.ID {
			queryParams = append(queryParams, v)
		}
		sql = strings.Replace(sql, "/*SLICE:id*/?", strings.Repeat(",?", len(arg.ID))[1:], 1)
	} else {
		sql = strings.Replace(sql, "/*SLICE:id*/?", "NULL", 1)
	}
	queryParams = append(queryParams, arg.Exported)
	rows, err := q.db.QueryContext(ctx, sql, queryParams...)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []int64{}
	for rows.Next() {
		var id int64
		if err := rows.Scan(&id); err != nil {
			return nil, err
		}
		items = append(items, id)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

SQL queries

-- name: Test :many
SELECT `id`
FROM `test`
WHERE `id` IN (sqlc.slice("id"))
  AND `exported` = ?;

Configuration

query_parameter_limit: 3

Playground URL

https://play.sqlc.dev/p/c146dea8a303a99e751b3608dede30a1bf8eea175078ac8e5580c5e2291c0810

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on May 6, 2023
  2. jo2y commented on May 7, 2023

    @jo2y

    I think my issue is similar and isn't directly related to sqlc.slice().
    https://play.sqlc.dev/p/1bac3ce80ff0800c1ab86168e1dacef38f61a0919c8e77e946fc1a434d694d7c

    The main changes I made from the default was to add query_parameter_limit: 3, an array column to the table and insert query. Then I deleted anything unused/unrelated. The array column causes pq.Array(arg.Ary) in the output instead of something like pq.Array(ary). So I think this is an issue with any generated parameters that are not a simple column types.

  3. added this to the v1.19.0 milestone on Jun 6, 2023
  4. Jille commented on Jun 28, 2023

    @Jille
    Contributor

    I sent a few PRs to address these two issues and some things I accidentally ran into while writing test cases for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions