Skip to content

Commit 707ff5c

Browse files
jfontanajnavarro
authored andcommitted
Add server command, delete query and version (#143)
* Add server command, delete query and version Signed-off-by: Javi Fontan <[email protected]> * Use net.JoinHostPort instead of fmt.Sprintf It supports IPv6 Signed-off-by: Javi Fontan <[email protected]>
1 parent 7d0f7f9 commit 707ff5c

File tree

6 files changed

+76
-223
lines changed

6 files changed

+76
-223
lines changed

README.md

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,36 @@
44

55
## Installation
66

7-
Check the [Releases](https://github.com/sqle/gitquery/releases) page to download
7+
Check the [Releases](https://github.com/src-d/gitquery/releases) page to download
88
the gitquery binary.
99

1010
## Usage
1111

1212
```bash
1313
Usage:
14-
gitquery [OPTIONS] <query | shell | version>
14+
gitquery [OPTIONS] <server | version>
1515

1616
Help Options:
1717
-h, --help Show this help message
1818

1919
Available commands:
20-
query Execute a SQL query a repository.
21-
shell Start an interactive session.
20+
server Start SQL server.
2221
version Show the version information.
2322
```
2423

25-
For example:
24+
A MySQL client is needed to connect to the server. For example:
2625

2726
```bash
28-
$ cd my_git_repo
29-
$ gitquery query 'SELECT hash, author_email, author_name FROM commits LIMIT 2;'
27+
$ mysql -u root -h 127.0.0.1
28+
MySQL [(none)]> SELECT hash, author_email, author_name FROM commits LIMIT 2;
3029
SELECT hash, author_email, author_name FROM commits LIMIT 2;
3130
+------------------------------------------+---------------------+-----------------------+
32-
| HASH | AUTHOR EMAIL | AUTHOR NAME |
31+
| hash | author_email | author_name |
3332
+------------------------------------------+---------------------+-----------------------+
3433
| 003dc36e0067b25333cb5d3a5ccc31fd028a1c83 | [email protected] | Santiago M. Mola |
3534
| 01ace9e4d144aaeb50eb630fed993375609bcf55 | [email protected] | Antonio Navarro Perez |
3635
+------------------------------------------+---------------------+-----------------------+
37-
```
38-
39-
You can use the interactive shell like you usually do to explore tables in postgreSQL per example:
40-
41-
```bash
42-
$ gitquery shell
43-
44-
gitQL SHELL
45-
-----------
46-
You must end your queries with ';'
47-
48-
!> SELECT hash, author_email, author_name FROM commits LIMIT 2;
49-
50-
--> Executing query: SELECT hash, author_email, author_name FROM commits LIMIT 2;
51-
52-
+------------------------------------------+---------------------+-----------------------+
53-
| HASH | AUTHOR EMAIL | AUTHOR NAME |
54-
+------------------------------------------+---------------------+-----------------------+
55-
| 003dc36e0067b25333cb5d3a5ccc31fd028a1c83 | [email protected] | Santiago M. Mola |
56-
| 01ace9e4d144aaeb50eb630fed993375609bcf55 | [email protected] | Antonio Navarro Perez |
57-
+------------------------------------------+---------------------+-----------------------+
58-
!>
36+
2 rows in set (0.01 sec)
5937
```
6038

6139
## Tables
@@ -83,4 +61,4 @@ We are continuously adding more functionality to gitquery. We support a subset o
8361

8462
## License
8563

86-
gitquery is licensed under the [MIT License](https://github.com/sqle/gitquery/blob/master/LICENSE).
64+
gitquery is licensed under the [MIT License](https://github.com/src-d/gitquery/blob/master/LICENSE).

cmd/gitquery/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ const (
1313

1414
func main() {
1515
parser := flags.NewNamedParser(name, flags.Default)
16-
parser.AddCommand("query", "Execute a SQL query a repository.", "", &CmdQuery{})
17-
parser.AddCommand("shell", "Start an interactive session.", "", &CmdShell{})
16+
parser.AddCommand("server", "Start SQL server.", "", &CmdServer{})
1817
parser.AddCommand("version", "Show the version information.", "", &CmdVersion{})
1918

2019
_, err := parser.Parse()

cmd/gitquery/query.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

cmd/gitquery/query_base.go

Lines changed: 0 additions & 85 deletions
This file was deleted.

cmd/gitquery/server.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"net"
5+
"strconv"
6+
7+
"github.com/src-d/gitquery"
8+
"github.com/src-d/gitquery/internal/function"
9+
sqle "gopkg.in/src-d/go-mysql-server.v0"
10+
"gopkg.in/src-d/go-mysql-server.v0/server"
11+
"gopkg.in/src-d/go-vitess.v0/mysql"
12+
)
13+
14+
// CmdServer defines server command
15+
type CmdServer struct {
16+
cmd
17+
18+
Git string `short:"g" long:"git" description:"Path where the git repositories are located, one per dir"`
19+
Host string `short:"h" long:"host" default:"localhost" description:"Host where the server is going to listen"`
20+
Port int `short:"p" long:"port" default:"3306" description:"Port where the server is going to listen"`
21+
User string `short:"u" long:"user" default:"root" description:"User name used for connection"`
22+
Password string `short:"P" long:"password" default:"" description:"Password used for connection"`
23+
24+
engine *sqle.Engine
25+
name string
26+
}
27+
28+
func (c *CmdServer) buildDatabase() error {
29+
if c.engine == nil {
30+
c.engine = sqle.New()
31+
}
32+
33+
c.print("opening %q repository...\n", c.Git)
34+
35+
var err error
36+
37+
pool := gitquery.NewRepositoryPool()
38+
err = pool.AddDir(c.Git)
39+
if err != nil {
40+
return err
41+
}
42+
43+
c.engine.AddDatabase(gitquery.NewDatabase(c.name, &pool))
44+
function.Register(c.engine.Catalog)
45+
return nil
46+
}
47+
48+
// Execute starts the server
49+
func (c *CmdServer) Execute(args []string) error {
50+
if err := c.buildDatabase(); err != nil {
51+
return err
52+
}
53+
54+
auth := mysql.NewAuthServerStatic()
55+
auth.Entries[c.User] = []*mysql.AuthServerStaticEntry{
56+
{Password: c.Password},
57+
}
58+
59+
hostString := net.JoinHostPort(c.Host, strconv.Itoa(c.Port))
60+
s, err := server.NewServer("tcp", hostString, auth, c.engine)
61+
if err != nil {
62+
return err
63+
}
64+
65+
return s.Start()
66+
}

cmd/gitquery/shell.go

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)