@@ -12,54 +12,54 @@ |
||
| 12 | 12 | use EmmetBlue\Core\Database\Abstraction\InsertQueryBuilder; |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | - * class QueryBuilderTest. |
|
| 16 | - * |
|
| 17 | - * @author Samuel Adeshina <[email protected]> |
|
| 18 | - * |
|
| 19 | - * @since v0.0.1 27/05/2016 14:27 |
|
| 20 | - */ |
|
| 15 | + * class QueryBuilderTest. |
|
| 16 | + * |
|
| 17 | + * @author Samuel Adeshina <[email protected]> |
|
| 18 | + * |
|
| 19 | + * @since v0.0.1 27/05/2016 14:27 |
|
| 20 | + */ |
|
| 21 | 21 | class InsertQueryBuilderTest extends \PHPUnit_Framework_TestCase |
| 22 | 22 | { |
| 23 | - public function testInsertBuilderWithOnlyConstructorParameter() |
|
| 24 | - { |
|
| 25 | - $queryBuilder = new InsertQueryBuilder('tbl_name'); |
|
| 23 | + public function testInsertBuilderWithOnlyConstructorParameter() |
|
| 24 | + { |
|
| 25 | + $queryBuilder = new InsertQueryBuilder('tbl_name'); |
|
| 26 | 26 | |
| 27 | - $builtQuery = (string) $queryBuilder; |
|
| 28 | - $expectedQuery = 'INSERT INTO tbl_name'; |
|
| 27 | + $builtQuery = (string) $queryBuilder; |
|
| 28 | + $expectedQuery = 'INSERT INTO tbl_name'; |
|
| 29 | 29 | |
| 30 | - $this->assertEquals($expectedQuery, $builtQuery); |
|
| 31 | - } |
|
| 30 | + $this->assertEquals($expectedQuery, $builtQuery); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public function testInsertBuilderWithIntoMethod() |
|
| 34 | - { |
|
| 35 | - $queryBuilder = new InsertQueryBuilder(); |
|
| 36 | - $queryBuilder = $queryBuilder->into('tbl_name'); |
|
| 33 | + public function testInsertBuilderWithIntoMethod() |
|
| 34 | + { |
|
| 35 | + $queryBuilder = new InsertQueryBuilder(); |
|
| 36 | + $queryBuilder = $queryBuilder->into('tbl_name'); |
|
| 37 | 37 | |
| 38 | - $builtQuery = (string) $queryBuilder; |
|
| 39 | - $expectedQuery = 'INSERT INTO tbl_name'; |
|
| 38 | + $builtQuery = (string) $queryBuilder; |
|
| 39 | + $expectedQuery = 'INSERT INTO tbl_name'; |
|
| 40 | 40 | |
| 41 | - $this->assertEquals($expectedQuery, $builtQuery); |
|
| 42 | - } |
|
| 41 | + $this->assertEquals($expectedQuery, $builtQuery); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - public function testInsertBuilderWithIntoAndTableColumnsMethod() |
|
| 45 | - { |
|
| 46 | - $queryBuilder = new InsertQueryBuilder(); |
|
| 47 | - $queryBuilder = $queryBuilder->into('tbl_name', ['tbl_col1', 'tbl_col2']); |
|
| 44 | + public function testInsertBuilderWithIntoAndTableColumnsMethod() |
|
| 45 | + { |
|
| 46 | + $queryBuilder = new InsertQueryBuilder(); |
|
| 47 | + $queryBuilder = $queryBuilder->into('tbl_name', ['tbl_col1', 'tbl_col2']); |
|
| 48 | 48 | |
| 49 | - $builtQuery = (string) $queryBuilder; |
|
| 50 | - $expectedQuery = 'INSERT INTO tbl_name(tbl_col1,tbl_col2)'; |
|
| 49 | + $builtQuery = (string) $queryBuilder; |
|
| 50 | + $expectedQuery = 'INSERT INTO tbl_name(tbl_col1,tbl_col2)'; |
|
| 51 | 51 | |
| 52 | - $this->assertEquals($expectedQuery, $builtQuery); |
|
| 53 | - } |
|
| 52 | + $this->assertEquals($expectedQuery, $builtQuery); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - public function testInsertBuilderWithConstructorParameterAndValuesKeyword() |
|
| 56 | - { |
|
| 57 | - $queryBuilder = new InsertQueryBuilder('tbl_name'); |
|
| 58 | - $queryBuilder = $queryBuilder->values(['tbl_col_val1', 'tbl_col_val2']); |
|
| 55 | + public function testInsertBuilderWithConstructorParameterAndValuesKeyword() |
|
| 56 | + { |
|
| 57 | + $queryBuilder = new InsertQueryBuilder('tbl_name'); |
|
| 58 | + $queryBuilder = $queryBuilder->values(['tbl_col_val1', 'tbl_col_val2']); |
|
| 59 | 59 | |
| 60 | - $expectedQuery = 'INSERT INTO tbl_name VALUES (tbl_col_val1,tbl_col_val2)'; |
|
| 61 | - $builtQuery = (string) $queryBuilder; |
|
| 60 | + $expectedQuery = 'INSERT INTO tbl_name VALUES (tbl_col_val1,tbl_col_val2)'; |
|
| 61 | + $builtQuery = (string) $queryBuilder; |
|
| 62 | 62 | |
| 63 | - $this->assertEquals($expectedQuery, $builtQuery); |
|
| 64 | - } |
|
| 63 | + $this->assertEquals($expectedQuery, $builtQuery); |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -11,27 +11,27 @@ |
||
| 11 | 11 | use EmmetBlue\Core\Database\Abstraction\QueryBuilder; |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | - * class QueryBuilderTest. |
|
| 15 | - * |
|
| 16 | - * @author Samuel Adeshina <[email protected]> |
|
| 17 | - * |
|
| 18 | - * @since v0.0.1 27/05/2016 14:27 |
|
| 19 | - */ |
|
| 14 | + * class QueryBuilderTest. |
|
| 15 | + * |
|
| 16 | + * @author Samuel Adeshina <[email protected]> |
|
| 17 | + * |
|
| 18 | + * @since v0.0.1 27/05/2016 14:27 |
|
| 19 | + */ |
|
| 20 | 20 | class QueryBuilderTest extends \PHPUnit_Framework_TestCase |
| 21 | 21 | { |
| 22 | - public function testBuilderActuallyBuilding() |
|
| 23 | - { |
|
| 24 | - $queryBuilder = new QueryBuilder('INSERT INTO tbl_name'); |
|
| 25 | - $queryBuilder = $queryBuilder->build("VALUES ('tbl_col1', 'tbl_col2')"); |
|
| 22 | + public function testBuilderActuallyBuilding() |
|
| 23 | + { |
|
| 24 | + $queryBuilder = new QueryBuilder('INSERT INTO tbl_name'); |
|
| 25 | + $queryBuilder = $queryBuilder->build("VALUES ('tbl_col1', 'tbl_col2')"); |
|
| 26 | 26 | |
| 27 | - $builtQuery = (string) $queryBuilder; |
|
| 28 | - $expectedQuery = "INSERT INTO tbl_name VALUES ('tbl_col1', 'tbl_col2')"; |
|
| 27 | + $builtQuery = (string) $queryBuilder; |
|
| 28 | + $expectedQuery = "INSERT INTO tbl_name VALUES ('tbl_col1', 'tbl_col2')"; |
|
| 29 | 29 | |
| 30 | - $this->assertEquals($expectedQuery, $builtQuery); |
|
| 31 | - } |
|
| 30 | + $this->assertEquals($expectedQuery, $builtQuery); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public function testWrapStringFunctionBehavesStatically() |
|
| 34 | - { |
|
| 35 | - $built = QueryBuilder::wrapString('Name', "'"); |
|
| 36 | - } |
|
| 33 | + public function testWrapStringFunctionBehavesStatically() |
|
| 34 | + { |
|
| 35 | + $built = QueryBuilder::wrapString('Name', "'"); |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -18,33 +18,33 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | interface ConnectableInterface |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Setter for the DSN string {@see $dsn} |
|
| 23 | - * |
|
| 24 | - * @param array $dsnArray |
|
| 25 | - * @return void |
|
| 26 | - */ |
|
| 27 | - public function setDsn(array $dsnArray); |
|
| 21 | + /** |
|
| 22 | + * Setter for the DSN string {@see $dsn} |
|
| 23 | + * |
|
| 24 | + * @param array $dsnArray |
|
| 25 | + * @return void |
|
| 26 | + */ |
|
| 27 | + public function setDsn(array $dsnArray); |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Establishes connection |
|
| 31 | - * |
|
| 32 | - * @param string $username optional |
|
| 33 | - * @param string $password optional |
|
| 34 | - * @throws [@todo get exceptions] |
|
| 35 | - * @return void |
|
| 36 | - */ |
|
| 37 | - public function connect(string $username, string $password=""); |
|
| 29 | + /** |
|
| 30 | + * Establishes connection |
|
| 31 | + * |
|
| 32 | + * @param string $username optional |
|
| 33 | + * @param string $password optional |
|
| 34 | + * @throws [@todo get exceptions] |
|
| 35 | + * @return void |
|
| 36 | + */ |
|
| 37 | + public function connect(string $username, string $password=""); |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Returns an instance a pdo intance of the connection object |
|
| 41 | - * |
|
| 42 | - * @return \PDO |
|
| 43 | - */ |
|
| 44 | - public function getConnection() : \PDO; |
|
| 39 | + /** |
|
| 40 | + * Returns an instance a pdo intance of the connection object |
|
| 41 | + * |
|
| 42 | + * @return \PDO |
|
| 43 | + */ |
|
| 44 | + public function getConnection() : \PDO; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Closes connection |
|
| 48 | - */ |
|
| 49 | - public function disableConnection(); |
|
| 46 | + /** |
|
| 47 | + * Closes connection |
|
| 48 | + */ |
|
| 49 | + public function disableConnection(); |
|
| 50 | 50 | } |
@@ -20,22 +20,22 @@ |
||
| 20 | 20 | { |
| 21 | 21 | public $connection; |
| 22 | 22 | |
| 23 | - public function __construct(string $adapter, array $dsnArray, string $username=null, string $password=null) |
|
| 24 | - { |
|
| 25 | - $class = __NAMESPACE__."\\Adapters\\$adapter"; |
|
| 26 | - $adapterObject = new $class; |
|
| 23 | + public function __construct(string $adapter, array $dsnArray, string $username=null, string $password=null) |
|
| 24 | + { |
|
| 25 | + $class = __NAMESPACE__."\\Adapters\\$adapter"; |
|
| 26 | + $adapterObject = new $class; |
|
| 27 | 27 | |
| 28 | - if ($adapterObject instanceof ConnectableInterface) |
|
| 29 | - { |
|
| 30 | - $adapterObject->setDsn($dsnArray); |
|
| 31 | - $adapterObject->connect($username, $password); |
|
| 32 | - } |
|
| 28 | + if ($adapterObject instanceof ConnectableInterface) |
|
| 29 | + { |
|
| 30 | + $adapterObject->setDsn($dsnArray); |
|
| 31 | + $adapterObject->connect($username, $password); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - $this->connection = $adapterObject->getConnection(); |
|
| 35 | - } |
|
| 34 | + $this->connection = $adapterObject->getConnection(); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - public function getConnection() : \PDO |
|
| 38 | - { |
|
| 39 | - return $this->connection; |
|
| 40 | - } |
|
| 37 | + public function getConnection() : \PDO |
|
| 38 | + { |
|
| 39 | + return $this->connection; |
|
| 40 | + } |
|
| 41 | 41 | } |
| 42 | 42 | \ No newline at end of file |
@@ -20,43 +20,43 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class DatabaseConnectionFactory |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * @var \PDO $connectionObject |
|
| 25 | - */ |
|
| 26 | - private static $connectionObject; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Gets the config values defined in the database-config.json file |
|
| 30 | - * and uses the values to create a new connection object. |
|
| 31 | - */ |
|
| 32 | - public static function bootstrap() |
|
| 33 | - { |
|
| 34 | - $databaseConfigJson = file_get_contents("bin/configs/database-config.json"); |
|
| 35 | - |
|
| 36 | - $databaseConfig = json_decode($databaseConfigJson); |
|
| 37 | - |
|
| 38 | - $adapter = $databaseConfig->adapter; |
|
| 39 | - $server = $databaseConfig->server; |
|
| 40 | - $database = $databaseConfig->database; |
|
| 41 | - $username = $databaseConfig->username; |
|
| 42 | - $password = $databaseConfig->password; |
|
| 43 | - |
|
| 44 | - self::$connectionObject = new Connection($adapter, [$server, $database], $username, $password); |
|
| 45 | - |
|
| 46 | - if ($databaseConfig->showError) |
|
| 47 | - { |
|
| 48 | - self::$connectionObject->getConnection()->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Returns a new connection object |
|
| 54 | - * |
|
| 55 | - * @return \PDO |
|
| 56 | - */ |
|
| 57 | - public static function getConnection() : \PDO |
|
| 58 | - { |
|
| 59 | - self::bootstrap(); |
|
| 60 | - return self::$connectionObject->getConnection(); |
|
| 61 | - } |
|
| 23 | + /** |
|
| 24 | + * @var \PDO $connectionObject |
|
| 25 | + */ |
|
| 26 | + private static $connectionObject; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Gets the config values defined in the database-config.json file |
|
| 30 | + * and uses the values to create a new connection object. |
|
| 31 | + */ |
|
| 32 | + public static function bootstrap() |
|
| 33 | + { |
|
| 34 | + $databaseConfigJson = file_get_contents("bin/configs/database-config.json"); |
|
| 35 | + |
|
| 36 | + $databaseConfig = json_decode($databaseConfigJson); |
|
| 37 | + |
|
| 38 | + $adapter = $databaseConfig->adapter; |
|
| 39 | + $server = $databaseConfig->server; |
|
| 40 | + $database = $databaseConfig->database; |
|
| 41 | + $username = $databaseConfig->username; |
|
| 42 | + $password = $databaseConfig->password; |
|
| 43 | + |
|
| 44 | + self::$connectionObject = new Connection($adapter, [$server, $database], $username, $password); |
|
| 45 | + |
|
| 46 | + if ($databaseConfig->showError) |
|
| 47 | + { |
|
| 48 | + self::$connectionObject->getConnection()->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Returns a new connection object |
|
| 54 | + * |
|
| 55 | + * @return \PDO |
|
| 56 | + */ |
|
| 57 | + public static function getConnection() : \PDO |
|
| 58 | + { |
|
| 59 | + self::bootstrap(); |
|
| 60 | + return self::$connectionObject->getConnection(); |
|
| 61 | + } |
|
| 62 | 62 | } |
@@ -17,31 +17,31 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class DeleteQueryBuilder extends QueryBuilder |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var \EmmetBlue\Core\Builder\QueryBuilder |
|
| 22 | - */ |
|
| 23 | - protected $queryBuilder; |
|
| 20 | + /** |
|
| 21 | + * @var \EmmetBlue\Core\Builder\QueryBuilder |
|
| 22 | + */ |
|
| 23 | + protected $queryBuilder; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @param string|null $tableName |
|
| 27 | - */ |
|
| 28 | - public function __construct() |
|
| 29 | - { |
|
| 30 | - $DeleteKeyword = "Delete"; |
|
| 31 | - $this->queryBuilder = $this->build($DeleteKeyword); |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * @param string|null $tableName |
|
| 27 | + */ |
|
| 28 | + public function __construct() |
|
| 29 | + { |
|
| 30 | + $DeleteKeyword = "Delete"; |
|
| 31 | + $this->queryBuilder = $this->build($DeleteKeyword); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * {@inheritdoc} |
|
| 36 | - * |
|
| 37 | - * @param string $tableName |
|
| 38 | - * |
|
| 39 | - * @return \EmmetBlue\Core\Builder\DeleteQueryBuilder |
|
| 40 | - */ |
|
| 41 | - public function from(string $tableName) |
|
| 42 | - { |
|
| 43 | - $this->queryBuilder = $this->queryBuilder->build("FROM ".$tableName); |
|
| 34 | + /** |
|
| 35 | + * {@inheritdoc} |
|
| 36 | + * |
|
| 37 | + * @param string $tableName |
|
| 38 | + * |
|
| 39 | + * @return \EmmetBlue\Core\Builder\DeleteQueryBuilder |
|
| 40 | + */ |
|
| 41 | + public function from(string $tableName) |
|
| 42 | + { |
|
| 43 | + $this->queryBuilder = $this->queryBuilder->build("FROM ".$tableName); |
|
| 44 | 44 | |
| 45 | - return $this; |
|
| 46 | - } |
|
| 45 | + return $this; |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -17,49 +17,49 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class UpdateQueryBuilder extends QueryBuilder |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var \EmmetBlue\Core\Builder\QueryBuilder |
|
| 22 | - */ |
|
| 23 | - protected $queryBuilder; |
|
| 20 | + /** |
|
| 21 | + * @var \EmmetBlue\Core\Builder\QueryBuilder |
|
| 22 | + */ |
|
| 23 | + protected $queryBuilder; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @param string|null $tableName |
|
| 27 | - */ |
|
| 28 | - public function __construct(string $tableName = null) |
|
| 29 | - { |
|
| 30 | - $updateKeyword = (is_null($tableName)) ? 'UPDATE' : 'UPDATE '.$tableName; |
|
| 31 | - $this->queryBuilder = $this->build($updateKeyword); |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * @param string|null $tableName |
|
| 27 | + */ |
|
| 28 | + public function __construct(string $tableName = null) |
|
| 29 | + { |
|
| 30 | + $updateKeyword = (is_null($tableName)) ? 'UPDATE' : 'UPDATE '.$tableName; |
|
| 31 | + $this->queryBuilder = $this->build($updateKeyword); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * {@inheritdoc} |
|
| 36 | - * |
|
| 37 | - * @param string $tableName |
|
| 38 | - * |
|
| 39 | - * @return \EmmetBlue\Core\Builder\UpdateQueryBuilder |
|
| 40 | - */ |
|
| 41 | - public function table(string $tableName) |
|
| 42 | - { |
|
| 43 | - $this->queryBuilder = $this->queryBuilder->build($tableName); |
|
| 34 | + /** |
|
| 35 | + * {@inheritdoc} |
|
| 36 | + * |
|
| 37 | + * @param string $tableName |
|
| 38 | + * |
|
| 39 | + * @return \EmmetBlue\Core\Builder\UpdateQueryBuilder |
|
| 40 | + */ |
|
| 41 | + public function table(string $tableName) |
|
| 42 | + { |
|
| 43 | + $this->queryBuilder = $this->queryBuilder->build($tableName); |
|
| 44 | 44 | |
| 45 | - return $this; |
|
| 46 | - } |
|
| 45 | + return $this; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * {@inheritdoc} |
|
| 50 | - * |
|
| 51 | - * @param string[] $inputValues |
|
| 52 | - * |
|
| 53 | - * @return \EmmetBlue\Core\Builder\UpdateQueryBuilder |
|
| 54 | - */ |
|
| 55 | - public function set(array $inputValues) |
|
| 56 | - { |
|
| 57 | - $valuesKeyword = 'SET '; |
|
| 48 | + /** |
|
| 49 | + * {@inheritdoc} |
|
| 50 | + * |
|
| 51 | + * @param string[] $inputValues |
|
| 52 | + * |
|
| 53 | + * @return \EmmetBlue\Core\Builder\UpdateQueryBuilder |
|
| 54 | + */ |
|
| 55 | + public function set(array $inputValues) |
|
| 56 | + { |
|
| 57 | + $valuesKeyword = 'SET '; |
|
| 58 | 58 | |
| 59 | - $valuesKeyword .= self::getImplodedStringWithKeys($inputValues); |
|
| 59 | + $valuesKeyword .= self::getImplodedStringWithKeys($inputValues); |
|
| 60 | 60 | |
| 61 | - $this->queryBuilder = $this->queryBuilder->build($valuesKeyword); |
|
| 61 | + $this->queryBuilder = $this->queryBuilder->build($valuesKeyword); |
|
| 62 | 62 | |
| 63 | - return $this; |
|
| 64 | - } |
|
| 63 | + return $this; |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -17,68 +17,68 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class InsertQueryBuilder extends QueryBuilder |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var \EmmetBlue\Core\Abstraction\QueryBuilder |
|
| 22 | - */ |
|
| 23 | - protected $queryBuilder; |
|
| 20 | + /** |
|
| 21 | + * @var \EmmetBlue\Core\Abstraction\QueryBuilder |
|
| 22 | + */ |
|
| 23 | + protected $queryBuilder; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @param string|null $tableName |
|
| 27 | - */ |
|
| 28 | - public function __construct(string $tableName = null) |
|
| 29 | - { |
|
| 30 | - $insertKeyword = (is_null($tableName)) ? 'INSERT' : 'INSERT INTO '.$tableName; |
|
| 31 | - $this->queryBuilder = $this->build($insertKeyword); |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * @param string|null $tableName |
|
| 27 | + */ |
|
| 28 | + public function __construct(string $tableName = null) |
|
| 29 | + { |
|
| 30 | + $insertKeyword = (is_null($tableName)) ? 'INSERT' : 'INSERT INTO '.$tableName; |
|
| 31 | + $this->queryBuilder = $this->build($insertKeyword); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * {@inheritdoc} |
|
| 36 | - * |
|
| 37 | - * @param string $tableName |
|
| 38 | - * @param string[] $tableColumns Optional, provide this to specify the |
|
| 39 | - * columns that should be acted on |
|
| 40 | - * |
|
| 41 | - * @return \EmmetBlue\Core\Abstraction\InsertQueryBuilder |
|
| 42 | - */ |
|
| 43 | - public function into(string $tableName, array $tableColumns = []) |
|
| 44 | - { |
|
| 45 | - $intoKeyword = 'INTO '.$tableName; |
|
| 34 | + /** |
|
| 35 | + * {@inheritdoc} |
|
| 36 | + * |
|
| 37 | + * @param string $tableName |
|
| 38 | + * @param string[] $tableColumns Optional, provide this to specify the |
|
| 39 | + * columns that should be acted on |
|
| 40 | + * |
|
| 41 | + * @return \EmmetBlue\Core\Abstraction\InsertQueryBuilder |
|
| 42 | + */ |
|
| 43 | + public function into(string $tableName, array $tableColumns = []) |
|
| 44 | + { |
|
| 45 | + $intoKeyword = 'INTO '.$tableName; |
|
| 46 | 46 | |
| 47 | - if (!empty($tableColumns)) { |
|
| 48 | - $intoKeyword .= $this->wrapString(self::getImplodedString($tableColumns), '(', ')'); |
|
| 49 | - } |
|
| 47 | + if (!empty($tableColumns)) { |
|
| 48 | + $intoKeyword .= $this->wrapString(self::getImplodedString($tableColumns), '(', ')'); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - $this->queryBuilder = $this->queryBuilder->build($intoKeyword); |
|
| 51 | + $this->queryBuilder = $this->queryBuilder->build($intoKeyword); |
|
| 52 | 52 | |
| 53 | - return $this; |
|
| 54 | - } |
|
| 53 | + return $this; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * {@inheritdoc} |
|
| 58 | - * |
|
| 59 | - * @param string[] $inputValues |
|
| 60 | - * |
|
| 61 | - * @return \EmmetBlue\Core\Abstraction\InsertQueryBuilder |
|
| 62 | - */ |
|
| 63 | - public function values(array $inputValues) |
|
| 64 | - { |
|
| 65 | - $valuesKeyword = 'VALUES '; |
|
| 66 | - $isMultidimentional = is_array($inputValues[0]); |
|
| 56 | + /** |
|
| 57 | + * {@inheritdoc} |
|
| 58 | + * |
|
| 59 | + * @param string[] $inputValues |
|
| 60 | + * |
|
| 61 | + * @return \EmmetBlue\Core\Abstraction\InsertQueryBuilder |
|
| 62 | + */ |
|
| 63 | + public function values(array $inputValues) |
|
| 64 | + { |
|
| 65 | + $valuesKeyword = 'VALUES '; |
|
| 66 | + $isMultidimentional = is_array($inputValues[0]); |
|
| 67 | 67 | |
| 68 | - if (!$isMultidimentional) { |
|
| 69 | - $valuesKeyword .= $this->wrapString(self::getImplodedString($inputValues), '(', ')'); |
|
| 70 | - } else { |
|
| 71 | - $tempValuesKeywords = []; |
|
| 72 | - foreach ($inputValues as $inputValue) { |
|
| 73 | - $tempValuesKeywords[] = $this->wrapString(self::getImplodedString($inputValue), '(', ')'); |
|
| 74 | - } |
|
| 68 | + if (!$isMultidimentional) { |
|
| 69 | + $valuesKeyword .= $this->wrapString(self::getImplodedString($inputValues), '(', ')'); |
|
| 70 | + } else { |
|
| 71 | + $tempValuesKeywords = []; |
|
| 72 | + foreach ($inputValues as $inputValue) { |
|
| 73 | + $tempValuesKeywords[] = $this->wrapString(self::getImplodedString($inputValue), '(', ')'); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - $valuesKeyword .= self::getImplodedString($tempValuesKeywords); |
|
| 77 | - unset($tempValuesKeywords); |
|
| 78 | - } |
|
| 76 | + $valuesKeyword .= self::getImplodedString($tempValuesKeywords); |
|
| 77 | + unset($tempValuesKeywords); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - $this->queryBuilder = $this->queryBuilder->build($valuesKeyword); |
|
| 80 | + $this->queryBuilder = $this->queryBuilder->build($valuesKeyword); |
|
| 81 | 81 | |
| 82 | - return $this; |
|
| 83 | - } |
|
| 82 | + return $this; |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -20,169 +20,169 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class QueryBuilder implements BuildableInterface |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * @var string Global Sql Statement |
|
| 25 | - */ |
|
| 26 | - private $sqlStatement; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var string SPACE |
|
| 30 | - */ |
|
| 31 | - const SPACE = ' '; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @param string | null $sqlStatement SQL Statement to be processed by the build method |
|
| 35 | - * |
|
| 36 | - * @throws {@todo Write exception class} |
|
| 37 | - */ |
|
| 38 | - public function __construct(string $sqlStatement = null) |
|
| 39 | - { |
|
| 40 | - self::setSqlStatement($sqlStatement); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Returns the string value of the global sqlStatement variable. |
|
| 45 | - * |
|
| 46 | - * @return string |
|
| 47 | - */ |
|
| 48 | - private function getSqlStatement() |
|
| 49 | - { |
|
| 50 | - return $this->sqlStatement; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Sets/Modifies the value of the global sqlStatement variable. |
|
| 55 | - * |
|
| 56 | - * @param string $sqlStatement to set/replace global equivalent to. |
|
| 57 | - * |
|
| 58 | - * @return null |
|
| 59 | - */ |
|
| 60 | - private function setSqlStatement(string $sqlStatement = null) |
|
| 61 | - { |
|
| 62 | - $this->sqlStatement = $sqlStatement; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Builds a QueryBuilder object. |
|
| 67 | - * {@see QueryBuildableInterface}. |
|
| 68 | - * |
|
| 69 | - * @param string | null $sqlStringToAppend |
|
| 70 | - * |
|
| 71 | - * @throws {@todo Create exceptions} |
|
| 72 | - * |
|
| 73 | - * @return QueryBuilder new instance of the QueryBuilder object. |
|
| 74 | - */ |
|
| 75 | - public function build(string $sqlStringToAppend) : BuildableInterface |
|
| 76 | - { |
|
| 77 | - $separator = (empty(self::getSqlStatement())) ? '' : self::SPACE; |
|
| 78 | - $newSqlString = self::getSqlStatement().$separator.$sqlStringToAppend; |
|
| 79 | - self::setSqlStatement($newSqlString); |
|
| 80 | - |
|
| 81 | - return $this; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * returns a `built sql` when the QueryBuilder object is casted to a string. |
|
| 86 | - * |
|
| 87 | - * @return string |
|
| 88 | - */ |
|
| 89 | - public function __toString() |
|
| 90 | - { |
|
| 91 | - return self::getSqlStatement(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Implodes an array into a string. |
|
| 96 | - * |
|
| 97 | - * @param array $arrayToImplode |
|
| 98 | - * @param string $delimiter Optional. |
|
| 99 | - * |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - protected function getImplodedString(array $arrayToImplode, string $delimiter = ',') : string |
|
| 103 | - { |
|
| 104 | - return implode($delimiter, $arrayToImplode); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Implodes an array into a string while keeping track of the keys. |
|
| 109 | - * |
|
| 110 | - * @param array $arrayToImplode |
|
| 111 | - * @param string $delimiter Optional. |
|
| 112 | - * |
|
| 113 | - * @return string |
|
| 114 | - */ |
|
| 115 | - protected function getImplodedStringWithKeys(array $arrayToImplode, string $keyDelimiter='=', string $delimiter = ',') : string |
|
| 116 | - { |
|
| 117 | - $implodedStrings = []; |
|
| 118 | - |
|
| 119 | - foreach ($arrayToImplode as $key=>$value) |
|
| 120 | - { |
|
| 121 | - $implodedStrings[] = $key.$keyDelimiter.$value; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return implode($delimiter, $implodedStrings); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Wraps a string with specified characters. |
|
| 129 | - * |
|
| 130 | - * @param string $strBefore |
|
| 131 | - * @param string|null $strAfter |
|
| 132 | - * @param string $strToWrap |
|
| 133 | - * |
|
| 134 | - * @return string |
|
| 135 | - */ |
|
| 136 | - public static function wrapString(string $strToWrap, string $strBefore, string $strAfter = null) : string |
|
| 137 | - { |
|
| 138 | - return $strBefore.$strToWrap.(is_null($strAfter) ? $strBefore : $strAfter); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * {@inheritdoc} |
|
| 143 | - * |
|
| 144 | - * @param string $condition |
|
| 145 | - * |
|
| 146 | - * @return \EmmetBlue\Core\Builder\BuildableInterface |
|
| 147 | - */ |
|
| 148 | - public function where(string $condition) |
|
| 149 | - { |
|
| 150 | - $whereString = "WHERE $condition"; |
|
| 151 | - |
|
| 152 | - $this->queryBuilder = $this->queryBuilder->build($whereString); |
|
| 153 | - |
|
| 154 | - return $this; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * {@inheritdoc} |
|
| 159 | - * |
|
| 160 | - * @param string $condition |
|
| 161 | - * |
|
| 162 | - * @return \EmmetBlue\Core\Builder\BuildableInterface |
|
| 163 | - */ |
|
| 164 | - public function andWhere(string $condition) |
|
| 165 | - { |
|
| 166 | - $whereString = "AND WHERE $condition"; |
|
| 167 | - |
|
| 168 | - $this->queryBuilder = $this->queryBuilder->build($whereString); |
|
| 169 | - |
|
| 170 | - return $this; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * {@inheritdoc} |
|
| 175 | - * |
|
| 176 | - * @param string $condition |
|
| 177 | - * |
|
| 178 | - * @return \EmmetBlue\Core\Builder\BuildableInterface |
|
| 179 | - */ |
|
| 180 | - public function orWhere(string $condition) |
|
| 181 | - { |
|
| 182 | - $whereString = "OR WHERE $condition"; |
|
| 183 | - |
|
| 184 | - $this->queryBuilder = $this->queryBuilder->build($whereString); |
|
| 185 | - |
|
| 186 | - return $this; |
|
| 187 | - } |
|
| 23 | + /** |
|
| 24 | + * @var string Global Sql Statement |
|
| 25 | + */ |
|
| 26 | + private $sqlStatement; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var string SPACE |
|
| 30 | + */ |
|
| 31 | + const SPACE = ' '; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @param string | null $sqlStatement SQL Statement to be processed by the build method |
|
| 35 | + * |
|
| 36 | + * @throws {@todo Write exception class} |
|
| 37 | + */ |
|
| 38 | + public function __construct(string $sqlStatement = null) |
|
| 39 | + { |
|
| 40 | + self::setSqlStatement($sqlStatement); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Returns the string value of the global sqlStatement variable. |
|
| 45 | + * |
|
| 46 | + * @return string |
|
| 47 | + */ |
|
| 48 | + private function getSqlStatement() |
|
| 49 | + { |
|
| 50 | + return $this->sqlStatement; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Sets/Modifies the value of the global sqlStatement variable. |
|
| 55 | + * |
|
| 56 | + * @param string $sqlStatement to set/replace global equivalent to. |
|
| 57 | + * |
|
| 58 | + * @return null |
|
| 59 | + */ |
|
| 60 | + private function setSqlStatement(string $sqlStatement = null) |
|
| 61 | + { |
|
| 62 | + $this->sqlStatement = $sqlStatement; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Builds a QueryBuilder object. |
|
| 67 | + * {@see QueryBuildableInterface}. |
|
| 68 | + * |
|
| 69 | + * @param string | null $sqlStringToAppend |
|
| 70 | + * |
|
| 71 | + * @throws {@todo Create exceptions} |
|
| 72 | + * |
|
| 73 | + * @return QueryBuilder new instance of the QueryBuilder object. |
|
| 74 | + */ |
|
| 75 | + public function build(string $sqlStringToAppend) : BuildableInterface |
|
| 76 | + { |
|
| 77 | + $separator = (empty(self::getSqlStatement())) ? '' : self::SPACE; |
|
| 78 | + $newSqlString = self::getSqlStatement().$separator.$sqlStringToAppend; |
|
| 79 | + self::setSqlStatement($newSqlString); |
|
| 80 | + |
|
| 81 | + return $this; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * returns a `built sql` when the QueryBuilder object is casted to a string. |
|
| 86 | + * |
|
| 87 | + * @return string |
|
| 88 | + */ |
|
| 89 | + public function __toString() |
|
| 90 | + { |
|
| 91 | + return self::getSqlStatement(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Implodes an array into a string. |
|
| 96 | + * |
|
| 97 | + * @param array $arrayToImplode |
|
| 98 | + * @param string $delimiter Optional. |
|
| 99 | + * |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + protected function getImplodedString(array $arrayToImplode, string $delimiter = ',') : string |
|
| 103 | + { |
|
| 104 | + return implode($delimiter, $arrayToImplode); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Implodes an array into a string while keeping track of the keys. |
|
| 109 | + * |
|
| 110 | + * @param array $arrayToImplode |
|
| 111 | + * @param string $delimiter Optional. |
|
| 112 | + * |
|
| 113 | + * @return string |
|
| 114 | + */ |
|
| 115 | + protected function getImplodedStringWithKeys(array $arrayToImplode, string $keyDelimiter='=', string $delimiter = ',') : string |
|
| 116 | + { |
|
| 117 | + $implodedStrings = []; |
|
| 118 | + |
|
| 119 | + foreach ($arrayToImplode as $key=>$value) |
|
| 120 | + { |
|
| 121 | + $implodedStrings[] = $key.$keyDelimiter.$value; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return implode($delimiter, $implodedStrings); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Wraps a string with specified characters. |
|
| 129 | + * |
|
| 130 | + * @param string $strBefore |
|
| 131 | + * @param string|null $strAfter |
|
| 132 | + * @param string $strToWrap |
|
| 133 | + * |
|
| 134 | + * @return string |
|
| 135 | + */ |
|
| 136 | + public static function wrapString(string $strToWrap, string $strBefore, string $strAfter = null) : string |
|
| 137 | + { |
|
| 138 | + return $strBefore.$strToWrap.(is_null($strAfter) ? $strBefore : $strAfter); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * {@inheritdoc} |
|
| 143 | + * |
|
| 144 | + * @param string $condition |
|
| 145 | + * |
|
| 146 | + * @return \EmmetBlue\Core\Builder\BuildableInterface |
|
| 147 | + */ |
|
| 148 | + public function where(string $condition) |
|
| 149 | + { |
|
| 150 | + $whereString = "WHERE $condition"; |
|
| 151 | + |
|
| 152 | + $this->queryBuilder = $this->queryBuilder->build($whereString); |
|
| 153 | + |
|
| 154 | + return $this; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * {@inheritdoc} |
|
| 159 | + * |
|
| 160 | + * @param string $condition |
|
| 161 | + * |
|
| 162 | + * @return \EmmetBlue\Core\Builder\BuildableInterface |
|
| 163 | + */ |
|
| 164 | + public function andWhere(string $condition) |
|
| 165 | + { |
|
| 166 | + $whereString = "AND WHERE $condition"; |
|
| 167 | + |
|
| 168 | + $this->queryBuilder = $this->queryBuilder->build($whereString); |
|
| 169 | + |
|
| 170 | + return $this; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * {@inheritdoc} |
|
| 175 | + * |
|
| 176 | + * @param string $condition |
|
| 177 | + * |
|
| 178 | + * @return \EmmetBlue\Core\Builder\BuildableInterface |
|
| 179 | + */ |
|
| 180 | + public function orWhere(string $condition) |
|
| 181 | + { |
|
| 182 | + $whereString = "OR WHERE $condition"; |
|
| 183 | + |
|
| 184 | + $this->queryBuilder = $this->queryBuilder->build($whereString); |
|
| 185 | + |
|
| 186 | + return $this; |
|
| 187 | + } |
|
| 188 | 188 | } |