1: <?php
2:
3: namespace webfilesframework\core\datasystem\database;
4:
5: use webfilesframework\core\datastore\types\database\resultHandler\MIResultHandler;
6: use webfilesframework\core\datastore\types\database\resultHandler\MMysqlResultHandler;
7: use webfilesframework\MWebfilesFrameworkException;
8:
9: /**
10: * description
11: *
12: * @author Sebastian Monzel < mail@sebastianmonzel.de >
13: * @since 0.1.7
14: */
15: class MDatabaseConnection
16: {
17: private $host = '127.0.0.1';
18:
19: private $databaseName = 'webfiles';
20: private $tablePrefix = 'default_';
21:
22: private $username = 'root';
23: private $password = '';
24:
25: /** @var \mysqli $connection */
26: private $connection;
27:
28: static private $instanceArray = array();
29:
30: /**
31: *
32: * @param string $host the host to connect to.
33: * @param string $database the database to connect to.
34: * @param string $tablePrefix the table prefix used for the actual connection to the database.
35: * @param string $username the username for the connection.
36: * @param string $password the password for the connection.
37: */
38: public function __construct(
39: $host = null,
40: $database = null,
41: $tablePrefix = null,
42: $username = null,
43: $password = null)
44: {
45:
46: if ($host != null) {
47: $this->host = $host;
48: }
49: if ($database != null) {
50: $this->databaseName = $database;
51: }
52: if ($tablePrefix != null) {
53: $this->tablePrefix = $tablePrefix;
54: }
55: if ($username != null) {
56: $this->username = $username;
57: }
58: if ($password != null) {
59: $this->password = $password;
60: }
61:
62: $this->connect();
63: }
64:
65: /**
66: * multiton to administrate the instances of the connection
67: * and global accessing
68: *
69: * @param String $instanceName
70: * @return object
71: */
72: static public function getInstance($instanceName)
73: {
74: if (!isset(self::$instanceArray[$instanceName])) {
75: self::$instanceArray[$instanceName] = new MDatabaseConnection();
76: }
77: return self::$instanceArray[$instanceName];
78: }
79:
80: /**
81: * connects to the database server
82: */
83: public function connect()
84: {
85: $this->connection = @new \mysqli(
86: $this->host,
87: $this->username,
88: $this->password,
89: $this->databaseName
90: );
91:
92: if (!$this->connection->connect_errno) {
93: $this->connection->autocommit(1);
94: return true;
95: } else {
96: echo mysqli_connect_error();
97: return false;
98: }
99: }
100:
101: /**
102: * closes the connection
103: */
104: public function close()
105: {
106: @$this->connection->close();
107: }
108:
109: /**
110: * queries the database with the specified sql command
111: *
112: * @param $sqlCommand
113: *
114: * @return bool|\mysqli_result
115: * @throws MWebfilesFrameworkException
116: */
117: public function query($sqlCommand)
118: {
119: $result = $this->connection->query($sqlCommand);
120: if ($this->getError() != null ) {
121: throw new MWebfilesFrameworkException("Error orrured on executing sql: " . $this->getError() .", SQL: " . $sqlCommand);
122: }
123:
124: return $result;
125: }
126:
127: /**
128: * @param $sqlCommand
129: *
130: * @return MIResultHandler
131: * @throws MWebfilesFrameworkException
132: */
133: public function queryAndHandle($sqlCommand)
134: {
135: $result = $this->query($sqlCommand);
136: if ($this->getError() != null ) {
137: throw new MWebfilesFrameworkException("Error orrured on executing sql: " . $this->getError() .", SQL: " . $sqlCommand);
138: }
139: return new MMysqlResultHandler($result);
140: }
141:
142: /**
143: * prints last error of the connection
144: */
145: public function getError()
146: {
147: return $this->connection->error;
148: }
149:
150: /**
151: * returns id of a last insert query
152: *
153: * @return int
154: */
155: public function getInsertId()
156: {
157: return $this->connection->insert_id;
158: }
159:
160: /**
161: * returns the hostname of the databaseserver
162: *
163: * @access public
164: * @return String
165: */
166: public function getHost()
167: {
168: return $this->host;
169: }
170:
171: /**
172: * returns the name of the user connected to the database server
173: *
174: * @access public
175: * @return String
176: */
177: public function getUsername()
178: {
179: return $this->username;
180: }
181:
182: /**
183: * @return null|string
184: */
185: public function getPassword()
186: {
187: return $this->password;
188: }
189:
190: /**
191: * @return null|string
192: */
193: public function getDatabaseName()
194: {
195: return $this->databaseName;
196: }
197:
198: /**
199: * Sets the database for the given connection
200: * @access public
201: * @param $databaseName
202: */
203: public function setDatabaseName($databaseName)
204: {
205: $this->databaseName = $databaseName;
206: }
207:
208: /**
209: * Returns the table prefix used for the given
210: * connection.
211: */
212: public function getTablePrefix()
213: {
214: return $this->tablePrefix;
215: }
216:
217: }
218: