1
<?php
2
/*
3
  Belokan Micro Framework
4
  Licensed under GNU GPL
5
  http://gitorious.org/belokan
6
  ---
7
  Belokan/Db.php:
8
  Database system
9
*/
10
11
abstract class Belokan_Db {
12
  
13
  protected
14
    $_connection,
15
    $_persistent,
16
    $_host,
17
    $_port,
18
    $_user,
19
    $_pass,
20
    $_base,
21
    $_query,
22
    $_numberOfQueries,
23
    $_results,
24
    $_rows;
25
  
26
  public function __construct ($db_params)
27
  {
28
    $this->_connection = null;
29
    if (isset($db_params['persistent']))
30
      $this->_persistent = $db_params['persistent'];
31
    else
32
      $this->_persistent = null;
33
    if (isset($db_params['host'])) $this->_host = $db_params['host'];
34
    else $this->_host = null;
35
    if (isset($db_params['port'])) $this->_port = $db_params['port'];
36
    else $this->_port = null;
37
    if (isset($db_params['user'])) $this->_user = $db_params['user'];
38
    else $this->_user = null;
39
    if (isset($db_params['pass'])) $this->_pass = $db_params['pass'];
40
    else $this->_pass = null;
41
    if (isset($db_params['base'])) $this->_base = $db_params['base'];
42
    else $this->_base = null;
43
    $this->_query = null;
44
    $this->_numberOfQueries = 0;
45
    $this->_results = null;
46
    $this->_rows = array();
47
  }
48
49
  abstract function connect ();
50
  
51
  public function reconnect ()
52
  {
53
    if (!$this->isConnected()) $this->connect();
54
  }
55
  
56
  public function isConnected ()
57
  {
58
    return $this->_connection != null;
59
  }
60
  
61
  public function query ($query)
62
  {
63
    $this->_query = $query;
64
    return $this;
65
  }
66
  
67
  abstract function execute ();
68
  
69
  abstract function execQuery ($query, $update_query=false);
70
  
71
  abstract function seek ($row);
72
  
73
  abstract function lastAutoIncrementValue ();
74
  
75
  abstract function fetchRows ($fetch_object=false);
76
  
77
  abstract function fetchRow ($fetch_object=false);
78
  
79
  abstract function numberOfRows ();
80
  
81
  abstract function affectedRows ();
82
  
83
  public function bindParam ($param, $value)
84
  {
85
    $this->_query = str_replace(':'.$param, $value, $this->_query);
86
    return $this;
87
  }
88
  
89
  public function bindParams ($params_values)
90
  {
91
    krsort($params_values);
92
    $params = array();
93
    $values = array();
94
    foreach ($params_values as $param => $value) {
95
      $params[] = ':'.$param;
96
      $values[] = $value;
97
    }
98
    $this->_query = str_replace($params, $values, $this->_query);
99
    return $this;
100
  }
101
  
102
  public function beginTransaction ()
103
  {
104
    $this->execQuery('BEGIN');
105
  }
106
  
107
  public function commit ()
108
  {
109
    $this->execQuery('COMMIT');
110
  }
111
  
112
  public function endTransaction ()
113
  {
114
    $this->execQuery('COMMIT');
115
  }
116
  
117
  public function rollBack ()
118
  {
119
    $this->execQuery('ROLLBACK');
120
  }
121
  
122
  public function getRawResults ()
123
  {
124
    return $this->_results;
125
  }
126
  
127
  public function numberOfQueries ()
128
  {
129
    return $this->_numberOfQueries;
130
  }
131
132
  abstract function escapeString ($str);
133
  
134
}