Skip to content

Commit fa34895

Browse files
committed
Fix SQLite indexing
1 parent 5750297 commit fa34895

File tree

3 files changed

+111
-95
lines changed

3 files changed

+111
-95
lines changed

AbstractMigrate.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ abstract protected function migrateTable();
1616

1717
public function __construct(string $table, ?string $prefix = null)
1818
{
19-
if (is_null($prefix)) {
19+
/*
20+
if (is_null($prefix)) {
2021
$prefix = getenv("MYSQL_PREFIX");
2122
if ($prefix === false) {
2223
throw new Exception("Table prefix is required!", 1);
2324
}
2425
}
26+
*/
2527
$this->mig = new Create($table, $prefix);
2628
$this->table = $table;
2729
}

Handlers/SQLite/SQLiteResult.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class SQLiteResult
1313
{
1414

15-
public $index = 0;
15+
public $index = -1;
1616
public int|string $num_rows = 0;
1717
public array|bool $rows = false;
1818
public array|bool $rowsObj = false;
@@ -135,15 +135,18 @@ public function free_result(): void
135135
*/
136136
protected function preFetchData(): void
137137
{
138-
$result = $this->query->fetchArray(SQLITE3_ASSOC);
139-
if($result !== false) {
140-
$this->rowsObj = $this->rows = [];
141-
$this->num_rows = 0;
142-
while ($row = $this->query->fetchArray(SQLITE3_ASSOC)) {
143-
$this->rows[] = $row;
144-
$this->rowsObj[] = (object)$row;
145-
$this->num_rows++;
146-
}
138+
$this->rowsObj = $this->rows = [];
139+
$this->num_rows = 0;
140+
$obj = $arr = array();
141+
while ($row = $this->query->fetchArray(SQLITE3_ASSOC)) {
142+
$arr[] = $row;
143+
$obj[] = (object)$row;
144+
$this->num_rows++;
145+
}
146+
147+
if(count($arr) > 0) {
148+
$this->rows = $arr;
149+
$this->rowsObj = $obj;
147150
}
148151
}
149152

@@ -167,7 +170,7 @@ protected function startIndex(): bool
167170
protected function endIndex(): void
168171
{
169172
if($this->index >= $this->num_rows) {
170-
$this->index = 0;
173+
$this->index = -1;
171174
}
172175
}
173176

tests/unitary-db.php

Lines changed: 94 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,104 @@
22

33
use database\migrations\Test;
44
use database\migrations\TestCategory;
5-
use MaplePHP\Query\Exceptions\ConnectException;
6-
use MaplePHP\Query\Exceptions\DBQueryException;
7-
use MaplePHP\Query\Exceptions\DBValidationException;
5+
use MaplePHP\Query\Handlers\PostgreSQLHandler;
86
use MaplePHP\Query\Handlers\SQLiteHandler;
97
use MaplePHP\Unitary\Unit;
108
use MaplePHP\Query\Connect;
119
use MaplePHP\Query\DB;
1210

1311
// Only validate if there is a connection open!
14-
try {
15-
if (Connect::hasInstance() && Connect::getInstance()->hasConnection()) {
16-
$unit = new Unit();
17-
18-
// Add a title to your tests (not required)
19-
$unit->addTitle("Testing MaplePHP Query library!");
20-
$unit->add("MySql Query builder", function ($inst) {
21-
22-
$db = Connect::getInstance();
23-
$select = $db::select("id,a.name,b.name AS cat", ["test", "a"])->whereParent(0)->where("status", 0, ">")->limit(6);
24-
$select->join(["test_category", "b"], "tid = id");
25-
26-
// 3 queries
27-
$obj = $select->get();
28-
$arr = $select->fetch();
29-
$pluck = DB::table("test")->pluck("name")->get();
30-
31-
$inst->add($obj, [
32-
"isObject" => [],
33-
"missingColumn" => function () use ($obj) {
34-
return (isset($obj->name) && isset($obj->cat));
35-
}
36-
], "Data is missing");
37-
38-
$inst->add($arr, [
39-
"isArray" => [],
40-
"noRows" => function () use ($arr) {
41-
return (count($arr) > 0);
42-
}
43-
], "Fetch feed empty");
44-
45-
$inst->add($pluck, [
46-
"isString" => [],
47-
"length" => [1]
48-
], "Pluck is expected to return string");
49-
50-
$select = $db::select("id,test.name,test_category.name AS cat", new Test)->whereParent(0)->where("status", 0, ">")->limit(6);
51-
$select->join(new TestCategory);
52-
$obj = $select->obj();
53-
54-
$inst->add($obj, [
55-
"isObject" => [],
56-
"missingColumn" => function () use ($obj) {
57-
return (isset($obj->name) && isset($obj->cat));
58-
}
59-
], "Data is missing");
60-
});
61-
62-
/**
63-
* This will test multiple databases AND
64-
* validate sqLite database
65-
*/
66-
$unit->add("sqLite Query builder", function ($inst) {
67-
68-
$sqLiteHandler = new SQLiteHandler(__DIR__ . "/database.sqlite");
69-
$sqLiteHandler->setPrefix("mp_");
70-
$connect = Connect::setHandler($sqLiteHandler, "mp");
71-
$connect->execute();
72-
73-
// Access sqLite connection
74-
$select = Connect::getInstance("mp")::select("id,name,content", "test")->whereStatus(1)->limit(3);
75-
$result = $select->fetch();
76-
$inst->add($select->fetch(), [
77-
"isArray" => [],
78-
"rows" => function () use ($result) {
79-
return (count($result) === 3);
80-
}
81-
], "Fetch should equal to 3");
82-
});
83-
84-
$unit->execute();
85-
}
86-
87-
} catch (ConnectException|DBValidationException|DBQueryException $e) {
88-
echo $e->getMessage();
12+
if (Connect::hasInstance() && Connect::getInstance()->hasConnection()) {
13+
14+
$unit = new Unit();
15+
16+
// Add a title to your tests (not required)
17+
$unit->addTitle("Testing MaplePHP Query library!");
18+
$unit->add("MySql Query builder", function ($inst) {
19+
20+
$db = Connect::getInstance();
21+
$select = $db::select("id,a.name,b.name AS cat", ["test", "a"])->whereParent(0)->where("status", 0, ">")->limit(6);
22+
$select->join(["test_category", "b"], "tid = id");
23+
24+
// 3 queries
25+
$obj = $select->get();
26+
$arr = $select->fetch();
27+
$pluck = DB::table("test")->pluck("name")->get();
28+
29+
$inst->add($obj, [
30+
"isObject" => [],
31+
"missingColumn" => function () use ($obj) {
32+
return (isset($obj->name) && isset($obj->cat));
33+
}
34+
], "Data is missing");
35+
36+
$inst->add($arr, [
37+
"isArray" => [],
38+
"noRows" => function () use ($arr) {
39+
return (count($arr) > 0);
40+
}
41+
], "Fetch feed empty");
42+
43+
$inst->add($pluck, [
44+
"isString" => [],
45+
"length" => [1]
46+
], "Pluck is expected to return string");
47+
48+
$select = $db::select("id,test.name,test_category.name AS cat", new Test)->whereParent(0)->where("status", 0, ">")->limit(6);
49+
$select->join(new TestCategory);
50+
$obj = $select->obj();
51+
52+
$inst->add($obj, [
53+
"isObject" => [],
54+
"missingColumn" => function () use ($obj) {
55+
return (isset($obj->name) && isset($obj->cat));
56+
}
57+
], "Data is missing");
58+
});
59+
60+
/**
61+
* This will test multiple databases AND
62+
* validate sqLite database
63+
*/
64+
$unit->add("sqLite Query builder", function ($inst) {
65+
66+
$sqLiteHandler = new SQLiteHandler(__DIR__ . "/database.sqlite");
67+
$sqLiteHandler->setPrefix("mp_");
68+
$connect = Connect::setHandler($sqLiteHandler, "lite");
69+
$connect->execute();
70+
71+
// Access sqLite connection
72+
$select = Connect::getInstance("lite")::select("id,name,content", "test")->whereStatus(1)->limit(3);
73+
$result = $select->fetch();
74+
$inst->add($result, [
75+
"isArray" => [],
76+
"rows" => function () use ($result) {
77+
return (count($result) === 3);
78+
}
79+
], "Fetch should equal to 3");
80+
});
81+
82+
/**
83+
* This will test multiple databases AND
84+
* validate sqLite database
85+
*/
86+
$unit->add("sqLite Query builder", function ($inst) {
87+
88+
$sqLiteHandler = new PostgreSQLHandler("127.0.0.1", "postgres", "", "maplephp");
89+
$sqLiteHandler->setPrefix("maple_");
90+
$connect = Connect::setHandler($sqLiteHandler, "psg");
91+
$connect->execute();
92+
93+
// Access sqLite connection
94+
$select = Connect::getInstance("psg")::select("id,name", ["test", "a"])->limit(2);
95+
$result = $select->fetch();
96+
$inst->add($result, [
97+
"isArray" => [],
98+
"rows" => function () use ($result) {
99+
return (count($result) === 2);
100+
}
101+
], "Fetch should equal to 2");
102+
});
103+
104+
$unit->execute();
89105
}
90-
91-
92-
93-
94-

0 commit comments

Comments
 (0)