Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Tests/Sqlite/SqliteDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,45 @@ public function testQuoteAndDecodeBinary()

$this->assertEquals($expected, $result);
}

#[TestDox('The custom ROW_NUMBER function correctly numbers rows')]
public function testRowNumberCustomFunction()
{
$this->loadExampleData();

static::$connection->setQuery('SELECT ROW_NUMBER(0)')->execute();

$query = static::$connection->createQuery()
->select('ROW_NUMBER() AS row_num, title')
->from('#__dbtest');

$results = static::$connection->setQuery($query)->loadObjectList();

$this->assertCount(4, $results);
$this->assertEquals(1, $results[0]->row_num);
$this->assertEquals(2, $results[1]->row_num);
$this->assertEquals(3, $results[2]->row_num);
$this->assertEquals(4, $results[3]->row_num);

$query = static::$connection->createQuery()
->update('#__dbtest')
->set('start_date = ' . static::$connection->quote('2019-10-19 00:00:00'))
->where('id = 4');

static::$connection->setQuery($query)->execute();

static::$connection->setQuery('SELECT ROW_NUMBER(0)')->execute();

$query = static::$connection->createQuery()
->select('ROW_NUMBER(NULL, start_date) AS row_num, title')
->from('#__dbtest');

$results = static::$connection->setQuery($query)->loadObjectList();

$this->assertCount(4, $results);
$this->assertEquals(1, $results[0]->row_num);
$this->assertEquals(2, $results[1]->row_num);
$this->assertEquals(3, $results[2]->row_num);
$this->assertEquals(1, $results[3]->row_num);
}
}
21 changes: 15 additions & 6 deletions src/Pdo/PdoDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,21 @@ public function connect()
$connectionString = str_replace($replace, $with, $format);

try {
$this->connection = new \PDO(
$connectionString,
$this->options['user'],
$this->options['password'],
$this->options['driverOptions']
);
if (PHP_VERSION_ID >= 80400) {
$this->connection = \PDO::connect(
$connectionString,
$this->options['user'],
$this->options['password'],
$this->options['driverOptions']
);
} else {
$this->connection = new \PDO(
$connectionString,
$this->options['user'],
$this->options['password'],
$this->options['driverOptions']
);
}
} catch (\PDOException $e) {
throw new ConnectionFailureException('Could not connect to PDO: ' . $e->getMessage(), $e->getCode(), $e);
}
Expand Down
51 changes: 30 additions & 21 deletions src/Sqlite/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,42 @@ public function connect()

parent::connect();

$this->connection->sqliteCreateFunction(
'ROW_NUMBER',
function ($init = null) {
static $rownum, $partition;
$rownumberfn = function ($init = null) {
static $rownum, $partition;

if ($init !== null) {
$rownum = $init;
$partition = null;
if ($init !== null) {
$rownum = $init;
$partition = null;

return $rownum;
}

$args = \func_get_args();
array_shift($args);
return $rownum;
}

$partitionBy = $args ? implode(',', $args) : null;
$args = \func_get_args();
array_shift($args);

if ($partitionBy === null || $partitionBy === $partition) {
$rownum++;
} else {
$rownum = 1;
$partition = $partitionBy;
}
$partitionBy = $args ? implode(',', $args) : null;

return $rownum;
if ($partitionBy === null || $partitionBy === $partition) {
$rownum++;
} else {
$rownum = 1;
$partition = $partitionBy;
}
);

return $rownum;
};

if ($this->connection instanceof \Pdo\Sqlite) {
$this->connection->createFunction(
'ROW_NUMBER',
$rownumberfn
);
} else {
$this->connection->sqliteCreateFunction(
'ROW_NUMBER',
$rownumberfn
);
}
}

/**
Expand Down
Loading