Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
4435c8bf
Commit
4435c8bf
authored
Jan 10, 2012
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
exp/sql: close Rows on EOF
Fixes #2624 R=rsc CC=golang-dev
https://golang.org/cl/5530068
parent
25cf9bde
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
11 deletions
+31
-11
fakedb_test.go
src/pkg/exp/sql/fakedb_test.go
+17
-8
sql.go
src/pkg/exp/sql/sql.go
+5
-2
sql_test.go
src/pkg/exp/sql/sql_test.go
+9
-1
No files found.
src/pkg/exp/sql/fakedb_test.go
View file @
4435c8bf
...
@@ -110,25 +110,34 @@ func init() {
...
@@ -110,25 +110,34 @@ func init() {
// Supports dsn forms:
// Supports dsn forms:
// <dbname>
// <dbname>
// <dbname>;
wipe
// <dbname>;
<opts> (no currently supported options)
func
(
d
*
fakeDriver
)
Open
(
dsn
string
)
(
driver
.
Conn
,
error
)
{
func
(
d
*
fakeDriver
)
Open
(
dsn
string
)
(
driver
.
Conn
,
error
)
{
d
.
mu
.
Lock
()
defer
d
.
mu
.
Unlock
()
d
.
openCount
++
if
d
.
dbs
==
nil
{
d
.
dbs
=
make
(
map
[
string
]
*
fakeDB
)
}
parts
:=
strings
.
Split
(
dsn
,
";"
)
parts
:=
strings
.
Split
(
dsn
,
";"
)
if
len
(
parts
)
<
1
{
if
len
(
parts
)
<
1
{
return
nil
,
errors
.
New
(
"fakedb: no database name"
)
return
nil
,
errors
.
New
(
"fakedb: no database name"
)
}
}
name
:=
parts
[
0
]
name
:=
parts
[
0
]
db
:=
d
.
getDB
(
name
)
d
.
mu
.
Lock
()
d
.
openCount
++
d
.
mu
.
Unlock
()
return
&
fakeConn
{
db
:
db
},
nil
}
func
(
d
*
fakeDriver
)
getDB
(
name
string
)
*
fakeDB
{
d
.
mu
.
Lock
()
defer
d
.
mu
.
Unlock
()
if
d
.
dbs
==
nil
{
d
.
dbs
=
make
(
map
[
string
]
*
fakeDB
)
}
db
,
ok
:=
d
.
dbs
[
name
]
db
,
ok
:=
d
.
dbs
[
name
]
if
!
ok
{
if
!
ok
{
db
=
&
fakeDB
{
name
:
name
}
db
=
&
fakeDB
{
name
:
name
}
d
.
dbs
[
name
]
=
db
d
.
dbs
[
name
]
=
db
}
}
return
&
fakeConn
{
db
:
db
},
nil
return
db
}
}
func
(
db
*
fakeDB
)
wipe
()
{
func
(
db
*
fakeDB
)
wipe
()
{
...
...
src/pkg/exp/sql/sql.go
View file @
4435c8bf
...
@@ -549,8 +549,8 @@ func (s *Stmt) Exec(args ...interface{}) (Result, error) {
...
@@ -549,8 +549,8 @@ func (s *Stmt) Exec(args ...interface{}) (Result, error) {
// statement, a function to call to release the connection, and a
// statement, a function to call to release the connection, and a
// statement bound to that connection.
// statement bound to that connection.
func
(
s
*
Stmt
)
connStmt
()
(
ci
driver
.
Conn
,
releaseConn
func
(),
si
driver
.
Stmt
,
err
error
)
{
func
(
s
*
Stmt
)
connStmt
()
(
ci
driver
.
Conn
,
releaseConn
func
(),
si
driver
.
Stmt
,
err
error
)
{
if
s
.
stickyE
rr
!=
nil
{
if
err
=
s
.
stickyErr
;
e
rr
!=
nil
{
return
nil
,
nil
,
nil
,
s
.
stickyErr
return
}
}
s
.
mu
.
Lock
()
s
.
mu
.
Lock
()
if
s
.
closed
{
if
s
.
closed
{
...
@@ -726,6 +726,9 @@ func (rs *Rows) Next() bool {
...
@@ -726,6 +726,9 @@ func (rs *Rows) Next() bool {
rs
.
lastcols
=
make
([]
interface
{},
len
(
rs
.
rowsi
.
Columns
()))
rs
.
lastcols
=
make
([]
interface
{},
len
(
rs
.
rowsi
.
Columns
()))
}
}
rs
.
lasterr
=
rs
.
rowsi
.
Next
(
rs
.
lastcols
)
rs
.
lasterr
=
rs
.
rowsi
.
Next
(
rs
.
lastcols
)
if
rs
.
lasterr
==
io
.
EOF
{
rs
.
Close
()
}
return
rs
.
lasterr
==
nil
return
rs
.
lasterr
==
nil
}
}
...
...
src/pkg/exp/sql/sql_test.go
View file @
4435c8bf
...
@@ -10,8 +10,10 @@ import (
...
@@ -10,8 +10,10 @@ import (
"testing"
"testing"
)
)
const
fakeDBName
=
"foo"
func
newTestDB
(
t
*
testing
.
T
,
name
string
)
*
DB
{
func
newTestDB
(
t
*
testing
.
T
,
name
string
)
*
DB
{
db
,
err
:=
Open
(
"test"
,
"foo"
)
db
,
err
:=
Open
(
"test"
,
fakeDBName
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"Open: %v"
,
err
)
t
.
Fatalf
(
"Open: %v"
,
err
)
}
}
...
@@ -73,6 +75,12 @@ func TestQuery(t *testing.T) {
...
@@ -73,6 +75,12 @@ func TestQuery(t *testing.T) {
if
!
reflect
.
DeepEqual
(
got
,
want
)
{
if
!
reflect
.
DeepEqual
(
got
,
want
)
{
t
.
Logf
(
" got: %#v
\n
want: %#v"
,
got
,
want
)
t
.
Logf
(
" got: %#v
\n
want: %#v"
,
got
,
want
)
}
}
// And verify that the final rows.Next() call, which hit EOF,
// also closed the rows connection.
if
n
:=
len
(
db
.
freeConn
);
n
!=
1
{
t
.
Errorf
(
"free conns after query hitting EOF = %d; want 1"
,
n
)
}
}
}
func
TestRowsColumns
(
t
*
testing
.
T
)
{
func
TestRowsColumns
(
t
*
testing
.
T
)
{
...
...
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