Skip to content

Commit f0c9c00

Browse files
committed
initial commit
0 parents  commit f0c9c00

File tree

13 files changed

+219
-0
lines changed

13 files changed

+219
-0
lines changed

.envrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.direnv

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "libsql-c"]
2+
path = libsql-c
3+
url = https://github.com/tursodatabase/libsql-c

Gemfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source "https://rubygems.org"
2+
3+
gem "rspec", "~> 3.10"
4+
5+
gem "ffi", "~> 1.17"

Gemfile.lock

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
diff-lcs (1.5.1)
5+
ffi (1.17.0)
6+
ffi (1.17.0-arm64-darwin)
7+
rspec (3.13.0)
8+
rspec-core (~> 3.13.0)
9+
rspec-expectations (~> 3.13.0)
10+
rspec-mocks (~> 3.13.0)
11+
rspec-core (3.13.1)
12+
rspec-support (~> 3.13.0)
13+
rspec-expectations (3.13.3)
14+
diff-lcs (>= 1.2.0, < 2.0)
15+
rspec-support (~> 3.13.0)
16+
rspec-mocks (3.13.2)
17+
diff-lcs (>= 1.2.0, < 2.0)
18+
rspec-support (~> 3.13.0)
19+
rspec-support (3.13.1)
20+
21+
PLATFORMS
22+
arm64-darwin-23
23+
ruby
24+
25+
DEPENDENCIES
26+
ffi (~> 1.17)
27+
rspec (~> 3.10)
28+
29+
BUNDLED WITH
30+
2.5.16

build.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env sh
2+
3+
set -xe
4+
5+
cd libsql-c
6+
7+
cargo zigbuild --target universal2-apple-darwin --release
8+
RUSTFLAGS="-C target-feature=-crt-static" cargo zigbuild --target aarch64-unknown-linux-musl --release
9+
RUSTFLAGS="-C target-feature=-crt-static" cargo zigbuild --target x86_64-unknown-linux-musl --release
10+
11+
rm -rf \
12+
../lib/lib/aarch64-unknown-linux-musl \
13+
../lib/lib/x86_64-unknown-linux-musl \
14+
../lib/lib/universal2-apple-darwin \
15+
16+
mkdir -p \
17+
../lib/lib/aarch64-unknown-linux-musl \
18+
../lib/lib/x86_64-unknown-linux-musl \
19+
../lib/lib/universal2-apple-darwin \
20+
21+
cp ./target/x86_64-unknown-linux-musl/release/liblibsql.so ../lib/lib/x86_64-unknown-linux-musl/
22+
cp ./target/aarch64-unknown-linux-musl/release/liblibsql.so ../lib/lib/aarch64-unknown-linux-musl/
23+
cp ./target/universal2-apple-darwin/release/liblibsql.dylib ../lib/lib/universal2-apple-darwin/

flake.lock

+61
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
description = "A very basic flake";
3+
4+
inputs = {
5+
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
6+
flake-utils = {
7+
url = "github:numtide/flake-utils";
8+
};
9+
};
10+
outputs = { self, nixpkgs, flake-utils }:
11+
flake-utils.lib.eachDefaultSystem (system:
12+
let
13+
pkgs = import nixpkgs { inherit system; };
14+
in
15+
{
16+
devShells.default =
17+
with pkgs;
18+
mkShell {
19+
buildInputs = [
20+
(ruby.withPackages (ps: with ps; [ bundler ]))
21+
turso-cli
22+
ruby-lsp
23+
] ++ lib.optionals stdenv.isDarwin [ iconv ];
24+
};
25+
});
26+
}
27+
14.9 MB
Binary file not shown.
35.5 MB
Binary file not shown.
16.1 MB
Binary file not shown.

lib/libsql.rb

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
require 'ffi';
3+
4+
module Libsql
5+
extend FFI::Library
6+
7+
lib = File.expand_path('../lib/universal2-apple-darwin/liblibsql.dylib', __FILE__)
8+
ffi_lib lib
9+
10+
Cypher = enum(:default, :aes256)
11+
Type = enum(
12+
:integer, 1,
13+
:real, 2,
14+
:text, 3,
15+
:blob, 4,
16+
:null, 5
17+
)
18+
19+
class Database < FFI::Struct
20+
layout :err => :pointer,
21+
:inner => :pointer
22+
end
23+
24+
class Connection < FFI::Struct
25+
layout :err => :pointer,
26+
:inner => :pointer
27+
end
28+
29+
class Transaction < FFI::Struct
30+
layout :err => :pointer,
31+
:inner => :pointer
32+
end
33+
34+
class Statement < FFI::Struct
35+
layout :err => :pointer,
36+
:inner => :pointer
37+
end
38+
39+
class DatabaseDesc < FFI::Struct
40+
layout :url => :string,
41+
:path => :string,
42+
:auth_token => :string,
43+
:encryption_key => :string,
44+
:sync_inteval => :uint64,
45+
:cypher => Cypher,
46+
:disable_read_your_writes => :bool,
47+
:webpki => :bool
48+
end
49+
50+
attach_function :libsql_database_init, [DatabaseDesc.by_value], Database.by_value
51+
attach_function :libsql_database_deinit, [Database.by_value], :void
52+
attach_function :libsql_database_sync, [Database.by_value], Database.by_value
53+
attach_function :libsql_database_connect, [Database.by_value], Connection.by_value
54+
55+
attach_function :libsql_connection_deinit, [Connection.by_value], :void
56+
attach_function :libsql_connection_transaction, [Connection.by_value], Transaction.by_value
57+
attach_function :libsql_connection_prepare, [Connection.by_value, :string], Statement.by_value
58+
59+
attach_function :libsql_error_message, [:pointer], :string
60+
end
61+
62+
desc = Libsql::DatabaseDesc.new
63+
64+
db = Libsql.libsql_database_init(desc)
65+
conn = Libsql.libsql_database_connect(db)
66+
67+
puts conn.values

libsql-c

Submodule libsql-c added at 5a56848

0 commit comments

Comments
 (0)