ch_connect 0.2.2 → 0.3.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 +4 -4
- data/.standard.yml +5 -0
- data/CHANGELOG.md +10 -1
- data/README.md +156 -87
- data/ext/ch_connect_native/ch_connect_native.c +1001 -0
- data/ext/ch_connect_native/extconf.rb +32 -0
- data/lib/ch_connect/config.rb +60 -19
- data/lib/ch_connect/connection.rb +382 -13
- data/lib/ch_connect/response.rb +0 -8
- data/lib/ch_connect/version.rb +1 -1
- data/lib/ch_connect.rb +14 -11
- data/vendor/clickhouse-c/LICENSE +203 -0
- data/vendor/clickhouse-c/VENDOR.md +24 -0
- data/vendor/clickhouse-c/clickhouse-async.h +301 -0
- data/vendor/clickhouse-c/clickhouse-client.h +995 -0
- data/vendor/clickhouse-c/clickhouse-compression.h +634 -0
- data/vendor/clickhouse-c/clickhouse.h +3394 -0
- metadata +19 -14
- data/lib/ch_connect/body_reader.rb +0 -79
- data/lib/ch_connect/http_transport.rb +0 -59
- data/lib/ch_connect/native_format_parser.rb +0 -405
- data/lib/ch_connect/transport_result.rb +0 -12
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
Copyright 2026 ClickHouse, 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 2026 ClickHouse, 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.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Vendored: clickhouse-c
|
|
2
|
+
|
|
3
|
+
- Source: https://github.com/ClickHouse/clickhouse-c
|
|
4
|
+
- Commit: dd47d2882646aeba5a1946c62a229d57abea6c28 (cloned 2026-07-14)
|
|
5
|
+
- License: Apache-2.0 (see LICENSE)
|
|
6
|
+
|
|
7
|
+
Only the headers the extension includes are vendored: `clickhouse.h`,
|
|
8
|
+
`clickhouse-compression.h`, `clickhouse-client.h`, `clickhouse-async.h`
|
|
9
|
+
(plus LICENSE). Upstream's tests, docs, tools, and the `clickhouse-posix-io.h`
|
|
10
|
+
/ `clickhouse-openssl.h` I/O backends are not shipped — the extension is
|
|
11
|
+
ioless (Ruby owns the socket and TLS).
|
|
12
|
+
|
|
13
|
+
## Local patches
|
|
14
|
+
|
|
15
|
+
- `clickhouse.h` — `chc__col_read`, `case CHC_TUPLE`: ClickHouse serializes the
|
|
16
|
+
empty `Tuple()` as one UInt8 (zero) per row (ClickHouse/ClickHouse#55061);
|
|
17
|
+
upstream reads nothing and desyncs the stream. Marked with
|
|
18
|
+
`LOCAL PATCH (ch_connect)`. Should be upstreamed.
|
|
19
|
+
|
|
20
|
+
## Updating
|
|
21
|
+
|
|
22
|
+
Clone the pinned or newer commit, copy the four headers + LICENSE over this
|
|
23
|
+
directory, re-apply the patches above (or drop them once fixed upstream), and
|
|
24
|
+
run the full spec suite with `CH_TRANSPORT=native`.
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* clickhouse-async.h
|
|
3
|
+
*
|
|
4
|
+
* Never touches a socket; caller manages bytes:
|
|
5
|
+
* - inbound socket bytes -> chc_async_submit
|
|
6
|
+
* - outbound bytes -> chc_async_pending_out / chc_async_consume_out
|
|
7
|
+
*
|
|
8
|
+
* handshake / recv_packet return CHC_WOULD_BLOCK when the input buffer
|
|
9
|
+
* drains mid-parse; the caller submit more and retries. Sends only ever
|
|
10
|
+
* append to *out buffer.
|
|
11
|
+
*
|
|
12
|
+
* No write backpressure: sends never block, never return CHC_WOULD_BLOCK, and
|
|
13
|
+
* grow *out unbounded (only OOM fails). Outbound flow control is the caller's.
|
|
14
|
+
* Use chc_async_pending_out: stop sending when enough pending, resume when
|
|
15
|
+
* chc_async_consume_out has drained it enough.
|
|
16
|
+
*
|
|
17
|
+
* recv keeps parser state alive across CHC_WOULD_BLOCK (in cli.recv_*): a Data
|
|
18
|
+
* block streamed over many reads resumes at the in-progress column rather than
|
|
19
|
+
* re-parsing from scratch (uncompressed path, via chc__block_resume_in).
|
|
20
|
+
* Compressed Data resumes at frame granularity: at most one frame is
|
|
21
|
+
* re-decompressed and one column re-parsed on retry.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
#ifndef CLICKHOUSE_ASYNC_H
|
|
25
|
+
#define CLICKHOUSE_ASYNC_H
|
|
26
|
+
|
|
27
|
+
#ifdef CHC_NO_ASYNC
|
|
28
|
+
# error "clickhouse-async.h needs the ioless reader; remove CHC_NO_ASYNC"
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
#include "clickhouse-client.h" /* pulls clickhouse.h + clickhouse-compression.h */
|
|
32
|
+
|
|
33
|
+
#ifdef __cplusplus
|
|
34
|
+
extern "C" {
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
typedef struct chc_async_client chc_async_client;
|
|
38
|
+
|
|
39
|
+
/* No I/O, cannot block; only OOM fails. opts->codec carries compression. */
|
|
40
|
+
int chc_async_client_init(chc_async_client **out, const chc_client_opts *opts,
|
|
41
|
+
const chc_alloc *al, chc_err *err);
|
|
42
|
+
void chc_async_client_free(chc_async_client *c);
|
|
43
|
+
|
|
44
|
+
/* byte shuttle */
|
|
45
|
+
int chc_async_submit(chc_async_client *c, const void *buf, size_t len,
|
|
46
|
+
chc_err *err);
|
|
47
|
+
void chc_async_pending_out(chc_async_client *c, const uint8_t **buf, size_t *len);
|
|
48
|
+
void chc_async_consume_out(chc_async_client *c, size_t n);
|
|
49
|
+
|
|
50
|
+
/* drive -- never block; CHC_WOULD_BLOCK = submit more / drain out */
|
|
51
|
+
int chc_async_handshake(chc_async_client *c, chc_err *err);
|
|
52
|
+
int chc_async_send_query(chc_async_client *c, const char *sql, size_t sql_len,
|
|
53
|
+
const char *query_id, size_t query_id_len, chc_err *err);
|
|
54
|
+
int chc_async_send_data(chc_async_client *c, const chc_block_builder *bb,
|
|
55
|
+
chc_err *err);
|
|
56
|
+
int chc_async_send_data_end(chc_async_client *c, chc_err *err);
|
|
57
|
+
int chc_async_recv_packet(chc_async_client *c, chc_packet *out, chc_err *err);
|
|
58
|
+
const chc_server_info *chc_async_server_info(const chc_async_client *c);
|
|
59
|
+
|
|
60
|
+
/* Mirror chc_packet_clear: free p->block / p->exception via the client's
|
|
61
|
+
* allocator. */
|
|
62
|
+
void chc_async_packet_clear(chc_async_client *c, chc_packet *p);
|
|
63
|
+
|
|
64
|
+
#ifdef __cplusplus
|
|
65
|
+
}
|
|
66
|
+
#endif
|
|
67
|
+
|
|
68
|
+
#ifdef CHC_IMPLEMENTATION
|
|
69
|
+
|
|
70
|
+
/* This block compiles in the same TU as clickhouse-client.h's implementation,
|
|
71
|
+
* so struct chc_client and the static chc__client_* / chc__recv_packet_resumable /
|
|
72
|
+
* chc__recv_pong / chc__client_recv_state_free / chc__read_* / chc__mem_sink_*
|
|
73
|
+
* internals are visible. */
|
|
74
|
+
|
|
75
|
+
enum {
|
|
76
|
+
CHC__HS_SEND_HELLO = 0, /* buffer Hello, advance */
|
|
77
|
+
CHC__HS_RECV_HELLO, /* parse server Hello (resumable) */
|
|
78
|
+
CHC__HS_POST_HELLO, /* buffer addendum quota_key + Ping */
|
|
79
|
+
CHC__HS_RECV_PONG, /* parse Pong (resumable) */
|
|
80
|
+
CHC__HS_DONE
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
struct chc_async_client {
|
|
84
|
+
chc_client cli; /* driven directly; not via chc_client_init */
|
|
85
|
+
|
|
86
|
+
chc__mem_sink out; /* outbound write sink; cli.io points at out_io */
|
|
87
|
+
chc_io out_io;
|
|
88
|
+
size_t out_consumed; /* bytes of out.buf the caller has drained */
|
|
89
|
+
|
|
90
|
+
int hs_phase; /* CHC__HS_* */
|
|
91
|
+
|
|
92
|
+
/* recv resumption state lives on cli (chc_client.recv_*), shared with the
|
|
93
|
+
* blocking decoder; persisted across CHC_WOULD_BLOCK. */
|
|
94
|
+
|
|
95
|
+
/* Owned copies of the Hello string fields (opts may not outlive init). */
|
|
96
|
+
char *client_name;
|
|
97
|
+
char *database;
|
|
98
|
+
char *user;
|
|
99
|
+
char *password;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
static char *
|
|
103
|
+
chc__async_dup(const chc_alloc *al, const char *s, chc_err *err)
|
|
104
|
+
{
|
|
105
|
+
return s ? chc__strdup(al, s, strlen(s), err) : NULL;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static void
|
|
109
|
+
chc__async_free_str(const chc_alloc *al, char *s)
|
|
110
|
+
{
|
|
111
|
+
if (s) al->free(al->ud, s, strlen(s) + 1);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
int
|
|
115
|
+
chc_async_client_init(chc_async_client **out, const chc_client_opts *opts,
|
|
116
|
+
const chc_alloc *al, chc_err *err)
|
|
117
|
+
{
|
|
118
|
+
chc_client_opts def_opts = {};
|
|
119
|
+
if (!opts) opts = &def_opts;
|
|
120
|
+
|
|
121
|
+
chc_async_client *c = chc__calloc(al, sizeof *c, err);
|
|
122
|
+
if (!c) return CHC_ERR_OOM;
|
|
123
|
+
|
|
124
|
+
c->cli.al = al;
|
|
125
|
+
c->cli.client_version_major = opts->client_version_major;
|
|
126
|
+
c->cli.client_version_minor = opts->client_version_minor;
|
|
127
|
+
c->cli.client_version_patch = opts->client_version_patch;
|
|
128
|
+
c->cli.client_revision = opts->client_revision ? opts->client_revision
|
|
129
|
+
: CHC_CLIENT_DEFAULT_REVISION;
|
|
130
|
+
c->cli.compression = opts->codec ? opts->compression : CHC_COMP_NONE;
|
|
131
|
+
c->cli.codec = opts->codec;
|
|
132
|
+
/* Seed server.revision so block/packet framing is well-defined before the
|
|
133
|
+
* handshake completes; recv after handshake uses min(client, server). */
|
|
134
|
+
c->cli.server.revision = c->cli.client_revision;
|
|
135
|
+
|
|
136
|
+
chc__mem_sink_init(&c->out, &c->out_io, al);
|
|
137
|
+
c->cli.io = &c->out_io;
|
|
138
|
+
|
|
139
|
+
int rc = chc_in_init_ioless(&c->cli.in, al);
|
|
140
|
+
if (rc != CHC_OK) { al->free(al->ud, c, sizeof *c); return rc; }
|
|
141
|
+
|
|
142
|
+
/* Own strings used after init. */
|
|
143
|
+
if ((opts->client_name && !(c->client_name = chc__async_dup(al, opts->client_name, err))) ||
|
|
144
|
+
(opts->database && !(c->database = chc__async_dup(al, opts->database, err))) ||
|
|
145
|
+
(opts->user && !(c->user = chc__async_dup(al, opts->user, err))) ||
|
|
146
|
+
(opts->password && !(c->password = chc__async_dup(al, opts->password, err)))) {
|
|
147
|
+
chc_async_client_free(c);
|
|
148
|
+
return CHC_ERR_OOM;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
c->hs_phase = CHC__HS_SEND_HELLO;
|
|
152
|
+
*out = c;
|
|
153
|
+
return CHC_OK;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
void
|
|
157
|
+
chc_async_client_free(chc_async_client *c)
|
|
158
|
+
{
|
|
159
|
+
if (!c) return;
|
|
160
|
+
const chc_alloc *al = c->cli.al;
|
|
161
|
+
chc__client_recv_state_free(&c->cli);
|
|
162
|
+
chc_in_free(&c->cli.in);
|
|
163
|
+
chc__mem_sink_free(&c->out);
|
|
164
|
+
chc__async_free_str(al, c->client_name);
|
|
165
|
+
chc__async_free_str(al, c->database);
|
|
166
|
+
chc__async_free_str(al, c->user);
|
|
167
|
+
chc__async_free_str(al, c->password);
|
|
168
|
+
al->free(al->ud, c, sizeof *c);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
int
|
|
172
|
+
chc_async_submit(chc_async_client *c, const void *buf, size_t len, chc_err *err)
|
|
173
|
+
{
|
|
174
|
+
return chc_in_submit(&c->cli.in, buf, len, err);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
void
|
|
178
|
+
chc_async_pending_out(chc_async_client *c, const uint8_t **buf, size_t *len)
|
|
179
|
+
{
|
|
180
|
+
*buf = c->out.buf + c->out_consumed;
|
|
181
|
+
*len = c->out.len - c->out_consumed;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
void
|
|
185
|
+
chc_async_consume_out(chc_async_client *c, size_t n)
|
|
186
|
+
{
|
|
187
|
+
size_t avail = c->out.len - c->out_consumed;
|
|
188
|
+
if (n > avail) n = avail;
|
|
189
|
+
c->out_consumed += n;
|
|
190
|
+
/* Fully drained: reset length so the sink doesn't grow unbounded. Keep the
|
|
191
|
+
* allocation (no realloc-shrink). */
|
|
192
|
+
if (c->out_consumed == c->out.len) {
|
|
193
|
+
c->out.len = 0;
|
|
194
|
+
c->out_consumed = 0;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
int
|
|
199
|
+
chc_async_handshake(chc_async_client *c, chc_err *err)
|
|
200
|
+
{
|
|
201
|
+
int rc;
|
|
202
|
+
chc_client *cli = &c->cli;
|
|
203
|
+
|
|
204
|
+
for (;;) {
|
|
205
|
+
switch (c->hs_phase) {
|
|
206
|
+
case CHC__HS_SEND_HELLO: {
|
|
207
|
+
chc_client_opts o = {
|
|
208
|
+
.client_name = c->client_name,
|
|
209
|
+
.client_version_major = cli->client_version_major,
|
|
210
|
+
.client_version_minor = cli->client_version_minor,
|
|
211
|
+
.client_version_patch = cli->client_version_patch,
|
|
212
|
+
.client_revision = cli->client_revision,
|
|
213
|
+
.database = c->database,
|
|
214
|
+
.user = c->user,
|
|
215
|
+
.password = c->password,
|
|
216
|
+
};
|
|
217
|
+
rc = chc__client_send_hello(cli, &o, err);
|
|
218
|
+
if (rc != CHC_OK) return rc;
|
|
219
|
+
c->hs_phase = CHC__HS_RECV_HELLO;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
case CHC__HS_RECV_HELLO:
|
|
224
|
+
chc__in_checkpoint(&cli->in);
|
|
225
|
+
rc = chc__client_recv_hello(cli, err);
|
|
226
|
+
if (rc == CHC_WOULD_BLOCK) { chc__in_rewind(&cli->in); return rc; }
|
|
227
|
+
if (rc != CHC_OK) return rc; /* server exception / protocol */
|
|
228
|
+
chc_in_reset(&cli->in);
|
|
229
|
+
if (cli->server.revision > cli->client_revision)
|
|
230
|
+
cli->server.revision = cli->client_revision;
|
|
231
|
+
c->hs_phase = CHC__HS_POST_HELLO;
|
|
232
|
+
continue;
|
|
233
|
+
|
|
234
|
+
case CHC__HS_POST_HELLO:
|
|
235
|
+
if (cli->server.revision >= CHC__REV_ADDENDUM) {
|
|
236
|
+
rc = chc__write_string(cli->io, "", 0, err); /* quota_key */
|
|
237
|
+
if (rc != CHC_OK) return rc;
|
|
238
|
+
}
|
|
239
|
+
rc = chc_client_send_ping(cli, err);
|
|
240
|
+
if (rc != CHC_OK) return rc;
|
|
241
|
+
c->hs_phase = CHC__HS_RECV_PONG;
|
|
242
|
+
continue;
|
|
243
|
+
|
|
244
|
+
case CHC__HS_RECV_PONG:
|
|
245
|
+
rc = chc__recv_pong(cli, err); /* owns checkpoint/rewind */
|
|
246
|
+
if (rc != CHC_OK) return rc;
|
|
247
|
+
chc_in_reset(&cli->in);
|
|
248
|
+
c->hs_phase = CHC__HS_DONE;
|
|
249
|
+
continue;
|
|
250
|
+
|
|
251
|
+
case CHC__HS_DONE:
|
|
252
|
+
default:
|
|
253
|
+
return CHC_OK;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
int
|
|
259
|
+
chc_async_send_query(chc_async_client *c, const char *sql, size_t sql_len,
|
|
260
|
+
const char *query_id, size_t query_id_len, chc_err *err)
|
|
261
|
+
{
|
|
262
|
+
return chc_client_send_query(&c->cli, sql, sql_len, query_id, query_id_len, err);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
int
|
|
266
|
+
chc_async_send_data(chc_async_client *c, const chc_block_builder *bb, chc_err *err)
|
|
267
|
+
{
|
|
268
|
+
return chc_client_send_data(&c->cli, bb, err);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
int
|
|
272
|
+
chc_async_send_data_end(chc_async_client *c, chc_err *err)
|
|
273
|
+
{
|
|
274
|
+
return chc_client_send_data(&c->cli, NULL, err);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const chc_server_info *
|
|
278
|
+
chc_async_server_info(const chc_async_client *c)
|
|
279
|
+
{
|
|
280
|
+
return &c->cli.server;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
void
|
|
284
|
+
chc_async_packet_clear(chc_async_client *c, chc_packet *p)
|
|
285
|
+
{
|
|
286
|
+
chc_packet_clear(&c->cli, p);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/* The recv packet decoder is shared with the blocking client
|
|
290
|
+
* (chc__recv_packet_resumable, clickhouse-client.h): the ioless `cli.in`
|
|
291
|
+
* makes it checkpoint at packet/column boundaries and yield CHC_WOULD_BLOCK
|
|
292
|
+
* on underrun, with c->cli.recv_* carrying parse state across calls. */
|
|
293
|
+
int
|
|
294
|
+
chc_async_recv_packet(chc_async_client *c, chc_packet *out, chc_err *err)
|
|
295
|
+
{
|
|
296
|
+
return chc__recv_packet_resumable(&c->cli, out, err);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
#endif /* CHC_IMPLEMENTATION */
|
|
300
|
+
|
|
301
|
+
#endif /* CLICKHOUSE_ASYNC_H */
|