4

Zend Framework 1.12 を使用して MSSQL 2008 サーバーにアクセスしています。データベース ドライバーとして FreeTDS を使用しています。

Zend_Db を使用して次のクエリを生成しています。

 $obj_sel = $obj_db
          -> select ()
          -> from (array ('ps' => 'ProductStock'), array ('PLU', 'stock' => 'SUM(ps.stock)'))
          -> join (array ('pc' => 'ProductCatalogue'), 'ps.PLU = pc.PLU', NULL)
          -> join (array ('gl' => 'Gemini_Location'), 'ps.Location = gl.LocationID', array ('LocationID'))
          -> where ('ps.status = 1')
          -> where ('ps.PLU IS NOT NULL');
           > where ('pc.rootPLU >= ?', $this -> int_start_rootplu);
          -> group ('ps.PLU')
          -> group ('gl.LocationID')
          -> order (array ('ps.PLU', 'gl.LocationID'));

これを実行して実行すると、正常に動作し、正しいと思われるクエリが返されます。

SELECT "ps"."PLU", SUM(ps.stock) AS "stock", "gl"."LocationID" FROM "ProductStock" AS "ps"
 INNER JOIN "ProductCatalogue" AS "pc" ON ps.PLU = pc.PLU
 INNER JOIN "Gemini_Location" AS "gl" ON ps.Location = gl.LocationID WHERE (ps.status = 1) AND (ps.PLU IS NOT NULL) AND (pc.rootPLU >= 93838) GROUP BY "ps"."PLU",
    "gl"."LocationID" ORDER BY "ps"."PLU" ASC, "gl"."LocationID" ASC

しかし、次のように制限またはオフセットをクエリに追加しようとすると:

 $obj_sel = $obj_db
          -> select ()
          -> from (array ('ps' => 'ProductStock'), array ('PLU', 'stock' => 'SUM(ps.stock)'))
          -> join (array ('pc' => 'ProductCatalogue'), 'ps.PLU = pc.PLU', NULL)
          -> join (array ('gl' => 'Gemini_Location'), 'ps.Location = gl.LocationID', array ('LocationID'))
          -> where ('ps.status = 1')
          -> where ('ps.PLU IS NOT NULL');
           > where ('pc.rootPLU >= ?', $this -> int_start_rootplu);
          -> group ('ps.PLU')
          -> group ('gl.LocationID')
          -> order (array ('ps.PLU', 'gl.LocationID'))
          -> limit (1000,2000);

SQL サーバーが実行を拒否する次のクエリを取得します。

SELECT * FROM (SELECT TOP 1000 * FROM (SELECT TOP 3000 "ps"."PLU", SUM(ps.stock) AS "stock", "gl"."LocationID" FROM "ProductStock" AS "ps"
 INNER JOIN "ProductCatalogue" AS "pc" ON ps.PLU = pc.PLU
 INNER JOIN "Gemini_Location" AS "gl" ON ps.Location = gl.LocationID WHERE (ps.status = 1) AND (ps.PLU IS NOT NULL) AND (pc.rootPLU >= 93838) GROUP BY "ps"."PLU",
    "gl"."LocationID" ORDER BY "ps"."PLU" ASC, "gl"."LocationID" ASC) AS inner_tbl ORDER BY "ps"."PLU" , "gl"."LocationID" DESC) AS outer_tbl ORDER BY "ps"."PLU" , "gl"."LocationID" asc

次のエラーが表示されます。

SQLSTATE[HY000]: 一般的なエラー: 4104 一般的な SQL Server エラー: SQL Server からのメッセージを確認してください [4104] (重大度 16) [(null)]

私は MySQL や Postgres ほど MSSQL に精通していないので、TOP 手法が正しいと仮定する必要があります。ただし、ここで SQL Zend DB が生成する内容が正しくないことは明らかです。

これは Zend DB の既知の問題ですか? もしそうなら、どうすれば回避できますか?

4

1 に答える 1

1

ZF 2 では正しく実装されていますが、ZF 1 では SQL Server に正しく実装されていません。

サブクラス化することで問題を解決しましたZend_Db_Adapter_Pdo_Mssql

class My_Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Mssql
{
    /**
     * @see Zend_Db_Adapter_Pdo_Mssql::limit()
     */
    public function limit($sql, $count, $offset = 0)
    {
        $count = intval($count);
        if ($count <= 0) {
            /** @see Zend_Db_Adapter_Exception */
            require_once 'Zend/Db/Adapter/Exception.php';
            throw new Zend_Db_Adapter_Exception('count parameter invalid: ' . $count);
        }

        $offset = intval($offset);
        if ($offset < 0) {
            /** @see Zend_Db_Adapter_Exception */
            require_once 'Zend/Db/Adapter/Exception.php';
            throw new Zend_Db_Adapter_Exception('offset parameter invalid: ' . $count);
        }

        if (0 == $offset) {
            $sql = preg_replace('/^SELECT\s+(DISTINCT\s)?/i', 'SELECT $1TOP ' . ($count+$offset) . ' ', $sql);
            return $sql;
        }

        $selectStart = stripos($sql, 'SELECT');
        $fromStart = stripos($sql, 'FROM');

        $orderby = stristr($sql, 'ORDER BY');

        if ($orderby === false) {
            $orderby = 'ORDER BY (SELECT 1)';
        }

        $sql = rtrim(str_replace($orderby, '', $sql));

        $selectParams = trim(substr($sql, $selectStart + 6, $fromStart - $selectStart - 6));
        $selectParams .= ', ROW_NUMBER() OVER (' . $orderby . ') AS [ZEND_ROW_NUMBER]';

        $sql = substr($sql, 0, $selectStart + 6) . ' ' . $selectParams . ' ' . substr($sql, $fromStart);

        $outerSql = 'SELECT * FROM (' . $sql . ') AS [ZEND_OFFSET_EMULATION]'
                  . ' WHERE [ZEND_OFFSET_EMULATION].[ZEND_ROW_NUMBER] BETWEEN '
                  . ($offset + 1) . ' AND '
                  . ($offset + $count)
                  . ' ORDER BY [ZEND_ROW_NUMBER] ASC';

        return $outerSql;
    }
}
于 2015-11-16T11:23:43.780 に答える