aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilly Micieli <micieli@laposte.net>2019-11-27 09:36:32 +0100
committerWilly Micieli <micieli@laposte.net>2019-11-27 09:36:32 +0100
commit7b8e0f14d786f63a61edf04bfa11236a5c3d7a6c (patch)
treee28797183f94a68593a6bc37f7c714fa00caefcd
parente65689b88c4d3c85371f25c8f9a199eb6b5c0e58 (diff)
downloadpandora-7b8e0f14d786f63a61edf04bfa11236a5c3d7a6c.zip
pandora-7b8e0f14d786f63a61edf04bfa11236a5c3d7a6c.tar.gz
add new class
-rw-r--r--composer.json6
-rw-r--r--eywa/Database/Connexion/Connect.php548
-rw-r--r--eywa/Database/Model/Model.php13
-rw-r--r--eywa/Database/Table/Column.php11
-rw-r--r--eywa/Database/Table/Table.php11
-rw-r--r--eywa/Model/Model.php10
-rw-r--r--helpers/const.php2
7 files changed, 587 insertions, 14 deletions
diff --git a/composer.json b/composer.json
index 2244ea0..9beee2f 100644
--- a/composer.json
+++ b/composer.json
@@ -23,15 +23,15 @@
"twig/twig": "^3.0",
"guzzlehttp/guzzle": "^6.4",
"symfony/console": "^5.0",
- "symfony/yaml": "4.4",
"symfony/var-dumper": "^5.0",
"swiftmailer/swiftmailer": "^6.2",
"psr/http-server-middleware": "^1.0",
"predis/predis": "^1.1",
"vlucas/phpdotenv": "^3.6",
- "nesbot/carbon": "^2.27"
+ "nesbot/carbon": "^2.27",
+ "ext-PDO": "*",
+ "codedungeon/phpunit-result-printer": "^0.26.2"
},
"require-dev": {
- "codedungeon/phpunit-result-printer": "^0.26.2"
}
}
diff --git a/eywa/Database/Connexion/Connect.php b/eywa/Database/Connexion/Connect.php
new file mode 100644
index 0000000..7afb6c3
--- /dev/null
+++ b/eywa/Database/Connexion/Connect.php
@@ -0,0 +1,548 @@
+<?php
+
+
+namespace Eywa\Database\Connexion {
+
+
+ use Eywa\Exception\Kedavra;
+ use PDO;
+ use PDOException;
+
+ class Connect
+ {
+
+ /**
+ *
+ * The base name
+ *
+ * @var string
+ *
+ */
+ private $database;
+
+ /**
+ *
+ * The username
+ *
+ * @var string
+ *
+ */
+ private $username;
+
+ /**
+ *
+ * The password
+ *
+ * @var string
+ *
+ */
+ private $password;
+
+ /**
+ *
+ * The driver
+ *
+ * @var string
+ *
+ */
+ private $driver;
+
+ /**
+ *
+ * The PDO fetch mode
+ *
+ * @var int
+ *
+ */
+ private $mode = PDO::FETCH_OBJ;
+
+ /**
+ *
+ * The pdo instance
+ *
+ * @var PDO
+ *
+ */
+ private $instance;
+
+ /**
+ *
+ * The dump directory path
+ *
+ * @var string
+ *
+ */
+ private $dump_path;
+
+ /**
+ *
+ * The connection hostname
+ *
+ * @var string
+ *
+ */
+ private $host = LOCALHOST;
+
+ /**
+ *
+ * Create a PDO connection
+ *
+ * @Inject({"db.driver","db.name","db.username", "db.password","db.host", "db.dump"})
+ *
+ * @method __construct
+ *
+ * @param string $driver
+ * @param string $base The base's name
+ * @param string $username The base's username
+ * @param string $password The base's password
+ * @param string $host The base's host
+ * @param string $dump_path The path to dump directory
+ *
+ *
+ */
+ public function __construct(string $driver, string $base, string $username, string $password, string $host, string $dump_path)
+ {
+
+ $this->dump_path = base('db') . DIRECTORY_SEPARATOR . $dump_path;
+ $this->driver = $driver;
+ $this->database = $base;
+ $this->username = $username;
+ $this->password = $password;
+ $this->host = $host;
+ }
+
+ /**
+ *
+ * Change the pdo mode
+ *
+ * @method set
+ *
+ * @param int $pdo_mode
+ *
+ * @return Connect
+ *
+ */
+ public function set(int $pdo_mode) : Connect
+ {
+
+ $this->mode = $pdo_mode;
+
+ return $this;
+ }
+
+ /**
+ *
+ * Execute all queries
+ *
+ * @method queries
+ *
+ * @param string ...$queries
+ *
+ * @return bool
+ */
+ public function queries(string ...$queries) : bool
+ {
+ return collect($queries)->for([ $this, 'execute' ])->ok();
+ }
+
+ /**
+ *
+ * Return the current host used
+ *
+ * @method host
+ *
+ * @return string
+ *
+ */
+ public function host() : string
+ {
+ return $this->host;
+ }
+
+ /**
+ *
+ * Return the current driver used
+ *
+ * @method driver
+ *
+ * @return string
+ *
+ */
+ public function driver() : string
+ {
+ return $this->driver;
+ }
+
+ /**
+ *
+ * Return the current base used
+ *
+ * @method base
+ *
+ * @return string
+ *
+ */
+ public function base() : string
+ {
+
+ return $this->database;
+ }
+
+ /**
+ *
+ * Return the current username
+ *
+ * @method user
+ *
+ * @return string
+ *
+ **/
+ public function user() : string
+ {
+ return $this->username;
+ }
+
+ /**
+ *
+ * Return the current password
+ *
+ * @method password
+ *
+ * @return string The current password
+ *
+ */
+ public function password() : string
+ {
+ return $this->password;
+ }
+
+ /**
+ *
+ * Return the current fetch mode
+ *
+ * @method fetch_mode
+ *
+ * @return int
+ *
+ */
+ public function fetch_mode() : int
+ {
+ return $this->mode;
+ }
+
+ /**
+ *
+ * Return the dump directory path
+ *
+ * @method dump_path
+ *
+ * @return string
+ *
+ */
+ public function dump_path() : string
+ {
+ return $this->dump_path;
+ }
+
+ /**
+ *
+ * Check if current driver is mysql
+ *
+ * @method mysql
+ *
+ * @return bool
+ *
+ */
+ public function mysql() : bool
+ {
+ return $this->driver() === MYSQL;
+ }
+
+ /**
+ *
+ * Check if current driver is postgresql
+ *
+ * @method postgresql
+ *
+ * @return bool
+ *
+ */
+ public function postgresql() : bool
+ {
+
+ return $this->driver() === POSTGRESQL;
+ }
+
+ /**
+ *
+ * Check if current driver is sqlite
+ *
+ * @method sqlite
+ *
+ * @return bool
+ *
+ */
+ public function sqlite() : bool
+ {
+
+ return $this->driver() === SQLITE;
+ }
+
+ /**
+ *
+ * Return the PDO instance on success
+ *
+ * @method instance
+ *
+ *
+ * @return PDO
+ *
+ * @throws Kedavra
+ *
+ */
+ public function pdo() : PDO
+ {
+
+ $instance = $this->getpdo();
+
+ if (is_string($instance))
+ throw new Kedavra($instance);
+
+ return $instance;
+ }
+
+ /**
+ *
+ * @param string $sql
+ * @param string[] $vars
+ *
+ * @throws Kedavra
+ *
+ * @return object
+ *
+ */
+ public function fetch(string $sql, string ...$vars)
+ {
+
+ $query = $this->pdo()->prepare($sql);
+
+ is_true(is_bool($query), true, $sql);
+
+ $query->execute($vars);
+
+ $x = $query->fetch($this->fetch_mode());
+
+ is_false($query->closeCursor(), true, "Fail to close the connection");
+
+ $query = null;
+
+ return $x;
+ }
+
+ /**
+ *
+ * Check if the driver is not the current
+ *
+ * @method not
+ *
+ * @param string $driver
+ *
+ * @return bool
+ *
+ */
+ public function not(string $driver) : bool
+ {
+ return $this->driver() !== $driver;
+ }
+
+ /**
+ *
+ * Execute a request and return result in an array
+ *
+ * @method request
+ *
+ * @param string $sql
+ * @param string[] $vars
+ *
+ * @throws Kedavra
+ *
+ * @return array
+ *
+ */
+ public function request(string $sql, string ...$vars) : array
+ {
+
+ $query = $this->pdo()->prepare($sql);
+
+ $query->execute($vars);
+
+ is_true(is_bool($query), true, $sql);
+
+ $x = $query->fetchAll($this->fetch_mode());
+
+ is_false($query->closeCursor(), true, "Fail to close the connection");
+
+ $query = null;
+
+ $sql = null;
+
+ return $x;
+ }
+
+ /**
+ *
+ * Execute a query and return true on success or false on failure
+ *
+ * @method execute
+ *
+ * @param string $sql
+ * @param string[] $vars
+ *
+ * @throws Kedavra
+ *
+ * @return bool
+ *
+ */
+ public function execute(string $sql, string ...$vars) : bool
+ {
+
+ $query = $this->pdo()->prepare($sql);
+
+ is_true(is_bool($query), true, $sql);
+
+ $x = $query->execute($vars);
+
+ is_false($query->closeCursor(), true, "Fail to close the connection");
+
+ $query = null;
+
+ return $x;
+ }
+
+ /**
+ *
+ * Start a transaction block
+ *
+ * @return Connect
+ *
+ * @throws Kedavra
+ *
+ */
+ public function transaction() : Connect
+ {
+
+ is_false($this->pdo()->beginTransaction(), true, "Transaction start fail");
+
+ return $this;
+ }
+
+ /**
+ *
+ * Commit the current transaction
+ *
+ * @return bool
+ *
+ * @throws Kedavra
+ *
+ */
+ public function commit() : bool
+ {
+
+ return $this->pdo()->commit();
+ }
+
+ /**
+ *
+ * Abort the current transaction
+ *
+ * @return Connect
+ *
+ * @throws Kedavra
+ *
+ */
+ public function rollback() : Connect
+ {
+
+ is_false($this->pdo()->rollBack(), true, "ROLLBACK as fail");
+
+ return $this;
+ }
+
+ /**
+ *
+ * @return string|PDO
+ *
+ */
+ private function getpdo()
+ {
+
+ $database = $this->database;
+ $username = $this->username;
+ $password = $this->password;
+ $driver = $this->driver;
+ $host = $this->host;
+ if(is_null($this->instance))
+ {
+ if($this->sqlite())
+ {
+ if(def($database))
+ {
+ try
+ {
+ $this->instance = new PDO("$driver:$database");
+ }
+ catch(PDOException $e)
+ {
+ return $e->getMessage();
+ }
+ }
+ else
+ {
+ try
+ {
+ $this->instance = new PDO("$driver::memory:");
+ }
+ catch(PDOException $e)
+ {
+ return $e->getMessage();
+ }
+ }
+ }
+ else
+ {
+ if(def($database))
+ {
+ try
+ {
+ $this->instance = new PDO("$driver:host=$host;dbname=$database", $username, $password);
+ }
+ catch(PDOException $e)
+ {
+ return $e->getMessage();
+ }
+ }
+ else
+ {
+ try
+ {
+ $this->instance = new PDO("$driver:host=$host;dbname=$database", $username, $password);
+ }
+ catch(PDOException $e)
+ {
+ return $e->getMessage();
+ }
+ }
+ }
+ }
+ else
+ {
+ return $this->instance;
+ }
+ $this->instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
+ $this->instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+ $this->instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
+
+ return $this->instance;
+ }
+ }
+} \ No newline at end of file
diff --git a/eywa/Database/Model/Model.php b/eywa/Database/Model/Model.php
new file mode 100644
index 0000000..fb7d8d8
--- /dev/null
+++ b/eywa/Database/Model/Model.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Eywa\Database\Model
+{
+ abstract class Model
+ {
+
+ public static function create(array $record)
+ {
+
+ }
+ }
+}
diff --git a/eywa/Database/Table/Column.php b/eywa/Database/Table/Column.php
new file mode 100644
index 0000000..2e0647a
--- /dev/null
+++ b/eywa/Database/Table/Column.php
@@ -0,0 +1,11 @@
+<?php
+
+
+namespace Eywa\Database\Table {
+
+
+ class Column
+ {
+
+ }
+} \ No newline at end of file
diff --git a/eywa/Database/Table/Table.php b/eywa/Database/Table/Table.php
new file mode 100644
index 0000000..f25dc12
--- /dev/null
+++ b/eywa/Database/Table/Table.php
@@ -0,0 +1,11 @@
+<?php
+
+
+namespace Eywa\Database\Table {
+
+
+ class Table
+ {
+
+ }
+} \ No newline at end of file
diff --git a/eywa/Model/Model.php b/eywa/Model/Model.php
deleted file mode 100644
index 42b5903..0000000
--- a/eywa/Model/Model.php
+++ /dev/null
@@ -1,10 +0,0 @@
-<?php
-
-namespace Eywa\Model
-{
- abstract class Model
- {
-
-
- }
-}
diff --git a/helpers/const.php b/helpers/const.php
index af72b76..a19887f 100644
--- a/helpers/const.php
+++ b/helpers/const.php
@@ -8,7 +8,7 @@ define('POST','POST');
define('PUT','PUT');
define('DELETE','DELETE');
-
+define('LOCALHOST','localhost');
define('ASC', 'ASC');