Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gosqlite
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gosqlite
Commits
efc5245d
Commit
efc5245d
authored
Jul 22, 2011
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add partial support to metadata.
- list tables, - list table columns.
parent
17ef1633
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
113 additions
and
2 deletions
+113
-2
Makefile
Makefile
+2
-1
meta.go
meta.go
+77
-0
sqlite.go
sqlite.go
+0
-1
sqlite_test.go
sqlite_test.go
+34
-0
No files found.
Makefile
View file @
efc5245d
...
...
@@ -8,6 +8,7 @@ TARG=github.com/gwenn/sqlite
CGOFILES
=
\
sqlite.go
\
backup.go
backup.go
\
meta.go
include
$(GOROOT)/src/Make.pkg
meta.go
0 → 100644
View file @
efc5245d
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package sqlite provides access to the SQLite library, version 3.
package
sqlite
/*
#include <sqlite3.h>
#include <stdlib.h>
// cgo doesn't support varargs
static char *my_mprintf(char *zFormat, char *arg) {
return sqlite3_mprintf(zFormat, arg);
}
*/
import
"C"
import
(
"os"
"unsafe"
)
func
(
c
*
Conn
)
Tables
()
([]
string
,
os
.
Error
)
{
s
,
err
:=
c
.
Prepare
(
"SELECT name FROM sqlite_master WHERE type IN ('table') AND name NOT LIKE 'sqlite_%'"
)
if
err
!=
nil
{
return
nil
,
err
}
var
tables
[]
string
=
make
([]
string
,
0
,
20
)
var
ok
bool
var
name
string
for
ok
,
err
=
s
.
Next
();
ok
;
ok
,
err
=
s
.
Next
()
{
s
.
Scan
(
&
name
)
tables
=
append
(
tables
,
name
)
}
if
err
!=
nil
{
return
nil
,
err
}
return
tables
,
nil
}
type
Column
struct
{
cid
int
name
string
dataType
string
notNull
bool
dfltValue
string
// FIXME type ?
pk
bool
}
func
(
c
*
Conn
)
Columns
(
table
string
)
([]
Column
,
os
.
Error
)
{
s
,
err
:=
c
.
Prepare
(
Mprintf
(
"pragma table_info(%Q)"
,
table
))
if
err
!=
nil
{
return
nil
,
err
}
if
err
!=
nil
{
return
nil
,
err
}
var
columns
[]
Column
=
make
([]
Column
,
0
,
20
)
var
ok
bool
for
ok
,
err
=
s
.
Next
();
ok
;
ok
,
err
=
s
.
Next
()
{
c
:=
Column
{}
s
.
Scan
(
&
c
.
cid
,
&
c
.
name
,
&
c
.
dataType
,
&
c
.
notNull
,
&
c
.
dfltValue
,
&
c
.
pk
)
columns
=
append
(
columns
,
c
)
}
return
columns
,
nil
}
func
Mprintf
(
format
string
,
arg
string
)
string
{
cf
:=
C
.
CString
(
format
)
defer
C
.
free
(
unsafe
.
Pointer
(
cf
))
ca
:=
C
.
CString
(
arg
)
defer
C
.
free
(
unsafe
.
Pointer
(
ca
))
zSQL
:=
C
.
my_mprintf
(
cf
,
ca
)
defer
C
.
sqlite3_free
(
unsafe
.
Pointer
(
zSQL
))
return
C
.
GoString
(
zSQL
)
}
sqlite.go
View file @
efc5245d
...
...
@@ -26,7 +26,6 @@ static int my_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
static int my_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
}
*/
import
"C"
...
...
sqlite_test.go
View file @
efc5245d
...
...
@@ -177,6 +177,40 @@ func TestInsertWithStatement(t *testing.T) {
}
}
func
TestTables
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
db
.
Close
()
tables
,
err
:=
db
.
Tables
()
if
err
!=
nil
{
t
.
Fatalf
(
"error looking for tables: %s"
,
err
)
}
if
len
(
tables
)
!=
0
{
t
.
Errorf
(
"Expected no table but got %d
\n
"
,
len
(
tables
))
}
createTable
(
db
,
t
)
tables
,
err
=
db
.
Tables
()
if
err
!=
nil
{
t
.
Fatalf
(
"error looking for tables: %s"
,
err
)
}
if
len
(
tables
)
!=
1
{
t
.
Errorf
(
"Expected one table but got %d
\n
"
,
len
(
tables
))
}
if
tables
[
0
]
!=
"test"
{
t
.
Errorf
(
"Wrong table name: 'test' <> %s
\n
"
,
tables
[
0
])
}
columns
,
err
:=
db
.
Columns
(
"test"
)
if
err
!=
nil
{
t
.
Fatalf
(
"error looking for columns: %s"
,
err
)
}
if
len
(
columns
)
!=
4
{
t
.
Errorf
(
"Expected 4 columns <> %d"
,
len
(
columns
))
}
column
:=
columns
[
2
]
if
column
.
name
!=
"int_num"
{
t
.
Errorf
(
"Wrong column name: 'int_num' <> %s"
,
column
.
name
)
}
}
func
BenchmarkScan
(
b
*
testing
.
B
)
{
b
.
StopTimer
()
db
,
_
:=
Open
(
""
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment