[limb-svn] r6846 - 3.x/trunk/limb/dbal/src/drivers/linter

svn at limb-project.com svn at limb-project.com
Fri Mar 21 13:37:09 MSK 2008


Author: svk
Date: 2008-03-21 13:37:09 +0300 (Fri, 21 Mar 2008)
New Revision: 6846
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6846

Modified:
   3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterConnection.class.php
   3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterQueryStatement.class.php
   3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterStatement.class.php
Log:
-- dbal linter driver changed

Modified: 3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterConnection.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterConnection.class.php	2008-03-19 15:45:47 UTC (rev 6845)
+++ 3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterConnection.class.php	2008-03-21 10:37:09 UTC (rev 6846)
@@ -32,6 +32,7 @@
   protected $cursorPool = array();
   protected $mode = null;
   protected $useConnection = false;
+  protected $debug = false;
   
 
   function __construct($config)
@@ -89,7 +90,12 @@
     {
       $this->_raiseError($conn);
       $this->connectionId = null;
+      $this->log("Connection failed");
     }
+    else
+    {
+      $this->log("Connected in mode ".$this->mode.". Connection #".$this->connectionId);
+    }
 	
 
 
@@ -99,6 +105,7 @@
 
   function __wakeup()
   {
+    $this->log("Wakeup called. Connection droppped");
     $this->connectionId = null;
   }
 
@@ -106,15 +113,37 @@
   {
     if($this->connectionId > 0)
     {
-      linter_close_connect($this->connectionId);
+      $res = linter_close_connect($this->connectionId);
+      if ($res < 0)
+      {
+        $this->log("Disconnect failed: #".$this->connectionId);
+      }
+      else
+      {
+        $this->log("Disconnected: #".$this->connectionId);
+      }
+      $this->cursorPool = array();
       $this->connectionId = null;
     }
   }
 
+  function __destruct()
+  {
+    $this->disconnect();
+  }
+  
   function closeCursor($cursorId)
   {
-    linter_close_cursor($cursorId);
-    unset($this->cursorPool[$cursorId]);
+    $res = linter_close_cursor($cursorId);
+    if ($res < 0)
+    {
+      $this->log("Cursor closing failed: Connection: ".$this->connectionId."; cursor: ".$cursorId."; pool size: ".count($this->cursorPool));
+    }
+    else
+    {
+      unset($this->cursorPool[$cursorId]);
+      $this->log("Cursor closed: Connection: ".$this->connectionId."; cursor: ".$cursorId."; pool size: ".count($this->cursorPool));
+    }
   }
   
   function _raiseError($code, $conn_id = null, $args=array())
@@ -149,6 +178,7 @@
         $err_row = $sys_err & 0xFFFF;
         $err_pos = $sys_err >> 16;
         $err_message .= sprintf(" at row %d, position %d", $err_row, $err_pos);
+        $err_message .= "Query: ".$args['sql'];
       }
       else
         $err_message .= sprintf(", system error %d", $sys_err);
@@ -189,6 +219,7 @@
       linter_set_cursor_opt($result, CO_FETCH_BLOBS_AS_USUAL_DATA, 1);
 
       $this->cursorPool[$result] = "opened";
+      $this->log("Cursor opened. Connection: ".$this->connectionId."; cursor: ".$result."; pool size: ".count($this->cursorPool));
     }
     else
       $result = true;
@@ -201,11 +232,11 @@
     if ($res < 0)
     {
       $res = $this->_raiseError($res, $this->useConnection ? $this->connectionId : $cur, array('sql' => $sql));
-//      if ($res != self::LINTER_EMPTY_DATASET)
-//      {
-//        var_dump($sql);
-//        var_dump($res);
-//      }
+      if ($res != self::LINTER_EMPTY_DATASET)
+      {
+        var_dump($sql);
+        var_dump($res);
+      }
       if (!$this->useConnection)
       {
         linter_close_cursor($cur);
@@ -217,34 +248,32 @@
       return $cur;
   }
   
-  function prepare($sql)
+  function execute($sql)
   {
     $result = null;
     $sql = $this->prepare_sql($sql);
     
     $result = $this->handle_cursor_pool();
+    $res = linter_exec_direct($this->useConnection ? $this->connectionId : $result, $sql);
     
-    $res = linter_prepare($this->useConnection ? $this->connectionId : $result, $sql);
+    $this->log("Query direct executed: $sql, result:$res");
     return $this->handle_call_result($res, $result, $sql);
   }
   
-  function execute($sql)
+  function executeStatement($stmt)
   {
-    $result = null;
+    $sql = $stmt->getSQL();
     $sql = $this->prepare_sql($sql);
+    $cursor = $this->handle_cursor_pool();
+    $res = linter_prepare($cursor, $sql);
+    $this->log("Query prepared: " . $sql . ", result: " . $res);
+    $cursor = $this->handle_call_result($res, $cursor, $sql);
     
-    $result = $this->handle_cursor_pool();
-    $res = linter_exec_direct($this->useConnection ? $this->connectionId : $result, $sql);
-    
-    return $this->handle_call_result($res, $result, $sql);
+    $res = linter_execute($cursor, $stmt->getParams());
+    $this->log("Prepared query executed: " . $sql . "Cursor: " . $cursor . ", result: " . $res);
+    return $this->handle_call_result($res, $cursor, $sql);
   }
   
-  function pexecute($curId, $args)
-  {
-    $result = linter_execute($curId, $args);
-    return $this->handle_call_result($result, $curId, "");
-  }
-  
   function cexecute($sql)
   {
     $this->useConnection = true;
@@ -257,13 +286,16 @@
   {
     $this->check_mode();
     $this->transactionCount += 1;
-    return $this->cexecute('set savepoint sp' . $this->transactionCount . ';');
+    $sql = 'set savepoint sp' . $this->transactionCount . ';';
+    return $this->cexecute($sql);
   }
 
   function commitTransaction()
   {
     $this->check_mode();
-    $result = $this->cexecute('commit to savepoint sp' . $this->transactionCount . ';');
+    if ($this->transactionCount === 0) return false;
+    $sql = 'commit to savepoint sp' . $this->transactionCount . ';';
+    $result = $this->cexecute($sql);
     if ($this->transactionCount) $this->transactionCount -= 1;
     return $result;
   }
@@ -272,7 +304,8 @@
   {
     if ($this->transactionCount === 0) return false;
     $this->check_mode();
-    $result = $this->cexecute('rollback to savepoint sp' . $this->transactionCount . ';');
+    $sql = 'rollback to savepoint sp' . $this->transactionCount . ';';
+    $result = $this->cexecute($sql);
     if ($this->transactionCount) $this->transactionCount -= 1;
     return $result;
   }
@@ -281,9 +314,11 @@
   {
     if (!($this->mode & TM_EXCLUSIVE))
     {
+      $mode = $this->mode;
       $this->disconnect();
       $this->mode |= TM_EXCLUSIVE;
       $this->connect();
+      //$this->mode = $mode;
     }
   }
   
@@ -423,6 +458,14 @@
     }
     
   }
+  
+  protected function log($message)
+  {
+    if ($this->debug)
+    {
+      error_log(date("Y-m-d H:i:s")."\t".$message."\t\n", 3, 'linter.log');
+    }
+  }
 }
 
 

Modified: 3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterQueryStatement.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterQueryStatement.class.php	2008-03-19 15:45:47 UTC (rev 6845)
+++ 3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterQueryStatement.class.php	2008-03-21 10:37:09 UTC (rev 6846)
@@ -54,6 +54,36 @@
   {
     return new lmbLinterRecordSet($this->connection, $this);
   }
+  
+  function count()
+  {
+    if(!(preg_match("/^\s*SELECT\s+DISTINCT/is", $this->original_sql) || preg_match('/\s+GROUP\s+BY\s+/is',$this->original_sql)) && preg_match("/^\s*SELECT\s+.+\s+FROM\s+/Uis", $this->original_sql))
+    {
+      $rewritesql = preg_replace('/^\s*SELECT\s.*\s+FROM\s/Uis','SELECT COUNT(*) FROM ', $this->original_sql);
+      $rewritesql = preg_replace('/(\sORDER\s+BY\s.*)/is','', $rewritesql);
+
+      $queryId = $this->execute($rewritesql);
+      $count = linter_get_cursor_opt($queryId, CO_ROW_COUNT);
+      $row = linter_get_data_row($queryId);
+      $this->connection->closeCursor($queryId);
+      if (is_array($row))
+      {
+        return $row[0];
+      }
+      else
+      {
+        return 0;
+      }
+    }
+
+    // could not re-write the query, try a different method.
+    $queryId = $this->execute($this->sql);
+    $count = linter_get_cursor_opt($queryId, CO_ROW_COUNT);
+    $this->connection->closeCursor($queryId);
+    return $count;
+  }
+  
+   
 }
 
 

Modified: 3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterStatement.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterStatement.class.php	2008-03-19 15:45:47 UTC (rev 6845)
+++ 3.x/trunk/limb/dbal/src/drivers/linter/lmbLinterStatement.class.php	2008-03-21 10:37:09 UTC (rev 6846)
@@ -17,6 +17,7 @@
 class lmbLinterStatement implements lmbDbStatement
 {
   protected $sql;
+  protected $original_sql;
   protected $prepared_sql;
   protected $statement;
   protected $connection;
@@ -28,6 +29,7 @@
   function __construct($connection, $sql)
   {
     $this->sql = $sql;
+    $this->original_sql = $sql;
     $this->connection = $connection;
   }
 
@@ -163,19 +165,42 @@
       return $this->prepared_sql;
   }
 
-  function execute()
+  function execute($sql = "")
   {
+    if (!empty($sql))
+    {
+      $stored_sql = $this->sql;
+      $this->sql = $sql;
+    }
     $this->_prepareStatement();
     if ($this->prepRequired)
-      return $this->connection->pexecute($this->queryId, $this->prepParams);
+    {
+      $this->queryId = $this->connection->executeStatement($this);
+      $this->prepared_sql = null;
+      if (isset($stored_sql))
+      {
+        $this->sql = $stored_sql;
+      }
+      //$res = linter_execute($this->queryId, $this->prepParams);
+      //return $this->connection->pexecute($this->queryId, $this->prepParams);
+    }
     else
     {
       $this->queryId = $this->connection->execute($this->getSQL());
       $this->prepared_sql = null;
-      return $this->queryId;
+      if (isset($stored_sql))
+      {
+        $this->sql = $stored_sql;
+      }
     }
+    return $this->queryId;
   }
   
+  function getParams()
+  {
+    return $this->prepParams;
+  }
+  
   function setConnection($connection)
   {
     $this->connection = $connection;
@@ -186,22 +211,6 @@
     $this->connection->closeCursor($this->queryId);    
   }
   
-  
-  protected function _prepareStatement()
-  {
-    $sql = $this->checkForParams($this->sql);
-    if (!is_null($sql))
-    {
-      $this->queryId = $this->connection->prepare($sql);
-  
-      if($this->queryId < 0)
-      {
-        $this->connection->_raiseError();
-        return;
-      }
-    }
-  }
-  
   protected function check_type($param)
   {
     if (is_string($param)) 
@@ -210,11 +219,12 @@
       return $param;
   }
   
-  protected function checkForParams($sql)
+  protected function _prepareStatement()
   {
     $newsql = '';
     $iter = 0;
     $nulls = false;
+    $sql = $this->sql;
 
     if (preg_match("#^select :(\w+):;?$#i", $sql, $m))
     {
@@ -223,12 +233,12 @@
       {
         $sql = str_replace(":".$m[1].":", $this->check_type($this->parameters[$m[1]]), $sql);
         $this->prepared_sql = $sql;
-        return null;
+        return true;
       }
       else
       {
         $this->prepared_sql = "select null;";
-        return null;
+        return true;
       }
     }
     while(preg_match('/^(\'[^\']*?\')|(--[^(\n)]*?\n)|(:(?-U)\w+:(?U))|.+/Us', $sql, $matches))
@@ -263,10 +273,9 @@
     else
       $this->prepRequired = false;
       
-    if ($nulls)
-      $this->prepared_sql = $newsql;
+    $this->prepared_sql = $newsql;
       
-    return $newsql;
+    return true;
   }
 
   function addOrder($sort_params)



More information about the limb-svn mailing list