chdb-ruby 0.1.0.rc.2
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/INSTALLATION.md +27 -0
- data/LICENSE +203 -0
- data/README.md +128 -0
- data/dependencies.yml +2 -0
- data/ext/chdb/chdb.c +32 -0
- data/ext/chdb/chdb_handle.c +49 -0
- data/ext/chdb/chdb_handle.h +16 -0
- data/ext/chdb/connection.c +118 -0
- data/ext/chdb/connection.h +23 -0
- data/ext/chdb/constants.h +15 -0
- data/ext/chdb/exception.c +16 -0
- data/ext/chdb/exception.h +10 -0
- data/ext/chdb/extconf.rb +196 -0
- data/ext/chdb/local_result.c +81 -0
- data/ext/chdb/local_result.h +26 -0
- data/lib/chdb/constants.rb +6 -0
- data/lib/chdb/data_path.rb +121 -0
- data/lib/chdb/database.rb +153 -0
- data/lib/chdb/errors.rb +22 -0
- data/lib/chdb/local_result.rb +12 -0
- data/lib/chdb/parameter_binding.rb +25 -0
- data/lib/chdb/result_handler.rb +28 -0
- data/lib/chdb/result_set.rb +57 -0
- data/lib/chdb/sql_processor.rb +23 -0
- data/lib/chdb/statement.rb +137 -0
- data/lib/chdb/version.rb +6 -0
- data/lib/chdb/version_info.rb +11 -0
- data/lib/chdb.rb +18 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 77408619eae7f84800fa14da51844d0180ea5d4cb7513d2f2c7f2c273f287787
|
4
|
+
data.tar.gz: 5cff1f1590fa5a70241d1a2e1ee1068e965487c6741ad7523921b5be0a35a8e8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1a501dd7c418c3af1edd49e77e69a50b6229cd8610121d7200d5e04633c61eeafe4fc87615f24c4e6d68a99bda3daa912575a6ecf919960e1398bef253d02855
|
7
|
+
data.tar.gz: 07752df52ce67356c9f32b37d6444bdcdb5d49c274d61d67e26c7752694aedcd53184a682bea3408057d217152a5584e4c71c6230559b70bf289162bcaa7744e
|
data/INSTALLATION.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
# Installation and Using chdb-ruby extensions
|
3
|
+
|
4
|
+
This document will help you install the `chdb-ruby` ruby gem.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
### Compilation During Installation
|
9
|
+
|
10
|
+
The gem will automatically compile native extensions during installation (`gem install`). Supported platforms are:
|
11
|
+
|
12
|
+
- `aarch64-linux`
|
13
|
+
- `arm64-darwin`
|
14
|
+
- `x86_64-linux`
|
15
|
+
- `x86_64-darwin`
|
16
|
+
|
17
|
+
The `chdb-ruby` gem does not provide a pure Ruby implementation. Installation will fail if your platform is unsupported.
|
18
|
+
|
19
|
+
## Runtime Dependencies
|
20
|
+
|
21
|
+
The `chdb-ruby` gem requires the `libchdb` C++ library as its core engine. The library will be automatically downloaded during installation. The extension uses `dlopen` to dynamically load the library. No manual configuration is required.
|
22
|
+
|
23
|
+
## Verify Installation
|
24
|
+
- Ruby:
|
25
|
+
```bash
|
26
|
+
require 'chdb'
|
27
|
+
```
|
data/LICENSE
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
Copyright 2025 chDB, Inc.
|
2
|
+
|
3
|
+
Apache License
|
4
|
+
Version 2.0, January 2004
|
5
|
+
http://www.apache.org/licenses/
|
6
|
+
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
8
|
+
|
9
|
+
1. Definitions.
|
10
|
+
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
12
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
13
|
+
|
14
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
15
|
+
the copyright owner that is granting the License.
|
16
|
+
|
17
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
18
|
+
other entities that control, are controlled by, or are under common
|
19
|
+
control with that entity. For the purposes of this definition,
|
20
|
+
"control" means (i) the power, direct or indirect, to cause the
|
21
|
+
direction or management of such entity, whether by contract or
|
22
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
23
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
24
|
+
|
25
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
26
|
+
exercising permissions granted by this License.
|
27
|
+
|
28
|
+
"Source" form shall mean the preferred form for making modifications,
|
29
|
+
including but not limited to software source code, documentation
|
30
|
+
source, and configuration files.
|
31
|
+
|
32
|
+
"Object" form shall mean any form resulting from mechanical
|
33
|
+
transformation or translation of a Source form, including but
|
34
|
+
not limited to compiled object code, generated documentation,
|
35
|
+
and conversions to other media types.
|
36
|
+
|
37
|
+
"Work" shall mean the work of authorship, whether in Source or
|
38
|
+
Object form, made available under the License, as indicated by a
|
39
|
+
copyright notice that is included in or attached to the work
|
40
|
+
(an example is provided in the Appendix below).
|
41
|
+
|
42
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
43
|
+
form, that is based on (or derived from) the Work and for which the
|
44
|
+
editorial revisions, annotations, elaborations, or other modifications
|
45
|
+
represent, as a whole, an original work of authorship. For the purposes
|
46
|
+
of this License, Derivative Works shall not include works that remain
|
47
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
48
|
+
the Work and Derivative Works thereof.
|
49
|
+
|
50
|
+
"Contribution" shall mean any work of authorship, including
|
51
|
+
the original version of the Work and any modifications or additions
|
52
|
+
to that Work or Derivative Works thereof, that is intentionally
|
53
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
54
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
55
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
56
|
+
means any form of electronic, verbal, or written communication sent
|
57
|
+
to the Licensor or its representatives, including but not limited to
|
58
|
+
communication on electronic mailing lists, source code control systems,
|
59
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
60
|
+
Licensor for the purpose of discussing and improving the Work, but
|
61
|
+
excluding communication that is conspicuously marked or otherwise
|
62
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
63
|
+
|
64
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
65
|
+
on behalf of whom a Contribution has been received by Licensor and
|
66
|
+
subsequently incorporated within the Work.
|
67
|
+
|
68
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
69
|
+
this License, each Contributor hereby grants to You a perpetual,
|
70
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
71
|
+
copyright license to reproduce, prepare Derivative Works of,
|
72
|
+
publicly display, publicly perform, sublicense, and distribute the
|
73
|
+
Work and such Derivative Works in Source or Object form.
|
74
|
+
|
75
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
76
|
+
this License, each Contributor hereby grants to You a perpetual,
|
77
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
78
|
+
(except as stated in this section) patent license to make, have made,
|
79
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
80
|
+
where such license applies only to those patent claims licensable
|
81
|
+
by such Contributor that are necessarily infringed by their
|
82
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
83
|
+
with the Work to which such Contribution(s) was submitted. If You
|
84
|
+
institute patent litigation against any entity (including a
|
85
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
86
|
+
or a Contribution incorporated within the Work constitutes direct
|
87
|
+
or contributory patent infringement, then any patent licenses
|
88
|
+
granted to You under this License for that Work shall terminate
|
89
|
+
as of the date such litigation is filed.
|
90
|
+
|
91
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
92
|
+
Work or Derivative Works thereof in any medium, with or without
|
93
|
+
modifications, and in Source or Object form, provided that You
|
94
|
+
meet the following conditions:
|
95
|
+
|
96
|
+
(a) You must give any other recipients of the Work or
|
97
|
+
Derivative Works a copy of this License; and
|
98
|
+
|
99
|
+
(b) You must cause any modified files to carry prominent notices
|
100
|
+
stating that You changed the files; and
|
101
|
+
|
102
|
+
(c) You must retain, in the Source form of any Derivative Works
|
103
|
+
that You distribute, all copyright, patent, trademark, and
|
104
|
+
attribution notices from the Source form of the Work,
|
105
|
+
excluding those notices that do not pertain to any part of
|
106
|
+
the Derivative Works; and
|
107
|
+
|
108
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
109
|
+
distribution, then any Derivative Works that You distribute must
|
110
|
+
include a readable copy of the attribution notices contained
|
111
|
+
within such NOTICE file, excluding those notices that do not
|
112
|
+
pertain to any part of the Derivative Works, in at least one
|
113
|
+
of the following places: within a NOTICE text file distributed
|
114
|
+
as part of the Derivative Works; within the Source form or
|
115
|
+
documentation, if provided along with the Derivative Works; or,
|
116
|
+
within a display generated by the Derivative Works, if and
|
117
|
+
wherever such third-party notices normally appear. The contents
|
118
|
+
of the NOTICE file are for informational purposes only and
|
119
|
+
do not modify the License. You may add Your own attribution
|
120
|
+
notices within Derivative Works that You distribute, alongside
|
121
|
+
or as an addendum to the NOTICE text from the Work, provided
|
122
|
+
that such additional attribution notices cannot be construed
|
123
|
+
as modifying the License.
|
124
|
+
|
125
|
+
You may add Your own copyright statement to Your modifications and
|
126
|
+
may provide additional or different license terms and conditions
|
127
|
+
for use, reproduction, or distribution of Your modifications, or
|
128
|
+
for any such Derivative Works as a whole, provided Your use,
|
129
|
+
reproduction, and distribution of the Work otherwise complies with
|
130
|
+
the conditions stated in this License.
|
131
|
+
|
132
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
133
|
+
any Contribution intentionally submitted for inclusion in the Work
|
134
|
+
by You to the Licensor shall be under the terms and conditions of
|
135
|
+
this License, without any additional terms or conditions.
|
136
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
137
|
+
the terms of any separate license agreement you may have executed
|
138
|
+
with Licensor regarding such Contributions.
|
139
|
+
|
140
|
+
6. Trademarks. This License does not grant permission to use the trade
|
141
|
+
names, trademarks, service marks, or product names of the Licensor,
|
142
|
+
except as required for reasonable and customary use in describing the
|
143
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
144
|
+
|
145
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
146
|
+
agreed to in writing, Licensor provides the Work (and each
|
147
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
148
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
149
|
+
implied, including, without limitation, any warranties or conditions
|
150
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
151
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
152
|
+
appropriateness of using or redistributing the Work and assume any
|
153
|
+
risks associated with Your exercise of permissions under this License.
|
154
|
+
|
155
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
156
|
+
whether in tort (including negligence), contract, or otherwise,
|
157
|
+
unless required by applicable law (such as deliberate and grossly
|
158
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
159
|
+
liable to You for damages, including any direct, indirect, special,
|
160
|
+
incidental, or consequential damages of any character arising as a
|
161
|
+
result of this License or out of the use or inability to use the
|
162
|
+
Work (including but not limited to damages for loss of goodwill,
|
163
|
+
work stoppage, computer failure or malfunction, or any and all
|
164
|
+
other commercial damages or losses), even if such Contributor
|
165
|
+
has been advised of the possibility of such damages.
|
166
|
+
|
167
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
168
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
169
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
170
|
+
or other liability obligations and/or rights consistent with this
|
171
|
+
License. However, in accepting such obligations, You may act only
|
172
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
173
|
+
of any other Contributor, and only if You agree to indemnify,
|
174
|
+
defend, and hold each Contributor harmless for any liability
|
175
|
+
incurred by, or claims asserted against, such Contributor by reason
|
176
|
+
of your accepting any such warranty or additional liability.
|
177
|
+
|
178
|
+
END OF TERMS AND CONDITIONS
|
179
|
+
|
180
|
+
APPENDIX: How to apply the Apache License to your work.
|
181
|
+
|
182
|
+
To apply the Apache License to your work, attach the following
|
183
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
184
|
+
replaced with your own identifying information. (Don't include
|
185
|
+
the brackets!) The text should be enclosed in the appropriate
|
186
|
+
comment syntax for the file format. We also recommend that a
|
187
|
+
file or class name and description of purpose be included on the
|
188
|
+
same "printed page" as the copyright notice for easier
|
189
|
+
identification within third-party archives.
|
190
|
+
|
191
|
+
Copyright 2025 chDB, Inc.
|
192
|
+
|
193
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
194
|
+
you may not use this file except in compliance with the License.
|
195
|
+
You may obtain a copy of the License at
|
196
|
+
|
197
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
198
|
+
|
199
|
+
Unless required by applicable law or agreed to in writing, software
|
200
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
201
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
202
|
+
See the License for the specific language governing permissions and
|
203
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
<a href="https://chdb.io" target="_blank">
|
2
|
+
<img src="https://avatars.githubusercontent.com/u/132536224" width=130 />
|
3
|
+
</a>
|
4
|
+
|
5
|
+
[](https://github.com/chdb-io/chdb-ruby/actions/workflows/chdb.yml)
|
6
|
+
|
7
|
+
# Ruby Interface for chdb
|
8
|
+
|
9
|
+
## Overview
|
10
|
+
|
11
|
+
This library allows Ruby programs to use the chDB embedded analytical database (https://github.com/chdb-io/chdb).
|
12
|
+
|
13
|
+
**Designed with SQLite3-compatible API style** - If you're familiar with Ruby's sqlite3 gem, you'll feel right at home with chdb-ruby's similar interface design.
|
14
|
+
|
15
|
+
Note that this module is only compatible with ChDB 3.0.0 or newer.
|
16
|
+
|
17
|
+
* Source code: https://github.com/chdb-io/chdb-ruby
|
18
|
+
* Download: http://rubygems.org/gems/chdb-ruby
|
19
|
+
* Documentation: https://clickhouse.com/docs/chdb
|
20
|
+
|
21
|
+
## Quick start
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
require 'chdb'
|
25
|
+
|
26
|
+
# Open a database
|
27
|
+
db = ChDB::Database.new('test_db', results_as_hash: true)
|
28
|
+
|
29
|
+
# Create a table
|
30
|
+
rows = db.execute <<-SQL
|
31
|
+
CREATE TABLE test_table(
|
32
|
+
id Int32,
|
33
|
+
name String)
|
34
|
+
ENGINE = MergeTree()
|
35
|
+
ORDER BY id);
|
36
|
+
SQL
|
37
|
+
|
38
|
+
# Execute a few inserts
|
39
|
+
{
|
40
|
+
1 => 'Alice',
|
41
|
+
2 => 'Bob'
|
42
|
+
}.each do |pair|
|
43
|
+
db.execute 'INSERT INTO test_table VALUES ( ?, ? )', pair
|
44
|
+
end
|
45
|
+
|
46
|
+
# Find a few rows
|
47
|
+
db.execute('SELECT * FROM test_table ORDER BY id') do |row|
|
48
|
+
p row
|
49
|
+
end
|
50
|
+
# [{ 'id' => '1', 'name' => 'Alice' },
|
51
|
+
# { 'id' => '2', 'name' => 'Bob' }]
|
52
|
+
|
53
|
+
# Open another database
|
54
|
+
db = ChDB::Database.new 'test2.db'
|
55
|
+
|
56
|
+
# Create another table
|
57
|
+
rows = db.execute <<-SQL
|
58
|
+
CREATE TABLE test2_table(
|
59
|
+
id Int32,
|
60
|
+
name String)
|
61
|
+
ENGINE = MergeTree()
|
62
|
+
ORDER BY id");
|
63
|
+
SQL
|
64
|
+
|
65
|
+
# Execute inserts with parameter markers
|
66
|
+
db.execute('INSERT INTO test2_table (id, name)
|
67
|
+
VALUES (?, ?)', [3, 'Charlie'])
|
68
|
+
|
69
|
+
db.execute2('SELECT * FROM test2_table') do |row|
|
70
|
+
p row
|
71
|
+
end
|
72
|
+
# [['id', 'name'], [3, 'Charlie']],
|
73
|
+
```
|
74
|
+
|
75
|
+
## Thread Safety
|
76
|
+
|
77
|
+
When using `ChDB::Database.new` to open a session, all read/write operations within that session are thread-safe. However, currently only one active session is allowed per process. Therefore, when you need to open another session, you must first close the previous session.
|
78
|
+
|
79
|
+
For example, the following code is fine because only the database
|
80
|
+
instance is shared among threads:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
require 'chdb'
|
84
|
+
|
85
|
+
db = ChDB::Database.new ":memory:'
|
86
|
+
|
87
|
+
latch = Queue.new
|
88
|
+
|
89
|
+
ts = 10.times.map {
|
90
|
+
Thread.new {
|
91
|
+
latch.pop
|
92
|
+
db.execute 'SELECT 1'
|
93
|
+
}
|
94
|
+
}
|
95
|
+
10.times { latch << nil }
|
96
|
+
|
97
|
+
p ts.map(&:value)
|
98
|
+
```
|
99
|
+
|
100
|
+
Other instances can be shared among threads, but they require that you provide
|
101
|
+
your own locking for thread safety. For example, `ChDB::Statement` objects
|
102
|
+
(prepared statements) are mutable, so applications must take care to add
|
103
|
+
appropriate locks to avoid data race conditions when sharing these objects
|
104
|
+
among threads.
|
105
|
+
|
106
|
+
It is generally recommended that if applications want to share a database among
|
107
|
+
threads, they _only_ share the database instance object. Other objects are
|
108
|
+
fine to share, but may require manual locking for thread safety.
|
109
|
+
|
110
|
+
## Support
|
111
|
+
|
112
|
+
### Installation
|
113
|
+
|
114
|
+
If you're having trouble with installation, please first read [`INSTALLATION.md`](./INSTALLATION.md).
|
115
|
+
|
116
|
+
### Bug reports
|
117
|
+
|
118
|
+
You can file the bug at the [github issues page](https://github.com/chdb-io/chdb-ruby/issues).
|
119
|
+
|
120
|
+
## License
|
121
|
+
|
122
|
+
This library is licensed under `Apache License 2.0`, see [`LICENSE`](./LICENSE).
|
123
|
+
|
124
|
+
## Acknowledgments
|
125
|
+
Special thanks to the following projects:
|
126
|
+
|
127
|
+
* [chDB 2.0 Ruby client](https://github.com/g3ortega/chdb) – As the foundational work for chDB 2.0, its design and architecture inspired this chDB 3.0 Ruby client.
|
128
|
+
* [SQLite3](https://github.com/sparklemotion/sqlite3-ruby) – We adopted patterns from its elegant Ruby API design.
|
data/dependencies.yml
ADDED
data/ext/chdb/chdb.c
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
|
3
|
+
#include "chdb.h"
|
4
|
+
#include "chdb_handle.h"
|
5
|
+
#include "constants.h"
|
6
|
+
#include "connection.h"
|
7
|
+
#include "exception.h"
|
8
|
+
#include "local_result.h"
|
9
|
+
|
10
|
+
void init_chdb_constants()
|
11
|
+
{
|
12
|
+
VALUE mChDB = rb_define_module("ChDB");
|
13
|
+
VALUE mChDBConstants = rb_define_module_under(mChDB, "Constants");
|
14
|
+
VALUE mmChDBOpen = rb_define_module_under(mChDBConstants, "Open");
|
15
|
+
|
16
|
+
rb_define_const(mmChDBOpen, "READONLY", INT2FIX(CHDB_OPEN_READONLY));
|
17
|
+
rb_define_const(mmChDBOpen, "READWRITE", INT2FIX(CHDB_OPEN_READWRITE));
|
18
|
+
rb_define_const(mmChDBOpen, "CREATE", INT2FIX(CHDB_OPEN_CREATE));
|
19
|
+
}
|
20
|
+
|
21
|
+
void Init_chdb_native()
|
22
|
+
{
|
23
|
+
DEBUG_PRINT("Initializing chdb extension");
|
24
|
+
|
25
|
+
init_exception();
|
26
|
+
init_chdb_handle();
|
27
|
+
init_chdb_constants();
|
28
|
+
init_local_result();
|
29
|
+
init_connection();
|
30
|
+
|
31
|
+
DEBUG_PRINT("chdb extension initialized successfully");
|
32
|
+
}
|
@@ -0,0 +1,49 @@
|
|
1
|
+
#include "chdb_handle.h"
|
2
|
+
|
3
|
+
#include <dlfcn.h>
|
4
|
+
#include <ruby.h>
|
5
|
+
|
6
|
+
#include "exception.h"
|
7
|
+
|
8
|
+
void *chdb_handle = NULL;
|
9
|
+
connect_chdb_func connect_chdb_ptr = NULL;
|
10
|
+
close_conn_func close_conn_ptr = NULL;
|
11
|
+
query_conn_func query_conn_ptr = NULL;
|
12
|
+
|
13
|
+
VALUE get_chdb_rb_path(void)
|
14
|
+
{
|
15
|
+
VALUE chdb_module = rb_const_get(rb_cObject, rb_intern("ChDB"));
|
16
|
+
return rb_funcall(chdb_module, rb_intern("lib_file_path"), 0);
|
17
|
+
}
|
18
|
+
|
19
|
+
void init_chdb_handle()
|
20
|
+
{
|
21
|
+
VALUE rb_path = get_chdb_rb_path();
|
22
|
+
VALUE lib_dir = rb_file_dirname(rb_file_dirname(rb_path));
|
23
|
+
VALUE lib_path = rb_str_cat2(lib_dir, "/lib/chdb/lib/libchdb.so");
|
24
|
+
// printf("chdb.rb path from Ruby: %s\n", StringValueCStr(lib_path));
|
25
|
+
|
26
|
+
connect_chdb_ptr = NULL;
|
27
|
+
close_conn_ptr = NULL;
|
28
|
+
query_conn_ptr = NULL;
|
29
|
+
|
30
|
+
chdb_handle = dlopen(RSTRING_PTR(lib_path), RTLD_LAZY | RTLD_GLOBAL);
|
31
|
+
if (!chdb_handle)
|
32
|
+
{
|
33
|
+
rb_raise(cChDBError, "Failed to load chdb library: %s\nCheck if libchdb.so exists at: %s",
|
34
|
+
dlerror(), RSTRING_PTR(lib_path));
|
35
|
+
}
|
36
|
+
|
37
|
+
connect_chdb_ptr = (connect_chdb_func)dlsym(chdb_handle, "connect_chdb");
|
38
|
+
close_conn_ptr = (close_conn_func)dlsym(chdb_handle, "close_conn");
|
39
|
+
query_conn_ptr = (query_conn_func)dlsym(chdb_handle, "query_conn");
|
40
|
+
|
41
|
+
if (!connect_chdb_ptr || !close_conn_ptr || !query_conn_ptr)
|
42
|
+
{
|
43
|
+
rb_raise(cChDBError, "Symbol loading failed: %s\nMissing functions: connect_chdb(%p) close_conn(%p) query_conn(%p)",
|
44
|
+
dlerror(),
|
45
|
+
(void*)connect_chdb_ptr,
|
46
|
+
(void*)close_conn_ptr,
|
47
|
+
(void*)query_conn_ptr);
|
48
|
+
}
|
49
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#ifndef CHDB_HANDLE_H
|
2
|
+
#define CHDB_HANDLE_H
|
3
|
+
|
4
|
+
typedef struct chdb_conn **(*connect_chdb_func)(int, char**);
|
5
|
+
typedef void (*close_conn_func)(struct chdb_conn**);
|
6
|
+
typedef struct local_result_v2 *(*query_conn_func)(struct chdb_conn*, const char*, const char*);
|
7
|
+
|
8
|
+
extern connect_chdb_func connect_chdb_ptr;
|
9
|
+
extern close_conn_func close_conn_ptr;
|
10
|
+
extern query_conn_func query_conn_ptr;
|
11
|
+
|
12
|
+
extern void *chdb_handle;
|
13
|
+
|
14
|
+
void init_chdb_handle();
|
15
|
+
|
16
|
+
#endif
|
@@ -0,0 +1,118 @@
|
|
1
|
+
#include "connection.h"
|
2
|
+
|
3
|
+
#include "chdb_handle.h"
|
4
|
+
#include "constants.h"
|
5
|
+
#include "exception.h"
|
6
|
+
#include "include/chdb.h"
|
7
|
+
#include "local_result.h"
|
8
|
+
|
9
|
+
static void connection_free(void *ptr)
|
10
|
+
{
|
11
|
+
Connection *conn = (Connection *)ptr;
|
12
|
+
DEBUG_PRINT("Closing connection: %p", (void*)conn->c_conn);
|
13
|
+
if (conn->c_conn)
|
14
|
+
{
|
15
|
+
close_conn_ptr(conn->c_conn);
|
16
|
+
conn->c_conn = NULL;
|
17
|
+
}
|
18
|
+
free(conn);
|
19
|
+
}
|
20
|
+
|
21
|
+
const rb_data_type_t ConnectionType =
|
22
|
+
{
|
23
|
+
"Connection",
|
24
|
+
{NULL, connection_free, NULL},
|
25
|
+
};
|
26
|
+
|
27
|
+
void init_connection()
|
28
|
+
{
|
29
|
+
VALUE mChDB = rb_define_module("ChDB");
|
30
|
+
VALUE cConnection = rb_define_class_under(mChDB, "Connection", rb_cObject);
|
31
|
+
rb_define_alloc_func(cConnection, connection_alloc);
|
32
|
+
rb_define_method(cConnection, "initialize", connection_initialize, 2);
|
33
|
+
rb_define_method(cConnection, "query", connection_query, 2);
|
34
|
+
rb_define_method(cConnection, "close", connection_close, 0);
|
35
|
+
}
|
36
|
+
|
37
|
+
VALUE connection_alloc(VALUE klass)
|
38
|
+
{
|
39
|
+
Connection *conn = ALLOC(Connection);
|
40
|
+
DEBUG_PRINT("Allocating Connection: %p", (void*)conn);
|
41
|
+
conn->c_conn = NULL;
|
42
|
+
return rb_data_typed_object_wrap(klass, conn, &ConnectionType);
|
43
|
+
}
|
44
|
+
|
45
|
+
VALUE connection_initialize(VALUE self, VALUE argc, VALUE argv)
|
46
|
+
{
|
47
|
+
Check_Type(argc, T_FIXNUM);
|
48
|
+
Check_Type(argv, T_ARRAY);
|
49
|
+
|
50
|
+
int c_argc = NUM2INT(argc);
|
51
|
+
char **c_argv = ALLOC_N(char *, c_argc);
|
52
|
+
|
53
|
+
for (int i = 0; i < c_argc; i++)
|
54
|
+
{
|
55
|
+
VALUE arg = rb_ary_entry(argv, i);
|
56
|
+
c_argv[i] = StringValueCStr(arg);
|
57
|
+
}
|
58
|
+
|
59
|
+
Connection *conn;
|
60
|
+
TypedData_Get_Struct(self, Connection, &ConnectionType, conn);
|
61
|
+
conn->c_conn = connect_chdb_ptr(c_argc, c_argv);
|
62
|
+
|
63
|
+
if (!conn->c_conn)
|
64
|
+
{
|
65
|
+
xfree(c_argv);
|
66
|
+
rb_raise(cChDBError, "Failed to connect to chDB");
|
67
|
+
}
|
68
|
+
|
69
|
+
xfree(c_argv);
|
70
|
+
rb_gc_unregister_address(&argv);
|
71
|
+
return self;
|
72
|
+
}
|
73
|
+
|
74
|
+
VALUE connection_query(VALUE self, VALUE query, VALUE format)
|
75
|
+
{
|
76
|
+
Connection *conn;
|
77
|
+
TypedData_Get_Struct(self, Connection, &ConnectionType, conn);
|
78
|
+
|
79
|
+
Check_Type(query, T_STRING);
|
80
|
+
Check_Type(format, T_STRING);
|
81
|
+
|
82
|
+
struct local_result_v2 *c_result = query_conn_ptr(
|
83
|
+
*conn->c_conn,
|
84
|
+
StringValueCStr(query),
|
85
|
+
StringValueCStr(format)
|
86
|
+
);
|
87
|
+
|
88
|
+
if (!c_result)
|
89
|
+
{
|
90
|
+
rb_raise(cChDBError, "Query failed with nil result");
|
91
|
+
}
|
92
|
+
|
93
|
+
if (c_result->error_message)
|
94
|
+
{
|
95
|
+
VALUE error_message = rb_str_new_cstr(c_result->error_message);
|
96
|
+
rb_raise(cChDBError, "CHDB error: %s", StringValueCStr(error_message));
|
97
|
+
}
|
98
|
+
|
99
|
+
VALUE result_obj = rb_class_new_instance(0, NULL, cLocalResult);
|
100
|
+
LocalResult *result;
|
101
|
+
TypedData_Get_Struct(result_obj, LocalResult, &LocalResultType, result);
|
102
|
+
result->c_result = c_result;
|
103
|
+
|
104
|
+
return result_obj;
|
105
|
+
}
|
106
|
+
|
107
|
+
VALUE connection_close(VALUE self)
|
108
|
+
{
|
109
|
+
Connection *conn;
|
110
|
+
TypedData_Get_Struct(self, Connection, &ConnectionType, conn);
|
111
|
+
|
112
|
+
if (conn->c_conn)
|
113
|
+
{
|
114
|
+
close_conn_ptr(conn->c_conn);
|
115
|
+
conn->c_conn = NULL;
|
116
|
+
}
|
117
|
+
return Qnil;
|
118
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#ifndef CHDB_CONNECTION_H
|
2
|
+
#define CHDB_CONNECTION_H
|
3
|
+
|
4
|
+
#include <ruby.h>
|
5
|
+
|
6
|
+
typedef struct
|
7
|
+
{
|
8
|
+
struct chdb_conn **c_conn;
|
9
|
+
} Connection;
|
10
|
+
|
11
|
+
extern const rb_data_type_t ConnectionType;
|
12
|
+
|
13
|
+
void init_connection();
|
14
|
+
|
15
|
+
VALUE connection_alloc(VALUE klass);
|
16
|
+
|
17
|
+
VALUE connection_initialize(VALUE self, VALUE argc, VALUE argv);
|
18
|
+
|
19
|
+
VALUE connection_query(VALUE self, VALUE query, VALUE format);
|
20
|
+
|
21
|
+
VALUE connection_close(VALUE self);
|
22
|
+
|
23
|
+
#endif
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef CHDB_CONSTANTS_H
|
2
|
+
#define CHDB_CONSTANTS_H
|
3
|
+
|
4
|
+
#define CHDB_OPEN_READONLY 0x00000001
|
5
|
+
#define CHDB_OPEN_READWRITE 0x00000002
|
6
|
+
#define CHDB_OPEN_CREATE 0x00000004
|
7
|
+
|
8
|
+
#define CHDB_DEBUG 0
|
9
|
+
#if CHDB_DEBUG
|
10
|
+
#define DEBUG_PRINT(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__)
|
11
|
+
#else
|
12
|
+
#define DEBUG_PRINT(fmt, ...) ((void)0)
|
13
|
+
#endif
|
14
|
+
|
15
|
+
#endif
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#include "exception.h"
|
2
|
+
|
3
|
+
VALUE cChDBError;
|
4
|
+
|
5
|
+
void init_exception()
|
6
|
+
{
|
7
|
+
VALUE mChDB = rb_define_module("ChDB");
|
8
|
+
if (rb_const_defined(mChDB, rb_intern("Exception")))
|
9
|
+
{
|
10
|
+
cChDBError = rb_const_get(mChDB, rb_intern("Exception"));
|
11
|
+
}
|
12
|
+
else
|
13
|
+
{
|
14
|
+
cChDBError = rb_define_class_under(mChDB, "Exception", rb_eStandardError);
|
15
|
+
}
|
16
|
+
}
|