forked from yql/yql-tables
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bonsai.branch.xml
208 lines (171 loc) · 9.33 KB
/
bonsai.branch.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<author>Erik Eldridge</author>
<description>
</description>
<sampleQuery>use 'http://test.erikeldridge.com/yql/tc.admin.xml' as tc.admin; insert into tc.admin (root) values("store://Is4R1AolbcX6Jdz7prCxi9")</sampleQuery>
</meta>
<bindings>
<select>
<inputs>
<key id="key" type="xs:string" paramType="variable" />
<key id="select" type="xs:string" paramType="variable"/>
<key id="update" type="xs:string" paramType="variable"/>
<key id="admin" type="xs:string" paramType="variable" default="false" />
</inputs>
<execute><![CDATA[
y.include('http://test.erikeldridge.com/yql/json2.js');
response.object = function(url) {
var queries = [],
results = [];
//get leaves
queries[0] = 'select * from yql.storage where name="{select}"'
.replace('{select}', select);
results[0] = y.xmlToJson(y.query(queries[0]).results);
//if admin mode, just dump sherpa keys
if ('true' === admin) {
return results[0].results.result.value
}
//count # leaves to determine if branch is full
var count = 0;
for each (var leaf in results[0].results.result.value) {
count ++;
}
//key is not in branch
if (!results[0].results.result.value[key]) {
return {
"meta" : {
"count" : count,
"select" : select,
"update" : update,
},
"results" : false
};
}
//key is in branch, so fetch & return value
queries[1] = "select * from yql.storage where name='{name}'"
.replace('{name}', results[0].results.result.value[key].select);
results[1] = y.xmlToJson(y.query(queries[1]).results);
return {
"meta" : {
"count" : count,
"select" : select,
"update" : update
},
"results" : results[1].results.result.value
};
}();
]]></execute>
</select>
<update>
<inputs>
<key id="key" type="xs:string" paramType="variable" required="true"/>
<key id="select" type="xs:string" paramType="variable" required="true"/>
<value id="val" type="xs:string" paramType="variable" required="true"/>
</inputs>
<execute><![CDATA[
y.include('http://test.erikeldridge.com/yql/json2.js');
response.object = function(url) {
var queries = [],
results = [];
//get leaves
queries[0] = 'select * from yql.storage where name="{select}"'
.replace('{select}', select);
results[0] = y.xmlToJson(y.query(queries[0]).results);
//key is not in branch
if (!results[0].results.result.value[key]) {
return {"error":"key could not be found"};
}
//key is in branch, so update value
queries[1] = "update yql.storage set value='{val}' where name='{update}'"
.replace('{val}', val)
.replace('{update}', results[0].results.result.value[key].update);
results[1] = y.xmlToJson(y.query(queries[1]).results);
return results[1];
}();
]]></execute>
</update>
<insert>
<inputs>
<value id="key" type="xs:string" paramType="variable" required="true"/>
<value id="val" type="xs:string" paramType="variable" required="true"/>
<value id="select" type="xs:string" paramType="variable" required="true"/>
<value id="update" type="xs:string" paramType="variable" required="true"/>
</inputs>
<execute><![CDATA[
y.include('http://test.erikeldridge.com/yql/json2.js');
response.object = function(url) {
var queries = [],
results = [];
//get leaves
queries[0] = 'select * from yql.storage where name="{select}"'
.replace('{select}', select);
results[0] = y.xmlToJson(y.query(queries[0]).results);
//key is already in branch
if (results[0].results.result.value[key]) {
return {"error":"key already defined"};
}
//if this branch has a pool, extract leaf from it
if (results[0].results.result.value['pool']) {
for (var i in results[0].results.result.value['pool']) {
var leaf = results[0].results.result.value['pool'][i];
delete results[0].results.result.value['pool'][i];
break;
};
//otherwise, create leaf
} else {
queries[1] = "insert into yql.storage.admin (value) values ('{val}')"
.replace('{val}', val);
results[1] = y.xmlToJson(y.query(queries[1]).results);
var leaf = results[1].results.inserted;
}
//add leaf to leaves collection
results[0].results.result.value[key] = leaf;
//save leaves back to branch
queries[2] = "update yql.storage set value='{val}' where name='{update}'"
.replace('{val}', JSON.stringify(results[0].results.result.value))
.replace('{update}', update);
results[2] = y.xmlToJson(y.query(queries[2]).results);
return results[2];
}();
]]></execute>
</insert>
<delete>
<inputs>
<key id="key" type="xs:string" paramType="variable" required="true"/>
<key id="select" type="xs:string" paramType="variable" required="true"/>
<key id="update" type="xs:string" paramType="variable" required="true"/>
</inputs>
<execute><![CDATA[
y.include('http://test.erikeldridge.com/yql/json2.js');
response.object = function(url) {
var queries = [],
results = [];
//get leaves
queries[0] = 'select * from yql.storage where name="{select}"'
.replace('{select}', select);
results[0] = y.xmlToJson(y.query(queries[0]).results);
//key is not in branch
if (!results[0].results.result.value[key]) {
return {"error":"key is not in branch"};
}
//add leaf to branch pool
//note: i'd love to use an array for the pool, but it seems we're limited to objects
if (!results[0].results.result.value['pool']) {
results[0].results.result.value['pool'] = {};
}
results[0].results.result.value['pool'][key] = results[0].results.result.value[key];
//remove leaf from branch
delete results[0].results.result.value[key];
//save updated branch
queries[2] = "update yql.storage set value='{val}' where name='{update}'"
.replace('{val}', JSON.stringify(results[0].results.result.value))
.replace('{update}', update);
results[2] = y.xmlToJson(y.query(queries[2]).results);
return results[2];
}();
]]></execute>
</delete>
</bindings>
</table>