Skip to content

Commit 160c051

Browse files
committed
Initial Release
1 parent 282ef02 commit 160c051

File tree

6 files changed

+555
-0
lines changed

6 files changed

+555
-0
lines changed

berry/mysql.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
require_once(__DIR__ . "/mysql/MySQLConnection.php");
4+
require_once(__DIR__ . "/mysql/MySQLCommand.php");
5+
require_once(__DIR__ . "/mysql/MySQLCommandParameter.php");
6+
require_once(__DIR__ . "/mysql/MySQLCommandParameters.php");
7+
require_once(__DIR__ . "/mysql/MySQLDataReader.php");
8+
9+
?>

berry/mysql/MySQLCommand.php

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<?php
2+
3+
/*
4+
MIT License
5+
6+
Copyright (c) 2022 Nikos Siatras
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
*/
26+
27+
class MySQLCommand
28+
{
29+
30+
private MySQLConnection $fMySQLConnection; // MySQLConnection of this command
31+
private String $fQuery; // Query to execute
32+
private $fPreparedStatement = NULL; // The prepared statement of this command
33+
public $Parameters = NULL; // Command parameters
34+
35+
/**
36+
* Constructs a new MySQLCommand
37+
* @param MySQLConnection $mySQLConnection is the MySQLConnection
38+
* @param string $query the query to execute
39+
*/
40+
public function __construct(MySQLConnection $mySQLConnection, String $query = "")
41+
{
42+
$this->fMySQLConnection = $mySQLConnection;
43+
$this->Parameters = new MySQLCommandParameters($this);
44+
$this->fQuery = $query;
45+
}
46+
47+
/**
48+
* Execute query and return the number of affected rows.
49+
* @return the number of affected rows.
50+
*/
51+
public function ExecuteQuery()
52+
{
53+
if (sizeof($this->Parameters->getParameters()) > 0) // Query with parameters (Prepared Statement)
54+
{
55+
$this->fPreparedStatement = $this->fMySQLConnection->getLink()->stmt_init();
56+
57+
if (!$this->fPreparedStatement->prepare($this->fQuery))
58+
{
59+
throw new Exception("ExecuteQuery failed to prepare statement: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error);
60+
}
61+
else
62+
{
63+
// Prepared statement created.
64+
// Bind parameters and execute
65+
$this->BindParametersToQuery();
66+
if (!$this->fPreparedStatement->execute())
67+
{
68+
throw new Exception("Execute query failed: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error);
69+
}
70+
else
71+
{
72+
return $this->fMySQLConnection->getLink()->affected_rows;
73+
}
74+
}
75+
}
76+
else // Straight mySQL Query withour parameters
77+
{
78+
if (!$this->fMySQLConnection->getLink()->query($this->fQuery))
79+
{
80+
throw new Exception("Execute query failed: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error);
81+
}
82+
else
83+
{
84+
return $this->fMySQLConnection->getLink()->affected_rows;
85+
}
86+
}
87+
}
88+
89+
/**
90+
* Execute reader command.
91+
* Returns MySQLDataReader
92+
* @return MySQLDataReader
93+
*/
94+
public function ExecuteReader()
95+
{
96+
$this->fPreparedStatement = NULL;
97+
98+
if (sizeof($this->Parameters->getParameters()) > 0) // Query with parameters (Prepared Statement)
99+
{
100+
$this->fPreparedStatement = $this->fMySQLConnection->getLink()->stmt_init();
101+
102+
if (!$this->fPreparedStatement->prepare($this->fQuery))
103+
{
104+
throw new Exception("ExecuteReader failed to prepare statement: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error);
105+
}
106+
else
107+
{
108+
// Prepared statement created.
109+
// Bind parameters and execute reader
110+
$this->BindParametersToQuery();
111+
if (!$this->fPreparedStatement->execute())
112+
{
113+
throw new Exception("Execute reader failed: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error);
114+
}
115+
116+
return new MySQLDataReader($this->fPreparedStatement);
117+
}
118+
}
119+
else // Simple query withour prepared statement and parameters
120+
{
121+
if (!$result = $this->fMySQLConnection->getLink()->query($this->fQuery))
122+
{
123+
throw new Exception("Execute reader failed: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error);
124+
}
125+
else
126+
{
127+
return new MySQLDataReader($result);
128+
}
129+
}
130+
}
131+
132+
/**
133+
* Sets a new query for the MySQLCommand
134+
* $query: is the query to set
135+
* @param type $query
136+
*/
137+
public function setQuery($query)
138+
{
139+
$this->fQuery = $query;
140+
}
141+
142+
/**
143+
* Return's the query been given to this command
144+
* @return string the command's query
145+
*/
146+
public function getQuery()
147+
{
148+
return $this->fQuery;
149+
}
150+
151+
/**
152+
* Returns the MySQLConnection of this MySQLCommand
153+
* @return type MySQLConnection
154+
*/
155+
public function getMySQLConnection()
156+
{
157+
return $this->fMySQLConnection;
158+
}
159+
160+
/**
161+
* Return's the last inserted ID
162+
* @return type
163+
*/
164+
public function getLastInsertID()
165+
{
166+
return $this->fMySQLConnection->getLink()->insert_id;
167+
}
168+
169+
/**
170+
* Bind parameter values to query.
171+
* This method should be called every time before query execution.
172+
* @return type
173+
*/
174+
private function BindParametersToQuery()
175+
{
176+
$parameterTypes = "";
177+
$ar = array();
178+
179+
foreach ($this->Parameters->getParameters() as $key => $value)
180+
{
181+
$parameterTypes .= $value->Type;
182+
}
183+
$ar[] = $parameterTypes;
184+
185+
foreach ($this->Parameters->getParameters() as $key => $value)
186+
{
187+
$ar[] = &$value->Value;
188+
}
189+
190+
call_user_func_array(array($this->fPreparedStatement, 'bind_param'), $ar);
191+
}
192+
193+
}
194+
195+
?>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
MIT License
5+
6+
Copyright (c) 2022 Nikos Siatras
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
*/
26+
27+
class MySQLCommandParameter
28+
{
29+
30+
public $Index;
31+
public $Value;
32+
public $Type;
33+
public $Length;
34+
35+
public function __construct($paramIndex, $value, $type, $length = NULL)
36+
{
37+
$this->Index = $paramIndex;
38+
$this->Value = $value;
39+
$this->Type = $type;
40+
$this->Length = $length;
41+
}
42+
43+
}
44+
45+
?>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/*
4+
MIT License
5+
6+
Copyright (c) 2022 Nikos Siatras
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
*/
26+
27+
class MySQLCommandParameters
28+
{
29+
30+
private $fParameters;
31+
private MySQLCommand $fMyCommand;
32+
33+
public function __construct(MySQLCommand $myCommand)
34+
{
35+
$this->fParameters = array();
36+
$this->fMyCommand = $myCommand;
37+
}
38+
39+
/**
40+
*
41+
* @param type $parameter
42+
* @param type $type
43+
*/
44+
private function Add($paramIndex, $value, $type, $length = NULL)
45+
{
46+
$parameter = new MySQLCommandParameter($paramIndex, $value, $type, $length);
47+
$this->fParameters[$paramIndex] = $parameter;
48+
}
49+
50+
/**
51+
* Adds or sets a string parameter.
52+
* @param type $paramIndex
53+
* @param type $value
54+
* @param type $length
55+
*/
56+
public function setString($paramIndex, $value, $length = NULL)
57+
{
58+
$this->Add($paramIndex, $value, "s", $length);
59+
}
60+
61+
/**
62+
* Adds or sets an integer parameter.
63+
* @param type $paramIndex
64+
* @param type $value
65+
* @param type $length
66+
*/
67+
public function setInteger($paramIndex, $value, $length = NULL)
68+
{
69+
$this->Add($paramIndex, $value, "i", $length);
70+
}
71+
72+
/**
73+
* Adds or sets a double parameter.
74+
* @param type $paramIndex
75+
* @param type $value
76+
* @param type $length
77+
*/
78+
public function setDouble($paramIndex, $value, $length = NULL)
79+
{
80+
$this->Add($paramIndex, $value, "d", $length);
81+
}
82+
83+
/**
84+
* Adds or sets a blob parameter.
85+
* @param type $paramIndex
86+
* @param type $value
87+
* @param type $length
88+
*/
89+
public function setBlob($paramIndex, $value, $length = NULL)
90+
{
91+
$this->Add($paramIndex, $value, "b", $length);
92+
}
93+
94+
/**
95+
* Remove all parameters
96+
*/
97+
public function Clear()
98+
{
99+
$this->fParameters = array();
100+
}
101+
102+
public function getParameters()
103+
{
104+
return $this->fParameters;
105+
}
106+
107+
}
108+
109+
?>

0 commit comments

Comments
 (0)