-
Notifications
You must be signed in to change notification settings - Fork 0
WIP: SQL INSERT table placeholders (#146) #169
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
Draft
apstndb
wants to merge
1
commit into
main
Choose a base branch
from
scaffold/issue-146-table-placeholder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package writer | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "cloud.google.com/go/spanner" | ||
| databasepb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb" | ||
| "github.com/apstndb/spanvalue/gcvctor" | ||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| func TestSQLInsertWriterWithTablePlaceholder_scaffold(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var out bytes.Buffer | ||
| w := mustNewSQLInsertWriter(t, &out, "", | ||
| WithSQLTablePlaceholder("TABLE_NAME"), | ||
| ) | ||
| if err := w.WriteValues([]string{"id"}, []spanner.GenericColumnValue{gcvctor.Int64Value(1)}); err != nil { | ||
| t.Fatalf("WriteValues() error = %v", err) | ||
| } | ||
|
|
||
| want := "INSERT INTO TABLE_NAME (`id`) VALUES (1);\n" | ||
| if diff := cmp.Diff(want, out.String()); diff != "" { | ||
| t.Fatalf("SQL output mismatch (-want +got):\n%s", diff) | ||
| } | ||
| } | ||
|
|
||
| func TestSQLInsertWriterWithTablePlaceholder_postgresColumnsQuoted(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var out bytes.Buffer | ||
| w := mustNewSQLInsertWriter(t, &out, "", | ||
| WithSQLTablePlaceholder("/* unresolved table */"), | ||
| WithSQLDialect(databasepb.DatabaseDialect_POSTGRESQL), | ||
| ) | ||
| if err := w.WriteValues([]string{"id"}, []spanner.GenericColumnValue{gcvctor.Int64Value(42)}); err != nil { | ||
| t.Fatalf("WriteValues() error = %v", err) | ||
| } | ||
|
|
||
| want := "INSERT INTO /* unresolved table */ (\"id\") VALUES (42);\n" | ||
| if diff := cmp.Diff(want, out.String()); diff != "" { | ||
| t.Fatalf("SQL output mismatch (-want +got):\n%s", diff) | ||
| } | ||
| } | ||
|
|
||
| func TestSQLInsertWriterWithTablePlaceholder_emptyTokenRejected(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| _, err := NewSQLInsertWriter(&bytes.Buffer{}, "users", WithSQLTablePlaceholder("")) | ||
| if !errors.Is(err, ErrEmptyTablePlaceholder) { | ||
| t.Fatalf("NewSQLInsertWriter() error = %v, want ErrEmptyTablePlaceholder", err) | ||
| } | ||
| } | ||
|
|
||
| func TestSQLInsertWriterEmptyTableWithoutPlaceholder(t *testing.T) { | ||
| t.Parallel() | ||
| t.Skip("WIP: #147 will reject empty table at construction; placeholder path should remain valid") | ||
|
|
||
| var out bytes.Buffer | ||
| _, err := NewSQLInsertWriter(&out, "") | ||
| if err != nil { | ||
| t.Fatalf("NewSQLInsertWriter() error = %v, want nil until #147 lands", err) | ||
| } | ||
| err = mustNewSQLInsertWriter(t, &out, "").WriteValues([]string{"id"}, []spanner.GenericColumnValue{gcvctor.Int64Value(1)}) | ||
| if !errors.Is(err, ErrEmptyTableName) { | ||
| t.Fatalf("WriteValues() error = %v, want ErrEmptyTableName", err) | ||
| } | ||
| } | ||
|
|
||
| func TestSQLInsertWriterTablePlaceholder_batchedGoldenSketch(t *testing.T) { | ||
| t.Parallel() | ||
| t.Skip("WIP #146: batched INSERT with placeholders not golden-tested yet") | ||
|
|
||
| var out bytes.Buffer | ||
| w := mustNewSQLInsertWriter(t, &out, "", | ||
| WithSQLTablePlaceholder("TABLE_NAME"), | ||
| WithSQLBatchSize(2), | ||
| ) | ||
| for _, id := range []int64{1, 2} { | ||
| if err := w.WriteValues([]string{"id"}, []spanner.GenericColumnValue{gcvctor.Int64Value(id)}); err != nil { | ||
| t.Fatalf("WriteValues() error = %v", err) | ||
| } | ||
| } | ||
| want := "INSERT INTO TABLE_NAME (`id`) VALUES\n (1),\n (2);\n" | ||
| if diff := cmp.Diff(want, out.String()); diff != "" { | ||
| t.Fatalf("SQL output mismatch (-want +got):\n%s", diff) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
tablePlaceholderis configured, the actual table name used in the generatedINSERTstatements will be the placeholder, nottable. However,TableName()currently still returnsw.table(which may be empty). To ensure consistency with its documented contract ('TableName returns the qualified table name used in INSERT statements'),TableName()should be updated to returnw.tablePlaceholderif it is set.