1: <?php
2:
3: namespace database;
4:
5: include_once "../../global.inc.php";
6: include_once PATH_CONFIG . "/config.db.php";
7:
8: /**
9: * The SQLManager manage the SQL-Connection for an API
10: * @author stubbfel
11: * @since 20.06.2013
12: */
13: abstract class SqlManager {
14:
15: /**
16: * Variable for address of the server
17: * @var string
18: */
19: private $serverAddress;
20:
21: /**
22: * Variable for name of the databaseuser
23: * @var string
24: */
25: private $userName;
26:
27: /**
28: * Variable for name of the database
29: * @var string
30: */
31: private $dbName;
32:
33: /**
34: * Variable for password of the user
35: * @var string
36: */
37: private $userPw;
38:
39: /**
40: * Variable for link of the connection to the server
41: * @var connection
42: */
43: private $link;
44:
45: /**
46: * String for an and-operrator
47: * @var string
48: */
49: protected static $andTerm = " and ";
50:
51: /**
52: * String for an or-operrator
53: * @var string
54: */
55: protected static $orTerm = " or ";
56:
57: /**
58: * String for quotes in a query
59: * @var string
60: */
61: protected static $quoteTerm = "\"";
62:
63: /**
64: * String for open Bracket in a query
65: * @var string
66: */
67: protected static $openBracket = "(";
68:
69: /**
70: * String for close Bracket in a query
71: * @var string
72: */
73: protected static $closeBracket = ")";
74:
75: /**
76: * Default-Constructor
77: */
78: public function __construct() {
79: $this->serverAddress = \config\DBConfig::$sqlServer;
80: $this->dbName = \config\DBConfig::$sqlDBName;
81: $this->userName = \config\DBConfig::$sqlDBUser;
82: $this->userPw = \config\DBConfig::$sqlDBUserPW
83:
84: ;
85: }
86:
87: /**
88: * Default-DEConstructor
89: */
90: public function __destruct() {
91:
92: // close connection
93: $this->closeConnection();
94:
95: // delete connection parameter
96: unset($this->serverAddress);
97: unset($this->dbName);
98: unset($this->userName);
99: unset($this->userPW
100:
101: );
102: }
103:
104: /**
105: * Method setup the connection to the Database
106: */
107: public function connect() {
108: $this->link = mysql_connect($this->serverAddress , $this->userName , $this->userPw);
109: if (!$this->link) {
110: exit("No Connection: " . mysql_error());
111: }
112: $selected = mysql_select_db($this->dbName , $this->link);
113: if (!$selected) {
114: exit("No DB: " . mysql_error());
115: }
116: }
117:
118: /**
119: * Method close the connection
120: */
121: public function closeConnection() {
122: if (
123:
124: $this->link) {
125: mysql_close($this->link);
126: unset($this->link);
127: $this->link = null;
128: }
129: }
130:
131: /**
132: * Method send a query to the Datebase and return the result
133: * @param string $query
134:
135:
136: * @return result[num][assoc]
137: */
138: protected function query($query) {
139:
140: // send error
141: $mysqlResult = mysql_query($query, $this->link );
142: if (!$mysqlResult) {
143: // echo $query;
144: exit("Query error: " . mysql_error());
145: }
146:
147: // fetch result
148: $rowNums = mysql_num_rows($mysqlResult);
149: $result = array();
150: for ($i = 0; $i < $rowNums; $i++) {
151: $row = mysql_fetch_assoc($mysqlResult);
152: $result[$i] = $row;
153: }
154:
155: // call gc
156: mysql_free_result($mysqlResult);
157:
158: return $result;
159: }
160: }
161:
162: ?>
163: