Fossil SCM

fossil-scm / src / json_query.c
Blame History Raw 97 lines
1
#ifdef FOSSIL_ENABLE_JSON
2
/*
3
** Copyright (c) 2011 D. Richard Hipp
4
**
5
** This program is free software; you can redistribute it and/or
6
** modify it under the terms of the Simplified BSD License (also
7
** known as the "2-Clause License" or "FreeBSD License".)
8
**
9
** This program is distributed in the hope that it will be useful,
10
** but without any warranty; without even the implied warranty of
11
** merchantability or fitness for a particular purpose.
12
**
13
** Author contact information:
14
** [email protected]
15
** http://www.hwaci.com/drh/
16
**
17
*/
18
19
#include "config.h"
20
#include "json_query.h"
21
22
#if INTERFACE
23
#include "json_detail.h"
24
#endif
25
26
27
/*
28
** Implementation of the /json/query page.
29
**
30
** Requires admin privileges. Intended primarily to assist me in
31
** coming up with JSON output structures for pending features.
32
**
33
** Options/parameters:
34
**
35
** sql=string - a SELECT statement
36
**
37
** format=string 'a' means each row is an Array of values, 'o'
38
** (default) creates each row as an Object.
39
**
40
** TODO: in CLI mode (only) use -S FILENAME to read the sql
41
** from a file.
42
*/
43
cson_value * json_page_query(void){
44
char const * zSql = NULL;
45
cson_value * payV;
46
char const * zFmt;
47
Stmt q = empty_Stmt;
48
int check;
49
if(!g.perm.Admin && !g.perm.Setup){
50
json_set_err(FSL_JSON_E_DENIED,
51
"Requires 'a' or 's' privileges.");
52
return NULL;
53
}
54
55
if( cson_value_is_string(g.json.reqPayload.v) ){
56
zSql = cson_string_cstr(cson_value_get_string(g.json.reqPayload.v));
57
}else{
58
zSql = json_find_option_cstr2("sql",NULL,"s",2);
59
}
60
61
if(!zSql || !*zSql){
62
json_set_err(FSL_JSON_E_MISSING_ARGS,
63
"'sql' (-s) argument is missing.");
64
return NULL;
65
}
66
67
zFmt = json_find_option_cstr2("format",NULL,"f",3);
68
if(!zFmt) zFmt = "o";
69
db_prepare(&q,"%s", zSql/*safe-for-%s*/);
70
if( 0 == sqlite3_column_count( q.pStmt ) ){
71
json_set_err(FSL_JSON_E_USAGE,
72
"Input query has no result columns. "
73
"Only SELECT-like queries are supported.");
74
db_finalize(&q);
75
return NULL;
76
}
77
switch(*zFmt){
78
case 'a':
79
check = cson_sqlite3_stmt_to_json(q.pStmt, &payV, 0);
80
break;
81
case 'o':
82
default:
83
check = cson_sqlite3_stmt_to_json(q.pStmt, &payV, 1);
84
};
85
db_finalize(&q);
86
if(0 != check){
87
json_set_err(FSL_JSON_E_UNKNOWN,
88
"Conversion to JSON failed with cson code #%d (%s).",
89
check, cson_rc_string(check));
90
assert(NULL==payV);
91
}
92
return payV;
93
94
}
95
96
#endif /* FOSSIL_ENABLE_JSON */
97

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button