Skip to content

Conversation

@olavloite
Copy link
Collaborator

Run tests for pull requests on any branch.

@olavloite olavloite requested a review from a team as a code owner May 21, 2025 12:59
@olavloite olavloite merged commit 63e9e4b into main May 21, 2025
19 checks passed
@olavloite olavloite deleted the run-tests-on-all-pull-requests branch May 21, 2025 13:03
olavloite added a commit that referenced this pull request Jun 4, 2025
* chore: find parameters with comments in place

Find query parameters in the SQL string while keeping comments in place. This both
improves performance, as we need to copy the string fewer times, but more imporantly;
it keeps the comments in the SQL string that is sent to Spanner. The latter is
important for the PostgreSQL dialect, as query hints in that dialect are embedded into
comments.

* build: run tests pull requests for all branches (#426)

* chore: ignore lint errors

* perf: add a cache for parsed statements (#425)

* perf: add a cache for parsed statements

The functions for determining the type of statement and which query parameters
are in a statement are deterministic and do not change over time. These can
therefore safely be cached. This improves the execution speed of frequently
executed SQL strings, as the same parsing does not need to happen for each
execution.

* chore: go mod tidy

* chore: create statementParser struct and make it dialect-aware (#427)

* chore: create statementParser struct and make it dialect-aware

Refactors the statement parser into a struct that contains a field for the database
dialect that is being used. This dialect field will be used in follow-up pull request
to implement dialect-specific parsing.

* chore: remove duplicate quoted string parsing (#428)

* chore: remove duplicate quoted string parsing

The calculateFindParams function contained its own logic for skipping over quoted string literals.
This code was redundant, as it is also implemented in the skip function. This change removes this
duplication and simplifies the calculateFindParams implementation significantly.

This also ensures that we only have one place where string literals are found and skipped in the
parser, which will make it easier to make this code dialect-aware.

* chore: add support for PostgreSQL-style comments (#429)

* chore: add support for PostgreSQL-style comments

Adds support for PostgreSQL-style comments and sets some of the tests up for multi-dialect
testing.

* chore: support PostgreSQL-style parameters (#431)

* chore: support PostgreSQL-style parameters

Adds support for both recognizing PostgreSQL-style query parameters, and
converting positional parameters to PostgreSQL-style query parameters.

When using the PostgreSQL-dialect, the driver accepts both GoogleSQL
and PostgreSQL-style named parameters (so both @param and $1). This to
be consistent with other PostgreSQL drivers in Go (e.g. pgx).

* chore: add PostgreSQL quoting rules (#432)

* chore: add PostgreSQL quoting rules

Add PostgreSQL quoting rules to the parser. This means:
1. Backslashes do not start an escape sequence.
2. Repeating the same quote twice inside a string literal is an escaped quote.
3. Backtick is not a valid quote character.
4. Triple-quoted strings are not supported. However, if the start of a string consists
   of three consequtive quotes, then that means 'start of a string' and then 'an escaped
   quote'.
5. Linefeeds are allowed in string literals.

This change does not add support for dollar-quoted strings. That will be added in a
follow-up pull request.

* chore: add support for dollar-quoted strings (#433)

* chore: add support for dollar-quoted strings

Adds support for dollar-quoted strings.

Also fixes an issue in the skipWhitespaces function, where multi-byte characters would be
skipped as-if they were spaces.

* test: add some more tests for PostgreSQL parsing (#434)

* test: add some more tests for PostgreSQL parsing

* feat: detect database dialect and support PostgreSQL databases (#435)

* feat: detect database dialect and support PostgreSQL databases

Automatically detect the dialect of the database that the driver connects to,
and modify the way that query parameters are recognized based on the dialect
of the database. This makes it possible to use this driver with Spanner databases
that use the PostgreSQL dialect.

* perf: cache client-side SQL parsing (#436)

Cache the outcome of parsing a potential client-side SQL statement to prevent
the same regular expressions to be tested repeatedly. Also, the initial check
whether a statement is a client-side statement has been replaced with a keyword
based check: Instead of trying to match the SQL statement against the list of
all possible client-side statements, the parser first checks whether the first
keyword is a keyword that could potentially be a client-side statement. If it
is not, then the function returns early.

Benchmarks:

```
BenchmarkDetectStatementTypeWithCache-8      12530068     942 ns/op
BenchmarkDetectStatementTypeWithoutCache-8    2861143    4182 ns/op
```
olavloite added a commit that referenced this pull request Jun 4, 2025
* chore: add simple token parser

* chore: add multi-byte support

* chore: add benchmark

* chore: find parameters with comments in place (#424)

* chore: find parameters with comments in place

Find query parameters in the SQL string while keeping comments in place. This both
improves performance, as we need to copy the string fewer times, but more imporantly;
it keeps the comments in the SQL string that is sent to Spanner. The latter is
important for the PostgreSQL dialect, as query hints in that dialect are embedded into
comments.

* build: run tests pull requests for all branches (#426)

* chore: ignore lint errors

* perf: add a cache for parsed statements (#425)

* perf: add a cache for parsed statements

The functions for determining the type of statement and which query parameters
are in a statement are deterministic and do not change over time. These can
therefore safely be cached. This improves the execution speed of frequently
executed SQL strings, as the same parsing does not need to happen for each
execution.

* chore: go mod tidy

* chore: create statementParser struct and make it dialect-aware (#427)

* chore: create statementParser struct and make it dialect-aware

Refactors the statement parser into a struct that contains a field for the database
dialect that is being used. This dialect field will be used in follow-up pull request
to implement dialect-specific parsing.

* chore: remove duplicate quoted string parsing (#428)

* chore: remove duplicate quoted string parsing

The calculateFindParams function contained its own logic for skipping over quoted string literals.
This code was redundant, as it is also implemented in the skip function. This change removes this
duplication and simplifies the calculateFindParams implementation significantly.

This also ensures that we only have one place where string literals are found and skipped in the
parser, which will make it easier to make this code dialect-aware.

* chore: add support for PostgreSQL-style comments (#429)

* chore: add support for PostgreSQL-style comments

Adds support for PostgreSQL-style comments and sets some of the tests up for multi-dialect
testing.

* chore: support PostgreSQL-style parameters (#431)

* chore: support PostgreSQL-style parameters

Adds support for both recognizing PostgreSQL-style query parameters, and
converting positional parameters to PostgreSQL-style query parameters.

When using the PostgreSQL-dialect, the driver accepts both GoogleSQL
and PostgreSQL-style named parameters (so both @param and $1). This to
be consistent with other PostgreSQL drivers in Go (e.g. pgx).

* chore: add PostgreSQL quoting rules (#432)

* chore: add PostgreSQL quoting rules

Add PostgreSQL quoting rules to the parser. This means:
1. Backslashes do not start an escape sequence.
2. Repeating the same quote twice inside a string literal is an escaped quote.
3. Backtick is not a valid quote character.
4. Triple-quoted strings are not supported. However, if the start of a string consists
   of three consequtive quotes, then that means 'start of a string' and then 'an escaped
   quote'.
5. Linefeeds are allowed in string literals.

This change does not add support for dollar-quoted strings. That will be added in a
follow-up pull request.

* chore: add support for dollar-quoted strings (#433)

* chore: add support for dollar-quoted strings

Adds support for dollar-quoted strings.

Also fixes an issue in the skipWhitespaces function, where multi-byte characters would be
skipped as-if they were spaces.

* test: add some more tests for PostgreSQL parsing (#434)

* test: add some more tests for PostgreSQL parsing

* feat: detect database dialect and support PostgreSQL databases (#435)

* feat: detect database dialect and support PostgreSQL databases

Automatically detect the dialect of the database that the driver connects to,
and modify the way that query parameters are recognized based on the dialect
of the database. This makes it possible to use this driver with Spanner databases
that use the PostgreSQL dialect.

* perf: cache client-side SQL parsing (#436)

Cache the outcome of parsing a potential client-side SQL statement to prevent
the same regular expressions to be tested repeatedly. Also, the initial check
whether a statement is a client-side statement has been replaced with a keyword
based check: Instead of trying to match the SQL statement against the list of
all possible client-side statements, the parser first checks whether the first
keyword is a keyword that could potentially be a client-side statement. If it
is not, then the function returns early.

Benchmarks:

```
BenchmarkDetectStatementTypeWithCache-8      12530068     942 ns/op
BenchmarkDetectStatementTypeWithoutCache-8    2861143    4182 ns/op
```

* chore: rename function to skipWhitespacesAndComments
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.

1 participant