xsqlparser icon indicating copy to clipboard operation
xsqlparser copied to clipboard

parseStatement failed: parseObjectName failed: parseListOfId: expect identifier

Open sunshine69 opened this issue 2 years ago • 2 comments

I got a sample code below, however it can not parse

package main

import (
	"bytes"
	"log"

	"github.com/k0kubun/pp"

	"github.com/akito0107/xsqlparser"
	"github.com/akito0107/xsqlparser/dialect"
	"github.com/akito0107/xsqlparser/sqlast"
)

func main() {
	parseCreate()
}
func parseCreate() {
	sql := "CREATE TABLE IF NOT EXISTS     `somadatabase`.`sometable` ( 			`clientID` char(36) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL COMMENT 'Client', 			`monday` int(11) NOT NULL COMMENT ' Mondays', 			`tuesday` int(11) NOT NULL COMMENT ' Tuesdays', 			`wednesday` int(11) NOT NULL COMMENT ' Wednesdays', 			`thursday` int(11) NOT NULL COMMENT ' Thursdays', 			`friday` int(11) NOT NULL COMMENT ' Fridays', 			`saturday` int(11) NOT NULL COMMENT ' Saturdays', 			`sunday` int(11) NOT NULL COMMENT ' Sundays', 			`allowanceOnUpdate` int(11) DEFAULT NULL COMMENT 'Previous allowance for the day affected by the last update', 			`updated` timestamp(6) NOT NULL, 			PRIMARY KEY (`clientID`) 		  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
	parser, err := xsqlparser.NewParser(bytes.NewBufferString(sql), &dialect.GenericSQLDialect{})
	if err != nil {
		log.Fatal(err)
	}
	stmt, err := parser.ParseStatement()
	if err != nil {
		log.Fatalf("[ERROR] %s", err)
	}
	pp.Println(stmt)

}

stevek@work-macbook 14:41 ~/s/g/xsqlparser> go run . 2022/06/10 14:41:39 [ERROR] parseStatement failed: parseObjectName failed: parseListOfId: expect identifier exit status 1 stevek@work-macbook 14:41 ~/s/g/xsqlparser>

sunshine69 avatar Jun 10 '22 04:06 sunshine69

well if I select MySQL dialect new errors, however the sql runs perfectly fine

go run .
2022/06/10 14:49:06 Expected ',' or ')' after column definition but &{SQLKeyword CHARACTER {1 93} {1 102}}
panic: Expected ',' or ')' after column definition but &{SQLKeyword CHARACTER {1 93} {1 102}}

goroutine 1 [running]:
log.Panicf({0x519fed?, 0x0?}, {0xc000067d30?, 0xc000067d40?, 0x4db46b?})
	/mnt/live/memory/images/002-ubuntu-devtool-x86_64.xzm/opt/go/src/log/log.go:392 +0x67
github.com/akito0107/xsqlparser.(*Parser).parseElements(0xc00015ccc0)
	/home/stevek/go/pkg/mod/github.com/akito0107/[email protected]/parser.go:682 +0x25f
github.com/akito0107/xsqlparser.(*Parser).parseCreateTable(0xc00015ccc0?, 0xc0001160c0)
	/home/stevek/go/pkg/mod/github.com/akito0107/[email protected]/parser.go:553 +0x127
github.com/akito0107/xsqlparser.(*Parser).parseCreate(0xc00015ccc0)
	/home/stevek/go/pkg/mod/github.com/akito0107/[email protected]/parser.go:523 +0x185
github.com/akito0107/xsqlparser.(*Parser).ParseStatement(0xc00015ccc0)
	/home/stevek/go/pkg/mod/github.com/akito0107/[email protected]/parser.go:145 +0x465
main.parseCreate()
	/home/stevek/src/golang-samples/xsqlparser/main.go:23 +0xdc
main.main()
	/home/stevek/src/golang-samples/xsqlparser/main.go:15 +0x17
exit status 2

sunshine69 avatar Jun 10 '22 04:06 sunshine69

I believe that this is because the MySQLDialect recognizes ` as a valid character to start an identifier with special characters in the name whereas the GenericSQLDialect only recognizes "

kendru avatar Jun 10 '22 21:06 kendru