diff --git a/www/mysql_adapter.php b/www/mysql_adapter.php new file mode 100644 index 0000000..1a4402a --- /dev/null +++ b/www/mysql_adapter.php @@ -0,0 +1,80 @@ +raw = mysqli_connect('mysql', 'root', 'example', 'dragon_fire'); + + if (!$this->raw) + { + echo 'Connection failed
'; + echo 'Error number: ' . mysqli_connect_errno() . '
'; + echo 'Error message: ' . mysqli_connect_error() . '
'; + 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; + } +} + +?>