Repository: duckdb/duckdb
File: extension/autocomplete/autocomplete_extension.cpp (C++)

Code before:
			if (!result.empty()) {
				auto &last_statement = result.back();
				last_statement->stmt_length = query.size() - last_statement->stmt_location;
				for (auto &statement : result) {
					statement->query = query.substr(statement->stmt_location, statement->stmt_length);
					statement->stmt_location = 0;
					statement->stmt_length = statement->query.size();
					if (statement->type == StatementType::CREATE_STATEMENT) {
						auto &create = statement->Cast<CreateStatement>();
						create.info->sql = statement->query;
					}
				}
			}
			return ParserOverrideResult(std::move(result));
		} catch (std::exception &e) {
			return ParserOverrideResult(e);
		}
	}
};

static void EnablePEGParserFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
	auto new_conn = make_shared_ptr<Connection>(*context.db);
	auto result = new_conn->Query("SET allow_parser_override_extension=strict");
	if (result->HasError()) {
		result->ThrowError();
	}
}

static void DisablePEGParserFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
	auto new_conn = make_shared_ptr<Connection>(*context.db);
	auto result = new_conn->Query("SET allow_parser_override_extension=default");
	if (result->HasError()) {
		result->ThrowError();
	}
}

static duckdb::unique_ptr<FunctionData> EnablePEGParserBind(ClientContext &context, TableFunctionBindInput &input,
                                                            vector<LogicalType> &return_types, vector<string> &names) {
	names.emplace_back("success");
	return_types.emplace_back(LogicalType::BOOLEAN);

	return nullptr;
}

static void LoadInternal(ExtensionLoader &loader) {
	TableFunction auto_complete_fun("sql_auto_complete", {LogicalType::VARCHAR}, SQLAutoCompleteFunction,
	                                SQLAutoCompleteBind, SQLAutoCompleteInit);
	auto_complete_fun.named_parameters["max_suggestion_count"] = LogicalType::UBIGINT;
	auto_complete_fun.named_parameters["max_file_suggestion_count"] = LogicalType::UBIGINT;
	auto_complete_fun.named_parameters["max_exact_suggestion_count"] = LogicalType::UBIGINT;
	loader.RegisterFunction(auto_complete_fun);

Review comment (suggestion):
I see what you are doing, but I think there has to be a `void DBConfig::SetOptionByName(const string &name, const Value &value)` function for this? to be called like` config.SetOptionByName('allow_parser_override_extension', Value('strict'));`

Code after:
			if (!result.empty()) {
				auto &last_statement = result.back();
				last_statement->stmt_length = query.size() - last_statement->stmt_location;
				for (auto &statement : result) {
					statement->query = query.substr(statement->stmt_location, statement->stmt_length);
					statement->stmt_location = 0;
					statement->stmt_length = statement->query.size();
					if (statement->type == StatementType::CREATE_STATEMENT) {
						auto &create = statement->Cast<CreateStatement>();
						create.info->sql = statement->query;
					}
				}
			}
			return ParserOverrideResult(std::move(result));
		} catch (std::exception &e) {
			return ParserOverrideResult(e);
		}
	}
};

static void EnablePEGParserFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
	auto &db_config = DBConfig::GetConfig(context);
	db_config.SetOptionByName("allow_parser_override_extension", Value("strict"));
}

static void DisablePEGParserFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
	auto &db_config = DBConfig::GetConfig(context);
	db_config.SetOptionByName("allow_parser_override_extension", Value("default"));
}

static duckdb::unique_ptr<FunctionData> EnablePEGParserBind(ClientContext &context, TableFunctionBindInput &input,
                                                            vector<LogicalType> &return_types, vector<string> &names) {
	names.emplace_back("success");
	return_types.emplace_back(LogicalType::BOOLEAN);

	return nullptr;
}

static void LoadInternal(ExtensionLoader &loader) {
	TableFunction auto_complete_fun("sql_auto_complete", {LogicalType::VARCHAR}, SQLAutoCompleteFunction,
	                                SQLAutoCompleteBind, SQLAutoCompleteInit);
	auto_complete_fun.named_parameters["max_suggestion_count"] = LogicalType::UBIGINT;
	auto_complete_fun.named_parameters["max_file_suggestion_count"] = LogicalType::UBIGINT;
	auto_complete_fun.named_parameters["max_exact_suggestion_count"] = LogicalType::UBIGINT;
	loader.RegisterFunction(auto_complete_fun);

	TableFunction check_peg_parser_fun("check_peg_parser", {LogicalType::VARCHAR}, CheckPEGParserFunction,
	                                   CheckPEGParserBind, nullptr);
	loader.RegisterFunction(check_peg_parser_fun);

	TableFunction enable_peg_parser("enable_peg_parser", {}, EnablePEGParserFunction, EnablePEGParserBind, nullptr);