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

Commit 298fa23

Browse files
authored
Merge pull request #19 from hafezdivandari/remove-doctrine-dbal
Remove Doctrine DBAL
2 parents eae5785 + e67ed94 commit 298fa23

12 files changed

+51
-48
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
],
1818
"require": {
1919
"php": "^8.1",
20-
"doctrine/dbal": "^3.6",
21-
"illuminate/contracts": "^9.0|^10.0",
20+
"illuminate/contracts": "^10.0",
21+
"illuminate/database": "^10.43",
2222
"openai-php/laravel": "^0.3.1",
2323
"spatie/laravel-package-tools": "^1.14.0",
2424
"spatie/once": "^3.1"

resources/views/prompts/query.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Only use the following tables and columns:
1010

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

1515
Question: "{!! $question !!}"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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.
22
Question: {{ $question }}
3-
Table Names: @foreach($tables as $table){{ $table->getName() }},@endforeach
3+
Table Names: @foreach($tables as $table){{ $table }},@endforeach
44

55
Relevant Table Names:

src/Oracle.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BeyondCode\Oracle\Exceptions\PotentiallyUnsafeQuery;
66
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Facades\Schema;
78
use Illuminate\Support\Str;
89
use OpenAI\Client;
910

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

105106
protected function getDialect(): string
106107
{
107-
$databasePlatform = DB::connection($this->connection)->getDoctrineConnection()->getDatabasePlatform();
108-
109-
return Str::before(class_basename($databasePlatform), 'Platform');
108+
$connection = DB::connection($this->connection);
109+
110+
return match (true) {
111+
$connection instanceof \Illuminate\Database\MySqlConnection && $connection->isMaria() => 'MariaDB',
112+
$connection instanceof \Illuminate\Database\MySqlConnection => 'MySQL',
113+
$connection instanceof \Illuminate\Database\PostgresConnection => 'PostgreSQL',
114+
$connection instanceof \Illuminate\Database\SQLiteConnection => 'SQLite',
115+
$connection instanceof \Illuminate\Database\SqlServerConnection => 'SQL Server',
116+
default => $connection->getDriverName(),
117+
};
110118
}
111119

112-
/**
113-
* @return \Doctrine\DBAL\Schema\Table[]
114-
*/
115120
protected function getTables(string $question): array
116121
{
117122
return once(function () use ($question) {
118-
$tables = DB::connection($this->connection)
119-
->getDoctrineSchemaManager()
120-
->listTables();
123+
$tables = Schema::connection($this->connection)->getTableListing();
121124

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

144147
return collect($tables)->filter(function ($table) use ($matchingTables) {
145-
return $matchingTables->contains(strtolower($table->getName()));
148+
return $matchingTables->contains(strtolower($table));
146149
})->toArray();
147150
}
148151
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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.
1+
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.
22
Use the following format:
33

44
Question: "Question here"
@@ -8,7 +8,7 @@ Answer: "Final answer here"
88

99
Only use the following tables and columns:
1010

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

1313
Question: "How many users do you have?"
1414
SQLQuery: "

tests/Fixtures/filtered-result-prompt.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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.
1+
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.
22
Use the following format:
33

44
Question: "Question here"
@@ -8,7 +8,7 @@ Answer: "Final answer here"
88

99
Only use the following tables and columns:
1010

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

1313
Question: "How many users do you have?"
1414
SQLQuery: "SELECT COUNT(*) FROM users;"

tests/Fixtures/query-prompt-l10.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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.
1+
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.
22
Use the following format:
33

44
Question: "Question here"
@@ -8,10 +8,10 @@ Answer: "Final answer here"
88

99
Only use the following tables and columns:
1010

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

1616
Question: "How many users do you have?"
1717
SQLQuery: "

tests/Fixtures/query-prompt.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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.
1+
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.
22
Use the following format:
33

44
Question: "Question here"
@@ -8,10 +8,10 @@ Answer: "Final answer here"
88

99
Only use the following tables and columns:
1010

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

1616
Question: "How many users do you have?"
1717
SQLQuery: "

tests/Fixtures/result-prompt-l10.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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.
1+
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.
22
Use the following format:
33

44
Question: "Question here"
@@ -8,10 +8,10 @@ Answer: "Final answer here"
88

99
Only use the following tables and columns:
1010

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

1616
Question: "How many users do you have?"
1717
SQLQuery: "SELECT COUNT(*) FROM users;"

tests/Fixtures/result-prompt.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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.
1+
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.
22
Use the following format:
33

44
Question: "Question here"
@@ -8,10 +8,10 @@ Answer: "Final answer here"
88

99
Only use the following tables and columns:
1010

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

1616
Question: "How many users do you have?"
1717
SQLQuery: "SELECT COUNT(*) FROM users;"

tests/Fixtures/unsafe-query-prompt-l10.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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.
1+
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.
22
Use the following format:
33

44
Question: "Question here"
@@ -8,10 +8,10 @@ Answer: "Final answer here"
88

99
Only use the following tables and columns:
1010

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

1616
Question: "How many users do you have?"
1717
SQLQuery: "DROP TABLE users;"

tests/Fixtures/unsafe-query-prompt.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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.
1+
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.
22
Use the following format:
33

44
Question: "Question here"
@@ -8,10 +8,10 @@ Answer: "Final answer here"
88

99
Only use the following tables and columns:
1010

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

1616
Question: "How many users do you have?"
1717
SQLQuery: "DROP TABLE users;"

0 commit comments

Comments
 (0)