@@ -25,6 +25,11 @@ const (
2525 CmdBatchOne = ":batchone"
2626)
2727
28+ type CmdParams struct {
29+ ManyKey string
30+ InsertMultiple bool
31+ }
32+
2833// A query name must be a valid Go identifier
2934//
3035// https://golang.org/ref/spec#Identifiers
@@ -44,7 +49,7 @@ func validateQueryName(name string) error {
4449 return nil
4550}
4651
47- func Parse (t string , commentStyle CommentSyntax ) (string , string , error ) {
52+ func Parse (t string , commentStyle CommentSyntax ) (string , string , CmdParams , error ) {
4853 for _ , line := range strings .Split (t , "\n " ) {
4954 var prefix string
5055 if strings .HasPrefix (line , "--" ) {
@@ -76,30 +81,51 @@ func Parse(t string, commentStyle CommentSyntax) (string, string, error) {
7681 continue
7782 }
7883 if ! strings .HasPrefix (rest , " name: " ) {
79- return "" , "" , fmt .Errorf ("invalid metadata: %s" , line )
84+ return "" , "" , CmdParams {}, fmt .Errorf ("invalid metadata: %s" , line )
8085 }
8186
8287 part := strings .Split (strings .TrimSpace (line ), " " )
8388 if prefix == "/*" {
8489 part = part [:len (part )- 1 ] // removes the trailing "*/" element
8590 }
8691 if len (part ) == 2 {
87- return "" , "" , fmt .Errorf ("missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s" , line )
88- }
89- if len (part ) != 4 {
90- return "" , "" , fmt .Errorf ("invalid query comment: %s" , line )
92+ return "" , "" , CmdParams {}, fmt .Errorf ("missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s" , line )
9193 }
9294 queryName := part [2 ]
9395 queryType := strings .TrimSpace (part [3 ])
9496 switch queryType {
9597 case CmdOne , CmdMany , CmdExec , CmdExecResult , CmdExecRows , CmdExecLastId , CmdCopyFrom , CmdBatchExec , CmdBatchMany , CmdBatchOne :
9698 default :
97- return "" , "" , fmt .Errorf ("invalid query type: %s" , queryType )
99+ return "" , "" , CmdParams {}, fmt .Errorf ("invalid query type: %s" , queryType )
98100 }
99101 if err := validateQueryName (queryName ); err != nil {
100- return "" , "" , err
102+ return "" , "" , CmdParams {}, err
103+ }
104+ cmdParams , err := parseCmdParams (part [4 :], queryType )
105+ if err != nil {
106+ return "" , "" , CmdParams {}, err
107+ }
108+ return queryName , queryType , cmdParams , nil
109+ }
110+ return "" , "" , CmdParams {}, nil
111+ }
112+
113+ func parseCmdParams (part []string , queryType string ) (CmdParams , error ) {
114+ var ret CmdParams
115+ for _ , p := range part {
116+ if p == "multiple" {
117+ if queryType != CmdExec && queryType != CmdExecResult && queryType != CmdExecRows && queryType != CmdExecLastId {
118+ return ret , fmt .Errorf ("query command parameter multiple is invalid for query type %s" , queryType )
119+ }
120+ ret .InsertMultiple = true
121+ } else if strings .HasPrefix (p , "key=" ) {
122+ if queryType != CmdMany {
123+ return ret , fmt .Errorf ("query command parameter %s is invalid for query type %s" , p , queryType )
124+ }
125+ ret .ManyKey = strings .TrimPrefix (p , "key=" )
126+ } else {
127+ return ret , fmt .Errorf ("invalid query command parameter %q" , p )
101128 }
102- return queryName , queryType , nil
103129 }
104- return "" , "" , nil
130+ return ret , nil
105131}
0 commit comments