-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
from_clause.go
65 lines (57 loc) · 1.48 KB
/
from_clause.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package parser
// #include "query_types.h"
// #include <stdlib.h>
import "C"
import (
"bytes"
"strings"
)
import "fmt"
type FromClauseType int
const (
FromClauseArray FromClauseType = C.FROM_ARRAY
FromClauseMerge FromClauseType = C.FROM_MERGE
FromClauseInnerJoin FromClauseType = C.FROM_INNER_JOIN
)
func (self *TableName) GetAlias() string {
if self.Alias != "" {
return self.Alias
}
return self.Name.Name
}
func (self *TableName) GetAliasString() string {
if self.Alias != "" {
return fmt.Sprintf(" as %s", self.Alias)
}
return ""
}
type TableName struct {
Name *Value
Alias string
}
type FromClause struct {
Type FromClauseType
Names []*TableName
}
func (self *FromClause) GetString() string {
buffer := bytes.NewBufferString("")
switch self.Type {
case FromClauseMerge:
fmt.Fprintf(buffer, "%s%s merge %s %s", self.Names[0].Name.GetString(), self.Names[1].GetAliasString(),
self.Names[1].Name.GetString(), self.Names[1].GetAliasString())
case FromClauseInnerJoin:
fmt.Fprintf(buffer, "%s%s inner join %s%s", self.Names[0].Name.GetString(), self.Names[0].GetAliasString(),
self.Names[1].Name.GetString(), self.Names[1].GetAliasString())
default:
names := make([]string, 0, len(self.Names))
for _, t := range self.Names {
alias := ""
if t.Alias != "" {
alias = fmt.Sprintf(" as %s", t.Alias)
}
names = append(names, fmt.Sprintf("%s%s", t.Name.GetString(), alias))
}
buffer.WriteString(strings.Join(names, ","))
}
return buffer.String()
}