Skip to content
Snippets Groups Projects
Commit 730e9c32 authored by Josh Pollock's avatar Josh Pollock
Browse files

Basic select and deletes

parent 92413f65
No related branches found
No related tags found
No related merge requests found
caldera-forms @ c4c2d1db
Subproject commit 3b0a81ff69ed4aaaaa92987c0fff4520e7252f25
Subproject commit c4c2d1db6042c82f2229c89ed7140c8b214c9c8e
......@@ -25,10 +25,9 @@
}
},
"scripts" : {
"tests" : "composer fixes && composer sniffs && composer unit-tests && composer wp-tests",
"tests" : "composer unit-tests && composer wp-tests",
"unit-tests": "phpunit --testsuite=unit",
"wp-install": "bash ./bin/install-docker.sh",
"wp-start": "docker-compose up -d",
"wp-tests": "docker-compose run --rm wordpress_phpunit phpunit --configuration phpunit-integration.xml.dist",
"phpunit-v": "phpunit --version",
"sniffs" : "phpcs src/ && phpcs Tests/",
......@@ -46,7 +45,7 @@
"phpunit/phpunit": "^7.0",
"squizlabs/php_codesniffer": "^3.2",
"jakub-onderka/php-parallel-lint": "^1.0",
"Desertsnowman/caldera-forms": "dev-develop",
"Desertsnowman/caldera-forms": "dev-feature/2404",
"composer/installers": "~1.0"
},
"extra" : {
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "a6eee6d6f3062af42dc7b830f7971dac",
"content-hash": "90b8c6ac2ab8c558b28b1aea0f40cc18",
"packages": [
{
"name": "nilportugues/sql-query-builder",
......@@ -129,11 +129,11 @@
"packages-dev": [
{
"name": "Desertsnowman/caldera-forms",
"version": "dev-develop",
"version": "dev-feature/2404",
"source": {
"type": "git",
"url": "git@github.com:CalderaWP/Caldera-Forms.git",
"reference": "3b0a81ff69ed4aaaaa92987c0fff4520e7252f25"
"reference": "c4c2d1db6042c82f2229c89ed7140c8b214c9c8e"
},
"type": "wordpress-plugin",
"license": [
......@@ -153,7 +153,7 @@
"forms",
"wordpress"
],
"time": "2018-04-03T13:13:16+00:00"
"time": "2018-04-04T21:06:37+00:00"
},
{
"name": "composer/installers",
......
<?php
namespace calderawp\CalderaFormsQuery\Delete;
use calderawp\CalderaFormsQuery\QueryBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
abstract class DeleteQueryBuilder extends QueryBuilder implements DoesDeleteQuery
{
/**
* @var Delete
*/
protected $deleteQuery;
/**
* @return Delete
*/
public function getDeleteQuery()
{
if( ! $this->deleteQuery ){
$this->deleteQuery = new Delete($this->getTableName());
}
return $this->deleteQuery;
}
/**
* @return Delete
*/
public function getCurrentQuery()
{
return $this->getDeleteQuery();
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery\Delete;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
/**
* Interface DoesDeleteQuery
*
* Interface that delete query builders MUST implement.
*/
interface DoesDeleteQuery
{
/**
* Get current delete query
*
* @return Delete
*/
public function getCurrentQuery();
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery\Delete;
class Entry extends DeleteQueryBuilder
{
/**
* Delete all entries with a specific form ID
*
* @param string $formId Form ID to delete entries of
* @return $this
*/
public function deleteByFormId($formId)
{
return $this->is('form_id', $formId);
}
/**
* Delete a specific entry by user ID
*
* @param $entryId
* @return $this
*/
public function deleteByEntryId($entryId)
{
return $this->is('id', $entryId);
}
/**
* Delete entries belonging to a specific user ID
*
* @param $userId
* @return $this
*/
public function deleteByUserId($userId)
{
return $this->is('user_id', $userId);
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery\Delete;
class EntryValues extends DeleteQueryBuilder
{
/**
* Delete field by entry ID
*
* @param int $entryId Entry ID
* @return $this
*/
public function deleteByEntryId($entryId)
{
$this
->getDeleteQuery()
->where()
->equals( 'entry_id', (int)$entryId )
;
return $this;
}
/**
* Delete all field values with a value
*
* @param string $fieldSlug
* @param string $fieldValue
* @return $this
*/
public function deleteByFieldValue($fieldSlug, $fieldValue)
{
$this
->getDeleteQuery()
->where()
->equals( 'value', $fieldValue )
;
$this
->getDeleteQuery()
->where()
->equals( 'slug', $fieldSlug );
return $this;
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery;
/**
* Class Escape
*
* SQL Escape functions
*/
class Escape
{
/**
* Copy of WPDB::esc_like()
*
* First half of escaping for LIKE special characters % and _ before preparing for MySQL.
*
* Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security.
*
* Example Prepared Statement:
*
* $wild = '%';
* $find = 'only 43% of planets';
* $like = $wild . $wpdb->esc_like( $find ) . $wild;
* $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE %s", $like );
*
* Example Escape Chain:
*
* $sql = esc_sql( $wpdb->esc_like( $input ) );
* @param string $text The raw text to be escaped. The input typed by the user should have no
* extra or deleted slashes.
* @return string Text in the form of a LIKE phrase. The output is not SQL safe. Call $wpdb::prepare()
* or real_escape next.
*/
public static function like( $text ) {
return addcslashes( $text, '_%\\' );
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery;
use calderawp\CalderaFormsQuery\Select\DoesSelectQuery;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
abstract class QueryBuilder implements DoesQueries
{
const ASC = 'ASC';
const DESC = 'DESC';
/**
* @var MySqlBuilder
*/
private $builder;
/**
* @var bool
*/
protected $isLike = false;
/**
* @var string
*/
private $tableName;
/**
* SelectQueryBuilder constructor.
* @param MySqlBuilder $builder Query builder
* @param string $tableName Name of table
*/
public function __construct(MySqlBuilder $builder, $tableName)
{
$this->builder = $builder;
$this->tableName = $tableName;
}
/**
* @return AbstractBaseQuery
*/
abstract protected function getCurrentQuery();
/** @inheritdoc */
public function getTableName()
{
return $this->tableName;
}
/** @inheritdoc */
public function getBuilder()
{
return $this->builder;
}
/**
* Add a where $column equals $value clause to query
*
* @param AbstractBaseQuery $queryBuilder
* @param string $column
* @param string $value
* @return $this
*/
protected function addWhereEquals( AbstractBaseQuery $queryBuilder, $column, $value )
{
$queryBuilder
->where()
->equals($column, $value);
return $this;
}
/**
* Replace all substitutions with actual values
*
* @param string $sql SQL query with substitutions
* @return string
*/
protected function substituteValues($sql)
{
$values = $this->getBuilder()->getValues();
foreach ($values as $identifier => $value) {
$values[$identifier] = $this->surroundValue($value);
}
return str_replace(array_keys($values), array_values($values), $sql);
}
/**
* @return string
*/
protected function getFirstDeliminator()
{
return $this->isLike ? "'%" : "'";
}
/**
* @return string
*/
protected function getSecondDeliminator()
{
return $this->isLike ? "%'" : "'"; }
/**
* Surround one value with quotes or %
*
* @param string $value Value to surround
* @return string
*/
protected function surroundValue($value)
{
$value = "{$this->getFirstDeliminator()}$value{$this->getSecondDeliminator()}";
if( ! $this->isLike){
return $value;
}
return Escape::like( $value );
}
/**
* Generate query for where column is value
*
* @param string $column
* @param string $value
* @return $this
*/
protected function is($column, $value)
{
return $this->addWhereEquals($this->getCurrentQuery(),$column, $value);
}
/** @inheritdoc */
public function getPreparedSql()
{
return $this->substituteValues($this->getBuilder()->write($this->getCurrentQuery()));
}
}
\ No newline at end of file
......@@ -6,21 +6,15 @@ namespace calderawp\CalderaFormsQuery\Select;
/**
* Class Entry
* @package calderawp\CalderaFormsQuery\Select
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`form_id` varchar(18) NOT NULL DEFAULT '',
`user_id` int(11) NOT NULL,
`datestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`status` varchar(20) NOT NULL DEFAULT 'active',
*/
class Entry extends SelectQueryBuilder
{
/**
* Get all entries for a specific form
*
*
* @param $formId
* @param string $formId Form Id
* @return $this
*/
public function queryByFormsId($formId)
......@@ -28,11 +22,23 @@ class Entry extends SelectQueryBuilder
return $this->is('form_id', $formId);
}
/**
* Get entry by ID
*
* @param int $entryId Entry ID
* @return $this
*/
public function queryByEntryId($entryId)
{
return $this->is('id', $entryId);
}
/**
* Get all entries for a specific user
*
* @param int $userId
* @return $this
*/
public function queryByUserId($userId)
{
return $this->is('user_id', $userId);
......
......@@ -5,72 +5,35 @@ namespace calderawp\CalderaFormsQuery\Select;
use calderawp\CalderaFormsQuery\DoesQueries;
use calderawp\CalderaFormsQuery\MySqlBuilder;
use calderawp\CalderaFormsQuery\QueryBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
abstract class SelectQueryBuilder implements DoesQueries, DoesSelectQuery
abstract class SelectQueryBuilder extends QueryBuilder implements DoesSelectQuery
{
const ASC = 'ASC';
const DESC = 'DESC';
/**
* @var MySqlBuilder
*/
private $builder;
/**
* @var Select
*/
private $query;
/**
* @var bool
*/
protected $isLike = false;
/**
* @var string
*/
private $tableName;
/**
* SelectQueryBuilder constructor.
* @param MySqlBuilder $builder Query builder
* @param string $tableName Name of table
*/
public function __construct(MySqlBuilder $builder, $tableName)
{
$this->builder = $builder;
$this->tableName = $tableName;
}
/** @inheritdoc */
public function getTableName()
{
return $this->tableName;
}
/** @inheritdoc */
public function getBuilder()
{
return $this->builder;
}
private $selectQuery;
/** @inheritdoc */
public function getSelectQuery()
{
if (empty($this->query)) {
$this->query = new \NilPortugues\Sql\QueryBuilder\Manipulation\Select($this->getTableName());
if (empty($this->selectQuery)) {
$this->selectQuery = new \NilPortugues\Sql\QueryBuilder\Manipulation\Select($this->getTableName());
}
return $this->query;
return $this->selectQuery;
}
/** @inheritdoc */
public function getPreparedSql()
/**
* @return Select
*/
protected function getCurrentQuery()
{
return $this->substituteValues($this->getBuilder()->write($this->getSelectQuery()));
return $this->getSelectQuery();
}
/**
......@@ -81,57 +44,11 @@ abstract class SelectQueryBuilder implements DoesQueries, DoesSelectQuery
public function addOrderBy($column, $ascending = true)
{
$order = $ascending ? self::ASC : self::DESC;
$this->getSelectQuery()->orderBy($column, $order);
$this->getCurrentQuery()->orderBy($column, $order);
return $this;
}
/**
* Generate query for where column is value
*
* @param string $column
* @param string $value
* @return $this
*/
protected function is($column, $value)
{
$this->getSelectQuery()
->where()
->equals($column, $value);
return $this;
}
/**
* Replace all substitutions with actual values
*
* @param string $sql SQL query with substitutions
* @return string
*/
protected function substituteValues($sql)
{
$values = $this->builder->getValues();
foreach ($values as $identifier => $value) {
$values[$identifier] = $this->surroundValue($value);
}
return str_replace(array_keys($values), array_values($values), $sql);
}
/**
* @return string
*/
protected function getDeliminator()
{
return $this->isLike ? '%' : "'";
}
/**
* Surround one value with quotes or %
*
* @param string $value Value to surround
* @return string
*/
protected function surroundValue($value)
{
return "{$this->getDeliminator()}$value{$this->getDeliminator()}";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment