fixing database connections

This commit is contained in:
2024-09-07 13:22:58 +03:00
parent cf3d621587
commit 0cd4e08716
16 changed files with 119 additions and 79 deletions
@@ -3,6 +3,7 @@
namespace Lucent\Query\DatabaseGraph;
use Illuminate\Support\Facades\DB;
use Lucent\Database\Database;
use Lucent\Query\QueryOptions;
class PgsqlDatabaseGraph implements DatabaseGraph
@@ -13,7 +14,7 @@ class PgsqlDatabaseGraph implements DatabaseGraph
*/
public function getChildren(array $ids, QueryOptions $options): array
{
$subquery = DB::table('edges AS g')
$subquery = Database::make()->table('edges AS g')
->select(DB::raw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field, 1 as depth '))
->whereIn('source', $ids);
@@ -23,14 +24,14 @@ class PgsqlDatabaseGraph implements DatabaseGraph
$subquery->limit($options->childrenLimit)
->unionAll(
DB::table(DB::raw("edges AS g, search_graph AS sg "))
Database::make()->table(DB::raw("edges AS g, search_graph AS sg "))
->selectRaw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field,sg.depth + 1 as depth')
->whereRaw("g.source = sg.target")
->where("depth", "<", $options->childrenDepth)
->orderBy("rank")
);
return DB::table('search_graph')
return Database::make()->table('search_graph')
// ->select(DB::raw("*, 1 as depth "))
->withRecursiveExpression('search_graph', $subquery)
->get()->toArray();
@@ -41,19 +42,19 @@ class PgsqlDatabaseGraph implements DatabaseGraph
*/
public function getParents(array $ids, QueryOptions $options): array
{
$subquery = DB::table('edges AS g')
$subquery = Database::make()->table('edges AS g')
->select(DB::raw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field, 1 as depth '))
->limit($options->parentsLimit)
->whereIn('g.target', $ids)
->unionAll(
DB::table(DB::raw("edges AS g, search_graph AS sg "))
Database::make()->table(DB::raw("edges AS g, search_graph AS sg "))
->selectRaw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field,sg.depth + 1 as depth')
->whereRaw("g.target = sg.source")
->where("depth", "<", $options->parentsDepth)
->orderBy("rank")
);
return DB::table('search_graph')
return Database::make()->table('search_graph')
// ->select(DB::raw('sg.source,sg.target,sg.rank,sg."sourceSchema",sg."targetSchema",sg.field,sg.depth'))
->withRecursiveExpression('search_graph', $subquery)
->get()->toArray();
@@ -3,6 +3,7 @@
namespace Lucent\Query\DatabaseGraph;
use Illuminate\Support\Facades\DB;
use Lucent\Database\Database;
use Lucent\Query\QueryOptions;
class SqliteDatabaseGraph implements DatabaseGraph
@@ -12,7 +13,7 @@ class SqliteDatabaseGraph implements DatabaseGraph
*/
public function getChildren(array $ids, QueryOptions $options): array
{
$subquery = DB::table('edges AS g')
$subquery = Database::make()->table('edges AS g')
->select(DB::raw('g.source,g.target,g.rank,g.sourceSchema,g.targetSchema,g.field, 1 as depth '))
->whereIn('source', $ids);
@@ -22,14 +23,14 @@ class SqliteDatabaseGraph implements DatabaseGraph
$subquery->limit($options->childrenLimit)
->unionAll(
DB::table(DB::raw("edges AS g, search_graph AS sg "))
Database::make()->table(DB::raw("edges AS g, search_graph AS sg "))
->selectRaw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field,sg.depth + 1 as depth')
->whereRaw("g.source = sg.target")
->where("depth", "<", $options->childrenDepth)
->orderBy("rank")
);
return DB::table('search_graph')
return Database::make()->table('search_graph')
// ->select(DB::raw("*, 1 as depth "))
->withRecursiveExpression('search_graph', $subquery)
->get()->toArray();
@@ -40,19 +41,19 @@ class SqliteDatabaseGraph implements DatabaseGraph
*/
public function getParents(array $ids, QueryOptions $options): array
{
$subquery = DB::table('edges AS g')
$subquery = Database::make()->table('edges AS g')
->select(DB::raw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field, 1 as depth '))
->limit($options->parentsLimit)
->whereIn('g.target', $ids)
->unionAll(
DB::table(DB::raw("edges AS g, search_graph AS sg "))
Database::make()->table(DB::raw("edges AS g, search_graph AS sg "))
->selectRaw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field,sg.depth + 1 as depth')
->whereRaw("g.target = sg.source")
->where("depth", "<", $options->parentsDepth)
->orderBy("rank")
);
return DB::table('search_graph')
return Database::make()->table('search_graph')
// ->select(DB::raw('sg.source,sg.target,sg.rank,sg."sourceSchema",sg."targetSchema",sg.field,sg.depth'))
->withRecursiveExpression('search_graph', $subquery)
->get()->toArray();
+2 -2
View File
@@ -4,7 +4,7 @@ namespace Lucent\Query;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use Lucent\Database\Database;
use Lucent\Query\BuilderConverter\BuilderConverter;
use Lucent\Query\Filter\AndFilter;
use Lucent\Query\Filter\Argument;
@@ -58,7 +58,7 @@ final class FilterParser
}
$targetIds = collect($graph->records)->pluck("id");
$sourceIds = DB::table("edges")->whereIn("target", $targetIds)->where("field", $k)->get()->pluck("source");
$sourceIds = Database::make()->table("edges")->whereIn("target", $targetIds)->where("field", $k)->get()->pluck("source");
return array_merge($c, $sourceIds->toArray());
}, []);
+4 -4
View File
@@ -3,7 +3,7 @@
namespace Lucent\Query;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use Lucent\Database\Database;
use Lucent\Edge\Edge;
use Lucent\Primitive\Collection;
use Lucent\Query\DatabaseGraph\DatabaseGraph;
@@ -67,7 +67,7 @@ final class Query
$edgesIds = collect($resultParentSourceTargetIds)->merge($resultChildrenEdgesTargetIds)->unique()->values()->toArray();
$edgeRecords = [];
if (!empty($edgesIds)) {
$edgeRecords = DB::table('records')
$edgeRecords = Database::make()->table('records')
->whereIn("id", $edgesIds)
->whereIn("status", $this->options->status)
->get()->toArray();
@@ -142,7 +142,7 @@ final class Query
private function mainQuery(): array
{
$query = DB::table("records");
$query = Database::make()->table("records");
$query = $this->parseFilters($query);
$query = $this->findNotLinked($query);
@@ -189,7 +189,7 @@ final class Query
function runWithCount(): Graph
{
$query = DB::table("records");
$query = Database::make()->table("records");
$query = $this->parseFilters($query);
$query = $this->findNotLinked($query);
$graph = $this->run();