/** * Return all the fields of a table. * * It also populate the metadata array attribute will all the informations about the * table fields. Type, Key, Extra, Null * TABLE_QUALIFIER TABLE_SCHEM TABLE_NAME COLUMN_NAME DATA_TYPE TYPE_NAME PRECISION LENGTH SCALE RADIX NULLABLE REMARKS * * @param string $table Name of the Table * @return array $field All the fields name */ function getTableField($table = "") { if (is_array($table)) { $atable = $table; } elseif (strlen($table) > 0) { $atable = $table; } else { $atable = $this->table; } if (is_array($atable)) { reset($atable); $numfields = 0; while (list($key, $table) = each($atable)) { //$table_def = mysql_query("SHOW FIELDS FROM $table", ); $table_def = odbc_columns($this->dbCon->id, $this->dbCon->getDatabase(), "%", $table); for ($i = 0; $i < odbc_num_rows($table_def); $i++) { $row_table_def = odbc_fetch_array($table_def); $field[$numfields] = $row_table_def["COLUMN_NAME"]; $fieldname = $row_table_def["COLUMN_NAME"]; $this->metadata[$table][$fieldname]["Type"] = $row_table_def["TYPE_NAME"]; $this->metadata[$table][$fieldname]["Null"] = $row_table_def["NULLABLE"]; $this->metadata[$table][$fieldname]["Key"] = $row_table_def["SCALE"]; //not sure of that one. $this->metadata[$table][$fieldname]["Extra"] = $row_table_def["REMARKS"]; $numfields++; } } } else { $table_def = odbc_columns($this->dbCon->id, $this->dbCon->getDatabase(), "%", $atable); for ($i = 0; $i < odbc_num_rows($table_def); $i++) { $row_table_def = odbc_fetch_array($table_def); $field[$i] = $row_table_def["COLUMN_NAME"]; $fieldname = $row_table_def["COLUMN_NAME"]; $this->metadata[$table][$fieldname]["Type"] = $row_table_def["TYPE_NAME"]; $this->metadata[$table][$fieldname]["Null"] = $row_table_def["NULLABLE"]; $this->metadata[$table][$fieldname]["Key"] = $row_table_def["SCALE"]; //not sure of that one. $this->metadata[$table][$fieldname]["Extra"] = $row_table_def["REMARKS"]; } } reset($field); return $field; }
/** * 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; }