Git - Framework SuperObject

fso: / db_mysql.php [ Download ]

  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
116
117
118
119
120
121
<?php

if ( !class_exists('FSO') )
    die('Framework SuperObject base class must be loaded to use its modules!');

class DB_mysql extends FSO_DB_Module
{
    //  {{{ public function __construct( $conf )

    /**
     * Constructor
     *
     * Set up database connection.  $conf must be an array containing at least
     * 'user', 'pass' and 'db' parameters.
     *
     * @param       array       $conf
     * @return      void
     */
    public function __construct( $conf )
    {
        $conf = array_merge(array(
            'host'  => 'localhost',
            'port'  => '3306',
        ), $conf);
        parent::__construct("mysql:host={$conf['host']};dbname={$conf['db']};port={$conf['port']}", 
            $conf['user'], $conf['pass']);
        $this->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);
    }

    //  }}}
    //  {{{ public function mkSelectList($fields)

    /**
     * Make a field list for a select query
     *
     * Copied from YSTV's inc/dbfuncs.php
     */
    public function mkSelectList(array $fields)
    {
        $ret = '';
        foreach ( $fields as $field => $alias )
            $ret .= " $field AS $alias,";
        return substr($ret, 0, -1);
    }

    //  }}}
    //  {{{ public function mkInsertList($fields)

    /**
     * Make a field=value list for a database insert
     *
     * Pass in an associative array where the key is the field name and the
     * value is the new value for the field.  Escaping will be taken care of here.
     *
     * Copied mostly from YSTV's inc/dbfuncs.php
     */
    public function mkInsertList(array $fields)
    {
        $ret = '';
        foreach ( $fields as $field => $value )
            $ret .= $field.' = '.$this->quote($value).',';
        return substr($ret, 0, -1);
    }

    //  }}}

    /**
     * Compatibility section
     *
     * This section provides function that act like old YSTV functions so that 
     * code can be migrated in stages.  $extra in these functions is an unused
     * variable, its just there to stop calls to functions that used to have
     * database stuff passed to them from failing.
     */

    //  {{{ public function compat_select_query($query, $extra = null)

    public function compat_select_query($query, $extra = null)
    {
        $result = $this->query($query);
        $ret['data'] = $result->fetchAll(PDO::FETCH_ASSOC);
        $ret['numrows'] = count($ret['data']);
        return $ret;
    }

    //  }}}
    //  {{{ public function compat_insert_query($query, $extra = null)

    public function compat_insert_query($query, $extra = null)
    {
        $this->query($query);
    }

    //  }}}
    //  {{{ public function compat_update_query($query, $extra = null)

    public function compat_update_query($query, $extra = null)
    {
        $this->query($query);
    }

    //  }}}
    //  {{{ public function compat_delete_query($query, $extra = null)

    public function compat_delete_query($query, $extra = null)
    {
        $this->query($query);
    }

    //  }}}
    //  {{{ public function compat_other_query($query, $extra = null)

    public function compat_other_query($query, $extra = null)
    {
        $this->query($query);
    }

    //  }}}
}

?>