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
69eefa95
Commit
69eefa95
authored
May 11, 2013
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Transaction method like in tclsqlite.
parent
506077b9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
1 deletion
+58
-1
sqlite.go
sqlite.go
+41
-0
sqlite_test.go
sqlite_test.go
+17
-1
No files found.
sqlite.go
View file @
69eefa95
...
@@ -24,6 +24,7 @@ import (
...
@@ -24,6 +24,7 @@ import (
"io"
"io"
"os"
"os"
"reflect"
"reflect"
"strconv"
"time"
"time"
"unsafe"
"unsafe"
)
)
...
@@ -180,6 +181,7 @@ type Conn struct {
...
@@ -180,6 +181,7 @@ type Conn struct {
udfs
map
[
string
]
*
sqliteFunction
udfs
map
[
string
]
*
sqliteFunction
modules
map
[
string
]
*
sqliteModule
modules
map
[
string
]
*
sqliteModule
timeUsed
time
.
Time
timeUsed
time
.
Time
nTransaction
uint8
}
}
// Version returns the run-time library version number
// Version returns the run-time library version number
...
@@ -465,6 +467,45 @@ func (c *Conn) Rollback() error {
...
@@ -465,6 +467,45 @@ func (c *Conn) Rollback() error {
return
c
.
exec
(
"ROLLBACK"
)
return
c
.
exec
(
"ROLLBACK"
)
}
}
// Transaction is used to execute a function inside an SQLite database transaction.
// The transaction is committed when the function completes (with no error),
// or it rolls back if the function fails.
// If the transaction occurs within another transaction (only one that is started using this method) a Savepoint is created.
// Two errors may be returned: the first is the one returned by the f function,
// the second is the one returned by begin/commit/rollback.
func
(
c
*
Conn
)
Transaction
(
t
TransactionType
,
f
func
(
c
*
Conn
)
error
)
(
gerr
error
,
serr
error
)
{
if
c
.
nTransaction
==
0
{
serr
=
c
.
BeginTransaction
(
t
)
}
else
{
serr
=
c
.
Savepoint
(
strconv
.
Itoa
(
int
(
c
.
nTransaction
)))
}
if
serr
!=
nil
{
return
}
c
.
nTransaction
++
defer
func
()
{
c
.
nTransaction
--
if
gerr
!=
nil
{
if
c
.
nTransaction
==
0
{
serr
=
c
.
Rollback
()
}
else
{
serr
=
c
.
RollbackSavepoint
(
strconv
.
Itoa
(
int
(
c
.
nTransaction
)))
}
}
else
{
if
c
.
nTransaction
==
0
{
serr
=
c
.
Commit
()
}
else
{
serr
=
c
.
ReleaseSavepoint
(
strconv
.
Itoa
(
int
(
c
.
nTransaction
)))
}
if
serr
!=
nil
{
c
.
Rollback
()
}
}
}()
gerr
=
f
(
c
)
return
}
// Savepoint starts a new transaction with a name.
// Savepoint starts a new transaction with a name.
// (See http://sqlite.org/lang_savepoint.html)
// (See http://sqlite.org/lang_savepoint.html)
func
(
c
*
Conn
)
Savepoint
(
name
string
)
error
{
func
(
c
*
Conn
)
Savepoint
(
name
string
)
error
{
...
...
sqlite_test.go
View file @
69eefa95
...
@@ -93,7 +93,7 @@ func TestCreateTable(t *testing.T) {
...
@@ -93,7 +93,7 @@ func TestCreateTable(t *testing.T) {
createTable
(
db
,
t
)
createTable
(
db
,
t
)
}
}
func
TestTransaction
(
t
*
testing
.
T
)
{
func
Test
Manual
Transaction
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
checkNoError
(
t
,
db
.
Begin
(),
"Error while beginning transaction: %s"
)
checkNoError
(
t
,
db
.
Begin
(),
"Error while beginning transaction: %s"
)
...
@@ -243,6 +243,22 @@ func TestExecMisuse(t *testing.T) {
...
@@ -243,6 +243,22 @@ func TestExecMisuse(t *testing.T) {
assert
(
t
,
"exec misuse expected"
,
err
!=
nil
)
assert
(
t
,
"exec misuse expected"
,
err
!=
nil
)
}
}
func
TestTransaction
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
createTable
(
db
,
t
)
gerr
,
serr
:=
db
.
Transaction
(
Immediate
,
func
(
_
*
Conn
)
error
{
err
,
nerr
:=
db
.
Transaction
(
Immediate
,
func
(
__
*
Conn
)
error
{
return
db
.
Exec
(
"INSERT INTO test VALUES (?, ?, ?, ?)"
,
0
,
273.1
,
1
,
"test"
)
})
checkNoError
(
t
,
err
,
"Applicative error: %s"
)
checkNoError
(
t
,
nerr
,
"SQLite error: %s"
)
return
err
})
checkNoError
(
t
,
gerr
,
"Applicative error: %s"
)
checkNoError
(
t
,
serr
,
"SQLite error: %s"
)
}
func
assertEquals
(
t
*
testing
.
T
,
format
string
,
expected
,
actual
interface
{})
{
func
assertEquals
(
t
*
testing
.
T
,
format
string
,
expected
,
actual
interface
{})
{
if
expected
!=
actual
{
if
expected
!=
actual
{
t
.
Errorf
(
format
,
expected
,
actual
)
t
.
Errorf
(
format
,
expected
,
actual
)
...
...
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