|
| 1 | +#include "duckdb.hpp" |
| 2 | + |
| 3 | +#include "duckdb/parser/parsed_data/create_table_function_info.hpp" |
| 4 | +#include "sqlite_scanner.hpp" |
| 5 | +#include "duckdb/main/database_manager.hpp" |
| 6 | +#include "duckdb/main/attached_database.hpp" |
| 7 | +#include "storage/sqlite_catalog.hpp" |
| 8 | +#include "storage/sqlite_table_entry.hpp" |
| 9 | +#include "storage/sqlite_transaction.hpp" |
| 10 | +#include "sqlite_db.hpp" |
| 11 | +#include "sqlite_stmt.hpp" |
| 12 | +#include "sqlite_utils.hpp" |
| 13 | + |
| 14 | +namespace duckdb { |
| 15 | + |
| 16 | +static unique_ptr<FunctionData> SQLiteQueryBind(ClientContext &context, TableFunctionBindInput &input, |
| 17 | + vector<LogicalType> &return_types, vector<string> &names) { |
| 18 | + auto result = make_uniq<SqliteBindData>(); |
| 19 | + |
| 20 | + if (input.inputs[0].IsNull() || input.inputs[1].IsNull()) { |
| 21 | + throw BinderException("Parameters to sqlite_query cannot be NULL"); |
| 22 | + } |
| 23 | + |
| 24 | + // look up the database to query |
| 25 | + auto db_name = input.inputs[0].GetValue<string>(); |
| 26 | + auto &db_manager = DatabaseManager::Get(context); |
| 27 | + auto db = db_manager.GetDatabase(context, db_name); |
| 28 | + if (!db) { |
| 29 | + throw BinderException("Failed to find attached database \"%s\" referenced in sqlite_query", db_name); |
| 30 | + } |
| 31 | + auto &catalog = db->GetCatalog(); |
| 32 | + if (catalog.GetCatalogType() != "sqlite") { |
| 33 | + throw BinderException("Attached database \"%s\" does not refer to a SQLite database", db_name); |
| 34 | + } |
| 35 | + auto &sqlite_catalog = catalog.Cast<SQLiteCatalog>(); |
| 36 | + auto &transaction = SQLiteTransaction::Get(context, catalog); |
| 37 | + auto sql = input.inputs[1].GetValue<string>(); |
| 38 | + // strip any trailing semicolons |
| 39 | + StringUtil::RTrim(sql); |
| 40 | + while (!sql.empty() && sql.back() == ';') { |
| 41 | + sql = sql.substr(0, sql.size() - 1); |
| 42 | + StringUtil::RTrim(sql); |
| 43 | + } |
| 44 | + |
| 45 | + auto &con = transaction.GetDB(); |
| 46 | + auto stmt = con.Prepare(sql); |
| 47 | + if (!stmt.stmt) { |
| 48 | + throw BinderException("Failed to prepare query \"%s\"", sql); |
| 49 | + } |
| 50 | + for(idx_t c = 0; c < stmt.GetColumnCount(); c++) { |
| 51 | + return_types.emplace_back(LogicalType::VARCHAR); |
| 52 | + names.emplace_back(stmt.GetName(c)); |
| 53 | + } |
| 54 | + stmt.Close(); |
| 55 | + if (names.empty()) { |
| 56 | + throw BinderException("Failed to execute query \"%s\" - query must return data", sql); |
| 57 | + } |
| 58 | + result->rows_per_group = optional_idx(); |
| 59 | + result->sql = std::move(sql); |
| 60 | + result->all_varchar = true; |
| 61 | + result->file_name = sqlite_catalog.GetDBPath(); |
| 62 | + result->global_db = &con; |
| 63 | + return std::move(result); |
| 64 | +} |
| 65 | + |
| 66 | +SQLiteQueryFunction::SQLiteQueryFunction() |
| 67 | + : TableFunction("sqlite_query", {LogicalType::VARCHAR, LogicalType::VARCHAR}, nullptr, SQLiteQueryBind) { |
| 68 | + SqliteScanFunction scan_function; |
| 69 | + init_global = scan_function.init_global; |
| 70 | + init_local = scan_function.init_local; |
| 71 | + function = scan_function.function; |
| 72 | + global_initialization = TableFunctionInitialization::INITIALIZE_ON_SCHEDULE; |
| 73 | +} |
| 74 | +} // namespace duckdb |
0 commit comments