Skip to content

Commit

Permalink
Merge pull request #18 from gchaincl/modules
Browse files Browse the repository at this point in the history
Go modules support and missing unit tests
  • Loading branch information
swithek authored Aug 15, 2019
2 parents 422abea + 095c2c4 commit 23aa1e8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
43 changes: 18 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
dotsql [![Build Status](https://travis-ci.org/gchaincl/dotsql.svg)](https://travis-ci.org/gchaincl/dotsql)
dotsql
[![GoDoc](https://godoc.org/github.com/gchaincl/dotsql?status.svg)](https://godoc.org/github.com/gchaincl/dotsql)
[![Build Status](https://travis-ci.org/gchaincl/dotsql.svg)](https://travis-ci.org/gchaincl/dotsql)
[![Test coverage](http://gocover.io/_badge/github.com/gchaincl/dotsql)](https://gocover.io/github.com/gchaincl/dotsql)
[![Go Report Card](https://goreportcard.com/badge/github.com/gchaincl/dotsql)](https://goreportcard.com/report/github.com/gchaincl/dotsql)
======

A Golang library for using SQL.
Expand All @@ -10,16 +14,14 @@ _Dotsql is heavily inspired by_ [yesql](https://github.com/krisajenkins/yesql).

Installation
--
Simple install the package to your `$GOPATH` with the `go` tool from shell:
```bash
$ go get github.com/gchaincl/dotsql
```
Make sure Git is installed on your machine and in your system's `$PATH`

Usage [![GoDoc](https://godoc.org/github.com/gchaincl/dotsql?status.svg)](https://godoc.org/github.com/gchaincl/dotsql)
Usage
--

First of all, you need to define queries into a file:
First of all, you need to define queries inside your sql file:

```sql
-- name: create-users-table
Expand All @@ -43,9 +45,10 @@ DROP TABLE users
```

Notice that every query has a name tag (`--name:<some name>`),
this will be helpful for referring to a specific query
this is needed to be able to uniquely identify each query
inside dotsql.

Then you should be able to run something like:
With your sql file prepared, you can load it up and start utilizing your queries:

```go
// Get a database handle
Expand All @@ -64,29 +67,19 @@ stmt, err := dot.Prepare(db, "drop-users-table")
result, err := stmt.Exec()
```

For a complete example please refer to [integration_test.go](https://github.com/gchaincl/dotsql/blob/master/integration_test.go) and [test_schema.sql](https://github.com/gchaincl/dotsql/blob/master/test_schema.sql)

Development
--

Dotsql is in a very early stage so api may change. Contributions are welcome!
Integration tests are tagged with `+integration`, so if you want to run them you should:
```bash
go test -tags=integration
```
_If integration tests takes too long remember to_ `go install github.com/mxk/go-sqlite/sqlite3`

Otherwise just run:
```bash
go test
You can also merge multiple dotsql instances created from different sql file inputs:
```go
dot1, err := dotsql.LoadFromFile("queries1.sql")
dot2, err := dotsql.LoadFromFile("queries2.sql")
dot := dotsql.Merge(dot1, dot2)
```

Embeding
--
To avoid distributing `sql` files with the binary, you will need to embed into it, tools like [gotic](https://github.com/gchaincl/gotic) may help
To avoid distributing `sql` files alongside the binary file, you will need to use tools like
[gotic](https://github.com/gchaincl/gotic) to embed / pack everything into one file.

TODO
----

--
- [ ] Enable text interpolation inside queries using `text/template`
- [ ] `sqlx` integration
42 changes: 24 additions & 18 deletions dotsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ func TestPrepare(t *testing.T) {
t.Error("stmt expected to be non-nil, got nil")
}

if err != nil {
t.Error("err expected to be nil, got non-nil")
}
failIfError(t, err)

ff = p.PrepareCalls()
if len(ff) != 1 {
Expand Down Expand Up @@ -149,9 +147,7 @@ func TestPrepareContext(t *testing.T) {
t.Error("stmt expected to be non-nil, got nil")
}

if err != nil {
t.Error("err expected to be nil, got non-nil")
}
failIfError(t, err)

ff = p.PrepareContextCalls()
if len(ff) != 1 {
Expand Down Expand Up @@ -228,9 +224,7 @@ func TestQuery(t *testing.T) {
t.Error("rows expected to be non-nil, got nil")
}

if err != nil {
t.Error("err expected to be nil, got non-nil")
}
failIfError(t, err)

ff = q.QueryCalls()
if len(ff) != 1 {
Expand Down Expand Up @@ -312,9 +306,7 @@ func TestQueryContext(t *testing.T) {
t.Error("rows expected to be non-nil, got nil")
}

if err != nil {
t.Error("err expected to be nil, got non-nil")
}
failIfError(t, err)

ff = q.QueryContextCalls()
if len(ff) != 1 {
Expand Down Expand Up @@ -558,9 +550,7 @@ func TestExec(t *testing.T) {
t.Error("result expected to be non-nil, got nil")
}

if err != nil {
t.Error("err expected to be nil, got non-nil")
}
failIfError(t, err)

ff = q.ExecCalls()
if len(ff) != 1 {
Expand Down Expand Up @@ -642,9 +632,7 @@ func TestExecContext(t *testing.T) {
t.Error("result expected to be non-nil, got nil")
}

if err != nil {
t.Error("err expected to be nil, got non-nil")
}
failIfError(t, err)

ff = q.ExecContextCalls()
if len(ff) != 1 {
Expand All @@ -665,6 +653,24 @@ func TestLoad(t *testing.T) {
failIfError(t, err)
}

func TestLoadFromFile(t *testing.T) {
dot, err := LoadFromFile("./non-existent.sql")
if err == nil {
t.Error("error expected to be non-nil, got nil")
}

if dot != nil {
t.Error("dotsql instance expected to be nil, got non-nil")
}

dot, err = LoadFromFile("./test_schema.sql")
failIfError(t, err)

if dot == nil {
t.Error("dotsql instance expected to be non-nil, got nil")
}
}

func TestLoadFromString(t *testing.T) {
_, err := LoadFromString("")
failIfError(t, err)
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/gchaincl/dotsql

go 1.12

require github.com/mxk/go-sqlite v0.0.0-20140611214908-167da9432e1f
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/mxk/go-sqlite v0.0.0-20140611214908-167da9432e1f h1:QlH4jpcTbMzpK5ymxjC6k/m22jkcS7uSUeiB9tF8qKs=
github.com/mxk/go-sqlite v0.0.0-20140611214908-167da9432e1f/go.mod h1:pkc41e3zYdLbnNZr/Zr5u/Ozr7D0p8EorhQiE+DmM4Y=

0 comments on commit 23aa1e8

Please sign in to comment.