Skip to content

Commit 38eac63

Browse files
committed
MySQL refactored
1 parent 79c93fd commit 38eac63

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

berry/mysql/MySQLCommandParameters.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private function Add(int $paramIndex, $value, string $type, $length = NULL): voi
2727
* @param type $value
2828
* @param type $length
2929
*/
30-
public function setString(int $paramIndex, string $value, $length = NULL): void
30+
public function setString(int $paramIndex, ?string $value, $length = NULL): void
3131
{
3232
$this->Add($paramIndex, $value, "s", $length);
3333
}
@@ -38,7 +38,7 @@ public function setString(int $paramIndex, string $value, $length = NULL): void
3838
* @param type $value
3939
* @param type $length
4040
*/
41-
public function setInteger(int $paramIndex, int $value, $length = NULL): void
41+
public function setInteger(int $paramIndex, ?int $value, $length = NULL): void
4242
{
4343
$this->Add($paramIndex, $value, "i", $length);
4444
}
@@ -49,7 +49,7 @@ public function setInteger(int $paramIndex, int $value, $length = NULL): void
4949
* @param type $value
5050
* @param type $length
5151
*/
52-
public function setDouble(int $paramIndex, float $value, $length = NULL): void
52+
public function setDouble(int $paramIndex, ?float $value, $length = NULL): void
5353
{
5454
$this->Add($paramIndex, $value, "d", $length);
5555
}
@@ -65,6 +65,21 @@ public function setBlob(int $paramIndex, $value, $length = NULL): void
6565
$this->Add($paramIndex, $value, "b", $length);
6666
}
6767

68+
/**
69+
* Adds or sets the current time measured in the number of seconds
70+
* since the Unix Epoch (January 1 1970 00:00:00 GMT).
71+
* This method should be used with DateTime MySql variables.
72+
* @param int $paramIndex
73+
* @param int $secondsSinceUnixEpoch is the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
74+
* @param type $length
75+
* @return void
76+
*/
77+
public function setDateTime(int $paramIndex, ?int $secondsSinceUnixEpoch): void
78+
{
79+
$datetime = ($secondsSinceUnixEpoch != null) ? date("Y-m-d H:i:s", $secondsSinceUnixEpoch) : null;
80+
$this->Add($paramIndex, $datetime, "s", NULL);
81+
}
82+
6883
/**
6984
* Remove all parameters
7085
*/
@@ -77,7 +92,6 @@ public function getParameters(): array
7792
{
7893
return $this->fParameters;
7994
}
80-
8195
}
8296

8397
?>

berry/mysql/MySQLConnection.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class MySQLConnection
44
{
55

66
private mysqli $fLink; // The mysqli link with the selected MySQL database
7+
private bool $fConnectionIsOpen = false;
78

89
/**
910
* Constructs a new MySQLConnection
@@ -26,6 +27,7 @@ public function __construct(string $serverIP, string $database, string $user, st
2627
{
2728
$this->fLink->set_charset($charset);
2829
}
30+
$this->fConnectionIsOpen = true;
2931
}
3032

3133
/**
@@ -44,7 +46,11 @@ public function EscapeString(string $string): string
4446
*/
4547
public function Close(): void
4648
{
47-
$this->fLink->close();
49+
if ($this->fConnectionIsOpen)
50+
{
51+
$this->fLink->close();
52+
$this->fConnectionIsOpen = false;
53+
}
4854
}
4955

5056
/**
@@ -55,7 +61,6 @@ public function getLink(): mysqli
5561
{
5662
return $this->fLink;
5763
}
58-
5964
}
6065

6166
?>

berry/mysql/MySQLDataReader.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,40 @@ public function Close()
7575
}
7676

7777
/**
78-
* Return a readed value
78+
* Return a read value
7979
* @param type $index
8080
* @return type
8181
*/
82-
public function getValue($index)
82+
public function getValue(int $index)
8383
{
8484
return $this->fReadValues[$index];
8585
}
8686

87+
/**
88+
* DATETIME is always stored and returned as UTC, regardless of what your system's timezone is.
89+
* This method reads a MySQL DATETIME value into a PHP DateTime Object and converts the TimeZone
90+
* to the given TimeZone.
91+
* @param int $index
92+
* @param DateTimeZone $timeZone
93+
* @return DateTime
94+
*/
95+
public function getDateTime(int $index, DateTimeZone $timeZone): DateTime
96+
{
97+
$dt = new DateTime($this->fReadValues[$index], new DateTimeZone('UTC'));
98+
$dt->setTimezone($timeZone);
99+
return $dt;
100+
}
101+
102+
/**
103+
* Return the read datetime value
104+
* @param int $index
105+
* @return DateTimeF
106+
*/
107+
public function getDateTimeUTC(int $index): DateTime
108+
{
109+
$dt = new DateTime($this->fReadValues[$index]);
110+
return $dt;
111+
}
87112
}
88113

89114
?>

0 commit comments

Comments
 (0)