-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKySQL.krl
121 lines (100 loc) · 3.59 KB
/
KySQL.krl
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
ruleset a169x379 {
meta {
name "KySQL"
description <<
KySQL - KRL MySQL Module
>>
author "Ed Orcutt, LOBOSLLC"
logging on
provides KyQuery, KyResult, KyRow, KyStatus, KyError, KyRowCount, KyInsertString,
KyStatusOK
configure using apikey = ""
and callback = ""
and username = ""
and password = ""
and database = ""
}
dispatch { }
global {
KyKey = apikey || "YOUR-TEST-KEY-HERE";
KyURL = callback || "http://example.org/KySQL.php";
dbUser = username || "test-database-username";
dbPass = password || "test-database-password";
dbName = database || "test-database-name";
KyStatusOK = 200;
// --------------------------------------------
// build the options hash for making a POST
postOptions = function(queryStr) {
{
"params": {
"kykey" : "#{KyKey}",
"dbuser" : "#{dbUser}",
"dbpass" : "#{dbPass}",
"dbname" : "#{dbName}",
"kquery" : "#{queryStr}"
},
"response_headers": ["status-message", "row-count"]
}
};
// --------------------------------------------
// send MySQL query to remote service
KyQuery = function(queryStr) {
opts = postOptions(queryStr);
http:post("#{KyURL}", opts)
};
// --------------------------------------------
KyResult = function(KyObject) {
KyObject.pick("$.content").decode();
};
// --------------------------------------------
KyRow = function(KyObject) {
KyObject.pick("$.content").decode().pick("$.results", true).head().head();
};
// --------------------------------------------
KyStatus = function(KyObject) {
KyObject.pick("$.status_code");
};
// --------------------------------------------
KyError = function(KyObject) {
KyObject.pick("$.status-message", true).head() || "";
};
// --------------------------------------------
KyRowCount = function(KyObject) {
KyObject.pick("$.row-count", true).head() || 0;
};
// --------------------------------------------
KyInsertString = function(KyTable, KyRow) {
// array of database table column names, sorted
colNameArray = KyRow.keys().sort();
// join into single string, with each column name within single quotes
colNameString = (colNameArray.map(function(x) {"#{x}"})).join(",");
// extract array of database table column values
valuesArray = colNameArray.map(function(x) {KyRow.pick("$..#{x}")});
// join into single string, with each value within single quotes
valuesString = (valuesArray.map(function(x) {"'#{x}'"})).join(",");
"INSERT INTO #{KyTable} (#{colNameString}) VALUES (#{valuesString})"
};
}
// ------------------------------------------------------------------------
rule test_query is inactive {
select when pageview ".*"
pre {
KyObject = KyQuery("SELECT * FROM neubook");
KyJSON = KyResult(KyObject);
status = KyStatus(KyObject);
errorMsg = KyError(KyObject);
myHash = {
"name" : "Ed Orcutt",
"email" : "[email protected]",
"phone" : "(801) 555-1234"
};
kyInsert = KyInsertString("neubook", myHash);
}
{
notify("KyStatus: " + status, errorMsg) with sticky = true;
}
}
// ------------------------------------------------------------------------
// Beyond here there be dragons :)
// ------------------------------------------------------------------------
}