Skip to content

Commit

Permalink
Fix a bug where a field is quoted like a value and not an identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Soneritics committed Jun 11, 2018
1 parent bf11ac0 commit 20cb74f
Showing 1 changed file with 74 additions and 74 deletions.
148 changes: 74 additions & 74 deletions Soneritics/Database/Query/Count.php
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
<?php
/*
* The MIT License
*
* Copyright 2014 Soneritics Webdevelopment.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace Database\Query;

use Database\Exceptions\FatalException;
use Database\DatabaseConnectionFactory;

/**
* Select query class.
*
* @author Jordi Jolink <mail@jordijolink.nl>
* @since 2-1-2015
*/
class Count extends Select
{
// Default select all fields
protected $fields = 'COUNT(*)';

/**
* Set the field to count.
* @param $fields
* @return $this
* @throws FatalException
*/
public function fields($fields)
{
if (!is_string($fields)) {
throw new FatalException('Only strings are allowed in COUNT query');
}

$this->fields = sprintf(
'COUNT(%s)',
DatabaseConnectionFactory::get()->quote($fields)
);

return $this;
}

/**
* Override the default execute function.
* @return int
*/
public function execute()
{
$result = parent::execute();
if ($result->count() === 0) {
return 0;
} else {
return $result->get()['COUNT(*)'];
}
}
}
<?php
/*
* The MIT License
*
* Copyright 2014 Soneritics Webdevelopment.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace Database\Query;

use Database\Exceptions\FatalException;
use Database\DatabaseConnectionFactory;

/**
* Select query class.
*
* @author Jordi Jolink <mail@jordijolink.nl>
* @since 2-1-2015
*/
class Count extends Select
{
// Default select all fields
protected $fields = 'COUNT(*)';

/**
* Set the field to count.
* @param $fields
* @return $this
* @throws FatalException
*/
public function fields($fields)
{
if (!is_string($fields)) {
throw new FatalException('Only strings are allowed in COUNT query');
}

$this->fields = sprintf(
'COUNT(%s)',
DatabaseConnectionFactory::get()->quoteIdentifier($fields)
);

return $this;
}

/**
* Override the default execute function.
* @return int
*/
public function execute()
{
$result = parent::execute();
if ($result->count() === 0) {
return 0;
} else {
return $result->get()['COUNT(*)'];
}
}
}

0 comments on commit 20cb74f

Please sign in to comment.