Pluf Framework

Sign in or create your account | Project List | Help

Pluf Framework Commit Details

Date:2009-10-19 17:45:40 (5 months 2 days ago)
Author:Loïc d'Anterroches
Commit:8cf66a115318414d124656cfa654849e64a01394
Message:Improved the performance of the ORM for fast backup, restore (20% faster).

Files: src/Pluf/DB.php (5 diffs)
src/Pluf/DB/Field.php (1 diff)
src/Pluf/Model.php (5 diffs)
src/Pluf/Test/Fixture.php (2 diffs)

Change Details

src/Pluf/DB.php
147147 */
148148function Pluf_DB_IdentityToDb($val, $db)
149149{
150    if (is_null($val)) {
150    if (null === $val) {
151151        return 'NULL';
152152    }
153153    return $db->esc($val);
...... 
163163
164164function Pluf_DB_SerializedToDb($val, $db)
165165{
166    if (is_null($val)) {
166    if (null === $val) {
167167        return 'NULL';
168168    }
169169    return $db->esc(serialize($val));
...... 
176176
177177function Pluf_DB_CompressedToDb($val, $db)
178178{
179    return (is_null($val)) ? 'NULL' : $db->esc(gzdeflate($val, 9));
179    return (null === $val) ? 'NULL' : $db->esc(gzdeflate($val, 9));
180180}
181181
182182function Pluf_DB_BooleanFromDb($val) {
...... 
187187}
188188
189189function Pluf_DB_BooleanToDb($val, $db) {
190    if (is_null($val)) {
190    if (null === $val) {
191191        return 'NULL';
192192    }
193193    if ($val) {
...... 
197197}
198198
199199function Pluf_DB_IntegerFromDb($val) {
200    if (is_null($val)) return null;
201    return (int) $val;
200    return (null === $val) ? null : (int) $val;
202201}
203202
204203function Pluf_DB_IntegerToDb($val, $db) {
205    if (is_null($val)) {
206        return 'NULL';
207    }
208    return (string)(int)$val;
204    return (null === $val) ? 'NULL' : (string)(int)$val;
209205}
210206
211207function Pluf_DB_PasswordToDb($val, $db) {
src/Pluf/DB/Field.php
6363    {
6464        $this->value = $value;
6565        $this->column = $column;
66        $this->extra = array_merge($this->extra, $extra);
66        if ($extra) {
67            $this->extra = array_merge($this->extra, $extra);
68        }
6769    }
6870
6971    /**
src/Pluf/Model.php
290290     */
291291    function _getConnection()
292292    {
293        static $con = null;
293294        if ($this->_con !== null) {
294295            return $this->_con;
295296        }
297        if ($con !== null) {
298            $this->_con = $con;
299            return $this->_con;
300        }
296301        $this->_con = &Pluf::db($this);
302        $con = $this->_con;
297303        return $this->_con;
298304    }
299305
...... 
323329     */
324330    function __get($prop)
325331    {
326        if (array_key_exists($prop, $this->_data)) return $this->_data[$prop];
327        else try {
328            return $this->__call($prop, array());
329        } catch (Exception $e) {
330            throw new Exception(sprintf('Cannot get property "%s".', $prop));
331        }
332        return (array_key_exists($prop, $this->_data)) ?
333            $this->_data[$prop] : $this->__call($prop, array());
332334    }
333335
334336    /**
...... 
339341     */
340342    function __set($prop, $val)
341343    {
342        if (!is_null($val) and isset($this->_cache['fk'][$prop])) {
344        if (null !== $val and isset($this->_cache['fk'][$prop])) {
343345            $this->_data[$prop] = $val->id;
344346            unset($this->_cache['get_'.$prop]);
345347        } else {
...... 
737739
738740    /**
739741     * Create the model into the database.
742     *
743     * If raw insert is requested, the preSave/postSave methods are
744     * not called and the current id of the object is directly
745     * used. This is particularily used when doing backup/restore of
746     * data.
740747     *
748     * @param bool Raw insert (false)
741749     * @return bool Success
742750     */
743    function create($force_id=false)
751    function create($raw=false)
744752    {
745        $this->preSave(true);
753        if (!$raw) {
754            $this->preSave(true);
755        }
746756        $req = 'INSERT INTO '.$this->getSqlTable()."\n";
747757        $icols = array();
748758        $ivals = array();
749759        $assoc = array();
750760        foreach ($this->_a['cols'] as $col=>$val) {
751761            $field = new $val['type']();
752            if ($col == 'id' and !$force_id) {
762            if ($col == 'id' and !$raw) {
753763                continue;
754764            } elseif ($field->type == 'manytomany') {
755765                // If is a defined array, we need to associate.
756                if (is_array($this->$col)) {
757                    $assoc[$val['model']] = $this->$col;
766                if (is_array($this->_data[$col])) {
767                    $assoc[$val['model']] = $this->_data[$col];
758768                }
759769                continue;
760770            }
761771            $icols[] = $this->_con->qn($col);
762            $ivals[] = $this->_toDb($this->$col, $col);
772            $ivals[] = $this->_toDb($this->_data[$col], $col);
763773        }
764774        $req .= '('.implode(', ', $icols).') VALUES ';
765775        $req .= '('.implode(','."\n", $ivals).')';
766776        $this->_con->execute($req);
767        if (false === ($id=$this->_con->getLastID())) {
768            throw new Exception($this->_con->getError());
777        if (!$raw) {
778            if (false === ($id=$this->_con->getLastID())) {
779                throw new Exception($this->_con->getError());
780            }
781            $this->_data['id'] = $id;
769782        }
770        $this->_data['id'] = $id;
771783        foreach ($assoc as $model=>$ids) {
772784            $this->batchAssoc($model, $ids);
773785        }
774        $this->postSave(true);
786        if (!$raw) {
787            $this->postSave(true);
788        }
775789        return true;
776790    }
777791
...... 
956970    function _fromDb($val, $col)
957971    {
958972        $m = $this->_con->type_cast[$this->_a['cols'][$col]['type']][0];
959        return $m($val);
973        return ($m == 'Pluf_DB_IdentityFromDb') ? $val : $m($val);
960974    }
961975
962976    /**
src/Pluf/Test/Fixture.php
4040        if (false === ($ffile=Pluf::fileExists($file))) {
4141            throw new Exception(sprintf(__('Fixture file not found: %s.'), $file));
4242        }
43        $json = file_get_contents($ffile);
44        return self::load($json);
43        return self::load(file_get_contents($ffile));
4544    }
4645
4746
48    public static function load($json)
47    public static function load($json, $deserialize=true)
4948    {
5049        $created = array();
51        $data = json_decode($json, true);
50        $data = ($deserialize) ? json_decode($json, true) : $json;
51        unset($json);
5252        foreach ($data as $model) {
5353            if ((int)$model['pk'] > 0) {
5454                $item = new $model['model']($model['pk']);
...... 
5858            }
5959            $m = new $model['model']();
6060            $m->setFromFormData($model['fields']);
61            $m->create(true); // we force the id
61            $m->create(true); // we load in raw mode
6262            $created[] = array($model['model'], $model['pk']);
6363        }
6464        return $created;
6565    }
6666
67    public static function dump($model)
67    /**
68     * Given a model or model name, dump the content.
69     *
70     * If the object is given, only this single object is dumped else
71     * the complete table.
72     *
73     * @param mixed Model object or model name
74     * @param bool Serialize as JSON (true)
75     * @return mixed Array or JSON string
76     */
77    public static function dump($model, $serialize=true)
6878    {
6979        if (is_object($model)) {
70            return json_encode(array(self::prepare($model)));
80            return ($serialize) ?
81                json_encode(array(self::prepare($model))) :
82                array(self::prepare($model));
7183        }
7284        $out = array();
73        foreach (Pluf::factory($model)->getList() as $item) {
85        foreach (Pluf::factory($model)->getList(array('order' =>'id ASC')) as $item) {
7486            $out[] = self::prepare($item);
7587        }
76        return json_encode($out);
88        return ($serialize) ? json_encode($out) : $out;
7789    }
7890
7991    /**

Archive Download the corresponding diff file

Branches:
master