-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48deac2
commit 8639d45
Showing
16 changed files
with
5,580 additions
and
788 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,82 @@ | ||
// Copyright 2024 Dolthub, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ast | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/dolthub/go-mysql-server/sql" | ||
vitess "github.com/dolthub/vitess/go/vt/sqlparser" | ||
|
||
"github.com/dolthub/doltgresql/postgres/parser/parser" | ||
) | ||
|
||
var _ sql.Parser = &PostgresParser{} | ||
|
||
// PostgresParser is a postgres syntax parser. | ||
// This parser is used as parser in the engine for Doltgres. | ||
type PostgresParser struct{} | ||
|
||
// NewPostgresParser creates new PostgresParser. | ||
func NewPostgresParser() *PostgresParser { return &PostgresParser{} } | ||
|
||
// ParseSimple implements sql.Parser interface. | ||
func (p *PostgresParser) ParseSimple(query string) (vitess.Statement, error) { | ||
stmt, _, _, err := p.ParseWithOptions(query, ';', false, vitess.ParserOptions{}) | ||
return stmt, err | ||
} | ||
|
||
// Parse implements sql.Parser interface. | ||
func (p *PostgresParser) Parse(_ *sql.Context, query string, multi bool) (vitess.Statement, string, string, error) { | ||
return p.ParseWithOptions(query, ';', multi, vitess.ParserOptions{}) | ||
} | ||
|
||
// ParseWithOptions implements sql.Parser interface. | ||
func (p *PostgresParser) ParseWithOptions(query string, delimiter rune, _ bool, _ vitess.ParserOptions) (vitess.Statement, string, string, error) { | ||
q := sql.RemoveSpaceAndDelimiter(query, delimiter) | ||
stmts, err := parser.Parse(q) | ||
if err != nil { | ||
return nil, "", "", err | ||
} | ||
if len(stmts) > 1 { | ||
return nil, "", "", fmt.Errorf("only a single statement at a time is currently supported") | ||
} | ||
if len(stmts) == 0 { | ||
return nil, q, "", nil | ||
} | ||
|
||
vitessAST, err := Convert(stmts[0]) | ||
if err != nil { | ||
return nil, "", "", err | ||
} | ||
if vitessAST == nil { | ||
q = stmts[0].AST.String() | ||
} | ||
|
||
return vitessAST, q, "", nil | ||
} | ||
|
||
// ParseOneWithOptions implements sql.Parser interface. | ||
func (p *PostgresParser) ParseOneWithOptions(query string, _ vitess.ParserOptions) (vitess.Statement, int, error) { | ||
stmt, err := parser.ParseOne(query) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
vitessAST, err := Convert(stmt) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
return vitessAST, 0, nil | ||
} |
This file contains 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
This file contains 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
This file contains 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.