Page MenuHomeDevCentral

D3207.id8204.diff
No OneTemporary

D3207.id8204.diff

diff --git a/database/README.md b/database/README.md
--- a/database/README.md
+++ b/database/README.md
@@ -9,13 +9,14 @@
### MySQLi
-| Key | Value | |
-|----------|--------------------------------------|:--------:|
-| engine | MySQLiEngine class reference | |
-| host | The MySQL hostname, e.g. "localhost" | |
-| username | The MySQL user to use for connection | |
-| password | The clear text password to use | |
-| database | The default db to select for queries | optional |
+| Key | Value | |
+|------------|--------------------------------------|:---------------|
+| engine | MySQLiEngine class reference | |
+| host | The MySQL hostname, e.g. "localhost" | |
+| username | The MySQL user to use for connection | |
+| password | The clear text password to use | |
+| database | The default db to select for queries | (optional) |
+| fetch_mode | The default mode to fetch rows | `MYSQLI_ASSOC` |
For example:
@@ -29,6 +30,21 @@
]
```
+#### About fetch_mode parameter
+
+The `fetch_mode` parameter is used to determine how to represent results:
+
+ * `MYSQLI_ASSOC` will use column names
+ * `MYSQLI_NUM` will use an enumerated array (0, 1, 2, …)
+ * `MYSQLI_BOTH` will use both of them
+
+The code offers `MYSQLI_ASSOC` as default value to allow to directly represent
+a row result as API output and encourage to take care of the column names for
+better code maintenance. If you wish to switch to default MySQLi behavior,
+use `MYSQLI_BOTH` instead.
+
+Those constants are defined by the MySQLi extension.
+
## Legacy drivers
The mysql extension has been deprecated in PHP 5.7 and removed in PHP 7.
diff --git a/database/src/DatabaseEngine.php b/database/src/DatabaseEngine.php
--- a/database/src/DatabaseEngine.php
+++ b/database/src/DatabaseEngine.php
@@ -86,7 +86,16 @@
// Fetches first row of the query, and return the first element
// If there isn't any result row, returns an empty string.
- return $result->fetchRow()[0] ?? "";
+ $row = $result->fetchRow();
+ if ($row === null) {
+ return "";
+ }
+
+ $key = array_key_first($row);
+ return match ($key) {
+ null => "",
+ default => $row[$key],
+ };
}
///
diff --git a/database/src/Engines/MySQLiEngine.php b/database/src/Engines/MySQLiEngine.php
--- a/database/src/Engines/MySQLiEngine.php
+++ b/database/src/Engines/MySQLiEngine.php
@@ -28,6 +28,8 @@
*/
private mysqli_driver $driver;
+ private int $fetchMode = MYSQLI_ASSOC;
+
/**
* Initializes a new instance of the database abstraction class,
* for MySQLi engine.
@@ -83,7 +85,7 @@
return $result;
}
- return new MySQLiDatabaseResult($result);
+ return new MySQLiDatabaseResult($result, $this->fetchMode);
}
/**
@@ -134,6 +136,10 @@
$this->db->set_charset($encoding);
}
+ public function setFetchMode (int $mode) {
+ $this->fetchMode = $mode;
+ }
+
///
/// Engine mechanics methods
///
@@ -157,6 +163,7 @@
'username' => '',
'password' => '',
'database' => '',
+ 'fetch_mode' => MYSQLI_ASSOC,
];
}
@@ -193,6 +200,9 @@
// Restore report mode as previously configured
$driver->report_mode = $configuredReportMode;
+ // Extra configuration
+ $instance->setFetchMode($config["fetch_mode"]);
+
return $instance;
}
diff --git a/database/src/Result/MySQLiDatabaseResult.php b/database/src/Result/MySQLiDatabaseResult.php
--- a/database/src/Result/MySQLiDatabaseResult.php
+++ b/database/src/Result/MySQLiDatabaseResult.php
@@ -13,7 +13,7 @@
public function __construct (
private mysqli_result $result,
- private int $resultType = MYSQLI_BOTH
+ private int $resultType = MYSQLI_ASSOC
) { }
///
diff --git a/database/tests/Engines/MySQLiEngineTest.php b/database/tests/Engines/MySQLiEngineTest.php
--- a/database/tests/Engines/MySQLiEngineTest.php
+++ b/database/tests/Engines/MySQLiEngineTest.php
@@ -80,9 +80,9 @@
// First, we get associative arrays like [0 => 10, 10u => 10]
// ^ position ^ column name
- $this->assertEquals(10, $this->db->fetchRow($result)[0]);
- $this->assertEquals(20, $this->db->fetchRow($result)[0]);
- $this->assertEquals(30, $this->db->fetchRow($result)[0]);
+ $this->assertEquals(10, $this->db->fetchRow($result)[10]);
+ $this->assertEquals(20, $this->db->fetchRow($result)[10]);
+ $this->assertEquals(30, $this->db->fetchRow($result)[10]);
// Then, we get a null value
$this->assertEquals(null, $this->db->fetchRow($result));
@@ -92,6 +92,20 @@
$sql = "SELECT 10 as score, 50 as `limit`";
$result = $this->db->query($sql);
+ $expected = [
+ // By column name
+ "score" => 10,
+ "limit" => 50
+ ];
+
+ $this->assertEquals($expected, $this->db->fetchRow($result));
+ }
+
+ public function testArrayShapeForFetchRowWithFetchModeBoth () {
+ $sql = "SELECT 10 as score, 50 as `limit`";
+ $this->db->setFetchMode(MYSQLI_BOTH);
+ $result = $this->db->query($sql);
+
$expected = [
// By position
0 => 10,
@@ -105,6 +119,20 @@
$this->assertEquals($expected, $this->db->fetchRow($result));
}
+ public function testArrayShapeForFetchRowWithFetchModeEnum () {
+ $sql = "SELECT 10 as score, 50 as `limit`";
+ $this->db->setFetchMode(MYSQLI_NUM);
+ $result = $this->db->query($sql);
+
+ $expected = [
+ // By position
+ 0 => 10,
+ 1 => 50,
+ ];
+
+ $this->assertEquals($expected, $this->db->fetchRow($result));
+ }
+
public function testQueryWhenItSucceeds () {
$result = $this->db->query("DELETE FROM numbers");
diff --git a/database/tests/Result/MySQLiDatabaseResultTest.php b/database/tests/Result/MySQLiDatabaseResultTest.php
--- a/database/tests/Result/MySQLiDatabaseResultTest.php
+++ b/database/tests/Result/MySQLiDatabaseResultTest.php
@@ -12,6 +12,7 @@
protected function setUp () : void {
$db = new MySQLiEngine('localhost', '', '', 'test_keruald_db');
+ $db->setFetchMode(MYSQLI_BOTH);
$sql = "SELECT id, name, category FROM ships";
$this->result = $db->query($sql);

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 24, 03:07 (20 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2259024
Default Alt Text
D3207.id8204.diff (6 KB)

Event Timeline