Skip to content

Commit

Permalink
lexbase: introduce EncAlwaysQuoted fmt flag
Browse files Browse the repository at this point in the history
To accomodate an oracle query, we need to quote the object name regardless if it is
a reserved keyword or is bare identifier. This is because oracle is case insensitive
without quotes. This flag is meant to be used ONLY to construct SELECT stmt for oracle
source.

For example, in oracle:

```
CREATE TABLE "t" ("iD" INT PRIMARY KEY, "AcC" INT)
```

We have to do

```
SELECT * FROM "t"
```

Otherwise we will encounter a "table doesn't exist" error.

Release note: None
  • Loading branch information
ZhouXing19 committed Jun 11, 2024
1 parent 4168c29 commit 4904abb
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/sql/lexbase/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ const (
// EncFirstFreeFlagBit needs to remain unused; it is used as base
// bit offset for tree.FmtFlags.
EncFirstFreeFlagBit

// EncAlwaysQuoted makes sure the string is always wrapped with quotes.
// This is used only to construct a statement against Oracle source,
// as Oracle is case insensitive if object name is not quoted.
EncAlwaysQuoted
)

// EncodeRestrictedSQLIdent writes the identifier in s to buf. The
// identifier is quoted if either the flags ask for it, the identifier
// contains special characters, or the identifier is a reserved SQL
// keyword.
func EncodeRestrictedSQLIdent(buf *bytes.Buffer, s string, flags EncodeFlags) {
if flags.HasFlags(EncBareIdentifiers) || (!isReservedKeyword(s) && IsBareIdentifier(s)) {
if !flags.HasFlags(EncAlwaysQuoted) && (flags.HasFlags(EncBareIdentifiers) || (!isReservedKeyword(s) && IsBareIdentifier(s))) {
buf.WriteString(s)
return
}
Expand All @@ -68,7 +73,7 @@ func EncodeRestrictedSQLIdent(buf *bytes.Buffer, s string, flags EncodeFlags) {
// The identifier is only quoted if the flags don't tell otherwise and
// the identifier contains special characters.
func EncodeUnrestrictedSQLIdent(buf *bytes.Buffer, s string, flags EncodeFlags) {
if flags.HasFlags(EncBareIdentifiers) || IsBareIdentifier(s) {
if !flags.HasFlags(EncAlwaysQuoted) && (flags.HasFlags(EncBareIdentifiers) || IsBareIdentifier(s)) {
buf.WriteString(s)
return
}
Expand Down

0 comments on commit 4904abb

Please sign in to comment.