ApiDataSourceTodo.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. class ApiDataSourceTodo {
  3. private $_dataSourceName;
  4. private $_sourceConnection;
  5. private $_tblName;
  6. private $_primaryKey;
  7. private $_fields = array();
  8. public function __construct($dataSourceName) {
  9. $this->_dataSourceName = $dataSourceName;
  10. }
  11. public function getSchema() {
  12. // TODO: read from schema files xml/ini
  13. // TODO: build data source by name $this->_dataSourceName (Mysql|SchemaFile)
  14. if (empty($this->_fields)) {
  15. $db = $this->getConnection();
  16. $sql = "show fields from `{$this->_tblName}` ";
  17. $res = $db->query($sql);
  18. if (!$res) {
  19. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  20. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">db errors (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($db->get_errors());echo'</pre>';}
  21. throw new HttpException("Read schema errors", 404);
  22. }
  23. while ($h = $db->fetch_row($res)) {
  24. $fieldName = $h[0];
  25. $fieldType = $h[1];
  26. $this->_fields[$fieldName] = array('type'=>$h[1], 'null'=>('YES' == $h[2]), 'default'=>$h[4]);
  27. if ('PRI' == $h[3]) {
  28. $this->_primaryKey = $fieldName;
  29. }
  30. }
  31. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">_fields (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_fields);echo'</pre>';}
  32. if (empty($this->_fields)) {
  33. throw new HttpException("Fields list empty", 404);
  34. }
  35. }
  36. }
  37. private function getConnection() {
  38. $this->_sourceConnection = DB::getDB($this->_dataSourceName);
  39. if (!$this->_sourceConnection) {
  40. throw new HttpException("Connect to data source error", 404);
  41. }
  42. return $this->_sourceConnection;
  43. }
  44. public function getItems($args) {
  45. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">args (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($args);echo'</pre>';}
  46. $items = array();
  47. $db = $this->getConnection();
  48. $fields = $this->getFields();
  49. $sqlFields = array();
  50. foreach ($fields as $fieldName => $field) {
  51. $sqlFields[] = "t.`{$fieldName}`";
  52. }
  53. $sqlFields = implode(", ", $sqlFields);
  54. if (!empty($args['limit']) && is_numeric($args['limit'])) {
  55. $sqlLimit = "limit {$args['limit']}";
  56. if (!empty($args['offset']) && is_numeric($args['offset'])) {
  57. $sqlLimit .= " offset {$args['offset']}";
  58. }
  59. }
  60. $sqlWhere = "1=1";
  61. $sqlQueryArgs = array();
  62. foreach ($fields as $fieldName => $field) {
  63. $queryValue = V::get("f_{$fieldName}", null, $_GET);
  64. if (!empty($queryValue)) {
  65. $sqlQueryValue = $db->_($queryValue);
  66. $sqlQueryArgs[] = "t.`{$fieldName}` like '{$sqlQueryValue}'";
  67. }
  68. }
  69. if (!empty($sqlQueryArgs)) {
  70. $sqlWhere .= " and " . implode(" and ", $sqlQueryArgs);
  71. }
  72. $sql = "select {$sqlFields}
  73. from `{$this->_tblName}` as t
  74. where {$sqlWhere}
  75. {$sqlLimit}
  76. -- order by
  77. ";
  78. IF(V::get('DBG','',$_GET)>2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  79. IF(V::get('DBG','',$_GET)>2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">this->_primaryKey (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_primaryKey);echo'</pre>';}
  80. $res = $db->query($sql);
  81. while ($r = $db->fetch($res)) {
  82. if (!$this->_primaryKey) {
  83. $items[] = $r;
  84. } else {
  85. $items[$r->{$this->_primaryKey}] = $r;
  86. }
  87. }
  88. return $items;
  89. }
  90. public function getItem($id, $params = []) {
  91. $items = array();
  92. $db = $this->getConnection();
  93. $fields = $this->getFields();
  94. $sqlFields = array();
  95. foreach ($fields as $fieldName => $field) {
  96. $sqlFields[] = "t.`{$fieldName}`";
  97. }
  98. $sqlFields = implode(", ", $sqlFields);
  99. $sqlWhere = "t.`{$this->_primaryKey}`='{$id}'";
  100. $sql = "select {$sqlFields}
  101. from `{$this->_tblName}` as t
  102. where {$sqlWhere}
  103. ";
  104. $res = $db->query($sql);
  105. if ($r = $db->fetch($res)) {
  106. $item = $r;
  107. }
  108. return $item;
  109. }
  110. public function createItem($itemRaw) {
  111. $item = array();
  112. $db = $this->getConnection();
  113. $fields = $this->getFields();
  114. foreach ($fields as $fieldName => $field) {
  115. if (array_key_exists($fieldName, $itemRaw)) {
  116. $item[$fieldName] = $itemRaw[$fieldName];
  117. }
  118. }
  119. if (array_key_exists($this->_primaryKey, $item)) {
  120. if (!empty($item[$this->_primaryKey])) {
  121. $item[$this->_primaryKey] = null;
  122. }
  123. }
  124. //echo "itemRaw:\n";print_r($itemRaw);
  125. //echo "fields:\n";print_r($fields);
  126. //echo "item:\n";var_dump($item);
  127. //exit;
  128. foreach ($item as $k => $v) {
  129. if ($k == $this->_primaryKey) {
  130. $v = 'NULL';
  131. } else if (strtoupper($v) == 'NOW()') {
  132. $v = 'NOW()';
  133. } else if (strtoupper($v) == 'NULL') {
  134. $v = 'NULL';
  135. } else if (substr($v, 0, strlen('GeomFromText')) == 'GeomFromText') {
  136. } else {
  137. $v = $db->_($v);
  138. $v = "'{$v}'";
  139. }
  140. $sql_arr ["`{$k}`"] = $v;
  141. }
  142. $sql = "insert into `{$this->_tblName}` (".implode(",", array_keys($sql_arr)).") values (".implode(",", array_values($sql_arr))."); ";
  143. //echo "sql:{$sql}\n";
  144. //exit;
  145. $db->query($sql);
  146. if ($db->has_errors()) {
  147. throw new Exception("Database Errors: " . implode("\n", $db->get_errors()));
  148. }
  149. return $db->insert_id();
  150. }
  151. public function getFields() {
  152. $this->getSchema();
  153. return $this->_fields;
  154. }
  155. public function setTable($tblName) {
  156. $this->_tblName = $tblName;
  157. }
  158. }