PageRenderTime 29ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 1ms

/zanphp/classes/class.mongodb.php

https://github.com/soequelle/ZanPHP
PHP | 621 lines | 618 code | 0 blank | 3 comment | 1 complexity | d6d06c58b62d71b4669d445298fa6d66 MD5 | raw file
  1. <?php
  2. /**
  3. * Access from index.php
  4. */
  5. if(!defined("_access")) {
  6. die("Error: You don't have permission to access here...");
  7. }
  8. /**
  9. * ZanPHP Db Class
  10. *
  11. * This class facilitates the creation of queries to the database
  12. *
  13. * @package ZanPHP
  14. * @subpackage core
  15. * @category classes
  16. * @author MilkZoft Developer Team
  17. * @link http://www.zanphp.com/documentation/en/classes/db_class
  18. */
  19. class ZP_MongoDB extends ZP_Load {
  20. /**
  21. *
  22. *
  23. * @var private static $connection = FALSE
  24. */
  25. private static $connection = FALSE;
  26. private $collection = NULL;
  27. /**
  28. * Contains the fields of the table
  29. *
  30. * @var private $fields
  31. */
  32. private $fields;
  33. private $data = FALSE;
  34. private $sort = array("_id" => 1);
  35. /**
  36. * Contains the query string
  37. *
  38. * @var private $query
  39. */
  40. private $query;
  41. public $json = NULL;
  42. /**
  43. * Contains the row content in fetch mode
  44. *
  45. * @var private $row
  46. */
  47. private $row;
  48. /**
  49. * Contains the values of the query
  50. *
  51. * @var private $values
  52. */
  53. private $values;
  54. private $limit = FALSE;
  55. private $skip = FALSE;
  56. private $hint = FALSE;
  57. private $condition = FALSE;
  58. /**
  59. * Load Database class
  60. *
  61. * @return void
  62. */
  63. public function __construct() {
  64. $this->helper("debugging");
  65. $this->config("database");
  66. $this->connect();
  67. }
  68. /**
  69. * Select database driver and make connection
  70. *
  71. * @return void
  72. */
  73. public function connect() {
  74. if(!self::$connection) {
  75. $this->Mongo = new Mongo("mongodb://". _dbNoSQLHost .":". _dbNoSQLPort);
  76. if(_dbNoSQLUser !== "" and _dbNoSQLPwd !== "") {
  77. $this->Mongo->selectDb(_dbNoSQLDatabase)->authenticate(_dbNoSQLUser, _dbNoSQLPwd);
  78. } else {
  79. $this->Mongo->selectDb(_dbNoSQLDatabase);
  80. }
  81. }
  82. }
  83. public function collection($collection = NULL) {
  84. if(!is_null($collection)) {
  85. $this->collection = $collection;
  86. }
  87. }
  88. /**
  89. * Count all records
  90. *
  91. * @return integer value
  92. */
  93. public function countAll() {
  94. return (is_object($this->cursor)) ? $this->cursor->count() : FALSE;
  95. }
  96. /**
  97. * Count records by SQL query
  98. *
  99. * @return integer value
  100. */
  101. public function countByQuery($query) {
  102. $this->find($query, FALSE);
  103. return (is_object($this->cursor)) ? $this->cursor->count() : FALSE;
  104. }
  105. private function data() {
  106. if($this->cursor->count() === 0) {
  107. return FALSE;
  108. } else {
  109. $this->cursor->sort($this->sort);
  110. if($this->limit > 0) {
  111. $this->cursor->limit($this->limit);
  112. $this->limit = FALSE;
  113. }
  114. if($this->skip > 0) {
  115. $this->cursor->skip($this->skip);
  116. }
  117. if(is_array($this->hint)) {
  118. $this->cursor->hint($this->hint);
  119. }
  120. $data = iterator_to_array($this->cursor, FALSE);
  121. }
  122. return $data;
  123. }
  124. public function drop() {
  125. $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->drop();
  126. return TRUE;
  127. }
  128. public function upload($fname = "file") {
  129. $GridFS = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->getGridFS();
  130. $name = FILES($fname, "name");
  131. return $GridFS->storeUpload($fname, $name);
  132. }
  133. public function getAllFiles() {
  134. $GridFS = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->getGridFS();
  135. $cursor = $GridFS->find();
  136. $i = 0;
  137. foreach($cursor as $object) {
  138. $files[$i]["filename"] = $object->getFilename();
  139. $files[$i]["content"] = $object->getBytes();
  140. $i++;
  141. }
  142. return $files;
  143. }
  144. public function getFile($_id, $mimeType = "image/jpeg", $return = FALSE) {
  145. $GridFS = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->getGridFS();
  146. $ID = new MongoId($_id);
  147. $file = $GridFS->findOne(array("_id" => $ID));
  148. if($return) {
  149. return $file;
  150. } else {
  151. header("Content-Type: $mimeType");
  152. print $file->getBytes();
  153. exit;
  154. }
  155. }
  156. public function deleteFile($_id) {
  157. $GridFS = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->getGridFS();
  158. $ID = new MongoId($_id);
  159. $GridFS->delete($ID);
  160. return TRUE;
  161. }
  162. public function delete($criteria, $justOne = TRUE, $safe = TRUE) {
  163. if(is_null($this->collection) or !$criteria) {
  164. return FALSE;
  165. }
  166. if($justOne and $safe) {
  167. $options = array("justOne" => TRUE, "safe" => TRUE);
  168. } elseif($justOne) {
  169. $options = array("justOne" => TRUE);
  170. } elseif($safe) {
  171. $options = array("safe" => TRUE);
  172. }
  173. $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->remove($criteria, $options);
  174. return TRUE;
  175. }
  176. public function deleteBy($field = FALSE, $value = FALSE, $justOne = TRUE, $safe = TRUE) {
  177. $criteria = array($field => $value);
  178. return $this->delete($criteria, $justOne, $safe);
  179. }
  180. public function sort($field, $order = "ASC") {
  181. $this->sort[$field] = ($order === "ASC") ? 1 : -1;
  182. }
  183. public function limit($limit = 1) {
  184. $this->limit = ($limit > 0) ? $limit : FALSE;
  185. }
  186. public function skip($skip = 1) {
  187. $this->skip = ($skip > 0) ? $skip : FALSE;
  188. }
  189. public function hint($field, $order = "ASC") {
  190. if($order === "ASC") {
  191. $this->hint[$field] = 1;
  192. } else {
  193. $this->hint[$field] = -1;
  194. }
  195. }
  196. public function ensureIndex($index = FALSE, $order = "ASC", $unique = FALSE) {
  197. if($index and $order === "ASC" and $unique) {
  198. $this->Mongo->ensureIndex(array($index => 1), array("unique" => TRUE));
  199. } elseif($index and $order === "DESC" and $unique) {
  200. $this->Mongo->ensureIndex(array($index => -1), array("unique" => TRUE));
  201. } elseif($index and $order === "ASC") {
  202. $this->Mongo->ensureIndex(array($index => 1));
  203. } elseif($index and $order === "DESC") {
  204. $this->Mongo->ensureIndex(array($index => -1));
  205. }
  206. return FALSE;
  207. }
  208. public function operator($field = NULL, $operator = "<", $value = 0, $json = FALSE) {
  209. if($operator === "<") {
  210. return (!$json) ? array($field => array('$lt' => $value)) : '';
  211. } elseif($operator === ">") {
  212. return (!$json) ? array($field => array('$gt' => $value)) : '';
  213. } elseif($operator === "<=") {
  214. return (!$json) ? array($field => array('$lte' => $value)) : '';
  215. } elseif($operator === ">=") {
  216. return (!$json) ? array($field => array('$gte' => $value)) : '';
  217. } elseif($operator === "!=" or $operator === "<>") {
  218. return (!$json) ? array($field => array('$ne' => $value)) : '';
  219. } elseif($operator === "in") {
  220. return (!$json) ? array($field => array('$in' => $value)) : '';
  221. } elseif($operator === "all") {
  222. return (!$json) ? array($field => array('$all' => $value)) : '';
  223. } elseif($operator === "exists") {
  224. return (!$json) ? array($field => array('$exists' => $value)) : '';
  225. } elseif($operator === "inc" or $operator === "++") {
  226. return (!$json) ? array($field => array('$inc' => $value)) : '';
  227. } elseif($operator === "or" or $operator === "||") {
  228. if(is_array($field)) {
  229. return (!$json) ? array('$or' => $field) : '';
  230. } else {
  231. return (!$json) ? array('$or' => array($field => $value)) : '';
  232. }
  233. } elseif($operator === "set") {
  234. return (!$json) ? array('$set' => array($field => $value)) : '';
  235. } elseif($operator === "unset") {
  236. return (!$json) ? array('$unset' => array($field => $value)) : '';
  237. } elseif($operator === "push") {
  238. return (!$json) ? array('$push' => array($field => $value)) : '';
  239. } elseif($operator === "pushAll") {
  240. return (!$json) ? array('$pushAll' => $field) : '';
  241. } elseif($operator === "addToSet") {
  242. if(is_array($field)) {
  243. return (!$json) ? array('$addToSet' => $field) : '';
  244. } else {
  245. return (!$json) ? array('$addToSet' => array($field => $value)) : '';
  246. }
  247. } elseif($operator === "pop") {
  248. return (!$json) ? array('$pop' => array($field => $value)) : '';
  249. }
  250. return FALSE;
  251. }
  252. public function regex($regex, $field) {
  253. $Regex = new MongoRegex($regex);
  254. return $this->find(array($field => $regex));
  255. }
  256. public function getNext() {
  257. return (is_object($this->cursor)) ? $this->cursor->getNext() : FALSE;
  258. }
  259. public function slice($field, $count = 1) {
  260. $this->condition = (object) array($field => array('$slice' => $count));
  261. }
  262. public function find($query = NULL, $return = TRUE) {
  263. if(is_null($query)) {
  264. $this->cursor = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->find();
  265. } else {
  266. if(!is_array($query) and is_string($query)) {
  267. $query = json_decode($query, TRUE);
  268. }
  269. if($this->condition) {
  270. $this->cursor = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->find($query, $this->condition);
  271. unset($this->condition);
  272. $this->condition = FALSE;
  273. }
  274. $this->cursor = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->find($query);
  275. }
  276. return $this->data();
  277. }
  278. public function get($type = "AND", $array, $return = FALSE, $add = TRUE) {
  279. if($add and !$return) {
  280. if($type === "AND") {
  281. foreach($array as $field => $value) {
  282. $this->json .= '"'. $field .'" : "'. $value .'", ';
  283. }
  284. } elseif($type === "OR") {
  285. $keys = array_keys($array);
  286. $j = 0;
  287. $or = FALSE;
  288. foreach($array as $values) {
  289. if(is_array($values)) {
  290. for($i = 0; $i <= count($values) - 1; $i++) {
  291. if(!$or) {
  292. $last = substr($this->json, strlen($this->json) - 1, strlen($this->json));
  293. if($last === "]") {
  294. $this->json .= ', "$or":[{';
  295. } else {
  296. $this->json .= '"$or":[{';
  297. }
  298. $or = TRUE;
  299. }
  300. if($j === count($keys) - 1) {
  301. $this->json .= '"'. $keys[$j] .'" : "'. $values[$i] .'"';
  302. } else {
  303. $this->json .= '"'. $keys[$j] .'" : "'. $values[$i] .'", ';
  304. }
  305. }
  306. }
  307. $j++;
  308. }
  309. $this->json .= '}]';
  310. }
  311. } else {
  312. if($type === "AND") {
  313. $i = 0;
  314. foreach($array as $field => $value) {
  315. if($i === count($array) - 1) {
  316. $last = substr($this->json, strlen($this->json) - 1, strlen($this->json));
  317. if($last === "]") {
  318. $this->json .= ', "'. $field .'" : "'. $value .'"';
  319. } else {
  320. $this->json .= '"'. $field .'" : "'. $value .'"';
  321. }
  322. } else {
  323. $last = substr($this->json, strlen($this->json) - 1, strlen($this->json));
  324. if($last === "]") {
  325. $this->json .= ', "'. $field .'" : "'. $value .'", ';
  326. } else {
  327. $this->json .= '"'. $field .'" : "'. $value .'", ';
  328. }
  329. }
  330. }
  331. } elseif($type === "OR") {
  332. $keys = array_keys($array);
  333. $j = 0;
  334. $or = FALSE;
  335. foreach($array as $values) {
  336. if(is_array($values)) {
  337. for($i = 0; $i <= count($values) - 1; $i++) {
  338. if(!$or) {
  339. $last = substr($this->json, strlen($this->json) - 1, strlen($this->json));
  340. if($last === "]") {
  341. $this->json .= ', "$or":[{';
  342. } else {
  343. $this->json .= '"$or":[{';
  344. }
  345. $or = TRUE;
  346. }
  347. if($j === count($keys) - 1) {
  348. $this->json .= '"'. $keys[$j] .'" : "'. $values[$i] .'"';
  349. } else {
  350. $this->json .= '"'. $keys[$j] .'" : "'. $values[$i] .'", ';
  351. }
  352. }
  353. }
  354. $j++;
  355. }
  356. $this->json .= '}]';
  357. }
  358. }
  359. if($return) {
  360. $return = $this->json;
  361. empty($this->json);
  362. return "{" . $return . "}";
  363. }
  364. }
  365. public function getLastID() {
  366. $this->cursor = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->find();
  367. $this->sort("_id", "DESC");
  368. $this->limit(1);
  369. $data = $this->data();
  370. return $data["_id"];
  371. }
  372. /**
  373. * Find record by primary key
  374. *
  375. * @param integer $ID
  376. * @return boolean value / array value
  377. */
  378. public function findByID($ID) {
  379. if(is_null($this->collection)) {
  380. return FALSE;
  381. }
  382. if(strlen($ID) === 24) {
  383. $ID = new MongoId($ID);
  384. }
  385. $query = array("_id" => $ID);
  386. $this->cursor = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->find($query);
  387. return $this->data();
  388. }
  389. /**
  390. * Find all records
  391. *
  392. * @param string $group = NULL
  393. * @param string $order = NULL
  394. * @param string $limit = NULL
  395. * @return array value
  396. */
  397. public function findAll($group = NULL, $order = NULL, $limit = NULL) {
  398. if(is_null($this->collection)) {
  399. return FALSE;
  400. }
  401. $this->cursor = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->find();
  402. return $this->data();
  403. }
  404. /**
  405. * Find records by specific field and value
  406. *
  407. * @param string $field
  408. * @param string $value
  409. * @param string $group = NULL
  410. * @param string $order = NULL
  411. * @param string $limit = NULL
  412. * @return array value
  413. */
  414. public function findBy($field, $value) {
  415. if(is_null($this->collection)) {
  416. return FALSE;
  417. }
  418. $query = array($field => $value);
  419. $this->cursor = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->find($query);
  420. return $this->data();
  421. }
  422. /**
  423. * Find the first record
  424. *
  425. * @return array value
  426. */
  427. public function findFirst() {
  428. if(is_null($this->collection)) {
  429. return FALSE;
  430. }
  431. $this->cursor = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->findOne();
  432. return $this->data();
  433. }
  434. /**
  435. * Find the last record
  436. *
  437. * @return array value
  438. */
  439. public function findLast() {
  440. $this->cursor = $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->find();
  441. $this->sort("_id", "DESC");
  442. $this->limit(1);
  443. return $this->data();
  444. }
  445. public function insert($_id = TRUE) {
  446. if(is_array($this->data)) {
  447. $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->insert($this->data, $_id);
  448. unset($this->data);
  449. $this->data = array();
  450. } else {
  451. print __("Insert error");
  452. }
  453. }
  454. public function join($table, $condition, $position = FALSE) {
  455. }
  456. public function like($data, $match = FALSE, $position = "both") {
  457. }
  458. public function rows() {
  459. return $this->cursor->count();
  460. }
  461. public function command($command) {
  462. $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->command($command);
  463. }
  464. public function save($option = NULL, $_id = TRUE) {
  465. if(is_null($option)) {
  466. $this->insert($_id);
  467. } elseif(is_array($option)) {
  468. $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->save($option);
  469. }
  470. }
  471. public function set($field, $value) {
  472. $this->data[$field] = $value;
  473. }
  474. public function update($criteria = FALSE, $update = FALSE, $options = FALSE) {
  475. if(is_null($this->collection) or !$criteria) {
  476. return FALSE;
  477. }
  478. $options = ($options) ? $options : array("upsert" => TRUE);
  479. if(!$update and is_array($this->data)) {
  480. $update = $this->data;
  481. }
  482. $this->Mongo->selectCollection(_dbNoSQLDatabase, $this->collection)->update($criteria, $update, $options);
  483. return TRUE;
  484. }
  485. }