turso 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0f137216982382d730f16f60d4d54b154fd86948c1a062d44e287d27c395a79
4
- data.tar.gz: da3364aa4a00bb7e18383dc19e3d75d012e5f4c1f6657f65832fff9d1bd4859f
3
+ metadata.gz: 84d6e0b4a1a771d9076be22e4376a5ee2b411df4c38f4a8e648e36f0ea842a18
4
+ data.tar.gz: 5d23d15b3f15f21341a84aaa023b273c0241da38e10edf736a675a73a5878328
5
5
  SHA512:
6
- metadata.gz: 6ad3d918170633203bacb29ca479b53dece61e2ea44832a79a411ef2931f0ad8230495af20a8b9e64f7def2672dcf7360dc92e0dd0a652ff2c627f9c52e55b9a
7
- data.tar.gz: 764bf603e8b367abf6598af2b987ccb357414bcef242a899c1226bf9484a044a6cb57f81606910fafac000cd5793dbb26c04b1de4066c19a4405bc3e6bb5280c
6
+ metadata.gz: 0ee347f73af6248f112c8cafe24bfce4741931a26138bcd17dea95ffc8488c1eb5116fedcf467ecd67e11f39244dafe767e741f231a98fc1fb09ff3516415d21
7
+ data.tar.gz: fbd0036b2131b80c98352acd288673c617289048c01f0c58f7c67c78638ee72cd00ef17e500b19424fb65256ee874d966d9f1005bdd081801a5e57315ae3e01a
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turso
4
+ class Connection
5
+ def initialize(native_connection)
6
+ @native = native_connection
7
+ @closed = false
8
+ @owner = owner_token
9
+ end
10
+
11
+ def closed?
12
+ @closed
13
+ end
14
+
15
+ def close
16
+ return if closed?
17
+ @native.close
18
+ @closed = true
19
+ end
20
+
21
+ def prepare(sql)
22
+ check_owner!
23
+ Statement.new(@native.prepare(sql), self)
24
+ end
25
+
26
+ def execute(sql, *bind_args)
27
+ stmt = prepare(sql)
28
+ begin
29
+ stmt.bind(*bind_args) unless bind_args.empty?
30
+ stmt.run
31
+ ensure
32
+ stmt.close
33
+ end
34
+ end
35
+
36
+ def execute_batch(sql)
37
+ check_owner!
38
+ @native.execute_batch(sql)
39
+ end
40
+
41
+ def query(sql, *bind_args)
42
+ stmt = prepare(sql)
43
+ stmt.bind(*bind_args) unless bind_args.empty?
44
+ ResultSet.new(stmt)
45
+ end
46
+
47
+ def get_first_row(sql, *bind_args)
48
+ stmt = prepare(sql)
49
+ begin
50
+ stmt.bind(*bind_args) unless bind_args.empty?
51
+ stmt.get
52
+ ensure
53
+ stmt.close
54
+ end
55
+ end
56
+
57
+ def get_first_value(sql, *bind_args)
58
+ row = get_first_row(sql, *bind_args)
59
+ row&.first
60
+ end
61
+
62
+ def busy_timeout=(ms)
63
+ check_owner!
64
+ @native.busy_timeout = ms.to_i
65
+ end
66
+
67
+ def query_timeout=(ms)
68
+ check_owner!
69
+ @native.query_timeout = ms.to_i
70
+ end
71
+
72
+ def query_timeout
73
+ check_owner!
74
+ @native.query_timeout
75
+ end
76
+
77
+ def interrupt
78
+ check_owner!
79
+ @native.interrupt
80
+ end
81
+
82
+ def total_changes
83
+ check_owner!
84
+ @native.total_changes
85
+ end
86
+
87
+ def changes
88
+ check_owner!
89
+ @native.changes
90
+ end
91
+
92
+ def last_insert_rowid
93
+ check_owner!
94
+ @native.last_insert_rowid
95
+ end
96
+
97
+ def savepoint(name)
98
+ execute("SAVEPOINT #{name}")
99
+ end
100
+
101
+ def release_savepoint(name)
102
+ execute("RELEASE SAVEPOINT #{name}")
103
+ end
104
+
105
+ def rollback_to_savepoint(name)
106
+ execute("ROLLBACK TO SAVEPOINT #{name}")
107
+ end
108
+
109
+ private
110
+
111
+ def owner_token
112
+ [Thread.current.object_id, Fiber.current.object_id]
113
+ end
114
+
115
+ def check_owner!
116
+ unless owner_token == @owner
117
+ raise Turso::Exception, "connection is already in use by another thread or fiber"
118
+ end
119
+ end
120
+ end
121
+ end
@@ -1,7 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "forwardable"
4
+
3
5
  module Turso
4
6
  class Database
7
+ extend Forwardable
8
+
9
+ def_delegators :connection, :prepare, :execute, :execute_batch, :query,
10
+ :get_first_row, :get_first_value, :busy_timeout, :busy_timeout=,
11
+ :query_timeout, :query_timeout=, :interrupt, :total_changes,
12
+ :changes, :last_insert_rowid
13
+
5
14
  def initialize(path = ":memory:", **options)
6
15
  options[:experimental_features] = normalize_experimental_features(options[:experimental_features])
7
16
  @native = NativeDatabase.new(path, options)
@@ -28,45 +37,8 @@ module Turso
28
37
  @closed = true
29
38
  end
30
39
 
31
- def prepare(sql)
32
- raise Turso::Exception, "database is closed" if closed?
33
- Statement.new(@native.connection.prepare(sql), @native.connection)
34
- end
35
-
36
- def execute(sql, *bind_args)
37
- stmt = prepare(sql)
38
- begin
39
- stmt.bind(*bind_args) unless bind_args.empty?
40
- stmt.run
41
- ensure
42
- stmt.close
43
- end
44
- end
45
-
46
- def execute_batch(sql)
47
- raise Turso::Exception, "database is closed" if closed?
48
- @native.connection.execute_batch(sql)
49
- end
50
-
51
- def query(sql, *bind_args)
52
- stmt = prepare(sql)
53
- stmt.bind(*bind_args) unless bind_args.empty?
54
- ResultSet.new(stmt)
55
- end
56
-
57
- def get_first_row(sql, *bind_args)
58
- stmt = prepare(sql)
59
- begin
60
- stmt.bind(*bind_args) unless bind_args.empty?
61
- stmt.get
62
- ensure
63
- stmt.close
64
- end
65
- end
66
-
67
- def get_first_value(sql, *bind_args)
68
- row = get_first_row(sql, *bind_args)
69
- row&.first
40
+ def connection
41
+ Connection.new(@native.connection)
70
42
  end
71
43
 
72
44
  def transaction(mode = :deferred)
@@ -81,29 +53,5 @@ module Turso
81
53
  raise
82
54
  end
83
55
  end
84
-
85
- def busy_timeout=(ms)
86
- @native.connection.busy_timeout = ms
87
- end
88
-
89
- def busy_timeout
90
- @native.connection.busy_timeout
91
- end
92
-
93
- def total_changes
94
- @native.total_changes
95
- end
96
-
97
- def changes
98
- @native.connection.changes
99
- end
100
-
101
- def last_insert_rowid
102
- @native.last_insert_rowid
103
- end
104
-
105
- def interrupt
106
- @native.connection.interrupt
107
- end
108
56
  end
109
57
  end
@@ -5,7 +5,7 @@ module Turso
5
5
  def initialize(native_statement, native_database)
6
6
  @native = native_statement
7
7
  @database = native_database
8
- @owner = Thread.current
8
+ @owner = owner_token
9
9
  end
10
10
 
11
11
  def bind(*args)
@@ -79,9 +79,13 @@ module Turso
79
79
 
80
80
  private
81
81
 
82
+ def owner_token
83
+ [Thread.current.object_id, Fiber.current.object_id]
84
+ end
85
+
82
86
  def check_owner!
83
- unless Thread.current.equal?(@owner)
84
- raise Turso::Exception, "statement is already in use by another thread"
87
+ unless owner_token == @owner
88
+ raise Turso::Exception, "statement is already in use by another thread or fiber"
85
89
  end
86
90
  end
87
91
 
Binary file
data/lib/turso/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Turso
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
data/lib/turso.rb CHANGED
@@ -5,6 +5,7 @@ require "turso/turso_ruby"
5
5
  require_relative "turso/row"
6
6
  require_relative "turso/result_set"
7
7
  require_relative "turso/statement"
8
+ require_relative "turso/connection"
8
9
  require_relative "turso/database"
9
10
  require_relative "turso/transaction"
10
11
  require_relative "turso/version"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Turso Team
@@ -47,6 +47,7 @@ files:
47
47
  - ext/turso_ruby/Cargo.toml
48
48
  - ext/turso_ruby/extconf.rb
49
49
  - lib/turso.rb
50
+ - lib/turso/connection.rb
50
51
  - lib/turso/database.rb
51
52
  - lib/turso/result_set.rb
52
53
  - lib/turso/row.rb