Skip to content

Commit

Permalink
SetUp moved to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Oct 27, 2019
1 parent c8eb9e5 commit 423ae20
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
18 changes: 0 additions & 18 deletions src/Ease/SQL/Engine.php
Expand Up @@ -42,24 +42,6 @@ public function __construct($identifier = null, $options = [])
}
}

/**
* SetUp Object to be ready for connect
*
* @param array $options Object Options (company,url,user,password,evidence,
* prefix,defaultUrlParams,debug)
*/
public function setUp($options = [])
{
$this->setupProperty($options, 'dbType', 'DB_TYPE');
$this->setupProperty($options, 'server', 'DB_HOST');
$this->setupProperty($options, 'username', 'DB_USERNAME');
$this->setupProperty($options, 'password', 'DB_PASSWORD');
$this->setupProperty($options, 'database', 'DB_DATABASE');
$this->setupProperty($options, 'port', 'DB_PORT');
$this->setupProperty($options, 'connectionSettings', 'DB_SETUP');
$this->setupProperty($options, 'myTable');
}

/**
*
* @return \Envms\FluentPDO
Expand Down
68 changes: 50 additions & 18 deletions src/Ease/SQL/Orm.php
@@ -1,4 +1,5 @@
<?php

/**
* Object Relation Model Trait
*
Expand All @@ -12,8 +13,8 @@
*
* @author Vítězslav Dvořák <info@vitexsoftware.cz>
*/
trait Orm
{
trait Orm {

/**
* IP serveru.
*
Expand Down Expand Up @@ -90,48 +91,64 @@ trait Orm
*/
public $errorNumber = null;

/**
* SetUp Object to be ready for connect
*
* @param array $options Object Options (company,url,user,password,evidence,
* prefix,defaultUrlParams,debug)
*/
public function setUp($options = []) {
$this->setupProperty($options, 'dbType', 'DB_TYPE');
$this->setupProperty($options, 'server', 'DB_HOST');
$this->setupProperty($options, 'username', 'DB_USERNAME');
$this->setupProperty($options, 'password', 'DB_PASSWORD');
$this->setupProperty($options, 'database', 'DB_DATABASE');
$this->setupProperty($options, 'port', 'DB_PORT');
$this->setupProperty($options, 'connectionSettings', 'DB_SETUP');
$this->setupProperty($options, 'myTable');
}

/**
* Perform connect to database.
*
* @return \PDO SQL connector
*/
public function pdoConnect()
{
public function pdoConnect() {
$result = false;
if (is_null($this->dbType)) {
$result = null;
} else {
switch ($this->dbType) {
case 'mysql':
$result = new \PDO($this->dbType.':dbname='.$this->database.';host='.$this->server.';port='.$this->port.';charset=utf8',
$this->username, $this->password,
[\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\'']);
$result = new \PDO($this->dbType . ':dbname=' . $this->database . ';host=' . $this->server . ';port=' . $this->port . ';charset=utf8',
$this->username, $this->password,
[\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\'']);
break;
case 'pgsql':
$result = new \PDO($this->dbType.':dbname='.$this->database.';host='.$this->server.';port='.$this->port,
$this->username, $this->password);
$result = new \PDO($this->dbType . ':dbname=' . $this->database . ';host=' . $this->server . ';port=' . $this->port,
$this->username, $this->password);
if (is_object($result)) {
$result->exec("SET NAMES 'UTF-8'");
}
break;

default:
throw new \Ease\Exception(_('Unimplemented Database type').': '.$this->dbType);
throw new \Ease\Exception(_('Unimplemented Database type') . ': ' . $this->dbType);
break;
}

if ($result instanceof \PDO) {
$this->errorNumber = $result->errorCode();
$this->errorText = $result->errorInfo();
$this->errorText = $result->errorInfo();

if ($this->errorNumber != '00000') {
$this->addStatusMessage('Connect: error #'.$this->errorNumer.' '.$this->errorText,
'error');
$this->addStatusMessage('Connect: error #' . $this->errorNumer . ' ' . $this->errorText,
'error');

$result = false;
} else {
if (!empty($this->connectionSettings))
foreach ($this->connectionSettings as $setName => $SetValue) {
foreach ($this->connectionSettings as $setName => $SetValue) {
if (strlen($setName)) {
$this->getPdo->exec("SET $setName $SetValue");
}
Expand All @@ -148,8 +165,7 @@ public function pdoConnect()
*
* @return \PDO
*/
public function getPdo()
{
public function getPdo() {
if (!$this->pdo instanceof \PDO) {
$this->pdo = $this->pdoConnect();
}
Expand All @@ -161,11 +177,27 @@ public function getPdo()
*
* @return \Envms\FluentPDO
*/
public function getFluentPDO()
{
public function getFluentPDO() {
if (!$this->fluent instanceof \Envms\FluentPDO) {
$this->fluent = new \Envms\FluentPDO\Query($this->getPdo());
}
return $this->fluent;
}

/**
*
* @return string
*/
public function getMyTable() {
return $this->myTable;
}

/**
*
* @param string $tablename
*/
public function setMyTable($tablename) {
$this->myTable = $tablename;
}

}

0 comments on commit 423ae20

Please sign in to comment.