PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/wwwsqldesigner-2.6/backend/php-pdo/index.php

https://bitbucket.org/Muntaser/cs425_project
PHP | 417 lines | 372 code | 36 blank | 9 comment | 29 complexity | 601246967367c1cb8bef510406ee1303 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. error_reporting(E_ALL);
  3. class BackendPhpPdo
  4. {
  5. public static function getConfig($id)
  6. {
  7. switch($id)
  8. {
  9. case 'saveloadlist':
  10. $type = 'pgsql';
  11. $dsn = 'pgsql:dbname=wwwsqldesigner';
  12. $user = 'wwwsqldesigner';
  13. $pass = 'xxx';
  14. $table = 'wwwsqldesigner';
  15. return array($type,$dsn,$user,$pass,$table);
  16. case 'import':
  17. $type = 'pgsql';
  18. $dsn = 'pgsql:dbname=wwwsqldesigner;host=localhost';
  19. $type = 'mysql';
  20. $dsn = 'mysql:dbname=wwwsqldesigner;host=localhost';
  21. $user = 'wwwsqldesigner';
  22. $pass = 'xxx';
  23. return array($type,$dsn,$user,$pass);
  24. }
  25. }
  26. public $what = '';
  27. # PDO
  28. public $type = NULL;
  29. public $pdo = NULL;
  30. # PDO STATEMENT
  31. public $pdo_statement = NULL;
  32. public $req = '';
  33. public $pos = 0;
  34. # EXCEPTION
  35. public $message = NULL;
  36. public $trace = array();
  37. # BENCH
  38. public $in = 0;
  39. public $out = 0;
  40. public static $benchs = array();
  41. # Constructors
  42. public static function getPdo($type,$dsn,$user,$password)
  43. {
  44. $obj = new BackendPhpPdo();
  45. $obj->what = 'PDO';
  46. $obj->type = $type;
  47. try {
  48. $obj->pdo = new PDO($dsn,$user,$password);
  49. } catch( Exception $e ) {
  50. self::getException($e);
  51. }
  52. if(empty($obj->pdo))
  53. self::getException($obj);
  54. return $obj;
  55. }
  56. public static function getPdoStatement($pdo=NULL,$pdo_statement=NULL,$req='',$in=NULL)
  57. {
  58. $obj = new BackendPhpPdo();
  59. $obj->what = 'PDO_STATEMENT';
  60. $obj->pdo = $pdo;
  61. $obj->pdo_statement = $pdo_statement;
  62. $obj->req = $req;
  63. $obj->in = $in;
  64. if(empty($obj->pdo_statement))
  65. self::getException($obj);
  66. return $obj;
  67. }
  68. public static function getException($message)
  69. {
  70. $trace = debug_backtrace();
  71. if(is_object($message))
  72. {
  73. if($message instanceOf Exception)
  74. $trace = $message->getTrace();
  75. $message = $message->getMessage();
  76. }
  77. $obj = new BackendPhpPdo();
  78. $obj->what = 'EXCEPTION';
  79. $obj->message = $message;
  80. $obj->trace = $trace;
  81. header("HTTP/1.0 500 Internal Server Error");
  82. echo $obj->getMessage();
  83. die();
  84. }
  85. public static function getBench($in=NULL,$message='')
  86. {
  87. $t = microtime(true);
  88. if(is_null($in))
  89. return $t;
  90. $obj = new BackendPhpPdo();
  91. $obj->what = 'BENCH';
  92. $obj->in = $in;
  93. $obj->out = $t;
  94. $obj->message = $message;
  95. self::$benchs[] = $obj;
  96. return $t;
  97. }
  98. # PDO
  99. public function prepare($req)
  100. {
  101. $in = self::getBench();
  102. $args = func_get_args();
  103. try {
  104. $pdo_statement = call_user_func_array(array($this->pdo,'prepare'),$args);
  105. } catch (Exception $e) {
  106. self::getException($e);
  107. }
  108. return self::getPDOStatement($this->pdo,$pdo_statement,$req,$in);
  109. }
  110. public function query($req)
  111. {
  112. $in = self::getBench();
  113. $args = func_get_args();
  114. try {
  115. $pdo_statement = call_user_func_array(array($this->pdo,'query'),$args);
  116. } catch (Exception $e) {
  117. self::getException($e);
  118. }
  119. return self::getPDOStatement($this->pdo,$pdo_statement,$req,$in);
  120. }
  121. public static $dones = array();
  122. public function getClass()
  123. {
  124. if(empty(self::$dones))
  125. require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'AbstractLayer.php');
  126. AbstractLayer::$pdo = $this;
  127. $class = 'Layer'.strtoupper($this->type);
  128. if(isset(self::$dones[$class]))
  129. return $class;
  130. $file = dirname(__FILE__).DIRECTORY_SEPARATOR.$class.'.php';
  131. if(!file_exists($file))
  132. self::getException('Layer not implemented : '.$this->type);
  133. require_once($file);
  134. self::$dones[$class] = 1;
  135. return $class;
  136. }
  137. public function getLayer()
  138. {
  139. $class = $this->getClass();
  140. $args = func_get_args();
  141. $method = str_replace(' ','',ucwords(str_replace('-',' ',array_shift($args))));
  142. if(!method_exists($class,$method))
  143. self::getException($class.'::'.$method.' not implemented');
  144. return call_user_func_array(array($class,$method),$args);
  145. }
  146. # PDO STATEMENT
  147. public function execute()
  148. {
  149. $count = count(explode('?',$this->req))-1;
  150. if($count != $this->pos)
  151. self::getException('Not enough or too much bindValue in regards to the number of "?" : '.$this->req.' ('.$this->pos.';'.$count.')');
  152. $this->pos = 0;
  153. $args = func_get_args();
  154. $status = call_user_func_array(array($this->pdo_statement,'execute'),$args);
  155. self::getBench($this->in,'execute');
  156. if(!$status)
  157. self::getException($this);
  158. return $this;
  159. }
  160. public function fetchName()
  161. {
  162. $args = func_get_args();
  163. $args[] = PDO::FETCH_ASSOC;
  164. $results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
  165. return $results;
  166. }
  167. public function fetchNum()
  168. {
  169. $args = func_get_args();
  170. $args[] = PDO::FETCH_NUM;
  171. $results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
  172. return $results;
  173. }
  174. public function fetchOne()
  175. {
  176. $args = func_get_args();
  177. $results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
  178. foreach($results as $k=>$result)
  179. if(count($result) == 2)
  180. $results[$k] = $result[0];
  181. return $results;
  182. }
  183. public function fetchAll()
  184. {
  185. $args = func_get_args();
  186. $results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
  187. self::getBench($this->in,'fetch');
  188. return $results;
  189. }
  190. public function bindValue($value,$type)
  191. {
  192. switch($type)
  193. {
  194. case 'int': $type = PDO::PARAM_INT; break;
  195. case 'string': $type = PDO::PARAM_STR; break;
  196. case 'resource': $type = PDO::PARAM_LOB; break;
  197. default: self::getException('Unknown type : '.$type); break;
  198. }
  199. $this->pdo_statement->bindValue(++$this->pos,$value,$type);
  200. return $this;
  201. }
  202. # General
  203. public function getMessage()
  204. {
  205. switch($this->what)
  206. {
  207. case 'EXCEPTION':
  208. $trace = array();
  209. foreach($this->trace as $e)
  210. {
  211. if(!isset($e['file'])) $e['file'] = '<i>user_func</i>';
  212. if(!isset($e['line'])) $e['line'] = '0';
  213. if(!isset($e['class'])) $e['class'] = '';
  214. if(!isset($e['type'])) $e['type'] = '';
  215. $trace[] = '<small>'.$e['file'].':'.$e['line'].'</small> '.$e['class'].$e['type'].$e['function'];
  216. }
  217. return '<strong>'.$this->message.'</strong>'."\n".'<hr />'."\n".implode('<br />'."\n",$trace);
  218. case 'PDO':
  219. if(empty($this->pdo))
  220. return 'Fail to connect.';
  221. $error = $this->pdo->errorInfo();
  222. return $error[0].':'.$error[1].' <pre>'.$error[2].'</pre><pre>'.$this->req.'</pre>';
  223. case 'PDO_STATEMENT':
  224. if(empty($this->pdo_statement))
  225. $error = $this->pdo->errorInfo();
  226. else
  227. $error = $this->pdo_statement->errorInfo();
  228. return $error[0].':'.$error[1].' <pre>'.$error[2].'</pre><pre>'.$this->req.'</pre>';
  229. case 'BENCH':
  230. return (round(($this->out-$this->in)*1000)/1000).'s '.$this->message;
  231. }
  232. }
  233. public function getTrace()
  234. {
  235. switch($this->what)
  236. {
  237. case 'EXCEPTION':
  238. return $this->trace;
  239. default:
  240. return array();
  241. }
  242. }
  243. public static function fetchDatatypes($file)
  244. {
  245. if(!file_exists($file))
  246. {
  247. $header = '<'.'?xml version="1.0" ?'.'>';
  248. $datatypes = '';
  249. }
  250. else
  251. {
  252. $datatypes = file($file);
  253. $header = array_shift($datatypes);
  254. $datatypes = implode('',$datatypes);
  255. }
  256. return array($header,$datatypes);
  257. }
  258. public static function buildXML($pdo)
  259. {
  260. list($header,$datatypes) = self::fetchDatatypes($pdo->getLayer('datatypes-file'));
  261. $xml = array();
  262. $xml[] = $header;
  263. $xml[] = '<sql db="'.$pdo->type.'">';
  264. $xml[] = $datatypes;
  265. $tables = $pdo->getLayer('tables');
  266. foreach($tables as $table)
  267. {
  268. $xmltable = '<table name="'.$table['name'].'">';
  269. if(!empty($table['comment']))
  270. $xmltable .= '<comment>'.$table['comment'].'</comment>';
  271. $columns = $pdo->getLayer('columns',$table);
  272. foreach($columns as $column)
  273. {
  274. $xmlcolumn = '<row name="'.$column['name'].'" null="'.$column['null'].'" autoincrement="'.$column['autoincrement'].'">';
  275. $xmlcolumn .= '<datatype>'.strtoupper($column['type']).'</datatype>';
  276. $xmlcolumn .= '<default>'.$column['default'].'</default>';
  277. if(!empty($column['comment']))
  278. $xmlcolumn .= '<comment>'.$column['comment'].'</comment>';
  279. $relations = $pdo->getLayer('relations',$table,$column);
  280. foreach($relations as $relation)
  281. $xmlcolumn .= '<relation table="'.$relation['table'].'" row="'.$relation['column'].'" />';
  282. $xmlcolumn .= '</row>';
  283. $xmltable .= $xmlcolumn;
  284. }
  285. $keys = $pdo->getLayer('keys',$table);
  286. foreach($keys as $name=>$key)
  287. {
  288. $xmlkey = '<key name="'.$name.'" type="'.$key['type'].'">';
  289. foreach($key['columns'] as $column)
  290. if(!empty($column))
  291. $xmlkey .= '<part>'.$column.'</part>';
  292. $xmlkey .= '</key>';
  293. $xmltable .= $xmlkey;
  294. }
  295. $xmltable .= '</table>';
  296. $xml[] = $xmltable;
  297. }
  298. $xml[] = '</sql>';
  299. return implode("\n",$xml);
  300. }
  301. public static function actionList()
  302. {
  303. list($type,$dsn,$user,$pass,$table) = self::getConfig('saveloadlist');
  304. $pdo = self::getPdo($type,$dsn,$user,$pass);
  305. $r = $pdo->getLayer('keywords',$table)->fetchOne();
  306. foreach($r as $keyword)
  307. echo $keyword."\n";
  308. }
  309. public static function actionSave($key='keyword')
  310. {
  311. $keyword = self::req($key);
  312. $data = file_get_contents("php://input");
  313. if (get_magic_quotes_gpc() || get_magic_quotes_runtime())
  314. $data = stripslashes($data);
  315. list($type,$dsn,$user,$pass,$table) = self::getConfig('saveloadlist');
  316. $pdo = self::getPdo($type,$dsn,$user,$pass);
  317. $r = $pdo->getLayer('keywords',$table,$keyword)->fetchOne();
  318. if(count($r) > 0)
  319. $pdo->getLayer('keywords-update',$table,$keyword,$data);
  320. else
  321. $pdo->getLayer('keywords-insert',$table,$keyword,$data);
  322. header("HTTP/1.0 201 Created");
  323. }
  324. public static function actionLoad($key='keyword')
  325. {
  326. $keyword = self::req($key);
  327. list($type,$dsn,$user,$pass,$table) = self::getConfig('saveloadlist');
  328. $pdo = self::getPdo($type,$dsn,$user,$pass);
  329. $r = $pdo->getLayer('keywords',$table,$keyword)->fetchOne();
  330. if(count($r) > 0)
  331. {
  332. header("Content-type: text/xml");
  333. echo $r[0];
  334. }
  335. else
  336. header("HTTP/1.0 404 Not Found");
  337. die();
  338. }
  339. public static function actionImport()
  340. {
  341. ob_start();
  342. list($type,$dsn,$user,$pass) = self::getConfig('import');
  343. $pdo = self::getPdo($type,$dsn,$user,$pass);
  344. $xml = self::buildXML($pdo);
  345. if(!ob_get_contents())
  346. header("Content-type: text/xml");
  347. echo $xml;
  348. die();
  349. }
  350. public static function req($key)
  351. {
  352. return isset($_REQUEST[$key])?$_REQUEST[$key]:NULL;
  353. }
  354. public static function controler($key='action')
  355. {
  356. $action = self::req($key);
  357. switch(strtolower($action))
  358. {
  359. case 'list':
  360. return self::actionList();
  361. case 'save':
  362. return self::actionSave();
  363. case 'load':
  364. return self::actionLoad();
  365. case 'import':
  366. return self::actionImport();
  367. default:
  368. header("HTTP/1.0 501 Not Implemented");
  369. echo 'Action not implemented';
  370. die();
  371. }
  372. }
  373. }
  374. function bench()
  375. {
  376. foreach(BackendPhpPdo::$benchs as $bench)
  377. echo $bench->getMessage().'<br />';
  378. }
  379. //register_shutdown_function('bench');
  380. BackendPhpPdo::controler();
  381. ?>