Skip to content

Commit 057fd4d

Browse files
committed
Do not use database/sql interface
Signed-off-by: Javi Fontan <[email protected]>
1 parent dbcde4c commit 057fd4d

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

cmd/gitquery/query.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ func (c *CmdQuery) Execute(args []string) error {
1414
return err
1515
}
1616

17-
rows, err := c.executeQuery(c.Args.SQL)
17+
schema, rows, err := c.executeQuery(c.Args.SQL)
1818
if err != nil {
1919
return err
2020
}
2121

22-
return c.printQuery(rows, c.Format)
22+
return c.printQuery(schema, rows, c.Format)
2323
}

cmd/gitquery/query_base.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"database/sql"
4+
"io"
55
"os"
66
"path/filepath"
77

@@ -11,6 +11,7 @@ import (
1111
gogit "gopkg.in/src-d/go-git.v4"
1212
"gopkg.in/src-d/go-git.v4/utils/ioutil"
1313
sqle "gopkg.in/src-d/go-mysql-server.v0"
14+
"gopkg.in/src-d/go-mysql-server.v0/sql"
1415
)
1516

1617
type cmdQueryBase struct {
@@ -19,7 +20,6 @@ type cmdQueryBase struct {
1920
Path string `short:"p" long:"path" description:"Path where the git repository is located"`
2021

2122
engine *sqle.Engine
22-
db *sql.DB
2323
name string
2424
}
2525

@@ -38,16 +38,15 @@ func (c *cmdQueryBase) buildDatabase() error {
3838

3939
c.name = filepath.Base(filepath.Join(c.Path, ".."))
4040
c.engine.AddDatabase(gitquery.NewDatabase(c.name, r))
41-
c.db, err = sql.Open("sqle", "")
4241
return err
4342
}
4443

45-
func (c *cmdQueryBase) executeQuery(sql string) (*sql.Rows, error) {
44+
func (c *cmdQueryBase) executeQuery(sql string) (sql.Schema, sql.RowIter, error) {
4645
c.print("executing %q at %q\n", sql, c.name)
47-
return c.db.Query(sql)
46+
return c.engine.Query(sql)
4847
}
4948

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) {
5150
defer ioutil.CheckClose(rows, &err)
5251

5352
f, err := format.NewFormat(formatId, os.Stdout)
@@ -56,34 +55,29 @@ func (c *cmdQueryBase) printQuery(rows *sql.Rows, formatId string) (err error) {
5655
}
5756
defer ioutil.CheckClose(f, &err)
5857

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
6261
}
6362

64-
if err := f.WriteHeader(cols); err != nil {
63+
if err := f.WriteHeader(columnNames); err != nil {
6564
return err
6665
}
6766

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-
7467
for {
75-
if !rows.Next() {
68+
row, err := rows.Next()
69+
if err == io.EOF {
7670
break
7771
}
7872

79-
if err := rows.Scan(valPtrs...); err != nil {
73+
if err != nil {
8074
return err
8175
}
8276

83-
if err := f.Write(vals); err != nil {
77+
if err := f.Write(row); err != nil {
8478
return err
8579
}
8680
}
8781

88-
return rows.Err()
82+
return nil
8983
}

cmd/gitquery/shell.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ func (c *CmdShell) Execute(args []string) error {
6666

6767
rl.Terminal.Print(fmt.Sprintf("\n--> Executing query: %s\n\n", query))
6868

69-
rows, err := c.executeQuery(query)
69+
schema, rows, err := c.executeQuery(query)
7070
if err != nil {
7171
red("ERROR: %v\n\n", err)
7272
continue
7373
}
7474

75-
if err := c.printQuery(rows, "pretty"); err != nil {
75+
if err := c.printQuery(schema, rows, "pretty"); err != nil {
7676
red("ERROR: %v\n\n", err)
7777
continue
7878
}

0 commit comments

Comments
 (0)