Last active
November 22, 2024 08:56
-
-
Save radityopw/3b3ef20c00826066e0cad2ae3b4932a5 to your computer and use it in GitHub Desktop.
duckdb php wrapper
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
#!/bin/bash | |
rm temp.duckdb | |
duckdb temp.duckdb <<-EOL | |
INSTALL postgres; | |
LOAD postgres; | |
ATTACH 'dbname=Adventureworks host=127.0.0.1 user=postgres password=postgres' AS db (TYPE POSTGRES, READ_ONLY); | |
SELECT count(*) as jml FROM db.sales.customer; | |
CREATE TABLE customer as (SELECT * FROM db.sales.customer); | |
SELECT count(*) as jml FROM customer; | |
EOL |
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 | |
require 'duckdb.php'; | |
/* | |
buat dulu file test.duckdb di folder ~/php | |
yang isinya tabel coba1(id int) | |
*/ | |
$db_file = '~/php/test.duckdb'; | |
var_dump(duckdb_query($db_file,"SELECT count(*) as jml FROM coba1;")); | |
var_dump(duckdb_query($db_file,"INSERT INTO coba1(id) VALUES(1);")); | |
var_dump(duckdb_query($db_file,"INSERT INTO coba2(id) VALUES(1);")); | |
var_dump(duckdb_error()); |
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 | |
$_DUCKDB_ERROR = false; | |
function duckdb_error(){ | |
global $_DUCKDB_ERROR; | |
return $_DUCKDB_ERROR; | |
} | |
function duckdb_query($file,$sql){ | |
global $_DUCKDB_ERROR; | |
$output = null; | |
$retval = null; | |
$_DUCKDB_ERROR = false; | |
$cmd = 'duckdb -json '.$file.' "'.$sql.'" 2>&1'; | |
exec($cmd, $output, $retval); | |
if($retval == 0){ | |
if($output) return json_decode($output[0],TRUE); | |
return TRUE; | |
}else{ | |
$_DUCKDB_ERROR = print_r($output,TRUE); | |
} | |
return FALSE; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment