Skip to content

Commit 655792e

Browse files
author
Enric Sala
committed
Improve the command execution support
Allow passing an optional database argument besides specifying if POST is required
1 parent aa09091 commit 655792e

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,20 @@ Notice that the `time()` and `timetable()` methods take an optional timezone arg
173173
Other commands
174174
--------------
175175

176-
Use `runCommand(command, requiresPost)` for executing commands not provided in the library:
176+
Use `runCommand(command, [database], [requiresPost])` for executing arbitrary commands:
177177

178178
```matlab
179-
% Show series and tag keys in a database
180-
influxdb.runCommand('SHOW SERIES ON "example" FROM "weather"');
181-
influxdb.runCommand('SHOW TAG KEYS ON "example" FROM "weather"');
179+
% Show databases then create one
180+
influxdb.runCommand('SHOW DATABASES')
181+
influxdb.runCommand('CREATE DATABASE "example"', true)
182182
183-
% Create and drop a database
184-
influxdb.runCommand('CREATE DATABASE "example"', true);
185-
influxdb.runCommand('DROP DATABASE "example"', true);
183+
% Show measurements and tag keys
184+
influxdb.runCommand('SHOW MEASUREMENTS', 'example')
185+
influxdb.runCommand('SHOW TAG KEYS', 'example')
186186
187187
% Create a retention policy that keeps data for one day
188-
influxdb.runCommand('CREATE RETENTION POLICY "one_day" ON "example" DURATION 1d REPLICATION 1', true);
189-
influxdb.runCommand('SHOW RETENTION POLICIES ON "example"', false);
188+
influxdb.runCommand('CREATE RETENTION POLICY "one_day" ON "example" DURATION 1d REPLICATION 1', true)
189+
influxdb.runCommand('SHOW RETENTION POLICIES', 'example')
190190
```
191191

192192
See the [InfluxDB documentation][influxdb-docs] for more schema exploration and management commands.

influxdb-client/InfluxDB.m

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@
4343

4444
% Show databases
4545
function databases = databases(obj)
46-
url = [obj.Url '/query'];
47-
opts = weboptions('Timeout', obj.ReadTimeout, ...
48-
'Username', obj.User, 'Password', obj.Password);
49-
response = webread(url, 'q', 'SHOW DATABASES', opts);
46+
response = obj.runCommand('SHOW DATABASES');
5047
databases = [response.results.series.values{:}];
5148
end
5249

@@ -108,15 +105,24 @@
108105
end
109106

110107
% Execute other queries or commands
111-
function response = runCommand(obj, command, requiresPost)
108+
function response = runCommand(obj, command, varargin)
109+
idx = find(cellfun(@(x) ischar(x), varargin), 1, 'first');
110+
database = iif(isempty(idx), '', varargin{idx});
111+
idx = find(cellfun(@(x) islogical(x), varargin), 1, 'first');
112+
requiresPost = iif(isempty(idx), false, varargin{idx});
113+
if isempty(database)
114+
params = {'q', command};
115+
else
116+
params = {'db', database, 'q', command};
117+
end
112118
url = [obj.Url '/query'];
113119
opts = weboptions('Username', obj.User, 'Password', obj.Password);
114-
if nargin > 2 && requiresPost
120+
if requiresPost
115121
opts.Timeout = obj.WriteTimeout;
116-
response = webwrite(url, 'q', command, opts);
122+
response = webwrite(url, params{:}, opts);
117123
else
118124
opts.Timeout = obj.ReadTimeout;
119-
response = webread(url, 'q', command, opts);
125+
response = webread(url, params{:}, opts);
120126
end
121127
end
122128
end

0 commit comments

Comments
 (0)