forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_stmt.go
More file actions
39 lines (36 loc) · 785 Bytes
/
insert_stmt.go
File metadata and controls
39 lines (36 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package validate
import (
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
)
func InsertStmt(stmt *ast.InsertStmt) error {
sel, ok := stmt.SelectStmt.(*ast.SelectStmt)
if !ok {
return nil
}
if sel.ValuesLists == nil {
return nil
}
if len(sel.ValuesLists.Items) != 1 {
return nil
}
sublist, ok := sel.ValuesLists.Items[0].(*ast.List)
if !ok {
return nil
}
colsLen := len(stmt.Cols.Items)
valsLen := len(sublist.Items)
switch {
case colsLen > valsLen:
return &sqlerr.Error{
Code: "42601",
Message: "INSERT has more target columns than expressions",
}
case colsLen < valsLen:
return &sqlerr.Error{
Code: "42601",
Message: "INSERT has more expressions than target columns",
}
}
return nil
}