Mysql.php 5.54 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
require_once("Common.php");
/**
 * mysql driver
 */
class Doctrine_Connection_Mysql extends Doctrine_Connection_Common {

    /**
     * the constructor
     * @param PDO $pdo  -- database handle
     */
    public function __construct(Doctrine_Manager $manager,PDO $pdo) {
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
14
        $this->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_ROWS);
zYne's avatar
zYne committed
15
        parent::__construct($manager,$pdo);
16
    }    
17 18 19 20 21 22 23 24 25
    /**
     * returns the regular expression operator 
     * (implemented by the connection drivers)
     *
     * @return string
     */
    public function getRegexpOperator() {
        return 'RLIKE';
    }
zYne's avatar
zYne committed
26 27 28 29
    /**
     * deletes all data access object from the collection
     * @param Doctrine_Collection $coll
     */
30

zYne's avatar
zYne committed
31 32 33 34 35 36
     /**
    public function deleteCollection(Doctrine_Collection $coll) {

        $a   = $coll->getTable()->getCompositePaths();
        $a   = array_merge(array($coll->getTable()->getComponentName()),$a);

37
        $graph = new Doctrine_Query();
zYne's avatar
zYne committed
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        foreach($coll as $k=>$record) {
            switch($record->getState()):
                case Doctrine_Record::STATE_DIRTY:
                case Doctrine_Record::STATE_CLEAN:
                    $ids[] = $record->getID();
                break;
            endswitch;
        }
        if(empty($ids))
            return array();

        $graph->parseQuery("FROM ".implode(", ",$a)." WHERE ".$coll->getTable()->getTableName().".id IN(".implode(", ",$ids).")");

        $query = $graph->buildDelete();

        $this->getDBH()->query($query);
        return $ids;
    }
    */
    /**
     * returns maximum identifier values
     *
     * @param array $names          an array of component names
     * @return array
     */
    public function getMaximumValues2(array $names) { 
        $values = array();
        foreach($names as $name) {
            $table     = $this->tables[$name];
            $keys      = $table->getPrimaryKeys();
            $tablename = $table->getTableName();

            if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {
                // record uses auto_increment column

                $sql[]    = "SELECT MAX(".$tablename.".".$table->getIdentifier().") as $tablename FROM ".$tablename;
                $values[$tablename] = 0;
                $array[] = $tablename;
            }
        }
        $sql    = implode(" UNION ",$sql);
        $stmt   = $this->getDBH()->query($sql);
        $data   = $stmt->fetchAll(PDO::FETCH_NUM);

        foreach($data as $k => $v) {
            $values[$array[$k]] = $v[0];
        }
        return $values;
    }
    /**
     * bulkInsert
     * inserts all the objects in the pending insert list into database
     * TODO: THIS IS NOT WORKING YET AS THERE ARE BUGS IN COMPONENTS USING SELF-REFERENCENCING
     *
     * @return boolean
     */
     
     /**
    public function bulkInsert() {
        if(empty($this->insert))
            return false;

        foreach($this->insert as $name => $inserts) {
            if( ! isset($inserts[0]))
                continue;

            $record    = $inserts[0];
            $table     = $record->getTable();
            $seq       = $table->getSequenceName();
            $keys      = $table->getPrimaryKeys();

            $marks = array();
            $params = array();
            foreach($inserts as $k => $record) {
                $record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onPreSave($record);
                // listen the onPreInsert event
                $record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onPreInsert($record);

                $array = $record->getPrepared();

                if(isset($this->validator)) {
                    if( ! $this->validator->validateRecord($record)) {
                        continue;
                    }
                }

                $key = implode(", ",array_keys($array));
                if( ! isset($params[$key]))
                    $params[$key] = array();

                $marks[$key][] = "(".substr(str_repeat("?, ",count($array)),0,-2).")";
                $params[$key] = array_merge($params[$key], array_values($array));


                // listen the onInsert event
                $record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onInsert($record);

                $record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onSave($record);
            }

            if( ! empty($marks)) {
                foreach($marks as $key => $list) {
                    $query = "INSERT INTO ".$table->getTableName()." (".$key.") VALUES ".implode(", ", $list);
                    $stmt  = $this->getDBH()->prepare($query);
                    $stmt->execute($params[$key]);
                }
            }
            if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {

                // record uses auto_increment column

                $sql  = "SELECT MAX(".$table->getIdentifier().") FROM ".$record->getTable()->getTableName();
                $stmt = $this->getDBH()->query($sql);
                $data = $stmt->fetch(PDO::FETCH_NUM);
                $id   = $data[0];
                $stmt->closeCursor();
                
                foreach(array_reverse($inserts) as $record) {

                    $record->setID((int) $id);
                    $id--;
                }
            }
        }

        $this->insert = array();
        return true;
    }
    */

}
169