filipefernandes/laravel-duckdb 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

filipefernandes/laravel-duckdb

Composer 安装命令:

composer require filipefernandes/laravel-duckdb

包简介

A Laravel database driver for DuckDB.

README 文档

README

Thumbnail

Laravel DuckDB Driver

A production-ready Laravel database driver for DuckDB, powered by satur/duckdb-auto. This package allows you to query DuckDB databases using Laravel's powerful Query Builder for analytical (OLAP) workloads.

Requirements

  • PHP 8.2+
  • Laravel 10.0+ or 11.0+
  • FFI Extension Enabled (ffi.enable=true or preload in php.ini)

Warning

Production Notice: Enabling FFI globally (ffi.enable=true) can have security implications. It is recommended to use ffi.enable=preload in production environments and preload the necessary scripts if possible, or strictly control access to the server.

Installation

  1. Install via Composer:
composer require filipefernandes/laravel-duckdb
  1. Configuration

Add the connection configuration to your config/database.php file:

'connections' => [
    
    'duckdb' => [
        'driver' => 'duckdb',
        'database' => env('DUCKDB_DB_FILE', storage_path('duckdb/analytics.db')),
        'prefix' => '',
        'read_only' => false,
    ],

],

Auto-Installer

This package automatically downloads and installs the DuckDB native binary and FFI header when your application boots — no manual setup required.

On first boot it will:

  1. Create storage/duckdb/ (if it doesn't exist)
  2. Download the correct native library for your OS from the DuckDB GitHub Releases
  3. Download the duckdb.h FFI header
  4. Set the required environment variables (DUCKDB_LIB_PATH, DUCKDB_HEADER_PATH) at runtime

Subsequent boots detect the files are already present and skip the download (idempotent).

Supported platforms

OS Architecture Library
Windows x86_64 duckdb.dll
Linux x86_64 libduckdb.so
Linux aarch64 libduckdb.so
macOS Universal libduckdb.dylib

Configuration

Publish the config file to override defaults:

php artisan vendor:publish --tag=duckdb-config

Available .env variables:

DUCKDB_AUTO_INSTALL=true          # Set to false to disable auto-installer
DUCKDB_INSTALL_PATH=/custom/path  # Override install directory (default: storage/duckdb)
DUCKDB_VERSION=1.2.1              # Pin a specific DuckDB release version

Note

ext-zip must be enabled in your php.ini (it is bundled with PHP by default on most platforms). If the installer fails (e.g. no internet access), it logs the error and does not crash the application — allowing you to manage binaries manually.

Usage

You can use the DB facade to interact with DuckDB.

Querying Files

You can query files directly (Parquet, CSV, JSON) without pre-configuring tables using the file() helper:

use Illuminate\Support\Facades\DB;

// Query a Parquet file
$users = DB::connection('duckdb')
    ->file('storage/app/data.parquet')
    ->where('age', '>', 25)
    ->get();

// Or get all records from a file
$allUsers = DB::connection('duckdb')->all('storage/app/data.parquet');

// Print formatted output (useful for debugging)
DB::connection('duckdb')->print("SELECT * FROM 'storage/app/data.parquet' LIMIT 10");

// Or chain print() on a query builder
DB::connection('duckdb')->file('storage/app/data.parquet')->print();

// Insert a single row into a file
DB::connection('duckdb')->create('storage/app/data.parquet', [
    'id' => 1,
    'name' => 'John Doe',
    'email' => 'john@example.com'
]);

// Insert multiple rows into a file
DB::connection('duckdb')->insertInto('storage/app/data.parquet', [
    ['id' => 1, 'name' => 'John', 'email' => 'john@example.com'],
    ['id' => 2, 'name' => 'Jane', 'email' => 'jane@example.com'],
]);

Basic Queries

use Illuminate\Support\Facades\DB;

// Select all users
$users = DB::connection('duckdb')->table('users')->get();

// Aggegrations
$count = DB::connection('duckdb')->table('events')->count();

Analytical Queries (OLAP)

DuckDB shines at analytics. You can query Parquet, CSV, or JSON files directly.

// Querying a Parquet file directly
$results = DB::connection('duckdb')
    ->select("SELECT * FROM 'path/to/data.parquet' LIMIT 10");


### Examples

```php
use Illuminate\Support\Facades\DB;

// 1. Get all users
$users = DB::connection('duckdb')->table('users')->get();

// 2. Aggregate count
$count = DB::connection('duckdb')->table('users')->count();

// 3. Conditional Fetch with Bindings
$user = DB::connection('duckdb')
    ->table('users')
    ->where('id', 1)
    ->first();

// 4. Raw SQL Select
$results = DB::connection('duckdb')->select("SELECT * FROM users WHERE email LIKE ?", ['%example.com']);

// 5. Analytical Query (DuckDB syntax)
// e.g. Extract hour from timestamp
$hourly = DB::connection('duckdb')
    ->select("SELECT date_part('hour', created_at) as hour, count(*) as count FROM users GROUP BY 1");

Limitations

  • No Eloquent Support: This driver is designed for read-heavy analytical workloads. Eloquent ORM features (Models, Relationships, saving models) are not supported and usually incompatible with DuckDB's constraints and lack of certain OLTP features. Use the Query Builder.
  • Transactions: DuckDB handles transactions differently. Nested transactions or complex locking expected by standard Laravel apps might not work as intended.
  • FFI Binding Interpolation: Due to the nature of the FFI integration, prepared statements with placeholders are emulated by valid string interpolation within the driver. Use caution with raw queries.

Security

Ensure your php.ini has FFI enabled:

[ffi]
ffi.enable=true

Testing

To run the test suite, make sure you have installed the dependencies:

composer install

Then run PHPUnit:

vendor/bin/phpunit

filipefernandes/laravel-duckdb 适用场景与选型建议

filipefernandes/laravel-duckdb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 12 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「driver」 「laravel」 「olap」 「ffi」 「duckdb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 filipefernandes/laravel-duckdb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 filipefernandes/laravel-duckdb 我们能提供哪些服务?
定制开发 / 二次开发

基于 filipefernandes/laravel-duckdb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-26