j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

PDO DBLIB for MS SQL having problems with " (double quote) quote character #286

Closed nbpalomino closed 8 years ago

nbpalomino commented 8 years ago

When working with pdo_dblib for connect to MS SQL Server 2008,

Idiorm uses " (double quote) as quote character but this produces query errors on Red Hat Server with PHP5.2 and above mentioned SQL Server.

I fixed using none quote character. As follows:

    protected static function _detect_identifier_quote_character($connection_name) {
        switch(self::get_db($connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME)) {
            case 'pgsql':
            case 'sqlsrv':
            //case 'dblib':
            case 'mssql':
            case 'sybase':
            case 'firebird':
                return '"';
            case 'dblib':
                return '';
            case 'mysql':
            case 'sqlite':
            case 'sqlite2':
            default:
                return '`';
        }
    }

Also I fixed a PDO::lastInsertId() for PDO_dblib as follows:

if($driver == 'dblib') {
    // MS SQL SERVER's driver "pdo_dblib" doesn't have PDO::lastInsertId();
    $stmt = $db->prepare("SELECT SCOPE_IDENTITY() AS id");
     if($stmt->execute()) {
         $row = $stmt->fetch();
         $this->_data[$column] = $row['id'];
      }
  } else {
      $this->_data[$column] = $db->lastInsertId();
  }

Im preparing a Pull Request for this fixes if is valid off course.

Thanks.

bradrozier commented 8 years ago

Concerning your pdo_dblib quoting issues, I recall having similar issues with SQL server when first using idiorm a while ago. I may have tried the solution you've proposed, but I think that created some other issues. What we ended up doing was to add this to our connection block when connecting to SQL server:

ORM::raw_execute('SET QUOTED_IDENTIFIER ON');

Hope it helps.

treffynnon commented 8 years ago

Quoting will not be turned off in Idiorm. You need to enable it on your server or connect request. On 17 Dec 2015 12:36, "bradrozier" notifications@github.com wrote:

Concerning your pdo_dblib quoting issues, I recall having similar issues with SQL server when first using idiorm a while ago. I may have tried the solution you've proposed, but I think that created some other issues. What we ended up doing was to add this to our connection block when connection to SQL server:

ORM::raw_execute('SET QUOTED_IDENTIFIER ON');

Hope it helps.

— Reply to this email directly or view it on GitHub https://github.com/j4mie/idiorm/issues/286#issuecomment-165318372.

nbpalomino commented 8 years ago

Ok about Quote Character I will use _ORM::raw_execute('SET QUOTEDIDENTIFIER ON'); .

But, what about PDO::lastInsertId(); the dblib driver have a built-in solution?