Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**This is included from unit tests to initialize a DB connection.
3
*
4
* LICENSE:
5
* This source file is subject to version 2.1 of the LGPL
6
* that is bundled with this package in the file LICENSE.
7
*
8
* COPYRIGHT:
9
* Empowered Media
10
* http://www.empoweredmedia.com
11
* 481 Eighth Avenue Suite 1530
12
* New York, NY 10001
13
*
14
* @copyright Empowered Media 2006
15
* @license http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
16
* @author Lukas Feiler <lukas.feiler@lukasfeiler.com>
17
* @package XML_Query2XML
18
* @version $Id: PDO_db_init.php 302587 2010-08-20 23:53:59Z clockwerx $
19
*/
20
 
21
class MyPDOStatement extends PDOStatement
22
{
23
    protected function __construct()
24
    {
25
        //parent::__construct();
26
    }
27
 
28
    /**Does what PDO::ATTR_FETCH_TABLE_NAMES was supposed to do.
29
    */
30
    public function fetch()
31
    {
32
        if ($record = parent::fetch()) {
33
            foreach ($record as $key => $value) {
34
                $newKey = $key;
35
                if (strpos($newKey, '.') !== false) {
36
                    $newKey = substr($newKey, strpos($newKey, '.') + 1);
37
                }
38
                $newRecord[$newKey] = $value;
39
            }
40
        } else {
41
            return $record;
42
        }
43
        return $newRecord;
44
    }
45
 
46
    /**Does what PDO::ATTR_FETCH_TABLE_NAMES was supposed to do.
47
    */
48
    public function fetchAll()
49
    {
50
        $records = parent::fetchAll();
51
        if (is_array($records)) {
52
            $newRecords = array();
53
            for ($i = 0; $i < count($records); $i++) {
54
                foreach ($records[$i] as $key => $value) {
55
                    $newKey = $key;
56
                    if (strpos($newKey, '.') !== false) {
57
                        $newKey = substr($newKey, strpos($newKey, '.') + 1);
58
                    }
59
                    $newRecords[$i][$newKey] = $value;
60
                }
61
            }
62
            return $newRecords;
63
        } else {
64
            return $records;
65
        }
66
    }
67
}
68
 
69
require_once dirname(dirname(__FILE__)) . '/settings.php';
70
list($protocol, $address) = explode('://', DSN);
71
if (strpos($address, '@') === false) {
72
    if ($protocol == 'sqlite') {
73
        $protocol .= '2';
74
    }
75
    if (strpos($address, '/C:\\') === 0) {
76
        $address = ltrim($address, '/');
77
    }
78
    $db = new PDO($protocol . ':' . $address);
79
    $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement', array()));
80
} else {
81
    list($credentials, $address) = explode('@', $address);
82
        if (strpos($credentials, ':') === false) {
83
        $username = $credentials;
84
        $password = '';
85
    } else {
86
        list($username, $password) = explode(':', $credentials);
87
    }
88
    list($host,$database) = explode('/', $address);
89
    $db = new PDO($protocol . ':host=' . $host . ';dbname=' . $database, $username, $password);
90
}
91
?>