https://github.com/tengattack/gluasql
A native Go implementation of SQL client for the GopherLua VM.
https://github.com/tengattack/gluasql
Last synced: 10 months ago
JSON representation
A native Go implementation of SQL client for the GopherLua VM.
- Host: GitHub
- URL: https://github.com/tengattack/gluasql
- Owner: tengattack
- License: mit
- Created: 2018-08-30T09:14:07.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-03-25T12:43:56.000Z (about 2 years ago)
- Last Synced: 2024-04-24T11:15:56.083Z (about 2 years ago)
- Language: Go
- Homepage:
- Size: 116 KB
- Stars: 11
- Watchers: 4
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LuaSql for GopherLua
A native Go implementation of SQL client for the [GopherLua](https://github.com/yuin/gopher-lua) VM.
## Using
### Loading Modules
```go
import (
"github.com/tengattack/gluasql"
)
// Bring up a GopherLua VM
L := lua.NewState()
defer L.Close()
// Preload LuaSql modules
gluasql.Preload(L)
```
Or if we only need `mysql` module:
```go
import (
mysql "github.com/tengattack/gluasql/mysql"
)
// Bring up a GopherLua VM
L := lua.NewState()
defer L.Close()
L.PreloadModule("mysql", mysql.Loader)
```
### MySQL
```lua
mysql = require('mysql')
c = mysql.new()
ok, err = c:connect({ host = '127.0.0.1', port = 3306, database = 'test', user = 'user', password = 'pass' })
if ok then
res, err = c:query('SELECT * FROM mytable LIMIT 2')
dump(res)
end
```
### PostgreSQL
import [lib/pq](https://github.com/lib/pq) first:
```go
import (
_ "github.com/lib/pq"
)
```
```lua
mysql = require('mysql')
c = mysql.new()
ok, err = c:connect('postgres://host=127.0.0.1 port=5432 user=user password=123456 dbname=foo sslmode=disable')
if ok then
res, err = c:query('SELECT * FROM mytable LIMIT 2')
dump(res)
end
```
### SQLite
Since it depends `go-sqlite3`, we need `gcc` to compile SQLite module, more details:
[https://github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
```lua
sqlite3 = require('sqlite3')
c = sqlite3.new()
ok, err = c:open('test.db', { cache = 'shared' })
if ok then
res, err = c:query('SELECT * FROM mytable LIMIT 2')
dump(res)
end
```
## Testing
```bash
$ go test -coverprofile=/tmp/go-code-cover github.com/tengattack/gluasql...
ok github.com/tengattack/gluasql/mysql 0.020s coverage: 79.9% of statements
ok github.com/tengattack/gluasql/sqlite3 0.019s coverage: 71.3% of statements
ok github.com/tengattack/gluasql/util 0.015s coverage: 76.9% of statements
```
## License
MIT