Created
April 11, 2025 09:29
-
-
Save carestad/8a53cbdeea56c79876dd390fe89c480a to your computer and use it in GitHub Desktop.
Laravel Snowflake connector. Requires https://github.com/snowflakedb/pdo_snowflake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Connectors; | |
use Illuminate\Database\Connectors\ConnectorInterface; | |
use Illuminate\Support\Arr; | |
use PDO; | |
class SnowflakeConnector implements ConnectorInterface | |
{ | |
public function connect(array $config) | |
{ | |
$dsn = 'snowflake:'; | |
$supportedDsnParams = [ | |
'account', | |
'warehouse', | |
'schema', | |
'database', | |
'authenticator', | |
'priv_key_file', | |
'priv_key_file_pwd', | |
'host', | |
'disableocspchecks', | |
'ocspfailopen', | |
'proxy', | |
'no_proxy', | |
'logintimeout', | |
'retrytimeout', | |
'maxhttpretries', | |
]; | |
$params = array_filter(Arr::only($config, $supportedDsnParams)); | |
if ($params['priv_key_file'] && !isset($params['authenticator'])) { | |
$params['authenticator'] = 'SNOWFLAKE_JWT'; | |
} | |
$dsn .= collect($params)->map(fn($value, $key) => "{$key}={$value}")->join(';'); | |
return new PDO($dsn, $config['username'], $config['password'] ?? ''); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Connectors\SnowflakeConnector; | |
use Illuminate\Database\PostgresConnection; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\ServiceProvider; | |
class SnowflakeProvider extends ServiceProvider | |
{ | |
/** | |
* Register services. | |
*/ | |
public function register(): void | |
{ | |
DB::extend('snowflake', function($config, $name) { | |
$connector = new SnowflakeConnector; | |
$pdo = $connector->connect($config); | |
return new PostgresConnection($pdo, $config['database'] ?? '', '', $config); | |
}); | |
} | |
/** | |
* Bootstrap services. | |
*/ | |
public function boot(): void | |
{ | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment