1
1
package main
2
2
3
3
import (
4
- "database/sql "
4
+ "io "
5
5
"os"
6
6
"path/filepath"
7
7
@@ -11,6 +11,7 @@ import (
11
11
gogit "gopkg.in/src-d/go-git.v4"
12
12
"gopkg.in/src-d/go-git.v4/utils/ioutil"
13
13
sqle "gopkg.in/src-d/go-mysql-server.v0"
14
+ "gopkg.in/src-d/go-mysql-server.v0/sql"
14
15
)
15
16
16
17
type cmdQueryBase struct {
@@ -19,7 +20,6 @@ type cmdQueryBase struct {
19
20
Path string `short:"p" long:"path" description:"Path where the git repository is located"`
20
21
21
22
engine * sqle.Engine
22
- db * sql.DB
23
23
name string
24
24
}
25
25
@@ -38,16 +38,15 @@ func (c *cmdQueryBase) buildDatabase() error {
38
38
39
39
c .name = filepath .Base (filepath .Join (c .Path , ".." ))
40
40
c .engine .AddDatabase (gitquery .NewDatabase (c .name , r ))
41
- c .db , err = sql .Open ("sqle" , "" )
42
41
return err
43
42
}
44
43
45
- func (c * cmdQueryBase ) executeQuery (sql string ) (* sql.Rows , error ) {
44
+ func (c * cmdQueryBase ) executeQuery (sql string ) (sql.Schema , sql. RowIter , error ) {
46
45
c .print ("executing %q at %q\n " , sql , c .name )
47
- return c .db .Query (sql )
46
+ return c .engine .Query (sql )
48
47
}
49
48
50
- func (c * cmdQueryBase ) printQuery (rows * sql.Rows , formatId string ) (err error ) {
49
+ func (c * cmdQueryBase ) printQuery (schema sql. Schema , rows sql.RowIter , formatId string ) (err error ) {
51
50
defer ioutil .CheckClose (rows , & err )
52
51
53
52
f , err := format .NewFormat (formatId , os .Stdout )
@@ -56,34 +55,29 @@ func (c *cmdQueryBase) printQuery(rows *sql.Rows, formatId string) (err error) {
56
55
}
57
56
defer ioutil .CheckClose (f , & err )
58
57
59
- cols , err := rows . Columns ( )
60
- if err != nil {
61
- return err
58
+ columnNames := make ([] string , len ( schema ) )
59
+ for i , column := range schema {
60
+ columnNames [ i ] = column . Name
62
61
}
63
62
64
- if err := f .WriteHeader (cols ); err != nil {
63
+ if err := f .WriteHeader (columnNames ); err != nil {
65
64
return err
66
65
}
67
66
68
- vals := make ([]interface {}, len (cols ))
69
- valPtrs := make ([]interface {}, len (cols ))
70
- for i := 0 ; i < len (cols ); i ++ {
71
- valPtrs [i ] = & vals [i ]
72
- }
73
-
74
67
for {
75
- if ! rows .Next () {
68
+ row , err := rows .Next ()
69
+ if err == io .EOF {
76
70
break
77
71
}
78
72
79
- if err := rows . Scan ( valPtrs ... ); err != nil {
73
+ if err != nil {
80
74
return err
81
75
}
82
76
83
- if err := f .Write (vals ); err != nil {
77
+ if err := f .Write (row ); err != nil {
84
78
return err
85
79
}
86
80
}
87
81
88
- return rows . Err ()
82
+ return nil
89
83
}
0 commit comments