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
30d005b4
Commit
30d005b4
authored
Jul 23, 2011
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add method to list FKs.
parent
7dc71ca7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
14 deletions
+77
-14
meta.go
meta.go
+42
-10
sqlite_test.go
sqlite_test.go
+35
-4
No files found.
meta.go
View file @
30d005b4
...
...
@@ -40,12 +40,12 @@ func (c *Conn) Tables() ([]string, os.Error) {
}
type
Column
struct
{
cid
int
name
string
dataType
string
notNull
bool
d
fltValue
string
// FIXME type ?
pk
bool
Cid
int
Name
string
DataType
string
NotNull
bool
D
fltValue
string
// FIXME type ?
Pk
bool
}
func
(
c
*
Conn
)
Columns
(
table
string
)
([]
Column
,
os
.
Error
)
{
...
...
@@ -53,19 +53,51 @@ func (c *Conn) Columns(table string) ([]Column, os.Error) {
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
)
err
=
s
.
Scan
(
&
c
.
Cid
,
&
c
.
Name
,
&
c
.
DataType
,
&
c
.
NotNull
,
&
c
.
DfltValue
,
&
c
.
Pk
)
if
err
!=
nil
{
return
nil
,
err
}
columns
=
append
(
columns
,
c
)
}
if
err
!=
nil
{
return
nil
,
err
}
return
columns
,
nil
}
type
ForeignKey
struct
{
Id
int
Seq
int
Table
string
From
string
To
string
}
func
(
c
*
Conn
)
ForeignKeys
(
table
string
)
([]
ForeignKey
,
os
.
Error
)
{
s
,
err
:=
c
.
Prepare
(
Mprintf
(
"pragma foreign_key_list(%Q)"
,
table
))
if
err
!=
nil
{
return
nil
,
err
}
var
fks
[]
ForeignKey
=
make
([]
ForeignKey
,
0
,
20
)
var
ok
bool
for
ok
,
err
=
s
.
Next
();
ok
;
ok
,
err
=
s
.
Next
()
{
fk
:=
ForeignKey
{}
err
=
s
.
NamedScan
(
"id"
,
&
fk
.
Id
,
"seq"
,
&
fk
.
Seq
,
"table"
,
&
fk
.
Table
,
"from"
,
&
fk
.
From
,
"to"
,
&
fk
.
To
)
if
err
!=
nil
{
return
nil
,
err
}
fks
=
append
(
fks
,
fk
)
}
if
err
!=
nil
{
return
nil
,
err
}
return
fks
,
nil
}
func
Mprintf
(
format
string
,
arg
string
)
string
{
cf
:=
C
.
CString
(
format
)
defer
C
.
free
(
unsafe
.
Pointer
(
cf
))
...
...
sqlite_test.go
View file @
30d005b4
...
...
@@ -198,6 +198,7 @@ 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
)
...
...
@@ -216,16 +217,46 @@ func TestTables(t *testing.T) {
if
tables
[
0
]
!=
"test"
{
t
.
Errorf
(
"Wrong table name: 'test' <> %s
\n
"
,
tables
[
0
])
}
}
func
TestColumns
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
db
.
Close
()
createTable
(
db
,
t
)
columns
,
err
:=
db
.
Columns
(
"test"
)
if
err
!=
nil
{
t
.
Fatalf
(
"error l
ooking for
columns: %s"
,
err
)
t
.
Fatalf
(
"error l
isting
columns: %s"
,
err
)
}
if
len
(
columns
)
!=
4
{
t
.
Error
f
(
"Expected 4 columns <> %d"
,
len
(
columns
))
t
.
Fatal
f
(
"Expected 4 columns <> %d"
,
len
(
columns
))
}
column
:=
columns
[
2
]
if
column
.
name
!=
"int_num"
{
t
.
Errorf
(
"Wrong column name: 'int_num' <> %s"
,
column
.
name
)
if
column
.
Name
!=
"int_num"
{
t
.
Errorf
(
"Wrong column name: 'int_num' <> %s"
,
column
.
Name
)
}
}
func
TestForeignKeys
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
db
.
Close
()
err
:=
db
.
Exec
(
"CREATE TABLE parent (id INTEGER PRIMARY KEY);"
+
"CREATE TABLE child (id INTEGER PRIMARY KEY, parentId INTEGER, "
+
"FOREIGN KEY (parentId) REFERENCES parent(id));"
)
if
err
!=
nil
{
t
.
Fatalf
(
"error creating tables: %s"
,
err
)
}
fks
,
err
:=
db
.
ForeignKeys
(
"child"
)
if
err
!=
nil
{
t
.
Fatalf
(
"error listing FKs: %s"
,
err
)
}
if
len
(
fks
)
!=
1
{
t
.
Fatalf
(
"Expected 1 FK <> %d"
,
len
(
fks
))
}
fk
:=
fks
[
0
]
if
fk
.
From
!=
"parentId"
||
fk
.
Table
!=
"parent"
||
fk
.
To
!=
"id"
{
t
.
Errorf
(
"Unexpected FK data: %#v"
,
fk
)
}
}
...
...
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