You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

106 lines
3.4 KiB

  1. <?php
  2. /** NotORM - simple reading data from the database
  3. * @link http://www.notorm.com/
  4. * @author Jakub Vrana, http://www.vrana.cz/
  5. * @copyright 2010 Jakub Vrana
  6. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  7. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
  8. */
  9. if (!interface_exists('JsonSerializable')) {
  10. interface JsonSerializable {
  11. function jsonSerialize();
  12. }
  13. }
  14. include_once dirname(__FILE__) . "/NotORM/Structure.php";
  15. include_once dirname(__FILE__) . "/NotORM/Cache.php";
  16. include_once dirname(__FILE__) . "/NotORM/Literal.php";
  17. include_once dirname(__FILE__) . "/NotORM/Result.php";
  18. include_once dirname(__FILE__) . "/NotORM/MultiResult.php";
  19. include_once dirname(__FILE__) . "/NotORM/Row.php";
  20. // friend visibility emulation
  21. abstract class NotORM_Abstract {
  22. protected $connection, $driver, $structure, $cache;
  23. protected $notORM, $table, $primary, $rows, $referenced = array();
  24. protected $debug = false;
  25. protected $debugTimer;
  26. protected $freeze = false;
  27. protected $rowClass = 'NotORM_Row';
  28. protected $jsonAsArray = false;
  29. protected $isKeepPrimaryKeyIndex = FALSE; //@dogstar 20151230
  30. protected function access($key, $delete = false) {
  31. }
  32. }
  33. /** Database representation
  34. * @property-write mixed $debug = false Enable debugging queries, true for error_log($query), callback($query, $parameters) otherwise
  35. * @property-write bool $freeze = false Disable persistence
  36. * @property-write string $rowClass = 'NotORM_Row' Class used for created objects
  37. * @property-write bool $jsonAsArray = false Use array instead of object in Result JSON serialization
  38. * @property-write string $transaction Assign 'BEGIN', 'COMMIT' or 'ROLLBACK' to start or stop transaction
  39. */
  40. class NotORM extends NotORM_Abstract {
  41. /** Create database representation
  42. * @param PDO
  43. * @param NotORM_Structure or null for new NotORM_Structure_Convention
  44. * @param NotORM_Cache or null for no cache
  45. */
  46. function __construct(PDO $connection, NotORM_Structure $structure = null, NotORM_Cache $cache = null) {
  47. $this->connection = $connection;
  48. $this->driver = $connection->getAttribute(PDO::ATTR_DRIVER_NAME);
  49. if (!isset($structure)) {
  50. $structure = new NotORM_Structure_Convention;
  51. }
  52. $this->structure = $structure;
  53. $this->cache = $cache;
  54. }
  55. /** Get table data to use as $db->table[1]
  56. * @param string
  57. * @return NotORM_Result
  58. */
  59. function __get($table) {
  60. return new NotORM_Result($this->structure->getReferencingTable($table, ''), $this, true);
  61. }
  62. /** Set write-only properties
  63. * @return null
  64. */
  65. function __set($name, $value) {
  66. if ($name == "debug" || $name == "debugTimer" || $name == "freeze" || $name == "rowClass" || $name == "jsonAsArray" || $name == 'isKeepPrimaryKeyIndex') {
  67. $this->$name = $value;
  68. }
  69. if ($name == "transaction") {
  70. switch (strtoupper($value)) {
  71. case "BEGIN": return $this->connection->beginTransaction();
  72. case "COMMIT": return $this->connection->commit();
  73. case "ROLLBACK": return $this->connection->rollback();
  74. }
  75. }
  76. }
  77. /** Get table data
  78. * @param string
  79. * @param array (["condition"[, array("value")]]) passed to NotORM_Result::where()
  80. * @return NotORM_Result
  81. */
  82. function __call($table, array $where) {
  83. $return = new NotORM_Result($this->structure->getReferencingTable($table, ''), $this);
  84. if ($where) {
  85. call_user_func_array(array($return, 'where'), $where);
  86. }
  87. return $return;
  88. }
  89. }