composer v1

This commit is contained in:
2021-06-27 20:26:44 +02:00
parent b4f1225a56
commit e78ed8bfc7
10 changed files with 1737 additions and 455 deletions

View File

@@ -1,5 +1,8 @@
<?php
namespace Franzz\Objects;
use \Settings;
/**
* MySql query manager and generator
* @author franzz
@@ -23,10 +26,10 @@ class Db extends PhpObject
/**
* SQL connection Handle
* @var mysqli
* @var \mysqli
*/
private $oConnection;
private $sDatabase;
private $asConf;
private $asOptions;
/**
@@ -37,20 +40,14 @@ class Db extends PhpObject
* 'cascading_delete'=>array('table_name1'=>array('linked_table1', 'linked_table2', ...), 'table_name2'=>...))
* @var Array
*/
public function __construct($sDbServer, $sLogin, $sPass, $sDatabase, $asOptions, $sEncoding='utf8mb4')
public function __construct($asConf, $asOptions)
{
parent::__construct(__FILE__, Settings::DEBUG);
$this->sDatabase = $sDatabase;
$this->asConf = $asConf;
$this->asOptions = $asOptions;
//$this->oConnection = mysql_connect(self::DB_SERVER, self::DB_LOGIN, self::DB_PASS);
$this->oConnection = new mysqli($sDbServer, $sLogin, $sPass);
$this->syncPhpParams($sEncoding);
/*
$dsn = 'mysql:dbname='.$this->sDatabase.';host='.self::DB_SERVER;
try {$dbh = new PDO($dsn, self::DB_LOGIN, self::DB_PASS);}
catch (PDOException $e) {$this->addError('Connexion échouée : ' . $e->getMessage());}
*/
parent::__construct(__FILE__, Settings::DEBUG);
$this->oConnection = new \mysqli($this->getConf('server'), $this->getConf('user'), $this->getConf('pass'));
$this->syncPhpParams($this->getConf('encoding'));
$this->setTrace(false);
if($this->oConnection->connect_error)
@@ -60,7 +57,7 @@ class Db extends PhpObject
}
else
{
if(!$this->oConnection->select_db($this->sDatabase))
if(!$this->oConnection->select_db($this->getConf('database')))
{
$this->addError('Could not find database "'.$this->sDatabase.'"');
$this->sDbState = self::DB_NO_DATA;
@@ -73,6 +70,10 @@ class Db extends PhpObject
}
}
private function getConf($sConf) {
return $this->asConf[$sConf] ?? null;
}
private function syncPhpParams($sEncoding)
{
//Characters encoding
@@ -91,7 +92,6 @@ class Db extends PhpObject
public function setTrace($bTrace=true)
{
$this->bTrace = $bTrace;
//if($bTrace) $this->setDebug(true);
}
public function getTrace()
@@ -108,7 +108,7 @@ class Db extends PhpObject
{
//Create Database
$this->setQuery("DROP DATABASE IF EXISTS ".$this->sDatabase);
$this->setQuery("CREATE DATABASE ".$this->sDatabase." DEFAULT CHARACTER SET ".Settings::DB_ENC." DEFAULT COLLATE ".Settings::DB_ENC."_general_ci");
$this->setQuery("CREATE DATABASE ".$this->sDatabase." DEFAULT CHARACTER SET ".$this->getConf('encoding')." DEFAULT COLLATE ".$this->getConf('encoding')."_general_ci");
$this->oConnection->select_db($this->sDatabase);
//Create tables
@@ -120,7 +120,7 @@ class Db extends PhpObject
$sAppPath = '';
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $sAppPath = 'C:\ProgramData\xampp\mysql\bin\\';
exec($sAppPath.'mysqldump --user='.Settings::DB_LOGIN.' --password='.Settings::DB_PASS.' '.Settings::DB_NAME.' --add-drop-table --result-file='.$sBackupFile);
exec($sAppPath.'mysqldump --user='.$this->getConf('user').' --password='.$this->getConf('pass').' '.$this->getConf('database').' --add-drop-table --result-file='.$sBackupFile);
if(file_exists($sBackupFile)) {
$sBackup = file_get_contents($sBackupFile);
unlink($sBackupFile);
@@ -133,7 +133,7 @@ class Db extends PhpObject
$sAppPath = '';
if(file_exists($sBackupFile)) {
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $sAppPath = 'C:\ProgramData\xampp\mysql\bin\\';
return exec($sAppPath.'mysql --user='.Settings::DB_LOGIN.' --password='.Settings::DB_PASS.' '.Settings::DB_NAME.' < '.$sBackupFile);
return exec($sAppPath.'mysql --user='.$this->getConf('user').' --password='.$this->getConf('pass').' '.$this->getConf('database').' < '.$sBackupFile);
}
else return false;
}
@@ -750,23 +750,4 @@ class Db extends PhpObject
return array_combine($asKeys, $asValues);
}
}
/*
public function select($asFields='*')
{
$oSql = new Sql($this, array('select'=>$asFields));
return $oSql;
}
public function from($sTable)
{
$oSql = new Sql($this, array('from'=>$sTable));
return $oSql;
}
public function getConnection()
{
return $this->oConnection;
}
*/
}