1
1
package main
2
2
3
- import (
4
- "errors"
5
- "fmt"
6
- "io"
7
- "os"
8
- "path/filepath"
9
-
10
- "github.com/gitql/gitql"
11
- gitqlgit "github.com/gitql/gitql/git"
12
- "github.com/gitql/gitql/internal/format"
13
- "github.com/gitql/gitql/sql"
14
-
15
- "gopkg.in/src-d/go-git.v4"
16
- )
17
-
18
3
type CmdQuery struct {
19
- cmd
4
+ cmdQueryBase
20
5
21
- Path string `short:"p" long:"path" description:"Path where the git repository is located"`
22
6
Format string `short:"f" long:"format" default:"pretty" description:"Ouptut format. Formats supported: pretty, csv, json."`
23
7
Args struct {
24
8
SQL string `positional-arg-name:"sql" required:"true" description:"SQL query to execute"`
25
9
} `positional-args:"yes"`
26
-
27
- r * git.Repository
28
- db sql.Database
29
10
}
30
11
31
12
func (c * CmdQuery ) Execute (args []string ) error {
@@ -37,119 +18,10 @@ func (c *CmdQuery) Execute(args []string) error {
37
18
return err
38
19
}
39
20
40
- return c .executeQuery ()
41
- }
42
-
43
- func (c * CmdQuery ) validate () error {
44
- var err error
45
- c .Path , err = findDotGitFolder (c .Path )
46
- return err
47
- }
48
-
49
- func (c * CmdQuery ) buildDatabase () error {
50
- c .print ("opening %q repository...\n " , c .Path )
51
-
52
- var err error
53
- c .r , err = git .NewFilesystemRepository (c .Path )
54
- if err != nil {
55
- return err
56
- }
57
-
58
- empty , err := c .r .IsEmpty ()
59
- if err != nil {
60
- return err
61
- }
62
-
63
- if empty {
64
- return errors .New ("error: the repository is empty" )
65
- }
66
-
67
- head , err := c .r .Head ()
68
- if err != nil {
69
- return err
70
- }
71
-
72
- c .print ("current HEAD %q\n " , head .Hash ())
73
-
74
- name := filepath .Base (filepath .Join (c .Path , ".." ))
75
- c .db = gitqlgit .NewDatabase (name , c .r )
76
- return nil
77
- }
78
-
79
- func (c * CmdQuery ) executeQuery () error {
80
- c .print ("executing %q at %q\n " , c .Args .SQL , c .db .Name ())
81
-
82
- fmt .Println (c .Args .SQL )
83
- e := gitql .New ()
84
- e .AddDatabase (c .db )
85
- schema , iter , err := e .Query (c .Args .SQL )
86
- if err != nil {
87
- return err
88
- }
89
-
90
- return c .printQuery (schema , iter )
91
- }
92
-
93
- func (c * CmdQuery ) printQuery (schema sql.Schema , iter sql.RowIter ) error {
94
- f , err := format .NewFormat (c .Format , os .Stdout )
21
+ schema , rowIter , err := c .executeQuery (c .Args .SQL )
95
22
if err != nil {
96
23
return err
97
24
}
98
25
99
- headers := []string {}
100
- for _ , f := range schema {
101
- headers = append (headers , f .Name )
102
- }
103
-
104
- if err := f .WriteHeader (headers ); err != nil {
105
- return err
106
- }
107
-
108
- for {
109
- row , err := iter .Next ()
110
- if err == io .EOF {
111
- break
112
- }
113
- if err != nil {
114
- return err
115
- }
116
-
117
- record := make ([]interface {}, len (row ))
118
- for i := 0 ; i < len (row ); i ++ {
119
- record [i ] = row [i ]
120
- }
121
-
122
- if err := f .Write (record ); err != nil {
123
- return err
124
- }
125
- }
126
-
127
- return f .Close ()
128
- }
129
-
130
- func findDotGitFolder (path string ) (string , error ) {
131
- if path == "" {
132
- var err error
133
- path , err = os .Getwd ()
134
- if err != nil {
135
- return "" , err
136
- }
137
- }
138
-
139
- git := filepath .Join (path , ".git" )
140
- _ , err := os .Stat (git )
141
- if err == nil {
142
- return git , nil
143
- }
144
-
145
- if ! os .IsNotExist (err ) {
146
- return "" , err
147
- }
148
-
149
- next := filepath .Join (path , ".." )
150
- if next == path {
151
- return "" , errors .New ("unable to find a git repository" )
152
- }
153
-
154
- return findDotGitFolder (next )
26
+ return c .printQuery (schema , rowIter , c .Format )
155
27
}
0 commit comments