-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mattn/master
Bring master up-to-date
- Loading branch information
Showing
29 changed files
with
84,344 additions
and
42,940 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.db | ||
*.exe | ||
*.dll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Yasuhiro Matsumoto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
"math" | ||
"math/rand" | ||
|
||
sqlite "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
// Computes x^y | ||
func pow(x, y int64) int64 { | ||
return int64(math.Pow(float64(x), float64(y))) | ||
} | ||
|
||
// Computes the bitwise exclusive-or of all its arguments | ||
func xor(xs ...int64) int64 { | ||
var ret int64 | ||
for _, x := range xs { | ||
ret ^= x | ||
} | ||
return ret | ||
} | ||
|
||
// Returns a random number. It's actually deterministic here because | ||
// we don't seed the RNG, but it's an example of a non-pure function | ||
// from SQLite's POV. | ||
func getrand() int64 { | ||
return rand.Int63() | ||
} | ||
|
||
// Computes the standard deviation of a GROUPed BY set of values | ||
type stddev struct { | ||
xs []int64 | ||
// Running average calculation | ||
sum int64 | ||
n int64 | ||
} | ||
|
||
func newStddev() *stddev { return &stddev{} } | ||
|
||
func (s *stddev) Step(x int64) { | ||
s.xs = append(s.xs, x) | ||
s.sum += x | ||
s.n++ | ||
} | ||
|
||
func (s *stddev) Done() float64 { | ||
mean := float64(s.sum) / float64(s.n) | ||
var sqDiff []float64 | ||
for _, x := range s.xs { | ||
sqDiff = append(sqDiff, math.Pow(float64(x)-mean, 2)) | ||
} | ||
var dev float64 | ||
for _, x := range sqDiff { | ||
dev += x | ||
} | ||
dev /= float64(len(sqDiff)) | ||
return math.Sqrt(dev) | ||
} | ||
|
||
func main() { | ||
sql.Register("sqlite3_custom", &sqlite.SQLiteDriver{ | ||
ConnectHook: func(conn *sqlite.SQLiteConn) error { | ||
if err := conn.RegisterFunc("pow", pow, true); err != nil { | ||
return err | ||
} | ||
if err := conn.RegisterFunc("xor", xor, true); err != nil { | ||
return err | ||
} | ||
if err := conn.RegisterFunc("rand", getrand, false); err != nil { | ||
return err | ||
} | ||
if err := conn.RegisterAggregator("stddev", newStddev, true); err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
}) | ||
|
||
db, err := sql.Open("sqlite3_custom", ":memory:") | ||
if err != nil { | ||
log.Fatal("Failed to open database:", err) | ||
} | ||
defer db.Close() | ||
|
||
var i int64 | ||
err = db.QueryRow("SELECT pow(2,3)").Scan(&i) | ||
if err != nil { | ||
log.Fatal("POW query error:", err) | ||
} | ||
fmt.Println("pow(2,3) =", i) // 8 | ||
|
||
err = db.QueryRow("SELECT xor(1,2,3,4,5,6)").Scan(&i) | ||
if err != nil { | ||
log.Fatal("XOR query error:", err) | ||
} | ||
fmt.Println("xor(1,2,3,4,5) =", i) // 7 | ||
|
||
err = db.QueryRow("SELECT rand()").Scan(&i) | ||
if err != nil { | ||
log.Fatal("RAND query error:", err) | ||
} | ||
fmt.Println("rand() =", i) // pseudorandom | ||
|
||
_, err = db.Exec("create table foo (department integer, profits integer)") | ||
if err != nil { | ||
log.Fatal("Failed to create table:", err) | ||
} | ||
_, err = db.Exec("insert into foo values (1, 10), (1, 20), (1, 45), (2, 42), (2, 115)") | ||
if err != nil { | ||
log.Fatal("Failed to insert records:", err) | ||
} | ||
|
||
rows, err := db.Query("select department, stddev(profits) from foo group by department") | ||
if err != nil { | ||
log.Fatal("STDDEV query error:", err) | ||
} | ||
defer rows.Close() | ||
for rows.Next() { | ||
var dept int64 | ||
var dev float64 | ||
if err := rows.Scan(&dept, &dev); err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("dept=%d stddev=%f\n", dept, dev) | ||
} | ||
if err := rows.Err(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
// Copyright (C) 2014 Yasuhiro Matsumoto <[email protected]>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package sqlite3 | ||
|
||
/* | ||
#include <sqlite3.h> | ||
#include <sqlite3-binding.h> | ||
#include <stdlib.h> | ||
*/ | ||
import "C" | ||
import ( | ||
"runtime" | ||
"unsafe" | ||
) | ||
|
||
type Backup struct { | ||
type SQLiteBackup struct { | ||
b *C.sqlite3_backup | ||
} | ||
|
||
func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*Backup, error) { | ||
func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*SQLiteBackup, error) { | ||
destptr := C.CString(dest) | ||
defer C.free(unsafe.Pointer(destptr)) | ||
srcptr := C.CString(src) | ||
defer C.free(unsafe.Pointer(srcptr)) | ||
|
||
if b := C.sqlite3_backup_init(c.db, destptr, conn.db, srcptr); b != nil { | ||
return &Backup{b: b}, nil | ||
bb := &SQLiteBackup{b: b} | ||
runtime.SetFinalizer(bb, (*SQLiteBackup).Finish) | ||
return bb, nil | ||
} | ||
return nil, c.lastError() | ||
} | ||
|
@@ -29,28 +37,34 @@ func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*Backup, | |
// This function returns a boolean indicating if the backup is done and | ||
// an error signalling any other error. Done is returned if the underlying C | ||
// function returns SQLITE_DONE (Code 101) | ||
func (b *Backup) Step(p int) (bool, error) { | ||
func (b *SQLiteBackup) Step(p int) (bool, error) { | ||
ret := C.sqlite3_backup_step(b.b, C.int(p)) | ||
if ret == 101 { | ||
if ret == C.SQLITE_DONE { | ||
return true, nil | ||
} else if ret != 0 { | ||
} else if ret != 0 && ret != C.SQLITE_LOCKED && ret != C.SQLITE_BUSY { | ||
return false, Error{Code: ErrNo(ret)} | ||
} | ||
return false, nil | ||
} | ||
|
||
func (b *Backup) Remaining() int { | ||
func (b *SQLiteBackup) Remaining() int { | ||
return int(C.sqlite3_backup_remaining(b.b)) | ||
} | ||
|
||
func (b *Backup) PageCount() int { | ||
func (b *SQLiteBackup) PageCount() int { | ||
return int(C.sqlite3_backup_pagecount(b.b)) | ||
} | ||
|
||
func (b *Backup) Finish() error { | ||
func (b *SQLiteBackup) Finish() error { | ||
return b.Close() | ||
} | ||
|
||
func (b *SQLiteBackup) Close() error { | ||
ret := C.sqlite3_backup_finish(b.b) | ||
if ret != 0 { | ||
return Error{Code: ErrNo(ret)} | ||
} | ||
b.b = nil | ||
runtime.SetFinalizer(b, nil) | ||
return nil | ||
} |
Oops, something went wrong.