Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Apr 9, 2016
2 parents 73e2c56 + b36bf40 commit b746f5c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 12 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Parable PHP Framework Changelog

### 0.4.1

__Changes__
- Components/Database and Components/Query now have a property (and appropriate get/set methods) to set whether 'all' should be quoted.
- Closures should now return their value, after which their return value is added to the Response content.

__Bugfixes__
- Since NULL values are no longer automatically skipped (due to bug fix in 0.3.3), query was trying to set the id even on inserts. sqlite didn't care, but mysql does. TableKey is now always skipped in inserts.
- MySQL mode now works.

### 0.4.0

__Changes__
Expand Down
27 changes: 27 additions & 0 deletions vendor/Devvoh/Components/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class Database {
*/
protected $instance = null;

/**
* @var bool
*/
protected $quoteAll = false;

/**
* Returns the type, if any
*
Expand Down Expand Up @@ -145,6 +150,26 @@ public function setDatabase($database) {
return $this;
}

/**
* Set whether the table name should be quoted
*
* @param $quoteAll
* @return $this
*/
public function setQuoteAll($quoteAll) {
$this->quoteAll = (bool)$quoteAll;
return $this;
}

/**
* Return whether table names should be quoted
*
* @return bool
*/
public function getQuoteAll() {
return $this->quoteAll;
}

/**
* Return instance, if any
*
Expand All @@ -157,13 +182,15 @@ public function getInstance() {
case 'sqlite3':
$instance = new \PDO('sqlite:' . $this->getLocation());
$this->setInstance($instance);
$this->setQuoteAll(true);
break;
case 'mysql':
if (!$this->getUsername() || !$this->getPassword() || !$this->getDatabase()) {
return false;
}
$instance = new \PDO('mysql:host=' . $this->getLocation() . ';dbname=' . $this->getDatabase(), $this->getUsername(), $this->getPassword());
$this->setInstance($instance);
$this->setQuoteAll(false);
}
}
return $this->instance;
Expand Down
49 changes: 42 additions & 7 deletions vendor/Devvoh/Components/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class Query {
*/
protected $joins = [];

/**
* @var bool
*/
protected $quoteAll = false;

/**
* Set the tableName to work on
*
Expand All @@ -83,7 +88,11 @@ public function setTableName($tableName) {
* @return string
*/
public function getTableName() {
return $this->tableName;
$tableName = $this->tableName;
if ($this->quoteAll) {
$tableName = $this->pdoInstance->quote($tableName);
}
return $tableName;
}

/**
Expand All @@ -109,6 +118,26 @@ public function getPdoInstance() {
return $this->pdoInstance;
}

/**
* Set whether the table name should be quoted
*
* @param $quoteAll
* @return $this
*/
public function setQuoteAll($quoteAll) {
$this->quoteAll = (bool)$quoteAll;
return $this;
}

/**
* Return whether table names should be quoted
*
* @return bool
*/
public function getQuoteAll() {
return $this->quoteAll;
}

/**
* Set the tableKey to work with (for delete & update
* )
Expand Down Expand Up @@ -252,7 +281,7 @@ public function __toString() {
// set select & what needs to be selected
$query[] = "SELECT " . $this->select;
// table
$query[] = "FROM " . $this->pdoInstance->quote($this->tableName);
$query[] = "FROM " . $this->getTableName();

// Add the left joins
if (count($this->joins) > 0) {
Expand Down Expand Up @@ -311,7 +340,7 @@ public function __toString() {
} elseif ($this->action === 'delete') {

// set delete to the proper table
$query[] = "DELETE FROM " . $this->pdoInstance->quote($this->tableName);
$query[] = "DELETE FROM " . $this->getTableName();

// now get the where clauses
if (count($this->where) > 0) {
Expand All @@ -329,7 +358,7 @@ public function __toString() {
} elseif ($this->action === 'update') {

// set update to the proper table
$query[] = "UPDATE " . $this->pdoInstance->quote($this->tableName);
$query[] = "UPDATE " . $this->getTableName();

// now get the values
if (count($this->values) > 0) {
Expand All @@ -346,7 +375,10 @@ public function __toString() {
} else {
$correctValue = $this->pdoInstance->quote($value);
}
$values[] = "'" . $key . "'=" . $correctValue;
if ($this->getQuoteAll()) {
$key = $this->getPdoInstance()->quote($key);
}
$values[] = $key . "=" . $correctValue;
} else {
$tableKey = $key;
$tableKeyValue = $value;
Expand All @@ -361,15 +393,18 @@ public function __toString() {
} elseif ($this->action === 'insert') {

// set insert to the proper table
$query[] = "INSERT INTO " . $this->pdoInstance->quote($this->tableName);
$query[] = "INSERT INTO " . $this->getTableName();

// now get the values
if (count($this->values) > 0) {

$keys = [];
$values = [];
foreach ($this->values as $key => $value) {
$keys[] = "'" . $key . "'";
if ($this->getQuoteAll()) {
$key = $this->pdoInstance->quote($key);
}
$keys[] = $key;

if ($value === null) {
$correctValue = 'NULL';
Expand Down
13 changes: 10 additions & 3 deletions vendor/Devvoh/Parable/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static function run() {
if (self::getConfig()->get('storage_type') && self::getConfig()->get('storage_location')) {
$config = [
'type' => self::getConfig()->get('storage_type'),
'location' => self::getDir(self::getConfig()->get('storage_location')),
'location' => self::getConfig()->get('storage_location'),
'username' => self::getConfig()->get('storage_username'),
'password' => self::getConfig()->get('storage_password'),
'database' => self::getConfig()->get('storage_database'),
Expand Down Expand Up @@ -663,6 +663,7 @@ public static function createQuery() {
$query = new \Devvoh\Components\Query();
if (self::getDatabase()) {
$query->setPdoInstance(self::getDatabase()->getInstance());
$query->setQuoteAll(self::getDatabase()->getQuoteAll());
}
return $query;
}
Expand Down Expand Up @@ -770,13 +771,14 @@ public static function executeRoute($route = null) {
}

$viewTemplate = null;
// Check for a closure
// Check for a closure and set the result to $closureReturn if valid
$closureReturn = null;
if (isset($route['closure'])) {
$closure = $route['closure'];
// Check if we can call it
if (is_callable($closure)) {
// Call it
$closure();
$closureReturn = $closure();
} else {
// Not callable, so false
return false;
Expand Down Expand Up @@ -808,6 +810,11 @@ public static function executeRoute($route = null) {
return false;
}
}

if ($closureReturn) {
self::getResponse()->appendContent($closureReturn);
}

// If valid $viewTemplate is set, load it into the view
if (!$viewTemplateRoute && $viewTemplate && file_exists($viewTemplate)) {
self::getView()->loadTemplate($viewTemplate);
Expand Down
4 changes: 3 additions & 1 deletion vendor/Devvoh/Parable/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public function save() {
$query->setAction('insert');

foreach ($array as $key => $value) {
$query->addValue($key, $value);
if ($key !== $this->tableKey) {
$query->addValue($key, $value);
}
}

// Since it's an insert, add created_at if the model implements it
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.0
0.4.1

0 comments on commit b746f5c

Please sign in to comment.