PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/powerdb/class.powerdb.php

https://bitbucket.org/davidwik/powerdb
PHP | 1341 lines | 989 code | 229 blank | 123 comment | 90 complexity | 9321f8f1cfbbd491932fce9f993ad285 MD5 | raw file
  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /**
  7. * Description of class
  8. *
  9. * @author david
  10. */
  11. class PowerDB {
  12. /**
  13. * The input data
  14. * @var array
  15. */
  16. protected $data;
  17. protected $outputString;
  18. protected $siblingOutputString;
  19. protected $objectName;
  20. protected $siblingsArr = array();
  21. protected $pkName;
  22. protected $tableName;
  23. const TABLE_PREFIX = "power_";
  24. const TYPE_POSTGRES = 0;
  25. const TYPE_MYSQL = 1;
  26. const TYPE_SQLITE = 2;
  27. public static function getTypes(){
  28. $json = file_get_contents("types.json");
  29. return json_decode($json);
  30. }
  31. public function generateFromPostData($data){
  32. // Clean up stuff that is empty
  33. foreach($data['property'] as $key => $value){
  34. if(strlen($value) <= 1){
  35. unset($data['property'][$key]);
  36. unset($data['type'][$key]);
  37. }
  38. }
  39. $this->data = $data;
  40. $this->objectName = $data['objectName'];
  41. $this->tableName = self::TABLE_PREFIX . strtolower($data['objectName']);
  42. $this->startBody();
  43. $this->registerSiblings();
  44. $this->addProperties();
  45. $this->generateConstructor();
  46. $this->generateTableString();
  47. $this->addGet();
  48. $this->addSaveNewFunction();
  49. $this->addUpdateFunction();
  50. $this->generateListFromResults();
  51. $this->addSave();
  52. $this->addDelete();
  53. $this->generateChildLinks();
  54. $this->generateSiblingMethods();
  55. $this->generateSaveChildren();
  56. $this->generateParentLinks();
  57. $this->generateDeleteChildrenMethods();
  58. $this->addGetListFunction();
  59. $this->addGetStaticListFunction();
  60. $this->fetchFromId();
  61. $this->addGetJSON();
  62. $this->addGetColumnNames();
  63. $this->addPropertyArray();
  64. $this->updateTable();
  65. $this->updateTableColumns();
  66. $this->dropTableColumns();
  67. $this->addTableColumns();
  68. $this->addTableExists();
  69. $this->endBody();
  70. return $this->outputString;
  71. }
  72. protected function registerSiblings(){
  73. foreach($this->data['type'] as $key => $val){
  74. if($val == "{SIBLING}"){
  75. $this->siblingsArr[] = array($this->objectName, $this->data['property'][$key]);
  76. }
  77. }
  78. }
  79. protected function createMapName(array $array){
  80. sort($array);
  81. $name = "";
  82. foreach($array as $a){
  83. $name .= $a;
  84. }
  85. return $name . "Map";
  86. }
  87. protected function addDelete(){
  88. $str = "\n\n\t/**";
  89. $str .= "\n\t * Deletes the object from the database";
  90. $str .= "\n\t * @param bool \$deep Delete all it's children";
  91. $str .= "\n\t * @param bool \$across Delete all SIBLING relations";
  92. $str .= "\n\t */";
  93. $str .= "\n\tpublic function delete(\$deep = false, \$across = false) {";
  94. $str .= "\n\t\tif(\$deep == true){";
  95. $str .= $this->generateDeleteChildTriggers();
  96. $str .= "\n\t\t}";
  97. $str .= $this->generateDeleteSiblingRelations();
  98. $str .= "\n\n\t\t\$statementString['mysql'] = \"DELETE FROM " . $this->tableName . " where " . strtolower($this->pkName) . "=? LIMIT 1\";";
  99. $str .= "\n\t\t\$statementString['pgsql'] = \"DELETE FROM " . $this->tableName . " where " . strtolower($this->pkName) . "=?\";";
  100. $str .= "\n\t\t\$stmt = Database::getInstance()->connection->prepare(\$statementString[Config::getInstance()->databaseDriver]);";
  101. $str .= "\n\t\t\$stmt->bindValue(1, \$this->" . $this->pkName . ");";
  102. $str .= "\n\t\treturn \$stmt->execute();";
  103. $str .= "\n\t}";
  104. $this->outputString .= $str;
  105. }
  106. protected function generateDeleteSiblingRelations(){
  107. $str = "";
  108. foreach($this->data['type'] as $key => $val){
  109. if($val == "{SIBLING}"){
  110. $tableName = $this->getSiblingTableFromKey($key);
  111. $str .= "\n\n\t\t// Deletes the saved relations with " . $this->data['property'][$key];
  112. $str .= "\n\t\t\$statementString['pgsql'] =\"DELETE FROM " . $tableName . " WHERE " . strtolower($this->pkName) . "=?\";";
  113. $str .= "\n\t\t\$statementString['mysql'] =\"DELETE FROM " . $tableName . " WHERE " . strtolower($this->pkName) . "=?\";";
  114. $str .= "\n\t\t\$stmt = Database::getInstance()->connection->prepare(\$statementString[Config::getInstance()->databaseDriver]);";
  115. $str .= "\n\t\t\$stmt->bindValue(1, \$this->" . $this->pkName . ");";
  116. $str .= "\n\t\t\$stmt->execute();";
  117. }
  118. }
  119. return $str;
  120. }
  121. protected function addGet(){
  122. $str = "\n\n\t/**";
  123. $str .= "\n\t * Loads the properties from database and returns object";
  124. $str .= "\n\t * @param integer \$key The primary key";
  125. $str .= "\n\t * @return " . $this->objectName ."|null";
  126. $str .= "\n\t */";
  127. $str .= "\n\tpublic function get(\$key){";
  128. $str .= "\n\t\tparent::get(\$key);";
  129. $str .= "\n\t\t\$key = (int) \$key;";
  130. $str .= "\n\t\t\$result = \$this->getList(array(array('" . strtolower($this->pkName) . "', '=', \$key)), false, false, 1);";
  131. $str .= "\n\t\t\$obj = array_pop(\$result);";
  132. $str .= "\n\t\tif(!\$obj){";
  133. $str .= "\n\t\t\treturn null;";
  134. $str .= "\n\t\t}";
  135. $str .= "\n\t\tforeach(\$obj as \$k => \$v){";
  136. $str .= "\n\t\t\t\$this->\$k = \$v;";
  137. $str .= "\n\t\t}";
  138. $str .= "\n\t\treturn \$obj;";
  139. $str .= "\n\t}";
  140. $this->outputString .= $str;
  141. }
  142. protected function generateSiblingArrays(){
  143. $str = "";
  144. foreach($this->data['type'] as $key => $val){
  145. if($val == "{SIBLING}"){
  146. $str .= $this->addSiblingArray($key);
  147. }
  148. }
  149. $this->outputString .= $str;
  150. }
  151. protected function addSiblingArray($key) {
  152. $str = "\n\n\t/**";
  153. $str .= "\n\t * Holding the sibling relations";
  154. $str .= "\n\t * @var array";
  155. $str .= "\n\t */";
  156. $str .= "\n\tprotected $" . $this->getVarName($key) . "List;";
  157. return $str;
  158. }
  159. protected function addPropertyArray(){
  160. $str = "\n\n\t/**";
  161. $str .= "\n\t * Returns a list of properties for table updates";
  162. $str .= "\n\t * @return array";
  163. $str .= "\n\t */";
  164. $str .= "\n\tprotected static function getPropertyArray(){";
  165. $str .= "\n\t\t\$properties = array();";
  166. foreach($this->data['property'] as $key => $value){
  167. switch($this->getType(self::TYPE_MYSQL, $this->data['type'][$key])){
  168. case "{CHILD}":
  169. break;
  170. case "{SIBLING}":
  171. break;
  172. default:
  173. $str .= "\n\n\t\t\$col = new stdClass();";
  174. $str .= "\n\t\t\$col->name = '" . $this->getDBName($key) . "';";
  175. $str .= "\n\t\t\$col->datatype = array();";
  176. $str .= "\n\t\t\$col->datatype['mysql'] = '" . $this->getType(self::TYPE_MYSQL, $this->data['type'][$key]) . "';";
  177. $str .= "\n\t\t\$col->datatype['pgsql'] = '" . $this->getType(self::TYPE_POSTGRES, $this->data['type'][$key]) . "';";
  178. $str .= "\n\t\t\$properties[] = \$col;";
  179. }
  180. }
  181. $str .= "\n\n\t\treturn \$properties;";
  182. $str .= "\n\t\t";
  183. $str .= "\n\t}";
  184. $this->outputString .= $str;
  185. }
  186. protected function updateTable(){
  187. $str = "\n\n\t/**";
  188. $str .= "\n\t *";
  189. $str .= "\n\t */";
  190. $str .= "\n\tfinal public static function updateTable(){";
  191. $str .= "\n\t\t\$dbColNames = self::getColumnNames();";
  192. $str .= "\n\t\t\$pkKey = array_search('" . strtolower($this->pkName) . "', \$dbColNames);";
  193. $str .= "\n\t\tunset(\$dbColNames[\$pkKey]);";
  194. $str .= "\n\t\t\$properties = self::getPropertyArray();";
  195. $str .= "\n\t\t\$newProperties = array();";
  196. $str .= "\n\t\t\$updateProperties = array();";
  197. $str .= "\n\t\t\$newNameList = array();";
  198. $str .= "\n\t\t\$dropProperties = array();";
  199. $str .= "\n\t\tforeach(\$properties as \$p){";
  200. $str .= "\n\t\t\tif(in_array(\$p->name, \$dbColNames)){";
  201. $str .= "\n\t\t\t\t\$updateProperties[] = \$p;";
  202. $str .= "\n\t\t\t}";
  203. $str .= "\n\t\t\telse {";
  204. $str .= "\n\t\t\t\t\$newProperties[] = \$p;";
  205. $str .= "\n\t\t\t}";
  206. $str .= "\n\t\t\t\$newNameList[] = \$p->name;";
  207. $str .= "\n\t\t}";
  208. $str .= "\n\n\t\tforeach(\$dbColNames as \$c){";
  209. $str .= "\n\t\t\tif(!in_array(\$c, \$newNameList)){";
  210. $str .= "\n\t\t\t\t\$dropProperties[] = \$c;";
  211. $str .= "\n\t\t\t}";
  212. $str .= "\n\t\t}";
  213. $str .= "\n\n\t\tself::updateColumns(\$updateProperties);";
  214. $str .= "\n\t\tself::addColumns(\$newProperties);";
  215. $str .= "\n\t\tself::dropColumns(\$dropProperties);";
  216. $str .= "\n\t}";
  217. $this->outputString .= $str;
  218. }
  219. protected function updateTableColumns(){
  220. $str = "\n\n\t/**";
  221. $str .= "\n\t *";
  222. $str .= "\n\t */";
  223. $str .= "\n\tprotected static function updateColumns(array \$list){";
  224. $str .= "\n\t\tforeach(\$list as \$l){";
  225. $str .= "\n\t\t\ttry {";
  226. $str .= "\n\t\t\t\t\$statement['mysql'] = \"ALTER TABLE " . $this->tableName . " CHANGE \" . \$l->name . \" \" . \$l->name . \" \" . \$l->datatype[Config::getInstance()->databaseDriver];";
  227. $str .= "\n\t\t\t\t\$statement['pgsql'] = \"ALTER TABLE " . $this->tableName . " ALTER COLUMN \" . \$l->name . \" TYPE \" . \$l->datatype[Config::getInstance()->databaseDriver];";
  228. $str .= "\n\t\t\t\t\$stmt = Database::getInstance()->connection->prepare(\$statement[Config::getInstance()->databaseDriver]);";
  229. $str .= "\n\t\t\t\t\$stmt->execute();";
  230. $str .= "\n\t\t\t}";
  231. $str .= "\n\t\t\tcatch(PDOException \$e){";
  232. $str .= "\n\t\t\t\tthrow \$e;";
  233. $str .= "\n\t\t\t}";
  234. $str .= "\n\t\t}";
  235. $str .="\n\t}";
  236. $this->outputString .= $str;
  237. }
  238. protected function addTableColumns(){
  239. $str = "\n\n\t/**";
  240. $str .= "\n\t *";
  241. $str .= "\n\t */";
  242. $str .= "\n\tprotected static function addColumns(array \$list){";
  243. $str .= "\n\t\tforeach(\$list as \$l){";
  244. $str .= "\n\t\t\ttry {";
  245. $str .= "\n\t\t\t\t\$statement['mysql'] = \"ALTER TABLE " . $this->tableName . " ADD \" . \$l->name . \" \" . \$l->datatype[Config::getInstance()->databaseDriver];";
  246. $str .= "\n\t\t\t\t\$statement['pgsql'] = \"ALTER TABLE " . $this->tableName . " ADD COLUMN \" . \$l->name . \" \" . \$l->datatype[Config::getInstance()->databaseDriver];";
  247. $str .= "\n\t\t\t\t\$stmt = Database::getInstance()->connection->prepare(\$statement[Config::getInstance()->databaseDriver]);";
  248. $str .= "\n\t\t\t\t\$stmt->execute();";
  249. $str .= "\n\t\t\t}";
  250. $str .= "\n\t\t\tcatch(PDOException \$e){";
  251. $str .= "\n\t\t\t\tthrow \$e;";
  252. $str .= "\n\t\t\t}";
  253. $str .= "\n\t\t}";
  254. $str .="\n\t}";
  255. $this->outputString .= $str;
  256. }
  257. protected function dropTableColumns(){
  258. $str = "\n\n\t/**";
  259. $str .= "\n\t *";
  260. $str .= "\n\t */";
  261. $str .= "\n\tprotected static function dropColumns(array \$list){";
  262. $str .= "\n\t\tforeach(\$list as \$l){";
  263. $str .= "\n\t\t\ttry {";
  264. $str .= "\n\t\t\t\t\$statement['mysql'] = \"ALTER TABLE " . $this->tableName . " DROP \" . \$l;";
  265. $str .= "\n\t\t\t\t\$statement['pgsql'] = \"ALTER TABLE " . $this->tableName . " DROP COLUMN \" . \$l . \" RESTRICT\";";
  266. $str .= "\n\t\t\t\t\$stmt = Database::getInstance()->connection->prepare(\$statement[Config::getInstance()->databaseDriver]);";
  267. $str .= "\n\t\t\t\t\$stmt->execute();";
  268. $str .= "\n\t\t\t}";
  269. $str .= "\n\t\t\tcatch(PDOException \$e){";
  270. $str .= "\n\t\t\t\tthrow \$e;";
  271. $str .= "\n\t\t\t}";
  272. $str .= "\n\t\t}";
  273. $str .="\n\t}";
  274. $this->outputString .= $str;
  275. }
  276. /**
  277. *
  278. */
  279. protected function addProperties(){
  280. $objName = $this->data['objectName'];
  281. $this->objectName = $objName;
  282. $objName[0] = strtolower($objName[0]);
  283. $objName .= "Id";
  284. $this->pkName = $objName;
  285. $encodedData = urlencode(base64_encode(gzcompress(json_encode($this->data),9)));
  286. $this->outputString .= "\n\n\t/**";
  287. $this->outputString .= "\n\t * PowerDB object link";
  288. $this->outputString .= "\n\t * @var string";
  289. $this->outputString .= "\n\t */";
  290. $this->outputString .= "\n\tprivate \$objectURL = ";
  291. if($_SERVER['SERVER_PORT'] != "80"){
  292. $this->outputString .= "\"http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/" . Router::getInstance()->property()->area . "?data=" . $encodedData . "\";";
  293. }
  294. else {
  295. $this->outputString .= "\"http://" . $_SERVER['SERVER_NAME'] . "/" . Router::getInstance()->property()->area . "?data=" . $encodedData . "\";";
  296. }
  297. $this->outputString .= "\n\n\t/**";
  298. $this->outputString .= "\n\t * " . $objName . " - This model's primary key";
  299. $this->outputString .= "\n\t * @var integer";
  300. $this->outputString .= "\n\t */";
  301. $this->outputString .= "\n\tpublic $" . $objName . ";";
  302. $this->generateChildArrays();
  303. $this->generateSiblingArrays();
  304. foreach($this->data['property'] as $key => $val){
  305. if($this->data['property'][$key]){
  306. if($this->checkIfInclude($key)){
  307. $this->outputString .= "\n\n\t/**";
  308. $this->outputString .= "\n\t * " . $this->data['property'][$key];
  309. $this->outputString .= "\n\t * @var " . self::getPHPType($this->data['type'][$key]) . "";
  310. $this->outputString .= "\n\t */";
  311. $t = self::getPHPType($this->data['type'][$key]);
  312. $this->outputString .= "\n\tpublic " . $this->getVarName($key, false);
  313. if($t == "integer"){
  314. $this->outputString .= " = 0";
  315. }
  316. else if($t == "float"){
  317. $this->outputString .= " = 0.0";
  318. }
  319. $this->outputString .= ";";
  320. }
  321. }
  322. }
  323. }
  324. protected function generateChildArrays(){
  325. foreach($this->data['type'] as $key => $val){
  326. if($val == "{CHILD}"){
  327. $this->addChildArray($key);
  328. }
  329. }
  330. }
  331. protected function addChildArray($key){
  332. $objName = $this->data['property'][$key];
  333. $arrName = $this->getArrName($key);
  334. $str = "\n\n\t/**";
  335. $str .= "\n\t * An array of " . $objName . "s";
  336. $str .= "\n\t * @var array " . $arrName;
  337. $str .= "\n\t */";
  338. $str .= "\n\tprotected $" . $arrName . " = array();";
  339. $this->outputString .= $str;
  340. }
  341. protected function generateDeleteChildrenMethods(){
  342. foreach($this->data['type'] as $key => $val){
  343. if($val == "{CHILD}"){
  344. $this->addDeleteChildren($key);
  345. }
  346. }
  347. }
  348. protected function addDeleteChildren($key){
  349. $objName = $this->data['property'][$key];
  350. $str = "\n\n\t/**";
  351. $str .= "\n\t * Deletes in the list of " . $objName . " children.";
  352. $str .= "\n\t */";
  353. $str .= "\n\tpublic function delete" . $objName . "List(array \$requestArray, \$deep = false) {";
  354. $str .= "\n\t\t\$requestArray[] = array('" . strtolower($this->pkName) . "', '=', \$this->" . $this->pkName . ");";
  355. $str .= "\n\t\t\$objList = \$this->get" . $objName . "List(\$requestArray);";
  356. $str .= "\n\n\t\tforeach(\$objList as $" . $objName .") {";
  357. $str .= "\n\t\t\t\$" . $objName . "->delete(\$deep);";
  358. $str .= "\n\t\t}";
  359. $str .= "\n\t\t";
  360. $str .= "\n\t}";
  361. $this->outputString .= $str;
  362. }
  363. protected function createSiblingTables(){
  364. $str = "";
  365. foreach($this->siblingsArr as $array){
  366. // MySQL
  367. $tableName = strtolower(self::TABLE_PREFIX . $this->createMapName($array));
  368. $str .= "\n\n\t\t\$sql['mysql'][] = \"DROP TABLE " . $tableName . "\";";
  369. $str .= "\n\t\t\$sql['mysql'][] = \"CREATE TABLE " . $tableName . " (";
  370. foreach($array as $a){
  371. $str .= strtolower($a) . "id INT UNSIGNED, ";
  372. }
  373. $str = substr($str, 0,-2);
  374. $str .= ", PRIMARY KEY(";// ;((
  375. foreach($array as $a){
  376. $str .= strtolower($a) . "id,";
  377. }
  378. $str = substr($str, 0, -1) . "))\";";
  379. // PostGRE
  380. $str .= "\n\n\t\t\$sql['pgsql'][] = \"DROP TABLE " . $tableName . "\";";
  381. $str .= "\n\t\t\$sql['pgsql'][] = \"CREATE TABLE " . $tableName . " (";
  382. foreach($array as $a){
  383. $str .= strtolower($a) . "id INT, ";
  384. }
  385. $str = substr($str, 0,-2);
  386. $str .= ", PRIMARY KEY(";// ;((
  387. foreach($array as $a){
  388. $str .= strtolower($a) . "id,";
  389. }
  390. $str = substr($str, 0, -1) . "))\";";
  391. }
  392. $str .= "\n";
  393. return $str;
  394. }
  395. /**
  396. * Checks the variable name, and sees whether it should be renamed as an reference.
  397. * @param int $key
  398. * @return string
  399. */
  400. protected function getVarName($key, $includeThis = false){
  401. $name = "";
  402. switch($this->data['type'][$key]){
  403. case "{PARENT}":
  404. $name = $this->data['property'][$key];
  405. $name[0] = strtolower($name[0]);
  406. $name = $name . "Id";
  407. break;
  408. case "{SIBLING}":
  409. $name = $this->data['property'][$key];
  410. $name[0] = strtolower($name[0]);
  411. return $name;
  412. default:
  413. $name = $this->data['property'][$key];
  414. break;
  415. }
  416. if($includeThis){
  417. return "\$this->" . $name;
  418. }
  419. else {
  420. return "$" . $name;
  421. }
  422. }
  423. protected function generateChildLinks(){
  424. foreach($this->data['type'] as $key => $val){
  425. if($val == "{CHILD}"){
  426. $this->addGetChildList($key);
  427. $this->addAddChild($key);
  428. }
  429. }
  430. }
  431. protected function addGetChildList($key){
  432. $objName = $this->data['property'][$key];
  433. $str = "\n\n\t/**";
  434. $str .= "\n\t * Get a list of associated " . $objName . "s";
  435. $str .= "\n\t * @param array \$requestArray";
  436. $str .= "\n\t * @param string \$sortBy";
  437. $str .= "\n\t * @param bool \$descending";
  438. $str .= "\n\t * @param integer \$limit";
  439. $str .= "\n\t * @return array";
  440. $str .= "\n\t */";
  441. $str .= "\n\tpublic function get" . $objName . "List(array \$requestArray = array(), \$sortBy = false, \$descending = false, \$limit = false) {";
  442. $ObjId = strtolower($this->pkName);
  443. $str .= "\n\t\tif(!\$this->" . $this->pkName . ") {";
  444. $str .= "\n\t\t\treturn array();";
  445. $str .= "\n\t\t}";
  446. $str .= "\n\t\t\$requestArray[] = array('" . $ObjId ."', '=', \$this->" . $this->pkName .");";
  447. $str .= "\n\t\t\$" . $objName . " = new " . $objName . "();";
  448. $str .= "\n\t\t\$arrayList = $" . $objName ."->getList(\$requestArray, \$sortBy, \$descending, \$limit);";
  449. $str .= "\n\t\treturn \$arrayList;";
  450. $str .= "\n\t}";
  451. $this->outputString .= $str;
  452. }
  453. private function getArrName($key){
  454. $objName = $this->data['property'][$key];
  455. $arrName = $objName;
  456. $arrName[0] = strtolower($arrName[0]);
  457. $arrName .= "List";
  458. return $arrName;
  459. }
  460. protected function addAddChild($key){
  461. $objName = $this->data['property'][$key];
  462. $arrName = $this->getArrName($key);
  463. $str = "\n\n\t/**";
  464. $str .= "\n\t * Add a " . $objName;
  465. $str .= "\n\t * @param " . $objName . " $" . $objName;
  466. $str .= "\n\t */";
  467. $str .= "\n\tpublic function add" . $objName . "(" . $objName . " $" . $objName .") {";
  468. $str .= "\n\t\t\$this->" . $arrName . "[] = $" . $objName . ";";
  469. $str .= "\n\t}\n";
  470. $this->outputString .= $str;
  471. }
  472. protected function generateDeleteChildTriggers(){
  473. $str = "";
  474. foreach($this->data['type'] as $key => $val){
  475. $objName = $this->data['property'][$key];
  476. if($val == "{CHILD}"){
  477. $str .= "\n\t\t\t\$this->delete" . $objName . "List(array(array('" . $this->pkName . "', '=', \$this->" . $this->pkName . ")), \$deep);";
  478. }
  479. }
  480. return $str;
  481. }
  482. protected function generateSiblingMethods(){
  483. foreach($this->data['type'] as $key => $val){
  484. if($val == "{SIBLING}"){
  485. $this->outputString .= $this->addSiblingMethod($key);
  486. $this->outputString .= $this->getSiblingList($key);
  487. $this->outputString .= $this->addRemoveSiblingLink($key);
  488. }
  489. }
  490. }
  491. protected function getSiblingList($key){
  492. $targetTable = strtolower(self::TABLE_PREFIX . $this->data['property'][$key]);
  493. $mapTable = $this->getSiblingTableFromKey($key);
  494. $accessor = $this->getDBName($key);
  495. $str = "\n\n\t/**";
  496. $str .= "\n\t * Gets a list of " . $this->data['property'][$key];
  497. $str .= "\n\t * @param array \$requestArray";
  498. $str .= "\n\t * @param string \$sortBy";
  499. $str .= "\n\t * @param bool \$descending";
  500. $str .= "\n\t * @param integer \$limit";
  501. $str .= "\n\t * @return array";
  502. $str .= "\n\t */";
  503. $str .= "\n\tpublic function get" . $this->data['property'][$key] . "List(array \$requestArray = array(), \$sortBy = false, \$descending = false, \$limit = false){";
  504. $str .= "\n\t\tif(!\$this->" . $this->pkName . "){";
  505. $str .= "\n\t\t\treturn array();";
  506. $str .= "\n\t\t}";
  507. $str .= "\n\t\t\$statement = \"SELECT " . $targetTable . ".* FROM " . $mapTable . " LEFT JOIN " . $targetTable . " ON (" . $mapTable . "." . $accessor . "=" . $targetTable . "." . $accessor . ")\";";
  508. $str .= "\n\t\t\$requestArray[] = array('" . $mapTable . "." . strtolower($this->pkName) . "', '=', \$this->" . $this->pkName . ");";
  509. $str .= "\n\t\t\$statement .= \$this->appendConditions(\$requestArray, \$sortBy, \$descending, \$limit, \"" . $targetTable . "\");";
  510. $str .= "\n\t\t\$stmt = Database::getInstance()->connection->prepare(\$statement);";
  511. $str .= "\n\t\t\$cnt = 1;";
  512. $str .= "\n\t\tforeach(\$this->getBindArray() as \$bind){";
  513. $str .= "\n\t\t\t\$stmt->bindValue(\$cnt, \$bind);";
  514. $str .= "\n\t\t\t\$cnt++;";
  515. $str .= "\n\t\t}";
  516. $str .= "\n\t\t\$stmt->execute();";
  517. $str .= "\n\t\t\$res = \$stmt->fetchAll(PDO::FETCH_OBJ);";
  518. $str .= "\n\t\treturn " . $this->data['property'][$key] . "::createListFromResults(\$res);";
  519. $str .= "\n\t}";
  520. return $str;
  521. }
  522. protected function generateListFromResults(){
  523. $str = "\n\n\t/**";
  524. $str .= "\n\t * Returns a list of " . $this->objectName . " built from a list of responses.";
  525. $str .= "\n\t * @param array";
  526. $str .= "\n\t */";
  527. $str .= "\n\tpublic static function createListFromResults(array \$list){";
  528. $str .= "\n\t\t\$arrayList = array();";
  529. $str .= "\n\t\tforeach(\$list as \$row){";
  530. $str .= "\n\n\t\t\t\$o = new " . $this->objectName . "();";
  531. $str .= "\n\t\t\t\$o->" . $this->pkName . " = \$row->" . strtolower($this->pkName) . ";";
  532. foreach($this->data['property'] as $key => $val) {
  533. if($this->checkIfInclude($key)) {
  534. $str .= "\n\t\t\t\$o->" . substr($this->getVarName($key), 1) . " = \$row->" . $this->getDBName($key) . ";";
  535. }
  536. }
  537. $str .= "\n\t\t\t\$arrayList[] = \$o;";
  538. $str .= "\n\t\t}";
  539. $str .= "\n\t\treturn \$arrayList;";
  540. $str .= "\n\t}";
  541. $this->outputString .= $str;
  542. }
  543. protected function addSiblingMethod($key){
  544. $objectName = $this->data['property'][$key];
  545. $str = "\n\n\t/**";
  546. $str .= "\n\t * Adds a many-to-many relation with an " . $objectName . " object.";
  547. $str .= "\n\t * @param " . $objectName;
  548. $str .= "\n\t */";
  549. $str .= "\n\tpublic function add" . $objectName . "(" . $objectName . " $" . $objectName . ") {";
  550. $str .= "\n\t\t\$this->" . $this->getVarName($key, true) . "List[] = $" . $objectName . ";";
  551. $str .= "\n\t}";
  552. return $str;
  553. }
  554. protected function getSiblingTableFromKey($key){
  555. $objectName = $this->data['property'][$key];
  556. foreach($this->siblingsArr as $arr){
  557. if(in_array($objectName, $arr)){
  558. return strtolower(self::TABLE_PREFIX . $this->createMapName($arr));
  559. }
  560. }
  561. }
  562. protected function addTableExists(){
  563. $str = "\n\n\t/**";
  564. $str .= "\n\t * Checks whether a table exists in the database";
  565. $str .= "\n\t * @return boolean";
  566. $str .= "\n\t */";
  567. $str .= "\n\tfinal public static function tableExists(){";
  568. $str .= "\n\t\t$" . $this->objectName . " = new " . $this->objectName . "();";
  569. $str .= "\n\t\treturn $" . $this->objectName . "->checkTableExists(\"" . $this->tableName . "\");";
  570. $str .= "\n\t}";
  571. $this->outputString .= $str;
  572. }
  573. protected function addRemoveSiblingLink($key){
  574. $objectName = $this->data['property'][$key];
  575. $tableName = $this->getSiblingTableFromKey($key);
  576. $str = "\n\n\t/**";
  577. $str .= "\n\t * Removes a many-to-many relation with a passed " . $objectName . " object.";
  578. $str .= "\n\t * @param " . $objectName;
  579. $str .= "\n\t */";
  580. $str .= "\n\tpublic function remove" . $objectName . "Map(" . $objectName . " $" . $objectName .") {";
  581. $str .= "\n\t\tif($" . $objectName . "->" . $this->getVarName($key) . "Id && \$this->" . $this->pkName .") {";
  582. $str .= "\n\t\t\t\$statementString = \"DELETE FROM " . $tableName ." WHERE " . $this->getDBName($key) . "=? AND ". strtolower($this->pkName) ."=?\";";
  583. $str .= "\n\t\t\t\$stmt = Database::getInstance()->connection->prepare(\$statementString);";
  584. $str .= "\n\t\t\t\$stmt->bindValue(1, \$" . $objectName ."->" . $this->getVarName($key) ."Id);";
  585. $str .= "\n\t\t\t\$stmt->bindValue(2, \$this->" . $this->pkName . ");";
  586. $str .= "\n\t\t\treturn \$stmt->execute();";
  587. $str .= "\n\t\t}";
  588. $str .= "\n\t\telse {";
  589. $str .= "\n\t\t\tthrow new ErrorException(\"Failed to get the Id from the object " . $objectName ." or from the current object\");";
  590. $str .= "\n\t\t}";
  591. $str .= "\n\t\t";
  592. $str .= "\n\t}";
  593. return $str;
  594. }
  595. protected function generateParentLinks(){
  596. foreach($this->data['type'] as $key => $val){
  597. if($val == "{PARENT}"){
  598. $this->addGetParent($key);
  599. $this->addSetParent($key);
  600. }
  601. }
  602. }
  603. protected function generateSaveChildren(){
  604. foreach($this->data['type'] as $key => $val){
  605. if($val == "{CHILD}"){
  606. $this->addSaveChildren($key);
  607. }
  608. }
  609. }
  610. protected function generateSaveChildrenTriggers(){
  611. $str = "";
  612. foreach($this->data['type'] as $key => $val){
  613. if($val == "{CHILD}"){
  614. $str .= $this->addSaveChildrenTrigger($key);
  615. }
  616. }
  617. return $str;
  618. }
  619. protected function addSaveChildrenTrigger($key){
  620. $objName = $this->data['property'][$key];
  621. $str = "\n\t\t\$this->save" . $objName . "List();";
  622. return $str;
  623. }
  624. protected function addSaveChildren($key){
  625. $objName = $this->data['property'][$key];
  626. $arrName = $this->getArrName($key);
  627. $str = "\n\n\t/**";
  628. $str .= "\n\t * Saves all the related children of " . $objName ." and refer them to current object.";
  629. $str .= "\n\t */";
  630. $str .= "\n\tprotected function save" . $objName . "List() {";
  631. $str .= "\n\t\tforeach(\$this->" . $arrName . " as $" . $objName . ") {";
  632. $str .= "\n\t\t\t$" . $objName . "->" . $this->pkName . " = \$this->" .$this->pkName . ";";
  633. $str .= "\n\t\t\t$" . $objName ."->save();";
  634. $str .= "\n\t\t}";
  635. $str .= "\n\t}";
  636. $this->outputString .= $str;
  637. }
  638. protected function generateSaveSiblingsTriggers(){
  639. $str = "";
  640. foreach($this->data['type'] as $key => $val){
  641. if($val == "{SIBLING}"){
  642. $str .= $this->addSaveSibling($key);
  643. }
  644. }
  645. return $str;
  646. }
  647. protected function addSaveSibling($key){
  648. $tablename = $this->getSiblingTableFromKey($key);
  649. $varName = $this->data['property'][$key];
  650. $str = "\n\t\tif(\$this->" . $this->getVarName($key, true) . "List){";
  651. $str .= "\n\t\t\tforeach(\$this->" . $this->getVarName($key, true) . "List as \$" . $this->data['property'][$key] . "){";
  652. $str .= "\n\t\t\t\tif(!\$" . $varName . "->" . $this->getVarName($key) . "Id" . "){";
  653. $str .= "\n\t\t\t\t\t\$" . $varName . "->save();";
  654. $str .= "\n\t\t\t\t}";
  655. $str .= "\n\t\t\t\t\$result = \$this->get" . $this->data['property'][$key] . "List(array(array('" . $this->getDBName($key) . "', '=', $" . $this->data['property'][$key] . "->" . $this->getVarName($key) . "Id)), false, false, 1);";
  656. $str .= "\n\n\t\t\t\t// If sibling already save, do not add!";
  657. $str .= "\n\t\t\t\tif(!count(\$result)){";
  658. $str .= "\n\t\t\t\t\t\$statement = \"INSERT INTO " . $tablename ." (" . $this->getDBName($key) . ", " . strtolower($this->pkName) . ") VALUES (?, ?)\";";
  659. $str .= "\n\t\t\t\t\t\$stmt = Database::getInstance()->connection->prepare(\$statement);";
  660. $str .= "\n\t\t\t\t\t\$stmt->bindValue(1, \$" . $varName . "->" . $this->getVarName($key) . "Id);";
  661. $str .= "\n\t\t\t\t\t\$stmt->bindValue(2, \$this->" . $this->pkName . ");";
  662. $str .= "\n\t\t\t\t\t\$stmt->execute();";
  663. $str .= "\n\t\t\t\t}";
  664. $str .= "\n\t\t\t}";
  665. $str .= "\n\t\t\t\$this->" . $this->getVarName($key, true) . "List = array();";
  666. $str .= "\n\t\t}";
  667. return $str;
  668. }
  669. public static function getPHPType($type){
  670. if(preg_match("/varchar/i", $type)){
  671. return "string";
  672. }
  673. else {
  674. switch($type){
  675. case "INT":
  676. return "integer";
  677. break;
  678. case "BIGINT":
  679. return "integer";
  680. break;
  681. case "TINYINT":
  682. return "integer";
  683. break;
  684. case "DECIMAL":
  685. return "float";
  686. break;
  687. case "TEXT":
  688. return "string";
  689. break;
  690. case "{CHILD}":
  691. return "array";
  692. break;
  693. case "{PARENT}":
  694. return "integer";
  695. break;
  696. default:
  697. return "type";
  698. }
  699. }
  700. }
  701. protected function generateConstructor(){
  702. $non = "";
  703. $str = "\n\n\t/**";
  704. $str .= "\n\t * Instantiates a new " . $this->objectName;
  705. $str .= "\n\t *";
  706. $hasProp = false;
  707. foreach($this->data['property'] as $key => $val){
  708. if($this->data['property'][$key]){
  709. if($this->checkIfInclude($key)){
  710. $t = self::getPHPType($this->data['type'][$key]);
  711. $str .= "\n\t * @param ";
  712. $str .= $t . " ";
  713. $str .= $this->getVarName($key);
  714. $hasProp = true;
  715. }
  716. }
  717. }
  718. if(!$hasProp){
  719. $non = "\n\n\t/**";
  720. $non .= "\n\t * Instantiates a new " . $this->objectName;
  721. $non .= "\n\t */";
  722. $non .= "\n\tpublic function __construct() {}";
  723. }
  724. $str .= "\n\t */";
  725. $str .= "\n\tpublic function __construct(";
  726. foreach($this->data['property'] as $key => $val){
  727. if($this->data['property'][$key]){
  728. if($this->checkIfInclude($key)){
  729. $str .= $this->getVarName($key);
  730. $t = self::getPHPType($this->data['type'][$key]);
  731. $str .= " = " . $this->getDefaultValue($key) . ", ";
  732. }
  733. }
  734. }
  735. $str = substr($str, 0, -2);
  736. $str .= ") {";
  737. $str .= "\n\t\t";
  738. foreach($this->data['property'] as $key => $val){
  739. if($this->data['property'][$key]){
  740. if($this->checkIfInclude($key)){
  741. $t = self::getPHPType($this->data['type'][$key]);
  742. $str .= "\n\t\t" . $this->getVarName($key, true);
  743. $str .= " = (" . $this->getVarName($key) . ") ? " . $this->getVarName($key) . " : " . $this->getDefaultValue($key) . ";";
  744. }
  745. }
  746. }
  747. $str .= "\n\t}";
  748. if($hasProp){
  749. $this->outputString .= $str;
  750. }
  751. else {
  752. $this->outputString .= $non;
  753. }
  754. }
  755. protected function getDefaultValue($key){
  756. $res = $this->getPHPType($this->data['type'][$key]);
  757. switch($this->getPHPType($this->data['type'][$key])){
  758. case "string":
  759. return "\"\"";
  760. break;
  761. case "integer":
  762. return 0;
  763. break;
  764. case "float":
  765. return 0.0;
  766. break;
  767. default:
  768. return "\"\"";
  769. }
  770. }
  771. protected function getDBName($key){
  772. $name = "";
  773. switch($this->data['type'][$key]){
  774. case "{PARENT}":
  775. $name = $this->data['property'][$key];
  776. $name = strtolower($name) . "id";
  777. break;
  778. case "{SIBLING}":
  779. $name = $this->data['property'][$key];
  780. $name = strtolower($name) . "id";
  781. break;
  782. default:
  783. $name = strtolower($this->data['property'][$key]);
  784. break;
  785. }
  786. return $name;
  787. }
  788. protected function generateTableString(){
  789. $str = "\n\n\t/**";
  790. $str .= "\n\t *";
  791. $str .= "\n\t * Generates the object table for the appropriate database driver";
  792. $str .= "\n\t */";
  793. $str .= "\n\tfinal public static function generateTable() {";
  794. // MYSQL
  795. $str .= "\n\t\t\$sql = array();";
  796. $str .= "\n\t\t\$sql['mysql'][] = \"DROP TABLE " . self::TABLE_PREFIX . strtolower($this->objectName) . "\";";
  797. $str .= "\n\t\t\$sql['mysql'][] = \"";
  798. $str .= "CREATE TABLE " . self::TABLE_PREFIX . strtolower($this->objectName) . " (" . strtolower($this->pkName) . " INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY(" . strtolower($this->pkName) . "), ";
  799. foreach($this->data['property'] as $key => $val){
  800. if($this->checkIfInclude($key)){
  801. $str .= $this->getDBName($key) . " " . self::getType(self::TYPE_MYSQL, $this->data['type'][$key]) . ", ";
  802. }
  803. }
  804. $str = substr($str, 0, -2);
  805. $str .= ")\";";
  806. $str .= $this->addIndex(self::TYPE_MYSQL);
  807. //POSTGRES
  808. $str .= "\n\n\t\t\$sql['pgsql'][] = \"DROP TABLE " . self::TABLE_PREFIX . strtolower($this->objectName) . "\";";
  809. $str .= "\n\t\t\$sql['pgsql'][] = \"";
  810. $str .= "CREATE TABLE " . self::TABLE_PREFIX . strtolower($this->objectName) . " (" . strtolower($this->pkName) . " SERIAL PRIMARY KEY, ";
  811. foreach($this->data['property'] as $key => $val){
  812. if($this->checkIfInclude($key)){
  813. $str .= $this->getDBName($key) . " " . self::getType(self::TYPE_POSTGRES, $this->data['type'][$key]) . ", ";
  814. }
  815. }
  816. $str = substr($str, 0, -2);
  817. $str .= ")\";";
  818. $str .= $this->addIndex(self::TYPE_POSTGRES);
  819. $str .= $this->createSiblingTables();
  820. $str .= "\n\t\t\$createTable = \$sql[Config::getInstance()->databaseDriver];";
  821. $str .= "\n\n\t\tforeach(\$createTable as \$c){";
  822. $str .= "\n\t\t\ttry {";
  823. $str .= "\n\t\t\t\tDatabase::query(\$c);";
  824. $str .= "\n\t\t\t} catch(PDOException \$e) {";
  825. $str .= "\n\t\t\t\t; // Do nothing since, the table be created or not existing.";
  826. $str .= "\n\t\t\t}";
  827. $str .= "\n\t\t}";
  828. $str .= "\n\t}";
  829. $this->outputString .= $str;
  830. }
  831. public function addSaveNewFunction(){
  832. $str = "\n\n\tpublic function saveNew() {";
  833. $str .= "\n\t\t\$statementString = \"INSERT INTO " . self::TABLE_PREFIX . strtolower($this->objectName) . " (";
  834. foreach($this->data['property'] as $key => $p){
  835. if($p){
  836. if($this->checkIfInclude($key)){
  837. $str .= $this->getDBName($key) . ", ";
  838. }
  839. }
  840. }
  841. $str = substr($str, 0, -2) . ") VALUES (";
  842. foreach($this->data['property'] as $key => $p){
  843. if($this->checkIfInclude($key)){
  844. $str .= "?, ";
  845. }
  846. }
  847. $str = substr($str, 0, -2) . ")\";";
  848. $str .= "\n\t\t\$stmt = Database::getInstance()->connection->prepare(\$statementString);";
  849. $cnt = 1;
  850. foreach($this->data['property'] as $key => $val){
  851. if($this->checkIfInclude($key)){
  852. $str .= "\n\t\t\$stmt->bindValue(" . $cnt . ", " . $this->getVarName($key, true) . ");";
  853. $cnt++;
  854. }
  855. }
  856. $str .= "\n\t\t\$stmt->execute();";
  857. $str .= "\n\t\t\$pk = \$this->getLastId(\"" . strtolower($this->tableName) . "\",\"" . strtolower($this->pkName) . "\");";
  858. $str .= "\n\t\t\$this->" . $this->pkName . " = \$pk;";
  859. $str .= $this->generateSaveChildrenTriggers();
  860. $str .= $this->generateSaveSiblingsTriggers();
  861. $str .= "\n\t\treturn \$this->" . $this->pkName .";";
  862. $str .= "\n\t}\n";
  863. $this->outputString .= $str;
  864. }
  865. public function addGetColumnNames(){
  866. $str = "\n\n\t/**";
  867. $str .= "\n\t * Returns the column names of the table in an array.";
  868. $str .= "\n\t * @return array";
  869. $str .= "\n\t */";
  870. $str .= "\n\tpublic static function getColumnNames(){";
  871. $str .= "\n\t\treturn Database::getColumnNames(Config::getInstance()->databaseName, \"" . $this->tableName . "\");";
  872. $str .= "\n\t}";
  873. $this->outputString .= $str;
  874. }
  875. public function addUpdateFunction(){
  876. $str = "\n\n\tpublic function update() {";
  877. $str .= "\n\t\t\$statementString = \"UPDATE " . self::TABLE_PREFIX . strtolower($this->objectName) . " SET ";
  878. foreach($this->data['property'] as $key => $p){
  879. if($p){
  880. if($this->checkIfInclude($key)){
  881. $str .= $this->getDBName($key) . "=?, ";
  882. }
  883. }
  884. }
  885. $str = substr($str, 0, -2);
  886. $str .= " WHERE " . strtolower($this->pkName) . "=? LIMIT 1\";";
  887. $str .= "\n\t\tif(Config::getInstance()->databaseDriver == \"pgsql\"){";
  888. $str .= "\n\t\t\t\$statementString = str_replace(\"LIMIT 1\", \"\", \$statementString);";
  889. $str .= "\n\t\t}";
  890. $str .= "\n\t\t\$stmt = Database::getInstance()->connection->prepare(\$statementString);";
  891. $cnt = 1;
  892. foreach($this->data['property'] as $key => $val){
  893. if($val){
  894. if($this->checkIfInclude($key)){
  895. $str .= "\n\t\t\$stmt->bindValue(" . $cnt . ", " . $this->getVarName($key, true) . ");";
  896. $cnt++;
  897. }
  898. }
  899. }
  900. $str .= "\n\t\t\$stmt->bindValue(" . $cnt . ", \$this->" . $this->pkName . ");";
  901. $str .= "\n\t\t\$stmt->execute();";
  902. $str .= $this->generateSaveChildrenTriggers();
  903. $str .= $this->generateSaveSiblingsTriggers();
  904. $str .= "\n\t}\n";
  905. $this->outputString .= $str;
  906. }
  907. protected function addIndex($DB_TYPE){
  908. $returnString = null;
  909. if($DB_TYPE == self::TYPE_MYSQL){
  910. foreach($this->data['property'] as $key => $value){
  911. if($this->data['type'][$key] == "{PARENT}"){
  912. $returnString .= "\n\t\t\$sql['mysql'][] = \"CREATE INDEX " . $this->getDBName($key) . " ON " . self::TABLE_PREFIX . strtolower($this->objectName) . " (" . $this->getDBName($key) . ")\";";
  913. }
  914. }
  915. }
  916. else if($DB_TYPE == self::TYPE_POSTGRES) {
  917. foreach($this->data['property'] as $key => $value){
  918. if($this->data['type'][$key] == "{PARENT}"){
  919. $returnString .= "\n\t\t\$sql['pgsql'][] = \"CREATE INDEX " . $this->getDBName($key) . " ON " . self::TABLE_PREFIX . strtolower($this->objectName) . " (" . $this->getDBName($key) . ")\";";
  920. }
  921. }
  922. }
  923. return $returnString;
  924. }
  925. public function checkIfInclude($key){
  926. $type = $this->data['type'][$key];
  927. if($type == "{SIBLING}" || $type == "{CHILD}"){
  928. return false;
  929. }
  930. else {
  931. return true;
  932. }
  933. }
  934. public static function getType($DB_TYPE, $type){
  935. // FOR MYSQL
  936. if($DB_TYPE == self::TYPE_MYSQL){
  937. if($type == "{PARENT}"){
  938. return "INT";
  939. }
  940. return $type;
  941. }
  942. // FOR POSTGRESQL
  943. else if($DB_TYPE == self::TYPE_POSTGRES){
  944. if($type == "TINYINT"){
  945. return "SMALLINT";
  946. }
  947. else if($type == "{PARENT}"){
  948. return "INT";
  949. }
  950. else {
  951. return $type;
  952. }
  953. }
  954. // FOR SQLITE
  955. else if($DB_TYPE == self::TYPE_SQLITE){
  956. if(preg_match("/int/i", $type)){
  957. return "INTEGER";
  958. }
  959. else if($type == "FLOAT"){
  960. return "REAL";
  961. }
  962. else {
  963. return $type;
  964. }
  965. }
  966. else {
  967. return $type;
  968. }
  969. }
  970. protected function addGetListFunction(){
  971. $str = "\n\n\t/**";
  972. $str .= "\n\t * Get a list of " . $this->objectName . "s";
  973. $str .= "\n\t * @param array \$requestArray";
  974. $str .= "\n\t * @param string \$sortBy";
  975. $str .= "\n\t * @param bool \$descending";
  976. $str .= "\n\t * @param integer \$limit";
  977. $str .= "\n\t * @return array";;
  978. $str .= "\n\t */";
  979. $str .= "\n\tpublic function getList(array \$requestArray = array(), \$sortBy = false, \$descending = false, \$limit = false) {";
  980. $str .= "\n\t\t\$statementString =\"SELECT * from " . $this->tableName . "\";";
  981. $str .= "\n\t\t\$statementString .= \$this->appendConditions(\$requestArray, \$sortBy, \$descending, \$limit);";
  982. $str .= "\n\t\t\$stmt = Database::getInstance()->connection->prepare(\$statementString);";
  983. $str .= "\n\t\t\$cnt = 1;";
  984. $str .= "\n\t\tforeach(\$this->getBindArray() as \$bind){";
  985. $str .= "\n\t\t\t\$stmt->bindValue(\$cnt, \$bind);";
  986. $str .= "\n\t\t\t\$cnt++;";
  987. $str .= "\n\t\t}";
  988. $str .= "\n\t\t\$stmt->execute();";
  989. $str .= "\n\t\t\$result = \$stmt->fetchAll(PDO::FETCH_OBJ);";
  990. $str .= "\n\n\t\t\$arrayList = array();";
  991. $str .= "\n\n\t\tforeach(\$result as \$row){";
  992. $str .= "\n\n\t\t\t\$o = new " . $this->objectName . "();";
  993. $str .= "\n\t\t\t\$o->" . $this->pkName . " = \$row->" . strtolower($this->pkName) . ";";
  994. foreach($this->data['property'] as $key => $val) {
  995. if($this->checkIfInclude($key)) {
  996. $str .= "\n\t\t\t\$o->" . substr($this->getVarName($key), 1) . " = \$row->" . $this->getDBName($key) . ";";
  997. }
  998. }
  999. $str .= "\n\t\t\t\$arrayList[] = \$o;";
  1000. $str .= "\n\t\t}";
  1001. $str .= "\n\t\treturn \$arrayList;\n\t}";
  1002. $this->outputString .= $str;
  1003. }
  1004. protected function addGetStaticListFunction(){
  1005. $str = "\n\n\t/**";
  1006. $str .= "\n\t * Get a list of " . $this->objectName . "s";
  1007. $str .= "\n\t * @param array \$requestArray";
  1008. $str .= "\n\t * @param string \$sortBy";
  1009. $str .= "\n\t * @param bool \$descending";
  1010. $str .= "\n\t * @param integer \$limit";
  1011. $str .= "\n\t * @return array";
  1012. $str .= "\n\t */";
  1013. $str .= "\n\tpublic static function fetchAll(array \$requestArray = array(), \$sortBy = false, \$descending = false, \$limit = false) {";
  1014. $str .= "\n\t\t\$obj = new " . $this->objectName . "();";
  1015. $str .= "\n\t\t\$result = \$obj->getList(\$requestArray, \$sortBy, \$descending, \$limit);";
  1016. $str .= "\n\t\tunset(\$obj);";
  1017. $str .= "\n\t\treturn \$result;";
  1018. $str .= "\n\t}";
  1019. $this->outputString .= $str;
  1020. }
  1021. protected function fetchFromId(){
  1022. $str = "\n\n\t/**";
  1023. $str .= "\n\t * Returns a " . $this->objectName;
  1024. $str .= "\n\t * @param integer $" . $this->pkName;
  1025. $str .= "\n\t * @return " . $this->objectName . "|null";
  1026. $str .= "\n\t */";
  1027. $str .= "\n\tpublic static function fetch(\$" . $this->pkName . "){";
  1028. $str .= "\n\t\t\$obj = new " . $this->objectName . "();";
  1029. $str .= "\n\t\t\$obj->get(\$" . $this->pkName . ");";
  1030. $str .= "\n\t\treturn \$obj;";
  1031. $str .= "\n\t}";
  1032. $this->outputString .= $str;
  1033. }
  1034. protected function addGetParent($key) {
  1035. $objName = $this->data['property'][$key];
  1036. $tableName = self::TABLE_PREFIX . strtolower($objName);
  1037. $pkName = strtolower($objName) . "id";
  1038. $str = "\n\n\t/**";
  1039. $str .= "\n\t * Returns the parent " . $objName;
  1040. $str .= "\n\t * @return " . $objName . "|null";
  1041. $str .= "\n\t */";
  1042. $str .= "\n\tpublic function get" . $objName . "() {";
  1043. $str .= "\n\t\t\$" . $objName . " = new " . $objName ."();";
  1044. $str .= "\n\t\t\$result = \$" . $objName . "->getList(array(array('" . $this->getDBName($key) . "', '=', " . $this->getVarName($key, true) . ")), false, false, 1);";
  1045. $str .= "\n\t\treturn array_pop(\$result);";
  1046. $str .= "\n\t}";
  1047. $this->outputString .= $str;
  1048. }
  1049. protected function addSetParent($key) {
  1050. $objName = $this->data['property'][$key];
  1051. $varName = $this->getVarName($key, true);
  1052. $oName = $this->getVarName($key);
  1053. $str = "\n\n\t/**";
  1054. $str .= "\n\t * Sets reference to parent " . $objName;
  1055. $str .= "\n\t */";
  1056. $str .= "\n\tpublic function set" . $objName . "(" . $objName . " $" . $objName .") {";
  1057. $str .= "\n\t\tif(!$" . $objName ."->" . substr($oName, 1) . "){";
  1058. $str .= "\n\t\t\t$" . $objName . "->add" . $this->objectName . "(\$this);";
  1059. $str .= "\n\t\t}";
  1060. $str .= "\n\t\telse {";
  1061. $str .= "\n\t\t\t" . $varName . " = $" . $objName . "->" . substr($oName, 1) .";";
  1062. $str .= "\n\t\t}";
  1063. $str .= "\n\t}";
  1064. $this->outputString .= $str;
  1065. }
  1066. protected function addGetJSON() {
  1067. $str = "\n\n\t/**";
  1068. $str .= "\n\t * Returns the json response of the object";
  1069. $str .= "\n\t */\n\tpublic function getJSON() {";
  1070. $str .= "\n\t\treturn json_encode(\$this);";
  1071. $str .= "\n\t}";
  1072. $this->outputString .= $str;
  1073. }
  1074. protected function addSave() {
  1075. $str = "\n\n\t/**";
  1076. $str .= "\n\t * Saves the object to the database and return the primary key.";
  1077. $str .= "\n\t * @return integer";
  1078. $str .= "\n\t */";
  1079. $str .= "\n\tpublic function save() {";
  1080. $str .= "\n\t\tif(\$this->" . $this->pkName . ") {";
  1081. $str .= "\n\t\t\t\$id = \$this->update();";
  1082. $str .= "\n\t\t}";
  1083. $str .= "\n\t\telse {";
  1084. $str .= "\n\t\t\t\$id = \$this->saveNew();";
  1085. $str .= "\n\t\t}";
  1086. $str .= "\n\t\treturn \$id;";
  1087. $str .= "\n\t}";
  1088. $this->outputString .= $str;
  1089. }
  1090. protected function startBody() {
  1091. $str = "<?php\n/**\n * Database wrapper for " . $this->objectName;
  1092. $str .= "\n * Do NOT edit this file. Extend this base class to implement your logic.";
  1093. $str .= "\n * Generated on " . Utils::getDateFromTimestamp(time(), true);
  1094. $str .= "\n * @package PowerDB";
  1095. $str .= "\n * @author PowerDB Magic Writer";
  1096. $str .= "\n */";
  1097. $str .= "\nclass Base" . $this->data['objectName'] . " extends PowerModel implements IPowerModel {\n";
  1098. $this->outputString = $str;
  1099. }
  1100. protected function endBody() {
  1101. $this->outputString .= "\n}";
  1102. }
  1103. }