Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Remove Doctrine DBAL #19

Merged
merged 1 commit into from
Feb 14, 2024
Merged
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
],
"require": {
"php": "^8.1",
"doctrine/dbal": "^3.6",
"illuminate/contracts": "^9.0|^10.0",
"illuminate/contracts": "^10.0",
"illuminate/database": "^10.43",
"openai-php/laravel": "^0.3.1",
"spatie/laravel-package-tools": "^1.14.0",
"spatie/once": "^3.1"
Expand Down
2 changes: 1 addition & 1 deletion resources/views/prompts/query.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Only use the following tables and columns:

@foreach($tables as $table)
"{{ $table->getName() }}" has columns: {{ collect($table->getColumns())->map(fn(\Doctrine\DBAL\Schema\Column $column) => $column->getName() . ' ('.$column->getType()->getName().')')->implode(', ') }}
"{{ $table }}" has columns: {{ collect(\Illuminate\Support\Facades\Schema::getColumns($table))->map(fn(array $column) => $column['name'] . ' ('.$column['type_name'].')')->implode(', ') }}
@endforeach

Question: "{!! $question !!}"
Expand Down
2 changes: 1 addition & 1 deletion resources/views/prompts/tables.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Given the below input question and list of potential tables, output a comma separated list of the table names that may be necessary to answer this question.
Question: {{ $question }}
Table Names: @foreach($tables as $table){{ $table->getName() }},@endforeach
Table Names: @foreach($tables as $table){{ $table }},@endforeach

Relevant Table Names:
23 changes: 13 additions & 10 deletions src/Oracle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BeyondCode\Oracle\Exceptions\PotentiallyUnsafeQuery;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use OpenAI\Client;

Expand Down Expand Up @@ -104,20 +105,22 @@ protected function ensureQueryIsSafe(string $query): void

protected function getDialect(): string
{
$databasePlatform = DB::connection($this->connection)->getDoctrineConnection()->getDatabasePlatform();

return Str::before(class_basename($databasePlatform), 'Platform');
$connection = DB::connection($this->connection);

return match (true) {
$connection instanceof \Illuminate\Database\MySqlConnection && $connection->isMaria() => 'MariaDB',
$connection instanceof \Illuminate\Database\MySqlConnection => 'MySQL',
$connection instanceof \Illuminate\Database\PostgresConnection => 'PostgreSQL',
$connection instanceof \Illuminate\Database\SQLiteConnection => 'SQLite',
$connection instanceof \Illuminate\Database\SqlServerConnection => 'SQL Server',
default => $connection->getDriverName(),
};
}

/**
* @return \Doctrine\DBAL\Schema\Table[]
*/
protected function getTables(string $question): array
{
return once(function () use ($question) {
$tables = DB::connection($this->connection)
->getDoctrineSchemaManager()
->listTables();
$tables = Schema::connection($this->connection)->getTableListing();

if (count($tables) < config('ask-database.max_tables_before_performing_lookup')) {
return $tables;
Expand All @@ -142,7 +145,7 @@ protected function filterMatchingTables(string $question, array $tables): array
->transform(fn (string $tableName) => strtolower(trim($tableName)));

return collect($tables)->filter(function ($table) use ($matchingTables) {
return $matchingTables->contains(strtolower($table->getName()));
return $matchingTables->contains(strtolower($table));
})->toArray();
}
}
4 changes: 2 additions & 2 deletions tests/Fixtures/filtered-query-prompt.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Given an input question, first create a syntactically correct Sqlite query to run, then look at the results of the query and return the answer.
Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
Use the following format:

Question: "Question here"
Expand All @@ -8,7 +8,7 @@ Answer: "Final answer here"

Only use the following tables and columns:

"users" has columns: id (integer), name (string), email (string), email_verified_at (datetime), password (string), remember_token (string), created_at (datetime), updated_at (datetime)
"users" has columns: id (integer), name (varchar), email (varchar), email_verified_at (datetime), password (varchar), remember_token (varchar), created_at (datetime), updated_at (datetime)

Question: "How many users do you have?"
SQLQuery: "
4 changes: 2 additions & 2 deletions tests/Fixtures/filtered-result-prompt.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Given an input question, first create a syntactically correct Sqlite query to run, then look at the results of the query and return the answer.
Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
Use the following format:

Question: "Question here"
Expand All @@ -8,7 +8,7 @@ Answer: "Final answer here"

Only use the following tables and columns:

"users" has columns: id (integer), name (string), email (string), email_verified_at (datetime), password (string), remember_token (string), created_at (datetime), updated_at (datetime)
"users" has columns: id (integer), name (varchar), email (varchar), email_verified_at (datetime), password (varchar), remember_token (varchar), created_at (datetime), updated_at (datetime)

Question: "How many users do you have?"
SQLQuery: "SELECT COUNT(*) FROM users;"
Expand Down
10 changes: 5 additions & 5 deletions tests/Fixtures/query-prompt-l10.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Given an input question, first create a syntactically correct Sqlite query to run, then look at the results of the query and return the answer.
Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
Use the following format:

Question: "Question here"
Expand All @@ -8,10 +8,10 @@ Answer: "Final answer here"

Only use the following tables and columns:

"failed_jobs" has columns: id (integer), uuid (string), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (string), batch (integer)
"password_reset_tokens" has columns: email (string), token (string), created_at (datetime)
"users" has columns: id (integer), name (string), email (string), email_verified_at (datetime), password (string), remember_token (string), created_at (datetime), updated_at (datetime)
"failed_jobs" has columns: id (integer), uuid (varchar), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (varchar), batch (integer)
"password_reset_tokens" has columns: email (varchar), token (varchar), created_at (datetime)
"users" has columns: id (integer), name (varchar), email (varchar), email_verified_at (datetime), password (varchar), remember_token (varchar), created_at (datetime), updated_at (datetime)

Question: "How many users do you have?"
SQLQuery: "
10 changes: 5 additions & 5 deletions tests/Fixtures/query-prompt.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Given an input question, first create a syntactically correct Sqlite query to run, then look at the results of the query and return the answer.
Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
Use the following format:

Question: "Question here"
Expand All @@ -8,10 +8,10 @@ Answer: "Final answer here"

Only use the following tables and columns:

"failed_jobs" has columns: id (integer), uuid (string), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (string), batch (integer)
"password_resets" has columns: email (string), token (string), created_at (datetime)
"users" has columns: id (integer), name (string), email (string), email_verified_at (datetime), password (string), remember_token (string), created_at (datetime), updated_at (datetime)
"failed_jobs" has columns: id (integer), uuid (varchar), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (varchar), batch (integer)
"password_resets" has columns: email (varchar), token (varchar), created_at (datetime)
"users" has columns: id (integer), name (varchar), email (varchar), email_verified_at (datetime), password (varchar), remember_token (varchar), created_at (datetime), updated_at (datetime)

Question: "How many users do you have?"
SQLQuery: "
10 changes: 5 additions & 5 deletions tests/Fixtures/result-prompt-l10.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Given an input question, first create a syntactically correct Sqlite query to run, then look at the results of the query and return the answer.
Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
Use the following format:

Question: "Question here"
Expand All @@ -8,10 +8,10 @@ Answer: "Final answer here"

Only use the following tables and columns:

"failed_jobs" has columns: id (integer), uuid (string), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (string), batch (integer)
"password_reset_tokens" has columns: email (string), token (string), created_at (datetime)
"users" has columns: id (integer), name (string), email (string), email_verified_at (datetime), password (string), remember_token (string), created_at (datetime), updated_at (datetime)
"failed_jobs" has columns: id (integer), uuid (varchar), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (varchar), batch (integer)
"password_reset_tokens" has columns: email (varchar), token (varchar), created_at (datetime)
"users" has columns: id (integer), name (varchar), email (varchar), email_verified_at (datetime), password (varchar), remember_token (varchar), created_at (datetime), updated_at (datetime)

Question: "How many users do you have?"
SQLQuery: "SELECT COUNT(*) FROM users;"
Expand Down
10 changes: 5 additions & 5 deletions tests/Fixtures/result-prompt.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Given an input question, first create a syntactically correct Sqlite query to run, then look at the results of the query and return the answer.
Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
Use the following format:

Question: "Question here"
Expand All @@ -8,10 +8,10 @@ Answer: "Final answer here"

Only use the following tables and columns:

"failed_jobs" has columns: id (integer), uuid (string), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (string), batch (integer)
"password_resets" has columns: email (string), token (string), created_at (datetime)
"users" has columns: id (integer), name (string), email (string), email_verified_at (datetime), password (string), remember_token (string), created_at (datetime), updated_at (datetime)
"failed_jobs" has columns: id (integer), uuid (varchar), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (varchar), batch (integer)
"password_resets" has columns: email (varchar), token (varchar), created_at (datetime)
"users" has columns: id (integer), name (varchar), email (varchar), email_verified_at (datetime), password (varchar), remember_token (varchar), created_at (datetime), updated_at (datetime)

Question: "How many users do you have?"
SQLQuery: "SELECT COUNT(*) FROM users;"
Expand Down
10 changes: 5 additions & 5 deletions tests/Fixtures/unsafe-query-prompt-l10.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Given an input question, first create a syntactically correct Sqlite query to run, then look at the results of the query and return the answer.
Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
Use the following format:

Question: "Question here"
Expand All @@ -8,10 +8,10 @@ Answer: "Final answer here"

Only use the following tables and columns:

"failed_jobs" has columns: id (integer), uuid (string), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (string), batch (integer)
"password_reset_tokens" has columns: email (string), token (string), created_at (datetime)
"users" has columns: id (integer), name (string), email (string), email_verified_at (datetime), password (string), remember_token (string), created_at (datetime), updated_at (datetime)
"failed_jobs" has columns: id (integer), uuid (varchar), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (varchar), batch (integer)
"password_reset_tokens" has columns: email (varchar), token (varchar), created_at (datetime)
"users" has columns: id (integer), name (varchar), email (varchar), email_verified_at (datetime), password (varchar), remember_token (varchar), created_at (datetime), updated_at (datetime)

Question: "How many users do you have?"
SQLQuery: "DROP TABLE users;"
Expand Down
10 changes: 5 additions & 5 deletions tests/Fixtures/unsafe-query-prompt.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Given an input question, first create a syntactically correct Sqlite query to run, then look at the results of the query and return the answer.
Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
Use the following format:

Question: "Question here"
Expand All @@ -8,10 +8,10 @@ Answer: "Final answer here"

Only use the following tables and columns:

"failed_jobs" has columns: id (integer), uuid (string), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (string), batch (integer)
"password_resets" has columns: email (string), token (string), created_at (datetime)
"users" has columns: id (integer), name (string), email (string), email_verified_at (datetime), password (string), remember_token (string), created_at (datetime), updated_at (datetime)
"failed_jobs" has columns: id (integer), uuid (varchar), connection (text), queue (text), payload (text), exception (text), failed_at (datetime)
"migrations" has columns: id (integer), migration (varchar), batch (integer)
"password_resets" has columns: email (varchar), token (varchar), created_at (datetime)
"users" has columns: id (integer), name (varchar), email (varchar), email_verified_at (datetime), password (varchar), remember_token (varchar), created_at (datetime), updated_at (datetime)

Question: "How many users do you have?"
SQLQuery: "DROP TABLE users;"
Expand Down