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 not null
28 define("QUERY_CREATE_TABLE_Edge", "
30 edgeid binary(16) primary key,
31 typeid binary(16) not null,
32 nodeid0 binary(16) not null,
33 nodeid1 binary(16) not null
37 define("QUERY_ADD_Node", "
39 nodeid, typeid, identifier
41 unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), ?
44 define("QUERY_ADD_Node_TYPES", "sss");
46 define("QUERY_ADD_Edge", "
48 edgeid, typeid, nodeid0, nodeid1
50 unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', ''))
53 define("QUERY_ADD_Edge_TYPES", "ssss");
55 define("QUERY_SELECT_ALL_Node", "select hex(nodeid), hex(typeid), identifier from Node");
56 define("QUERY_SELECT_ALL_Edge", "select hex(edgeid), hex(typeid), hex(nodeid0), hex(nodeid1) from Edge");
59 mysqli_report(MYSQLI_REPORT_ERROR);
64 if(isset($_GET['action'])){
65 $action = $_GET['action'];
66 if(strcmp($action, 'rebuild') == 0){
68 exitWithResponseCode("CEA95615-649C-4837-9E24-0C968FA57647", "OK");
69 } else if(strcmp($action, 'getallnode') == 0){
70 $stmt = $db->prepare(QUERY_SELECT_ALL_Node);
73 $stmt->store_result();
74 if($stmt->errno != 0){
75 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
78 $stmt->bind_result($uuid, $typeid, $ident);
79 while($stmt->fetch()){
80 $uuid = strtolower($uuid);
82 echo(getFormedUUIDString($uuid));
84 echo(getFormedUUIDString($typeid));
92 } else if(strcmp($action, 'viewallnode') == 0){
93 $stmt = $db->prepare(QUERY_SELECT_ALL_Node);
96 $stmt->store_result();
97 if($stmt->errno != 0){
98 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
101 $stmt->bind_result($uuid, $typeid, $ident);
102 while($stmt->fetch()){
103 $uuid = strtolower($uuid);
105 echo(getFormedUUIDString($uuid));
107 echo(getFormedUUIDString($typeid));
113 echo($stmt->num_rows);
116 } else if(strcmp($action, 'addnode') == 0){
117 if(isset($_GET['nodeid'])){
118 $uuid = $_GET['nodeid'];
120 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid needed.");
122 if(isset($_GET['typeid'])){
123 $typeid = $_GET['typeid'];
125 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
127 if(isset($_GET['ident'])){
128 $ident = $_GET['ident'];
130 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "ident needed.");
133 $stmt = $db->prepare(QUERY_ADD_Node);
134 $mts = getTimeStampMs();
135 $stmt->bind_param(QUERY_ADD_Node_TYPES, $nodeid, $typeid, $ident);
138 $stmt->store_result();
139 if($stmt->errno != 0){
140 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db));
143 exitWithResponseCode("CEA95615-649C-4837-9E24-0C968FA57647", "OK");
144 } else if(strcmp($action, 'saytest') == 0){
146 //for Mac OSX say command.
147 system("say " . escapeshellarg("sayのテストをしています。"));
153 exitWithResponseCode("B539657C-0FA6-49C2-AFB0-13AF5C7866ED");
155 function exitWithResponseCode($errid, $description = "")
157 die('["' . $errid .'","' . $description . '"]');
162 $db = new mysqli('localhost', DATABASE_USER, DATABASE_PWD, DATABASE_NAME);
164 if (mysqli_connect_error()) {
166 exitWithResponseCode("3A8CF3C8-E6B6-4A99-9134-343CA341B591", mysqli_connect_error());
170 $db->set_charset("utf8");
173 $stmt = $db->prepare("show tables");
176 $stmt->store_result();
177 if($stmt->errno != 0){
178 exitWithResponseCode("80FA2D65-9473-40B0-A3CE-159AE8E67017");
181 $stmt->bind_result($tablename);
183 while($stmt->fetch()){
184 if($tablename == "Node"){
187 if($tablename == "Edge"){
191 if(($found & 3) != 3){
199 function rebuildDB($db)
204 $stmt = $db->prepare("drop table if exists Node");
210 $stmt = $db->prepare("drop table if exists Edge");
218 $stmt = $db->prepare(QUERY_CREATE_TABLE_Node);
224 $stmt = $db->prepare(QUERY_CREATE_TABLE_Edge);
230 function getFormedUUIDString($str)
232 $str = strtolower($str);
234 substr($str, 0, 8) . "-" .
235 substr($str, 8, 4) . "-" .
236 substr($str, 12, 4) . "-" .
237 substr($str, 16, 4) . "-" .
242 function getTimeStampMs()
244 return ceil(microtime(true)*1000);