Subversion-Projekte lars-tiefland.content-management

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// $Id: EbatNs_DatabaseProvider.php 4066 2011-11-03 08:13:59Z tiefland $
3
// $Log: EbatNs_DatabaseProvider.php,v $
4
// Revision 1.2  2008-05-02 15:04:05  carsten
5
// Initial, PHP5
6
//
7
//
8
class EbatNs_DatabaseConfig
9
{
10
    protected $host;
11
 
12
    protected $database;
13
 
14
    protected $user;
15
 
16
    protected $password;
17
 
18
    function setConfig ($host, $db, $user, $password)
19
    {
20
        $this->host = $host;
21
        $this->database = $db;
22
        $this->user = $user;
23
        $this->password = $password;
24
    }
25
}
26
 
27
class EbatNs_DatabaseProvider extends EbatNs_DatabaseConfig
28
{
29
    protected $dbHandle;
30
 
31
    function __construct()
32
    {
33
    }
34
 
35
    function getConnection ()
36
    {
37
        if ($this->dbHandle === null)
38
        {
39
            $this->dbHandle = mysql_pconnect($this->host, $this->user, $this->password, 0);
40
            mysql_select_db($this->database, $this->dbHandle);
41
        }
42
        return $this->dbHandle;
43
    }
44
 
45
    function getGeneratedId ()
46
    {
47
        return mysql_insert_id($this->getConnection());
48
    }
49
 
50
    function executeInsert ($table, $rowData)
51
    {
52
        $sql = 'insert into ' . $table . ' ';
53
        $sql .= '(' . join(',', array_keys($rowData)) . ') ';
54
        $sql .= 'values (';
55
 
56
        foreach ($rowData as $k => $v)
57
        {
58
            $rowData[$k] = "'" . addslashes($v) . "'";
59
        }
60
 
61
        $sql .= join(',', $rowData) . ')';
62
        $this->executeSql($sql);
63
        return $this->getGeneratedId();
64
    }
65
 
66
    function executeUpdate ($table, $rowData, $priKeyName, $priKeyValue, $extraCondition = null)
67
    {
68
        $sql = 'update ' . $table . ' set ';
69
 
70
        if (array_key_exists($priKeyName, $rowData))
71
            unset($rowData[$priKeyName]);
72
 
73
        $updateData = array();
74
        foreach ($rowData as $k => $v)
75
        {
76
            $updateData[] = $k . "= '" . addslashes($v) . "'";
77
        }
78
 
79
        $sql .= join(',', $updateData);
80
        $sql .= " where $priKeyName ='$priKeyValue'";
81
        if ($extraCondition)
82
            $sql .= ' and ' . $extraCondition;
83
        return $this->executeSqlNoQuery($sql, null);
84
    }
85
 
86
    function executeDelete ($table, $priKeyName, $priKeyValue)
87
    {
88
        $sql = 'delete from ' . $table;
89
        $sql .= ' where ' . $priKeyName . "='" . $priKeyValue . "'";
90
        return $this->executeSqlNoQuery($sql);
91
    }
92
 
93
    function executeSqlNoQuery ($sql, $dbHandle = null)
94
    {
95
        $rs = $this->executeSql($sql, $dbHandle);
96
        if ($rs)
97
        {
98
            if ($dbHandle === null)
99
            {
100
                return mysql_affected_rows($this->dbHandle);
101
            } else {
102
                return mysql_affected_rows($dbHandle);
103
            }
104
        } else {
105
            return 0;
106
        }
107
    }
108
 
109
    // executes a sql-statement against the db. Any errors will be printed on screen !
110
    function executeSql ($sql, $dbHandle = null)
111
    {
112
        if ($dbHandle == null)
113
            $dbHandle = $this->getConnection();
114
 
115
        $rs = mysql_query($sql, $dbHandle);
116
 
117
        return $rs;
118
    }
119
 
120
    // Execute the statement and then fetches ALL rows
121
    function querySqlSet ($sql, $dbHandle = null)
122
    {
123
        if ($dbHandle === null)
124
        {
125
            $dbHandle = $this->getConnection();
126
        }
127
 
128
        $rs = $this->executeSql($sql, $dbHandle);
129
        $rows = array();
130
        while ($row = @mysql_fetch_assoc($rs))
131
        {
132
            if ($row)
133
                $rows[] = $row;
134
        }
135
 
136
        @mysql_free_result($rs);
137
 
138
        return $rows;
139
    }
140
 
141
    function querySql ($sql, $dbHandle = null)
142
    {
143
        if ($dbHandle === null)
144
        {
145
            $dbHandle = $this->getConnection();
146
        }
147
 
148
        $rs = $this->executeSql($sql, $dbHandle);
149
 
150
        $row = @mysql_fetch_assoc($rs);
151
        @mysql_free_result($rs);
152
 
153
        return $row;
154
    }
155
}
156
?>