2014-12-03 21:08:08 +01:00
|
|
|
<?php
|
2014-12-17 13:48:03 +01:00
|
|
|
/**
|
|
|
|
* This software is governed by the CeCILL-B license. If a copy of this license
|
|
|
|
* is not distributed with this file, you can obtain one at
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
|
|
|
|
*
|
|
|
|
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
|
2016-08-04 21:51:37 +02:00
|
|
|
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
|
2014-12-17 13:48:03 +01:00
|
|
|
*
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
|
|
|
|
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
|
|
|
|
*
|
|
|
|
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
|
|
|
|
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
|
|
|
*/
|
2014-12-03 21:08:08 +01:00
|
|
|
namespace Framadate;
|
|
|
|
|
2015-01-18 16:14:45 +01:00
|
|
|
use PDO;
|
|
|
|
|
2014-12-21 23:48:22 +01:00
|
|
|
class FramaDB {
|
2014-12-03 21:08:08 +01:00
|
|
|
/**
|
|
|
|
* PDO Object, connection to database.
|
2021-12-20 17:46:50 +01:00
|
|
|
* @var PDO
|
2014-12-03 21:08:08 +01:00
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
private $pdo;
|
2014-12-03 21:08:08 +01:00
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function __construct(string $connection_string, string $user, string $password) {
|
|
|
|
$this->pdo = new PDO($connection_string, $user, $password);
|
|
|
|
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
|
|
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
2014-12-03 21:08:08 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 01:33:56 +01:00
|
|
|
/**
|
2021-12-20 17:46:50 +01:00
|
|
|
* @return PDO Connection to database
|
2014-12-31 01:33:56 +01:00
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function getPDO(): PDO
|
|
|
|
{
|
2014-12-31 01:33:56 +01:00
|
|
|
return $this->pdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all tables in database.
|
|
|
|
*
|
|
|
|
* @return array The array of table names
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function allTables(): array
|
|
|
|
{
|
|
|
|
return $this->pdo->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN);
|
2014-12-03 21:08:08 +01:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
/**
|
|
|
|
* @return \PDOStatement|false
|
|
|
|
*/
|
|
|
|
public function prepare(string $sql) {
|
2014-12-03 21:08:08 +01:00
|
|
|
return $this->pdo->prepare($sql);
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function beginTransaction(): void
|
|
|
|
{
|
2014-12-03 21:08:08 +01:00
|
|
|
$this->pdo->beginTransaction();
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function commit(): void
|
|
|
|
{
|
2014-12-03 21:08:08 +01:00
|
|
|
$this->pdo->commit();
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function rollback(): void
|
|
|
|
{
|
2014-12-23 00:30:05 +01:00
|
|
|
$this->pdo->rollback();
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function errorCode(): ?string {
|
2014-12-23 00:30:05 +01:00
|
|
|
return $this->pdo->errorCode();
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function errorInfo(): array
|
|
|
|
{
|
2014-12-23 00:30:05 +01:00
|
|
|
return $this->pdo->errorInfo();
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
/**
|
|
|
|
* @return \PDOStatement|false
|
|
|
|
*/
|
|
|
|
public function query($sql) {
|
2014-12-03 21:08:08 +01:00
|
|
|
return $this->pdo->query($sql);
|
|
|
|
}
|
2014-12-07 23:12:08 +01:00
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function lastInsertId(): string {
|
2015-04-03 00:11:36 +02:00
|
|
|
return $this->pdo->lastInsertId();
|
2015-01-06 23:52:52 +01:00
|
|
|
}
|
2014-12-03 21:08:08 +01:00
|
|
|
}
|