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