Commit 95bb795
authored
chore: add simple token-based parser (#285)
* 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 skipWhitespacesAndComments1 parent c68065d commit 95bb795
File tree
18 files changed
+2534
-390
lines changed- benchmarks
- examples
- snippets
- testutil
18 files changed
+2534
-390
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
828 | 828 | | |
829 | 829 | | |
830 | 830 | | |
| 831 | + | |
831 | 832 | | |
832 | 833 | | |
833 | 834 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
| 207 | + | |
207 | 208 | | |
208 | 209 | | |
209 | 210 | | |
| |||
724 | 725 | | |
725 | 726 | | |
726 | 727 | | |
727 | | - | |
| 728 | + | |
728 | 729 | | |
729 | 730 | | |
730 | 731 | | |
| |||
733 | 734 | | |
734 | 735 | | |
735 | 736 | | |
736 | | - | |
| 737 | + | |
737 | 738 | | |
738 | 739 | | |
739 | 740 | | |
| |||
754 | 755 | | |
755 | 756 | | |
756 | 757 | | |
757 | | - | |
| 758 | + | |
758 | 759 | | |
759 | 760 | | |
760 | 761 | | |
761 | | - | |
| 762 | + | |
762 | 763 | | |
763 | 764 | | |
764 | 765 | | |
| |||
797 | 798 | | |
798 | 799 | | |
799 | 800 | | |
800 | | - | |
| 801 | + | |
801 | 802 | | |
802 | 803 | | |
803 | 804 | | |
| |||
812 | 813 | | |
813 | 814 | | |
814 | 815 | | |
815 | | - | |
| 816 | + | |
816 | 817 | | |
817 | 818 | | |
818 | 819 | | |
| |||
824 | 825 | | |
825 | 826 | | |
826 | 827 | | |
827 | | - | |
| 828 | + | |
828 | 829 | | |
829 | 830 | | |
830 | 831 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| 37 | + | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
| |||
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
51 | 57 | | |
52 | 58 | | |
53 | 59 | | |
| |||
91 | 97 | | |
92 | 98 | | |
93 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
94 | 113 | | |
95 | 114 | | |
96 | 115 | | |
| |||
210 | 229 | | |
211 | 230 | | |
212 | 231 | | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
213 | 242 | | |
214 | 243 | | |
215 | 244 | | |
| |||
335 | 364 | | |
336 | 365 | | |
337 | 366 | | |
| 367 | + | |
338 | 368 | | |
339 | 369 | | |
340 | 370 | | |
| |||
480 | 510 | | |
481 | 511 | | |
482 | 512 | | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
483 | 518 | | |
484 | 519 | | |
485 | 520 | | |
| |||
574 | 609 | | |
575 | 610 | | |
576 | 611 | | |
| 612 | + | |
577 | 613 | | |
578 | 614 | | |
579 | 615 | | |
| |||
614 | 650 | | |
615 | 651 | | |
616 | 652 | | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
617 | 675 | | |
618 | 676 | | |
619 | 677 | | |
620 | 678 | | |
621 | | - | |
622 | | - | |
| 679 | + | |
623 | 680 | | |
624 | 681 | | |
625 | 682 | | |
| |||
630 | 687 | | |
631 | 688 | | |
632 | 689 | | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
633 | 710 | | |
634 | 711 | | |
635 | 712 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
533 | 534 | | |
534 | 535 | | |
535 | 536 | | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
536 | 541 | | |
| 542 | + | |
537 | 543 | | |
538 | 544 | | |
539 | 545 | | |
| |||
568 | 574 | | |
569 | 575 | | |
570 | 576 | | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
571 | 581 | | |
| 582 | + | |
572 | 583 | | |
573 | 584 | | |
574 | 585 | | |
| |||
605 | 616 | | |
606 | 617 | | |
607 | 618 | | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
608 | 623 | | |
609 | | - | |
| 624 | + | |
610 | 625 | | |
611 | 626 | | |
612 | 627 | | |
| |||
649 | 664 | | |
650 | 665 | | |
651 | 666 | | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
652 | 671 | | |
653 | 672 | | |
| 673 | + | |
654 | 674 | | |
655 | 675 | | |
656 | 676 | | |
| |||
677 | 697 | | |
678 | 698 | | |
679 | 699 | | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
680 | 704 | | |
| 705 | + | |
681 | 706 | | |
682 | 707 | | |
683 | 708 | | |
| |||
693 | 718 | | |
694 | 719 | | |
695 | 720 | | |
696 | | - | |
| 721 | + | |
697 | 722 | | |
698 | 723 | | |
699 | 724 | | |
| |||
0 commit comments