123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace Test\DB\QueryBuilder;
- use OC\DB\QueryBuilder\Literal;
- use OC\DB\QueryBuilder\Parameter;
- use OC\DB\QueryBuilder\QuoteHelper;
- use OCP\DB\QueryBuilder\ILiteral;
- use OCP\DB\QueryBuilder\IParameter;
- class QuoteHelperTest extends \Test\TestCase {
-
- protected $helper;
- protected function setUp(): void {
- parent::setUp();
- $this->helper = new QuoteHelper();
- }
- public function dataQuoteColumnName() {
- return [
- ['column', '`column`'],
- [new Literal('literal'), 'literal'],
- [new Literal(1), '1'],
- [new Parameter(':param'), ':param'],
-
-
- ['null', 'null'],
- ];
- }
-
- public function testQuoteColumnName($input, $expected): void {
- $this->assertSame(
- $expected,
- $this->helper->quoteColumnName($input)
- );
- }
- public function dataQuoteColumnNames() {
- return [
-
- ['d.column', '`d`.`column`'],
- ['column', '`column`'],
- [new Literal('literal'), 'literal'],
- [new Literal(1), '1'],
- [new Parameter(':param'), ':param'],
-
- [['column'], ['`column`']],
- [[new Literal('literal')], ['literal']],
- [[new Literal(1)], ['1']],
- [[new Parameter(':param')], [':param']],
-
- [['column1', 'column2'], ['`column1`', '`column2`']],
- [['column', new Literal('literal')], ['`column`', 'literal']],
- [['column', new Literal(1)], ['`column`', '1']],
- [['column', new Parameter(':param')], ['`column`', ':param']],
- ];
- }
-
- public function testQuoteColumnNames($input, $expected): void {
- $this->assertSame(
- $expected,
- $this->helper->quoteColumnNames($input)
- );
- }
-
- public function quoteColumnNames($strings) {
- if (!is_array($strings)) {
- return $this->quoteColumnName($strings);
- }
- $return = [];
- foreach ($strings as $string) {
- $return[] = $this->quoteColumnName($string);
- }
- return $return;
- }
-
- public function quoteColumnName($string) {
- if ($string instanceof IParameter) {
- return $string->getName();
- }
- if ($string instanceof ILiteral) {
- return $string->getLiteral();
- }
- if ($string === null) {
- return $string;
- }
- if (!is_string($string)) {
- throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed');
- }
- if (substr_count($string, '.')) {
- [$alias, $columnName] = explode('.', $string);
- return '`' . $alias . '`.`' . $columnName . '`';
- }
- return '`' . $string . '`';
- }
- }
|