/** * 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); } if ($this->sql_query == "") { if (is_array($this->table)) { reset($this->table); $this->sql_query = "select * from "; while (list($key, $table) = each($this->table)) { $this->sql_query .= $table . ","; } $this->sql_query = ereg_replace(",\$", "", $this->sql_query); } else { $this->sql_query = "select * from {$this->table}"; } } 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; } if ($this->dbCon->getAllwaysSelectDb()) { $this->dbCon->setDatabase($this->dbCon->getDatabase()); } $rquery = odbc_exec($this->dbCon->id, "{$this->sql_query} {$this->sql_order} {$qpos}"); if (!is_resource($rquery)) { $sqlerror = odbc_errormsg(); if ($sqlerror) { $this->setError("<b>SQL Query Error :</b>" . $sqlerror . " ({$this->sql_query} {$this->sql_order} {$qpos})"); } } if (!$this->max_rows) { $this->num_rows = @odbc_num_rows($rquery); } $this->insert_id = 0; $this->result = $rquery; $this->cursor = 0; if ($this->dbCon->getBackupSync()) { if (eregi("^alter", $this->sql_query) || eregi("^create", $this->sql_query) || eregi("^drop", $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 = odbc_exec($this->dbCon->id, $qInsSync); } 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 (eregi("^insert", $this->sql_query) || eregi("^update", $this->sql_query) || eregi("^delete", $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 = odbc_exec($this->dbCon->id, $qInsSync); } 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; }
/** * 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; }
/** * Execute an sqlQuery * * Execute a query the query string and database connexion object need * to be passe has parameters or be previously define * * @param sting $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); } // if ($this->sql_query == "") { // if (is_array($this->table)) { // reset($this->table) ; // $this->sql_query = "select * from " ; // while (list($key, $table) = each($this->table)) { // $this->sql_query .= $table."," ; // } // $this->sql_query = ereg_replace(",$", "", $this->sql_query) ; // } else { // $this->sql_query= "select * from $this->table" ; // } // } if (empty($this->sql_query)) { $this->setError("No query to execute"); return false; } if ($this->max_rows) { if (!$this->pos) { $this->pos = 0; } // $qpos = " limit ".$this->pos.", ".$this->max_rows ; PL oposite of MySQL $qpos = " limit " . $this->max_rows . " offset " . $this->pos; } else { if (!$this->pos) { $this->pos = ""; } $qpos = $this->pos; } // convert quote from mysql queries // This will need to be removed as it may generate significant problem is queries uses backquotes // in their content $this->sql_query = str_replace('`', '"', $this->sql_query); $rquery = pg_query($this->dbCon->id, "{$this->sql_query} {$this->sql_order} {$qpos}"); if (!is_resource($rquery)) { $sqlerror = pg_last_error(); if ($sqlerror) { $this->setError("<b>SQL Query Error :</b>" . $sqlerror . " ({$this->sql_query} {$this->sql_order} {$qpos})"); } } else { // if (!$this->max_rows) { PL 20020920 commented out to make it work on the fetch() $this->num_rows = @pg_numrows($rquery); // } } //$this->insert_id = pg_getlastoid($rquery) ; $this->result = $rquery; $this->cursor = 0; if ($this->dbCon->getBackupSync()) { if (eregi("^alter", $this->sql_query) || eregi("^create", $this->sql_query) || eregi("^drop", $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 = pg_query($this->dbCon->db, $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 (eregi("^insert", $this->sql_query) || eregi("^update", $this->sql_query) || eregi("^delete", $this->sql_query)) { if ($this->dbCon->getUseDatabase()) { $qInsSync = "insert into " . $this->dbCon->getTableBackupSync() . " ( actiontime, sqlstatement, dbname) values ( '" . time() . "', '" . addslashes("{$this->sql_query} {$this->sql_order} {$qpos}") . "', '" . $this->dbCon->db . "') "; $rquery = pg_query($this->dbCon->db, $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; }