Skip to content

Commit 1427f7d

Browse files
committed
Updated for 32-bit
1 parent 74d16c1 commit 1427f7d

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

pkg/sqlite3/conn.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sqlite3
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"runtime"
78
"sync"
89

@@ -38,6 +39,15 @@ type TxnFunc func(SQTransaction) error
3839
func OpenPath(path string, flags sqlite3.OpenFlags) (*Conn, error) {
3940
poolconn := new(Conn)
4041

42+
// If no create flag then check to make sure database exists
43+
if path != defaultMemory && flags&sqlite3.SQLITE_OPEN_CREATE == 0 {
44+
if _, err := os.Stat(path); os.IsNotExist(err) {
45+
return nil, ErrNotFound.Withf("%q", path)
46+
} else if err != nil {
47+
return nil, err
48+
}
49+
}
50+
4151
// Open database with flags
4252
if conn, err := sqlite3.OpenPathEx(path, flags, ""); err != nil {
4353
return nil, err

pkg/sqlite3/pool.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
// PoolConfig is the starting configuration for a pool
2929
type PoolConfig struct {
30-
Max int64 `yaml:"max"` // The maximum number of connections in the pool
30+
Max int32 `yaml:"max"` // The maximum number of connections in the pool
3131
Schemas map[string]string `yaml:"databases"` // Schema names mapped onto path for database file
3232
Trace bool `yaml:"trace"` // Profiling for statements
3333
Create bool `yaml:"create"` // When false, do not allow creation of new file-based databases
@@ -45,7 +45,7 @@ type Pool struct {
4545
errs chan<- error
4646
ctx context.Context
4747
cancel context.CancelFunc
48-
n int64
48+
n int32
4949
}
5050

5151
////////////////////////////////////////////////////////////////////////////////
@@ -85,7 +85,7 @@ func OpenPool(config PoolConfig, errs chan<- error) (*Pool, error) {
8585
if config.Max == 0 {
8686
config.Max = defaultPoolConfig.Max
8787
} else {
88-
config.Max = maxInt64(config.Max, 1)
88+
config.Max = maxInt32(config.Max, 1)
8989
}
9090

9191
// Set default flags if not set
@@ -156,20 +156,20 @@ func (p *Pool) String() string {
156156
// PUBLIC METHODS
157157

158158
// Max returns the maximum number of connections allowed
159-
func (p *Pool) Max() int64 {
160-
return atomic.LoadInt64(&p.PoolConfig.Max)
159+
func (p *Pool) Max() int32 {
160+
return atomic.LoadInt32(&p.PoolConfig.Max)
161161
}
162162

163163
// SetMax allowed connections released from pool. Note this does not change
164164
// the maximum instantly, it will settle to this value over time. Set as value
165165
// zero to disable opening new connections
166-
func (p *Pool) SetMax(n int64) {
167-
atomic.StoreInt64(&p.PoolConfig.Max, maxInt64(n, 0))
166+
func (p *Pool) SetMax(n int32) {
167+
atomic.StoreInt32(&p.PoolConfig.Max, maxInt32(n, 0))
168168
}
169169

170170
// Cur returns the current number of used connections
171-
func (p *Pool) Cur() int64 {
172-
return atomic.LoadInt64(&p.n)
171+
func (p *Pool) Cur() int32 {
172+
return atomic.LoadInt32(&p.n)
173173
}
174174

175175
// Get a connection from the pool, and return it to the pool when the context
@@ -190,7 +190,7 @@ func (p *Pool) Get(ctx context.Context) SQConnection {
190190
panic("Expected conn.c to be nil")
191191
} else {
192192
conn.c = make(chan struct{})
193-
atomic.AddInt64(&p.n, 1)
193+
atomic.AddInt32(&p.n, 1)
194194
}
195195

196196
// Release the connection in the background
@@ -297,7 +297,7 @@ func (p *Pool) put(conn *Conn) {
297297
conn.c = nil
298298
}
299299
// Choose to put back into pool or close connection
300-
n := atomic.AddInt64(&p.n, -1)
300+
n := atomic.AddInt32(&p.n, -1)
301301
if n >= p.Max() {
302302
p.Pool.Put(conn)
303303
} else if err := conn.Close(); err != nil {
@@ -371,8 +371,8 @@ func (p *Pool) trace(c *Conn, s *sqlite3.Statement, ns int64) {
371371
fmt.Printf("TRACE %q => %v\n", s, time.Duration(ns)*time.Nanosecond)
372372
}
373373

374-
// maxInt64 returns the maximum of two values
375-
func maxInt64(a, b int64) int64 {
374+
// maxInt32 returns the maximum of two values
375+
func maxInt32(a, b int32) int32 {
376376
if a > b {
377377
return a
378378
}

pkg/sqlite3/schema_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Test_Schema_001(t *testing.T) {
1919
// Create the pool
2020
pool, err := NewPool("", errs)
2121
if err != nil {
22-
t.Error(err)
22+
t.Fatal(err)
2323
}
2424
defer pool.Close()
2525

@@ -50,9 +50,10 @@ func Test_Schema_002(t *testing.T) {
5050
"main": filepath.Join(tmpdir, "main.sqlite"),
5151
"test": filepath.Join(tmpdir, "test.sqlite"),
5252
},
53+
Create: true,
5354
}, errs)
5455
if err != nil {
55-
t.Error(err)
56+
t.Fatal(err)
5657
}
5758
defer pool.Close()
5859

@@ -83,9 +84,10 @@ func Test_Schema_003(t *testing.T) {
8384
"main": filepath.Join(tmpdir, "main.sqlite"),
8485
"test": filepath.Join(tmpdir, "test.sqlite"),
8586
},
87+
Create: true,
8688
}, errs)
8789
if err != nil {
88-
t.Error(err)
90+
t.Fatal(err)
8991
}
9092
defer pool.Close()
9193

@@ -146,9 +148,10 @@ func Test_Schema_004(t *testing.T) {
146148
"main": filepath.Join(tmpdir, "main.sqlite"),
147149
"test": filepath.Join(tmpdir, "test.sqlite"),
148150
},
151+
Create: true,
149152
}, errs)
150153
if err != nil {
151-
t.Error(err)
154+
t.Fatal(err)
152155
}
153156
defer pool.Close()
154157

@@ -195,9 +198,10 @@ func Test_Schema_006(t *testing.T) {
195198
Schemas: map[string]string{
196199
"main": filepath.Join(tmpdir, "main.sqlite"),
197200
},
201+
Create: true,
198202
}, errs)
199203
if err != nil {
200-
t.Error(err)
204+
t.Fatal(err)
201205
}
202206
defer pool.Close()
203207

@@ -206,6 +210,7 @@ func Test_Schema_006(t *testing.T) {
206210
if conn == nil {
207211
t.Fatal("Unexpected nil connection")
208212
}
213+
defer pool.Put(conn)
209214

210215
// Create a table
211216
if err := conn.Exec(N("table_a").CreateTable(
@@ -245,7 +250,7 @@ func Test_Schema_007(t *testing.T) {
245250
Trace: true,
246251
}, errs)
247252
if err != nil {
248-
t.Error(err)
253+
t.Fatal(err)
249254
}
250255
defer pool.Close()
251256

@@ -254,6 +259,7 @@ func Test_Schema_007(t *testing.T) {
254259
if conn == nil {
255260
t.Fatal("Unexpected nil connection")
256261
}
262+
defer pool.Put(conn)
257263

258264
// Create a table
259265
if err := conn.Exec(N("table_a").CreateTable(

sys/sqlite3/sqliteex_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func Test_SQLiteEx_002(t *testing.T) {
9797
// Run long running query, expect interrupted error
9898
if st, err := db.Prepare(longRunningQuery); err != nil {
9999
t.Error(err)
100-
} else if r, err := st.Exec(0, 9999999999); err != nil && err != sqlite3.SQLITE_INTERRUPT {
100+
} else if r, err := st.Exec(0, 99999999); err != nil && err != sqlite3.SQLITE_INTERRUPT {
101101
t.Error("Error returned:", err)
102102
} else if r != nil {
103103
t.Log(r)

0 commit comments

Comments
 (0)