Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Positional Parameter Support #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Positional Parameter Support #30

wants to merge 2 commits into from

Conversation

rodriguise
Copy link

@rodriguise rodriguise commented Jul 7, 2020

This PR provides support for:

  1. Alternate parameter symbols (i.e. $ rather than ?)
  2. Position parameters in the FilterExp, i.e. (name = $1, age = $2).

There are set via two new Config parameters:

{
	ParamSymbol string

	PositionalParams bool
}

Addionally, a Config parameter ParamOffset int is also provided.

This supports the use case where the FilterExp is appended to a larger expression and is not the initial parameter set for example:

	parser := rql.MustNewParser(rql.Config{
		Model:            Application{},
		ColumnFn:         swag.ToJSONName,
		ParamSymbol:      "$",
		PositionalParams: true,
		ParamOffset:      9,
	})

	params, err := parser.ParseQuery(query)
	if err != nil {
		return err
	}

	row := b.db.QueryRowx(
		`UPDATE applications SET
		name=$1, description=$2, type=$3, login_uris=$4, redirect_uris=$5, logout_uris=$6, allowed_grants=$7, permissions=$8
		WHERE `+params.FilterExp+
			` RETURNING *`,
		args...)

where params.FilterExp would be id=$9

Ultimately this allows for the use of this library to create statements directly compatible with Postgres and other dialects that use positional parameters.

Test Cases

Some of the internal testing methods had to be modified to support both of these use cases, namely the split function that only supported the ? character. This was modified to support any character as well as handle positional arguments by ignoring digits post the parameter symbol.

@ashtonian
Copy link
Contributor

ashtonian commented Aug 7, 2023

I initially thought this was better outside but it makes it really hard to support operations with question marks in them for postgres like the jsonb operator ? or ?|. It just also gets tricky because sometimes the pgx driver for example expects the array in an expanded format as separate args instead of as one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants