Add database connexion #6
Merged
maxime.batista
merged 7 commits from setup-database
into master
1 year ago
@ -1,12 +1,19 @@
|
||||
import ReactDOM from "react-dom/client";
|
||||
import React from "react";
|
||||
|
||||
interface DisplayResultsProps {
|
||||
results: readonly { name: string, description: string}[]
|
||||
}
|
||||
|
||||
export default function DisplayResults({password, username}: any) {
|
||||
export default function DisplayResults({results}: DisplayResultsProps) {
|
||||
const list = results
|
||||
.map(({name, description}) =>
|
||||
<div>
|
||||
<p>username: {name}</p>
|
||||
<p>description: {description}</p>
|
||||
</div>
|
||||
)
|
||||
return (
|
||||
<div>
|
||||
<p>username: {username}</p>
|
||||
<p>password: {password}</p>
|
||||
{list}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
|||||||
<?php
|
|||||||
|
|||||||
/**
|
|||||||
* @return PDO The PDO instance of the configuration's database connexion.
|
|||||||
*/
|
|||||||
function get_database(): PDO {
|
|||||||
// defined by profiles.
|
|||||||
global $data_source_name;
|
|||||||
$pdo = new PDO($data_source_name, DATABASE_USER, DATABASE_PASSWORD);
|
|||||||
|
|||||||
$database_exists = $pdo->query("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'")->fetchColumn() > 0;
|
|||||||
|
|||||||
if ($database_exists) {
|
|||||||
return $pdo;
|
|||||||
}
|
|||||||
|
|||||||
foreach (scandir(__DIR__) as $file) {
|
|||||||
if (preg_match("/.*\.sql$/i", $file)) {
|
|||||||
$content = file_get_contents(__DIR__ . "/" . $file);
|
|||||||
|
|||||||
$pdo->exec($content);
|
|||||||
}
|
|||||||
}
|
|||||||
|
|||||||
|
|||||||
maxime.batista marked this conversation as resolved
|
|||||||
return $pdo;
|
|||||||
}
|
|||||||
|
|||||||
|
|||||||
|
@ -0,0 +1,8 @@
|
||||
|
||||
-- drop tables here
|
||||
DROP TABLE IF EXISTS FormEntries;
|
||||
|
||||
CREATE TABLE FormEntries(name varchar, description varchar);
|
||||
|
||||
|
||||
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
use \PDO;
|
||||
|
||||
class Connexion {
|
||||
|
||||
private PDO $pdo;
|
||||
|
||||
/**
|
||||
* @param PDO $pdo
|
||||
*/
|
||||
public function __construct(PDO $pdo)
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a request
|
||||
* @param string $query
|
||||
* @param array $args
|
||||
* @return void
|
||||
*/
|
||||
public function exec(string $query, array $args) {
|
||||
$stmnt = $this->pdo->prepare($query);
|
||||
foreach ($args as $name => $value) {
|
||||
$stmnt->bindValue($name, $value[0], $value[1]);
|
||||
}
|
||||
$stmnt->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a request, and return the returned rows
|
||||
* @param string $query the SQL request
|
||||
* @param array $args an array containing the arguments label, value and type: ex: `[":label" => [$value, PDO::PARAM_TYPE]`
|
||||
* @return array the returned rows of the request
|
||||
*/
|
||||
public function fetch(string $query, array $args): array {
|
||||
$stmnt = $this->pdo->prepare($query);
|
||||
foreach ($args as $name => $value) {
|
||||
$stmnt->bindValue($name, $value[0], $value[1]);
|
||||
}
|
||||
$stmnt->execute();
|
||||
return $stmnt->fetchAll();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Gateway;
|
||||
|
||||
use PDO;
|
||||
use App\Connexion;
|
||||
|
||||
/**
|
||||
* A sample gateway, that stores the sample form's result.
|
||||
*/
|
||||
class FormResultGateway {
|
||||
|
||||
private Connexion $con;
|
||||
|
||||
public function __construct(Connexion $con) {
|
||||
$this->con = $con;
|
||||
}
|
||||
|
||||
|
||||
function insert(string $username, string $description) {
|
||||
$this->con->exec(
|
||||
"INSERT INTO FormEntries VALUES (:name, :description)",
|
||||
[
|
||||
":name" => [$username, PDO::PARAM_STR],
|
||||
"description" => [$description, PDO::PARAM_STR]
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
function listResults(): array {
|
||||
return $this->con->fetch("SELECT * FROM FormEntries", []);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue