OSDN Git Service

memdbのphpフロントエンドを実装中
[chnosproject/AI004.git] / memdb / memorydb.php
1 <?php
2
3 //
4 // Settings
5 //
6
7 // grant 権限内容(all privileges) on 権限対象(dbname.tablename) to ユーザー@ホスト名 [ identified by "パスワード"]; 
8
9 //データベースユーザー名
10 define("DATABASE_USER", "aiclient");
11 //データベースパスワード
12 define("DATABASE_PWD", "WhoAmI?");
13 //データベース名
14 define("DATABASE_NAME", "MemoryDB");
15
16 //
17 // Static values
18 //
19
20 define("QUERY_CREATE_TABLE_Node", "
21 create table Node (
22         nodeid binary(16) primary key,
23         typeid binary(16) not null,
24         identifier text character set utf8 not null
25 )
26 ");
27
28 define("QUERY_CREATE_TABLE_Edge", "
29 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
34 )
35 ");
36
37 define("QUERY_ADD_Node", "
38 insert into Node (
39         nodeid, typeid, identifier
40 ) values (
41         unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), ?
42 )
43 ");
44 define("QUERY_ADD_Node_TYPES", "sss");
45
46 define("QUERY_ADD_Edge", "
47 insert into Node (
48         edgeid, typeid, nodeid0, nodeid1
49 ) values (
50         unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', ''))
51 )
52 ");
53 define("QUERY_ADD_Edge_TYPES", "ssss");
54
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");
57
58 //FOR DEBUG
59 mysqli_report(MYSQLI_REPORT_ERROR);
60
61 $db = connectDB();
62
63 //action解釈
64 if(isset($_GET['action'])){
65         $action = $_GET['action'];
66         if(strcmp($action, 'rebuild') == 0){
67                 rebuildDB($db);
68                 exitWithResponseCode("CEA95615-649C-4837-9E24-0C968FA57647", "OK");
69         } else if(strcmp($action, 'getallnode') == 0){
70                 $stmt = $db->prepare(QUERY_SELECT_ALL_Node);
71                 $stmt->execute();
72                 //
73                 $stmt->store_result();
74                 if($stmt->errno != 0){
75                         exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
76                 }
77                 
78                 $stmt->bind_result($uuid, $typeid, $ident);
79                 while($stmt->fetch()){
80                         $uuid = strtolower($uuid);
81                         echo('["');
82                         echo(getFormedUUIDString($uuid));
83                         echo('","');
84                         echo(getFormedUUIDString($typeid));
85                         echo('","');
86                         echo($ident);
87                         echo('"]');
88                         echo(PHP_EOL);
89                 }
90                 $stmt->close();
91                 exit();
92         } else if(strcmp($action, 'viewallnode') == 0){
93                 $stmt = $db->prepare(QUERY_SELECT_ALL_Node);
94                 $stmt->execute();
95                 //
96                 $stmt->store_result();
97                 if($stmt->errno != 0){
98                         exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
99                 }
100                 
101                 $stmt->bind_result($uuid, $typeid, $ident);
102                 while($stmt->fetch()){
103                         $uuid = strtolower($uuid);
104                         echo('["');
105                         echo(getFormedUUIDString($uuid));
106                         echo('","');
107                         echo(getFormedUUIDString($typeid));
108                         echo('","');
109                         echo($ident);
110                         echo('"]');
111                         echo("<br />");
112                 }
113                 echo($stmt->num_rows);
114                 $stmt->close();
115                 exit(" OK");
116         } else if(strcmp($action, 'addnode') == 0){
117                 if(isset($_GET['nodeid'])){
118                         $uuid = $_GET['nodeid'];
119                 } else{
120                         exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid needed.");
121                 }
122                 if(isset($_GET['typeid'])){
123                         $typeid = $_GET['typeid'];
124                 } else{
125                         exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
126                 }
127                 if(isset($_GET['ident'])){
128                         $ident = $_GET['ident'];
129                 } else{
130                         exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "ident needed.");
131                 }
132
133                 $stmt = $db->prepare(QUERY_ADD_Node);
134                 $mts = getTimeStampMs();
135                 $stmt->bind_param(QUERY_ADD_Node_TYPES, $nodeid, $typeid, $ident);
136                 $stmt->execute();
137                 //
138                 $stmt->store_result();
139                 if($stmt->errno != 0){
140                         exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db));
141                 }
142                 $stmt->close();
143                 exitWithResponseCode("CEA95615-649C-4837-9E24-0C968FA57647", "OK");
144         } else if(strcmp($action, 'saytest') == 0){
145                 /*
146                 //for Mac OSX say command.
147                 system("say " . escapeshellarg("sayのテストをしています。"));
148                 */
149         }
150 }
151
152 //NOP error
153 exitWithResponseCode("B539657C-0FA6-49C2-AFB0-13AF5C7866ED");
154
155 function exitWithResponseCode($errid, $description = "")
156 {
157         die('["' . $errid .'","' . $description . '"]');
158 }
159
160 function connectDB()
161 {
162         $db = new mysqli('localhost', DATABASE_USER, DATABASE_PWD, DATABASE_NAME);
163         
164         if (mysqli_connect_error()) {
165                 // DB connect error
166                 exitWithResponseCode("3A8CF3C8-E6B6-4A99-9134-343CA341B591", mysqli_connect_error());
167         }
168         
169         // 文字化け防止
170         $db->set_charset("utf8");
171         
172         //データベース存在確認
173         $stmt = $db->prepare("show tables");
174         $stmt->execute();
175         //
176         $stmt->store_result();
177         if($stmt->errno != 0){
178                 exitWithResponseCode("80FA2D65-9473-40B0-A3CE-159AE8E67017");
179         }
180         //テーブルの存在確認
181         $stmt->bind_result($tablename);
182         $found = 0;
183         while($stmt->fetch()){
184                 if($tablename == "Node"){
185                         $found |= 1;
186                 }
187                 if($tablename == "Edge"){
188                         $found |= 2;
189                 }
190         }
191         if(($found & 3) != 3){
192                 rebuildDB($db);
193         }
194         $stmt->close();
195         
196         return $db;
197 }
198
199 function rebuildDB($db)
200 {
201         // すでにあるテーブルの削除
202         
203         // Node
204         $stmt = $db->prepare("drop table if exists Node");
205         $stmt->execute();
206         // エラーチェック省略
207         $stmt->close();
208         
209         // Edge
210         $stmt = $db->prepare("drop table if exists Edge");
211         $stmt->execute();
212         // エラーチェック省略
213         $stmt->close();
214         
215         // 再構築
216         
217         // Node
218         $stmt = $db->prepare(QUERY_CREATE_TABLE_Node);
219         $stmt->execute();
220         // エラーチェック省略
221         $stmt->close();
222         
223         // Edge
224         $stmt = $db->prepare(QUERY_CREATE_TABLE_Edge);
225         $stmt->execute();
226         // エラーチェック省略
227         $stmt->close();
228 }
229
230 function getFormedUUIDString($str)
231 {
232         $str = strtolower($str);
233         return (
234                 substr($str, 0, 8) . "-" . 
235                 substr($str, 8, 4) . "-" . 
236                 substr($str, 12, 4) . "-" . 
237                 substr($str, 16, 4) . "-" . 
238                 substr($str, 20, 12)
239         );
240 }
241
242 function getTimeStampMs()
243 {
244         return ceil(microtime(true)*1000);
245 }
246 ?>