diff --git a/resources/classes/database.php b/resources/classes/database.php index 37d6bd0f0d..f745b8a489 100644 --- a/resources/classes/database.php +++ b/resources/classes/database.php @@ -297,7 +297,6 @@ //driver and type point to the same value $this->driver = $config->get('database.0.type', 'pgsql'); - $this->driver = $config->get('database.0.type', 'pgsql'); $this->type = $config->get('database.0.type', 'pgsql'); $this->host = $config->get('database.0.host', '127.0.0.1'); $this->port = $config->get('database.0.port', '5432'); @@ -787,6 +786,89 @@ } } + /** + * Checks if the column exists in the database. + *
Note:
+ * Tables and Column names must be sanitized. Otherwise, a warning will be
+ * emitted and false will be returned.
"; + print_r($backtrace); + echo ""; + return false; + } + + //check the sqlite database to see if the column exists + //if ($this->db_type == "sqlite") { + // $table_info = $this->table_info($table_name); + // if ($this->sqlite_column_exists($table_info, $column_name)) { + // return true; + // } + // else { + // return false; + // } + //} + + //check the postgresql database to see if the column exists + if ($this->type == "pgsql") { + $sql = "SELECT attname FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '$table_name' limit 1) AND attname = '$column_name'; "; + } + + //check the mysql database to see if the column exists + if ($this->type == "mysql") { + //$sql .= "SELECT * FROM information_schema.COLUMNS where TABLE_SCHEMA = '$db_name' and TABLE_NAME = '$table_name' and COLUMN_NAME = '$column_name' "; + $sql = "show columns from $table_name where field = '$column_name' "; + } + + //return the results from the sql query + if (empty($sql)) { + return false; + } + else { + $prep_statement = $this->db->prepare($sql); + $prep_statement->execute(); + $result = $prep_statement->fetchAll(PDO::FETCH_NAMED); + if (!$result) { + return false; + } + if (count($result) > 0) { + return true; + } + else { + return false; + } + unset ($prep_statement); + } + + } + /** * Queries {@link database::table_info()} to return the fields. * @access public