peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.33k stars 202 forks source link

PDO's constructor's string doesn't accept dbname as parameter with PDO for postgres #1039

Closed gabrielesilinic closed 2 years ago

gabrielesilinic commented 2 years ago

so basically this function works okay in php but with peachpie doesn't since it throws the following exception {"Keyword not supported: dbname (Parameter 'keyword')"} {$1} , there is any workaround in order to use PDO with psql anyway while waiting for a fix? (i really don't know where to look into peachpie code to fix it myself, but mainly i don't know anything about nuget, sorry about that, i thought someone could fix it much faster than me anyway)

example of how the connect string could look like when put together (since i know my way of putting it together looks messy)

'pgsql:host=localhost;port=5432;dbname=testdb;user=postgres;password=wonderfulpassword'

Code:

function dbconnect() : PDO {
    $db_settings=parse_ini_file("includes/database.ini");//just extracts info from configuration file
    if($db_settings==false){
        echo '<br><h1>Couldn\' connect to database</h1><br>';
        $cdir = getcwd();
        echo $cdir();
        exit;
    }//from here creates the string
    $connstr='pgsql:host='.$db_settings['host'].';database='.$db_settings['database'].
    ';port='.$db_settings['port'];
    $connstr=sprintf("pgsql:host=%s;port=%s;dbname=%s;user=%s;password=%s",
    $db_settings['host'],$db_settings['port'],$db_settings['database'],
    $db_settings["username"],$db_settings["password"]);
    try {//here tries to connect, it does so okay in php but fails in peachpie
        $pdo=new PDO($connstr);
        return $pdo;
    } catch (PDOException $e) {
        $cdir=getcwd();//error report section
        echo $cdir();
        echo "<h1>Can't connect";
        print_r($e);
        echo " '</h1><br>";
        echo "<h2>$connstr</h2>";
        throw new Exception("database can't connect");
    }
}

edits: peachpie pdo pgsql version 1.0.14

jakubmisek commented 2 years ago

Thank you for reporting the issue.

It seems the .NET driver (https://github.com/npgsql/npgsql) does not recognize dbname:.

To work around this issue, use db: or Database: instead.

This will have to be fixed by preprocessing the connection string at https://github.com/peachpiecompiler/peachpie/blob/6205a6fc06f98d571bd4dc4b060be852a84da598/src/PDO/Peachpie.Library.PDO.PgSQL/PDOPgSQLDriver.cs#L27