turso 0.1.0
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 +7 -0
- data/ext/turso_ruby/Cargo.toml +12 -0
- data/ext/turso_ruby/extconf.rb +8 -0
- data/lib/turso/db.rb +52 -0
- data/lib/turso/result_set.rb +35 -0
- data/lib/turso/row.rb +32 -0
- data/lib/turso/transaction.rb +25 -0
- data/lib/turso.rb +8 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c46fb14c7a5e67e4ea6443ceb0d85f63f6759cf6367d08b5c398ae571172a70b
|
|
4
|
+
data.tar.gz: 41e96a29b5a885e0ae0fc84c28723cc10231d7a9813dcd2aaff86632cf97ba9a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 901580da790ee2b6fa1a59e6e391a6f340743c45be115604358fa0375f79cefc069421f36a30dbfba106d260670cb0f280e82c554ca259e928e359a3f5d56aaf
|
|
7
|
+
data.tar.gz: c7af406e10d77625d5317ff528eb1953deb4b45a2ede9c6671174651eb944c0322cae3ebf33f80d57fbaa40a98be336d33c327311657f6b777a4a0888f91bb38
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "turso_ruby"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
|
|
6
|
+
[lib]
|
|
7
|
+
crate-type = ["cdylib"]
|
|
8
|
+
|
|
9
|
+
[dependencies]
|
|
10
|
+
magnus = { version = "0.8", features = ["rb-sys"] }
|
|
11
|
+
turso_sdk_kit = { path = "../../../../sdk-kit" }
|
|
12
|
+
turso_core = { path = "../../../../core" }
|
data/lib/turso/db.rb
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Turso
|
|
4
|
+
class DB
|
|
5
|
+
def initialize(path, **opts)
|
|
6
|
+
@database = Database.new(path, opts)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def execute(sql, params = [])
|
|
10
|
+
stmt = @database.connection.prepare(sql)
|
|
11
|
+
stmt.bind_positional(params) unless params.empty?
|
|
12
|
+
case stmt.execute
|
|
13
|
+
when Integer
|
|
14
|
+
@database.last_insert_rowid
|
|
15
|
+
when 0
|
|
16
|
+
@database.changes
|
|
17
|
+
end
|
|
18
|
+
ensure
|
|
19
|
+
stmt&.finalize
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def query(sql, params = [])
|
|
23
|
+
stmt = @database.connection.prepare(sql)
|
|
24
|
+
stmt.bind_positional(params) unless params.empty?
|
|
25
|
+
ResultSet.new(stmt)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def transaction(&block)
|
|
29
|
+
if block
|
|
30
|
+
execute("BEGIN")
|
|
31
|
+
begin
|
|
32
|
+
result = block.call(self)
|
|
33
|
+
execute("COMMIT")
|
|
34
|
+
result
|
|
35
|
+
rescue StandardError
|
|
36
|
+
execute("ROLLBACK")
|
|
37
|
+
raise
|
|
38
|
+
end
|
|
39
|
+
else
|
|
40
|
+
Transaction.new(self)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def close
|
|
45
|
+
@database.close
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def closed?
|
|
49
|
+
!@database.open?
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Turso
|
|
4
|
+
class ResultSet
|
|
5
|
+
include Enumerable
|
|
6
|
+
|
|
7
|
+
def initialize(statement)
|
|
8
|
+
@statement = statement
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def each
|
|
12
|
+
return to_enum unless block_given?
|
|
13
|
+
|
|
14
|
+
columns = column_names
|
|
15
|
+
loop do
|
|
16
|
+
case @statement.step
|
|
17
|
+
when 1 # ROW
|
|
18
|
+
yield Row.new(columns, @statement.row)
|
|
19
|
+
when 0 # DONE
|
|
20
|
+
break
|
|
21
|
+
else
|
|
22
|
+
raise Turso::Error, "unexpected step status"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
self
|
|
26
|
+
ensure
|
|
27
|
+
@statement.finalize
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def column_names
|
|
31
|
+
count = @statement.column_count
|
|
32
|
+
(0...count).map { |i| @statement.column_name(i) }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
data/lib/turso/row.rb
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Turso
|
|
4
|
+
class Row
|
|
5
|
+
def initialize(columns, values)
|
|
6
|
+
@columns = columns
|
|
7
|
+
@values = values
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def [](key)
|
|
11
|
+
case key
|
|
12
|
+
when Integer then @values[key]
|
|
13
|
+
when String, Symbol then @values[@columns.index(key.to_s)]
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def to_h
|
|
18
|
+
@columns.zip(@values).to_h
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def keys = @columns
|
|
22
|
+
def values = @values
|
|
23
|
+
|
|
24
|
+
def fetch(key, &fallback)
|
|
25
|
+
value = self[key]
|
|
26
|
+
return value unless value.nil?
|
|
27
|
+
return fallback.call(key) if fallback
|
|
28
|
+
|
|
29
|
+
raise KeyError, "key not found: #{key.inspect}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Turso
|
|
4
|
+
class Transaction
|
|
5
|
+
def initialize(db)
|
|
6
|
+
@db = db
|
|
7
|
+
@active = false
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def begin
|
|
11
|
+
@db.execute("BEGIN")
|
|
12
|
+
@active = true
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def commit
|
|
16
|
+
@db.execute("COMMIT")
|
|
17
|
+
@active = false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def rollback
|
|
21
|
+
@db.execute("ROLLBACK")
|
|
22
|
+
@active = false
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/turso.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: turso
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Turso Team
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rb_sys
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.9'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.9'
|
|
26
|
+
executables: []
|
|
27
|
+
extensions:
|
|
28
|
+
- ext/turso_ruby/extconf.rb
|
|
29
|
+
extra_rdoc_files: []
|
|
30
|
+
files:
|
|
31
|
+
- ext/turso_ruby/Cargo.toml
|
|
32
|
+
- ext/turso_ruby/extconf.rb
|
|
33
|
+
- lib/turso.rb
|
|
34
|
+
- lib/turso/db.rb
|
|
35
|
+
- lib/turso/result_set.rb
|
|
36
|
+
- lib/turso/row.rb
|
|
37
|
+
- lib/turso/transaction.rb
|
|
38
|
+
licenses:
|
|
39
|
+
- MIT
|
|
40
|
+
metadata: {}
|
|
41
|
+
rdoc_options: []
|
|
42
|
+
require_paths:
|
|
43
|
+
- lib
|
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: 3.0.0
|
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
requirements: []
|
|
55
|
+
rubygems_version: 4.0.15
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: Ruby bindings for Turso
|
|
58
|
+
test_files: []
|