7 // grant 権限内容(all privileges) on 権限対象(dbname.tablename) to ユーザー@ホスト名 [ identified by "パスワード"];
10 define("DATABASE_USER", "aiclient");
12 define("DATABASE_PWD", "WhoAmI?");
14 define("DATABASE_NAME", "MemoryDB");
20 define("QUERY_CREATE_TABLE_Node", "
22 nodeid binary(16) primary key,
23 typeid binary(16) not null,
24 identifier text character set utf8,
29 define("QUERY_CREATE_TABLE_Edge", "
31 edgeid binary(16) primary key,
32 typeid binary(16) not null,
33 nodeid0 binary(16) not null,
34 nodeid1 binary(16) not null,
41 define("QUERY_ADD_Node", "
43 nodeid, typeid, identifier, modtimestamp
45 unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), ?, ?
48 define("QUERY_ADD_Node_TYPES", "sssi");
50 define("QUERY_ADD_Edge", "
52 edgeid, typeid, nodeid0, nodeid1, modtimestamp
54 unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), ?
57 define("QUERY_ADD_Edge_TYPES", "ssssi");
61 define("QUERY_SELECT_ALL_Node", "select hex(nodeid), hex(typeid), identifier from Node");
62 define("QUERY_SELECT_ALL_Node_With_modtimestamp", "select hex(nodeid), hex(typeid), identifier, modtimestamp from Node");
63 define("QUERY_SELECT_ALL_Edge", "select hex(edgeid), hex(typeid), hex(nodeid0), hex(nodeid1) from Edge");
65 define("QUERY_SELECT_modified_Node", "select hex(nodeid), hex(typeid), identifier from Node WHERE modtimestamp>?");
66 define("QUERY_SELECT_modified_Node_TYPES", "i");
69 mysqli_report(MYSQLI_REPORT_ERROR);
74 if(isset($_GET['action'])){
75 $action = $_GET['action'];
76 if(strcmp($action, 'rebuild') == 0){
78 exitWithResponseCode("CEA95615-649C-4837-9E24-0C968FA57647", "OK");
79 } else if(strcmp($action, 'getallnode') == 0){
80 $stmt = $db->prepare(QUERY_SELECT_ALL_Node);
83 $stmt->store_result();
84 if($stmt->errno != 0){
85 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
88 $stmt->bind_result($uuid, $typeid, $ident);
89 while($stmt->fetch()){
90 $uuid = strtolower($uuid);
92 echo(getFormedUUIDString($uuid));
94 echo(getFormedUUIDString($typeid));
102 echoMemoryDBNetworkTimestamp();
104 } else if(strcmp($action, 'getnodemod') == 0){
105 if(isset($_GET['t'])){
110 $stmt = $db->prepare(QUERY_SELECT_modified_Node);
111 $stmt->bind_param(QUERY_SELECT_modified_Node_TYPES, $ts);
114 $stmt->store_result();
115 if($stmt->errno != 0){
116 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
119 $stmt->bind_result($uuid, $typeid, $ident);
120 while($stmt->fetch()){
121 $uuid = strtolower($uuid);
123 echo(getFormedUUIDString($uuid));
125 echo(getFormedUUIDString($typeid));
133 echoMemoryDBNetworkTimestamp();
135 } else if(strcmp($action, 'viewallnode') == 0){
136 $stmt = $db->prepare(QUERY_SELECT_ALL_Node_With_modtimestamp);
139 $stmt->store_result();
140 if($stmt->errno != 0){
141 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
144 $stmt->bind_result($uuid, $typeid, $ident, $mts);
145 while($stmt->fetch()){
146 $uuid = strtolower($uuid);
148 echo(getFormedUUIDString($uuid));
150 echo(getFormedUUIDString($typeid));
157 echo($stmt->num_rows);
159 exit(" OK " . getTimeStampMs());
160 } else if(strcmp($action, 'addnode') == 0){
161 if(isset($_GET['nid'])){
162 $nodeid = $_GET['nid'];
164 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid needed.");
166 if(isset($_GET['tid'])){
167 $typeid = $_GET['tid'];
169 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
171 if(isset($_GET['ident'])){
172 $ident = $_GET['ident'];
174 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "ident needed.");
177 $stmt = $db->prepare(QUERY_ADD_Node);
178 $mts = getTimeStampMs();
179 $stmt->bind_param(QUERY_ADD_Node_TYPES, $nodeid, $typeid, $ident, $mts);
182 $stmt->store_result();
183 if($stmt->errno != 0){
184 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db));
187 exitWithResponseCode("CEA95615-649C-4837-9E24-0C968FA57647", "OK");
188 } else if(strcmp($action, 'saytest') == 0){
190 //for Mac OSX say command.
191 system("say " . escapeshellarg("sayのテストをしています。"));
197 exitWithResponseCode("B539657C-0FA6-49C2-AFB0-13AF5C7866ED");
199 function exitWithResponseCode($errid, $description = "")
201 die('["' . $errid .'","' . $description . '"]');
204 function echoMemoryDBNetworkTimestamp()
206 echo('["a2560a9c-dcf7-4746-ac14-347188518cf2","e3346fd4-ac17-41c3-b3c7-e04972e5c014","');
207 echo(getTimeStampMs());
214 $db = new mysqli('localhost', DATABASE_USER, DATABASE_PWD, DATABASE_NAME);
216 if (mysqli_connect_error()) {
218 exitWithResponseCode("3A8CF3C8-E6B6-4A99-9134-343CA341B591", mysqli_connect_error());
222 $db->set_charset("utf8");
225 $stmt = $db->prepare("show tables");
228 $stmt->store_result();
229 if($stmt->errno != 0){
230 exitWithResponseCode("80FA2D65-9473-40B0-A3CE-159AE8E67017");
233 $stmt->bind_result($tablename);
235 while($stmt->fetch()){
236 if($tablename == "Node"){
239 if($tablename == "Edge"){
243 if(($found & 3) != 3){
251 function rebuildDB($db)
258 $stmt = $db->query("drop table if exists Node");
261 $stmt = $db->query("drop table if exists Edge");
268 $stmt = $db->query(QUERY_CREATE_TABLE_Node);
271 $stmt = $db->query(QUERY_CREATE_TABLE_Edge);
274 function getFormedUUIDString($str)
276 $str = strtolower($str);
278 substr($str, 0, 8) . "-" .
279 substr($str, 8, 4) . "-" .
280 substr($str, 12, 4) . "-" .
281 substr($str, 16, 4) . "-" .
286 function getTimeStampMs()
288 return ceil(microtime(true)*1000);