feat: ✨ add first draft of MySQL adapter class
This commit is contained in:
parent
1146e69f84
commit
8f9d94faab
1 changed files with 80 additions and 0 deletions
80
www/mysql_adapter.php
Normal file
80
www/mysql_adapter.php
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class MySQLAdapter {
|
||||||
|
public $raw;
|
||||||
|
|
||||||
|
function __construct() {
|
||||||
|
$this->raw = mysqli_connect('mysql', 'root', 'example', 'dragon_fire');
|
||||||
|
|
||||||
|
if (!$this->raw)
|
||||||
|
{
|
||||||
|
echo 'Connection failed<br>';
|
||||||
|
echo 'Error number: ' . mysqli_connect_errno() . '<br>';
|
||||||
|
echo 'Error message: ' . mysqli_connect_error() . '<br>';
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _exec($qery, $argtypes, ...$args) {
|
||||||
|
$stmt = $this->raw->prepare($qery);
|
||||||
|
$stmt->bind_param($argtypes, ...$args);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
return $stmt->get_result();
|
||||||
|
}
|
||||||
|
|
||||||
|
function _prepare_post_data($post_data) {
|
||||||
|
if($post_data == null) {
|
||||||
|
$post_data = [
|
||||||
|
"found" => false
|
||||||
|
];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$post_data["found"] = true;
|
||||||
|
$post_data["post_metadata"] = json_decode($post_data["post_metadata"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $post_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_post_by_path($post_path, $with_subposts = true) {
|
||||||
|
$qry = "SELECT * FROM posts WHERE post_path = ?";
|
||||||
|
|
||||||
|
$post_path = chop($post_path, '/');
|
||||||
|
|
||||||
|
$post_data = $this->_prepare_post_data($this->_exec($qry, "s", $post_path)->fetch_assoc());
|
||||||
|
|
||||||
|
if($with_subposts) {
|
||||||
|
$post_data['subposts'] = $this->get_subposts_by_path($post_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $post_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_subposts_by_path($path) {
|
||||||
|
global $sql;
|
||||||
|
|
||||||
|
$path = chop($path, '/');
|
||||||
|
|
||||||
|
$path_depth = substr_count($path, "/");
|
||||||
|
|
||||||
|
$qry = "SELECT post_path, post_metadata
|
||||||
|
FROM posts
|
||||||
|
WHERE (post_path LIKE CONCAT(?,'/%'))
|
||||||
|
AND post_path_depth = ?
|
||||||
|
ORDER BY post_create_time DESC
|
||||||
|
LIMIT 10";
|
||||||
|
|
||||||
|
$post_data = $this->_exec($qry, "si", $path, $path_depth)->fetch_all(MYSQLI_ASSOC);
|
||||||
|
|
||||||
|
$fn = function($data) {
|
||||||
|
return $this->_prepare_post_data($data);
|
||||||
|
};
|
||||||
|
|
||||||
|
$post_data = array_map($fn, $post_data);
|
||||||
|
|
||||||
|
return $post_data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
Loading…
Add table
Add a link
Reference in a new issue