Skip to content

Commit dd366fb

Browse files
authored
Merge pull request #40 from nezed/readonly-option
Rename useQueryString to readonly
2 parents e26de91 + 274f25c commit dd366fb

File tree

7 files changed

+32
-23
lines changed

7 files changed

+32
-23
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ var options = {
8585
profile: "web",
8686
database: "test"
8787
},
88-
omitFormat: false
88+
omitFormat: false,
89+
readonly: true,
8990
};
9091

9192
var clickHouse = new ClickHouse (options);
@@ -128,6 +129,7 @@ Driver options:
128129
which returns dataset. Currently `SELECT|SHOW|DESC|DESCRIBE|EXISTS\s+TABLE`.
129130
You can change this behaviour by providing this option. In this case you should
130131
add `FORMAT JSONCompact` by yourself. Should be detected automatically. Default `false`;
132+
* **readonly**: tells driver to send query with HTTP GET method. Same as [`readonly=1` setting](https://clickhouse.yandex/docs/en/operations/settings/permissions_for_queries/#settings_readonly). [More details](https://clickhouse.yandex/docs/en/interfaces/http/)
131133

132134

133135
### var stream = clickHouse.query (statement, [options], [callback])

src/clickhouse.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ ClickHouse.prototype.query = function (chQuery, options, cb) {
255255
options.omitFormat = options.omitFormat || this.options.omitFormat || false;
256256
options.dataObjects = options.dataObjects || this.options.dataObjects || false;
257257
options.format = options.format || this.options.format || null;
258+
options.readonly = options.readonly || this.options.readonly || this.options.useQueryString || false;
258259

259260
// we're adding `queryOptions` passed for constructor if any
260261
var queryObject = Object.assign ({}, this.options.queryOptions, options.queryOptions);
@@ -318,7 +319,7 @@ ClickHouse.prototype.query = function (chQuery, options, cb) {
318319
reqData.format = options.format;
319320

320321
// use query string to submit ClickHouse query — useful to mock CH server
321-
if (this.options.useQueryString) {
322+
if (options.readonly) {
322323
queryObject.query = chQuery + ((options.omitFormat) ? '' : ' FORMAT ' + options.format + formatEnding);
323324
reqParams.method = 'GET';
324325
} else {

test/01-simulated.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe ("simulated queries", function () {
107107
});
108108

109109
it ("selects using callback", function (done) {
110-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
110+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
111111
ch.query ("SELECT 1", {syncParser: true}, function (err, result) {
112112
assert (!err);
113113
assert (result.meta, "result should be Object with `data` key to represent rows");
@@ -118,7 +118,7 @@ describe ("simulated queries", function () {
118118
});
119119

120120
it ("selects numbers using callback", function (done) {
121-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
121+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
122122
ch.query ("SELECT number FROM system.numbers LIMIT 10", {syncParser: true}, function (err, result) {
123123
assert (!err);
124124
assert (result.data, "result should be Object with `data` key to represent rows");
@@ -135,7 +135,7 @@ describe ("simulated queries", function () {
135135
});
136136

137137
it ("selects numbers using stream", function (done) {
138-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
138+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
139139
var rows = [];
140140
var stream = ch.query ("SELECT number FROM system.numbers LIMIT 10", function (err, result) {
141141
assert (!err);

test/02-real-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe ("real server", function () {
5151
});
5252

5353
it ("returns error", function (done) {
54-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
54+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
5555
var stream = ch.query ("ABCDEFGHIJKLMN", {syncParser: true}, function (err, result) {
5656
// assert (err);
5757
// done ();

test/03-errors.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe ("error parsing", function () {
1414
dbCreated = false;
1515

1616
it ("will not throw on http error", function (done) {
17-
var ch = new ClickHouse ({host: host, port: 59999, useQueryString: true});
17+
var ch = new ClickHouse ({host: host, port: 59999, readonly: true});
1818
var stream = ch.query ("ABCDEFGHIJKLMN", {syncParser: true}, function (err, result) {
1919
// assert (err);
2020
// done ();
@@ -26,7 +26,7 @@ describe ("error parsing", function () {
2626
});
2727

2828
it ("returns error for unknown sql", function (done) {
29-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
29+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
3030
var stream = ch.query ("ABCDEFGHIJKLMN", {syncParser: true}, function (err, result) {
3131
// assert (err);
3232
// done ();
@@ -44,7 +44,7 @@ describe ("error parsing", function () {
4444
});
4545

4646
it ("returns error with line/col for sql with garbage", function (done) {
47-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
47+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
4848
var stream = ch.query ("CREATE\n\t\tABCDEFGHIJKLMN", {syncParser: true}, function (err, result) {
4949
// assert (err);
5050
// done ();
@@ -62,7 +62,7 @@ describe ("error parsing", function () {
6262
});
6363

6464
it ("returns error for empty sql", function (done) {
65-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
65+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
6666

6767
function countCallbacks (err) {
6868
countCallbacks.count = (countCallbacks.count || 0) + 1;
@@ -94,10 +94,9 @@ describe ("error parsing", function () {
9494
});
9595

9696
it ("returns error for unknown table", function (done) {
97-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
97+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
9898
var stream = ch.query ("SELECT * FROM xxx", {syncParser: true}, function (err, result) {
99-
// assert (err);
100-
// done ();
99+
assert (err);
101100
});
102101

103102
stream.on ('error', function (err) {
@@ -112,6 +111,13 @@ describe ("error parsing", function () {
112111
});
113112
});
114113

114+
it ("returns error for writing in readonly mode", function (done) {
115+
var ch = new ClickHouse ({host: host, port: port});
116+
ch.querying ("CREATE TABLE xxx (a UInt8) ENGINE = Memory()", {readonly: true})
117+
.then(() => done('Should fail in readonly mode'))
118+
.catch(() => done())
119+
});
120+
115121

116122

117123
});

test/04-select.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe ("select data from database", function () {
1010
dbCreated = false;
1111

1212
it ("selects using callback", function (done) {
13-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
13+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
1414
ch.query ("SELECT 1", {syncParser: true}, function (err, result) {
1515
assert (!err);
1616
assert (result.meta, "result should be Object with `data` key to represent rows");
@@ -30,7 +30,7 @@ describe ("select data from database", function () {
3030
});
3131

3232
it ("selects numbers using callback", function (done) {
33-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
33+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
3434
ch.query ("SELECT number FROM system.numbers LIMIT 10", {syncParser: true}, function (err, result) {
3535
assert (!err);
3636
assert (result.meta, "result should be Object with `data` key to represent rows");
@@ -47,7 +47,7 @@ describe ("select data from database", function () {
4747
});
4848

4949
it ("selects numbers using promise should already have parsed data", function () {
50-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
50+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
5151
return ch.querying ("SELECT number FROM system.numbers LIMIT 10").then (function (result) {
5252
assert (result.meta, "result should be Object with `data` key to represent rows");
5353
assert (result.data, "result should be Object with `meta` key to represent column info");
@@ -63,7 +63,7 @@ describe ("select data from database", function () {
6363
});
6464

6565
it ("selects numbers as dataObjects using promise", function () {
66-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true, dataObjects: true});
66+
var ch = new ClickHouse ({host: host, port: port, readonly: true, dataObjects: true});
6767
return ch.querying ("SELECT number FROM system.numbers LIMIT 10").then (function (result) {
6868
assert (result.meta, "result should be Object with `data` key to represent rows");
6969
assert (result.data, "result should be Object with `meta` key to represent column info");

test/90-torture.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe ("torturing", function () {
6262

6363
this.timeout (timeout);
6464

65-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
65+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
6666
var queryCount = 0;
6767
var symbolsTransferred = 0;
6868

@@ -95,7 +95,7 @@ describe ("torturing", function () {
9595

9696
this.timeout (timeout);
9797

98-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
98+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
9999
var queryCount = 0;
100100
var symbolsTransferred = 0;
101101

@@ -127,7 +127,7 @@ describe ("torturing", function () {
127127

128128
this.timeout (timeout);
129129

130-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
130+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
131131
var queryCount = 0;
132132
var symbolsTransferred = 0;
133133

@@ -159,7 +159,7 @@ describe ("torturing", function () {
159159

160160
this.timeout (timeout);
161161

162-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
162+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
163163
var queryCount = 0;
164164
var symbolsTransferred = 0;
165165

@@ -191,7 +191,7 @@ describe ("torturing", function () {
191191

192192
this.timeout (timeout);
193193

194-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
194+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
195195
var queryCount = 0;
196196
var symbolsTransferred = 0;
197197

@@ -223,7 +223,7 @@ describe ("torturing", function () {
223223

224224
this.timeout (timeout);
225225

226-
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
226+
var ch = new ClickHouse ({host: host, port: port, readonly: true});
227227
var queryCount = 0;
228228
var symbolsTransferred = 0;
229229

0 commit comments

Comments
 (0)