Example #1
0
 /**
  * Execute an sqlQuery.
  *
  * Execute a query the query string and database connexion object need
  * to be passe has parameters or be previously define.
  *
  * @param string $sql   String with the SQL Query.
  * @param object sqlConnect $dbCon   Connexion object if not previously define in the contructor.
  * @return ResultSet $rquery
  * @access public
  */
 function query($sql = "", $dbCon = 0)
 {
     if ($dbCon != 0) {
         $this->dbCon = $dbCon;
     }
     if (strlen($sql) > 0) {
         $this->sql_query = trim($sql);
     }
     if (!is_resource($this->dbCon->id)) {
         $this->setError("Query Error: No open or valid connexion has been provide to execute the query: " . $this->sql_query);
         return false;
     }
     if (empty($this->sql_query)) {
         //    $this->setError("No query to execute.:".var_export(get_object_vars($this), true));
         $this->setLog(" query(): No query to execute.");
         return false;
     }
     if ($this->max_rows) {
         if (!$this->pos) {
             $this->pos = 0;
         }
         $qpos = " limit " . $this->pos . ", " . $this->max_rows;
     } else {
         if (!$this->pos) {
             $this->pos = "";
         }
         $qpos = $this->pos;
     }
     /** Temporary for ";" compatibility with postgresql **/
     //  if (substr($this->sql_query, -1,1) == ";") {
     //      $this->sql_query = substr($this->sql_query, 0, strlen($this->sql_query)-1) ;
     //  }
     // This is to fix the mysql_select_db problem when connexion have same username.
     if ($this->dbCon->getAllwaysSelectDb()) {
         $this->dbCon->setDatabase($this->dbCon->getDatabase());
         //echo $this->dbCon->getDatabase();
     }
     // convert quote from postgresql queries
     // this was a bad idea, break wordpress install for example!!!
     //$this->sql_query=str_replace('"', '`',$this->sql_query);
     if ($this->dbCon->getUseCluster()) {
         if (preg_match("/^select/i", $this->sql_query)) {
             $this->query_connexion = $this->dbCon->id;
         } else {
             $this->query_connexion = $this->dbCon->wid;
         }
     } else {
         $this->query_connexion = $this->dbCon->id;
     }
     if (preg_match("/^select/i", $this->sql_query)) {
         $rquery = mysql_query($this->sql_query . " " . $this->sql_order . " " . $qpos, $this->query_connexion);
         $this->setLog($this->sql_query . " " . $this->sql_order . " " . $qpos);
     } else {
         $rquery = mysql_query($this->sql_query, $this->query_connexion);
         $this->setLog($this->sql_query);
     }
     $sqlerror = "";
     if (!is_resource($rquery)) {
         $sqlerror = mysql_error($this->query_connexion);
         if (!empty($sqlerror)) {
             $this->setError("<b>SQL Query Error :</b>" . mysql_errno($this->query_connexion) . " - " . $sqlerror . " (" . $this->sql_query . ")");
         }
     }
     if (!$this->max_rows) {
         $this->num_rows = @mysql_num_rows($rquery);
     }
     //$this->insert_id = mysql_insert_id() ;
     $this->insert_id = 0;
     $this->result = $rquery;
     $this->cursor = 0;
     if ($this->dbCon->getBackupSync()) {
         if (preg_match("/^alter/i", $this->sql_query) || preg_match("/^create/i", $this->sql_query) || preg_match("/^drop/i", $this->sql_query)) {
             if ($this->dbCon->getUseDatabase()) {
                 $qInsSync = "insert into " . $this->dbCon->getTableBackupSync() . " ( actiontime, sqlstatement, dbname) values ( '" . time() . "', '" . addslashes($this->sql_query) . "', '" . $this->dbCon->db . "') ";
                 $rquery = mysql_query($qInsSync, $this->dbCon->id);
             } else {
                 $file = $this->dbCon->getProjectDirectory() . "/" . $this->dbCon->getTableBackupSync() . ".struct.sql";
                 $fp = fopen($file, "a");
                 $syncquery = $this->sql_query . ";\n";
                 fwrite($fp, $syncquery, strlen($syncquery));
                 fclose($fp);
             }
         }
         if (preg_match("/^insert/i", $this->sql_query) || preg_match("/^update/i", $this->sql_query) || preg_match("/^delete/i", $this->sql_query)) {
             if ($this->dbCon->getUseDatabase()) {
                 $qInsSync = "insert into " . $this->dbCon->getTableBackupSync() . " ( actiontime, sqlstatement, dbname) values ( '" . time() . "', '" . addslashes($this->sql_query) . "', '" . $this->dbCon->db . "') ";
                 $rquery = mysql_query($qInsSync, $this->dbCon->id);
             } else {
                 $file = $this->dbCon->getProjectDirectory() . "/" . $this->dbCon->getTableBackupSync() . ".data.sql";
                 $fp = fopen($file, "a");
                 $syncquery = $this->sql_query . ";\n";
                 fwrite($fp, $syncquery, strlen($syncquery));
                 fclose($fp);
             }
         }
     }
     return $rquery;
 }