Example #1
0
 /**
  * read_from_db
  *
  * reads all fields specified in $this->struct from the database
  * and auto-converts them to database-independent values based on the field type (see $colformat)
  *
  * calls $this->read_from_db_postprocess() to postprocess the result
  *
  * @param array or string condition -see build_select_query() for details
  * @param array searchmode - see build_select_query() for details
  * @param integer limit - maximum number of rows to return
  * @param integer offset - number of first row to return
  * @return array - rows (as associative array, with the ID as key)
  */
 protected function read_from_db($condition, $searchmode = array(), $limit = -1, $offset = -1)
 {
     $queryparts = $this->build_select_query($condition, $searchmode);
     $query = $queryparts['select_cols'] . $queryparts['from_where_order'];
     $limit = (int) $limit;
     # make sure $limit and $offset are really integers
     $offset = (int) $offset;
     if ($limit > -1 && $offset > -1) {
         $query .= " LIMIT {$limit} OFFSET {$offset} ";
     }
     $result = db_query($query);
     $db_result = array();
     if ($result['rows'] != 0) {
         while ($row = db_assoc($result['result'])) {
             $db_result[$row[$this->id_field]] = $row;
         }
     }
     $db_result = $this->read_from_db_postprocess($db_result);
     return $db_result;
 }
Example #2
0
function list_tables($views_mode = false)
{
    // @tables
    // @views
    global $db_driver, $db_link, $db_name;
    if ($views_mode && !views_supported()) {
        return array();
    }
    static $cache_tables;
    static $cache_views;
    if ($views_mode) {
        if (isset($cache_views)) {
            return $cache_views;
        }
    } else {
        if (isset($cache_tables)) {
            return $cache_tables;
        }
    }
    static $all_tables;
    // tables and views
    if ('mysql' == $db_driver) {
        if (!isset($all_tables)) {
            $all_tables = db_assoc("SHOW FULL TABLES");
            // assoc: table name => table type (BASE TABLE or VIEW)
        }
        // This chunk of code is the same as in pgsql driver.
        if ($views_mode) {
            $views = array();
            foreach ($all_tables as $view => $type) {
                if ($type != 'VIEW') {
                    continue;
                }
                $views[] = $view;
            }
            $cache_views = $views;
            return $views;
        } else {
            $tables = array();
            foreach ($all_tables as $table => $type) {
                if ($type != 'BASE TABLE') {
                    continue;
                }
                $tables[] = $table;
            }
            $cache_tables = $tables;
            return $tables;
        }
    } else {
        if ('pgsql' == $db_driver) {
            if (!isset($all_tables)) {
                $query = "SELECT table_name, table_type ";
                $query .= "FROM information_schema.tables ";
                $query .= "WHERE table_schema = 'public' ";
                $query .= "AND (table_type = 'BASE TABLE' OR table_type = 'VIEW') ";
                $query .= "ORDER BY table_name ";
                $all_tables = db_assoc($query);
            }
            // This chunk of code is the same as in mysql driver.
            if ($views_mode) {
                $views = array();
                foreach ($all_tables as $view => $type) {
                    if ($type != 'VIEW') {
                        continue;
                    }
                    $views[] = $view;
                }
                $cache_views = $views;
                return $views;
            } else {
                $tables = array();
                foreach ($all_tables as $table => $type) {
                    if ($type != 'BASE TABLE') {
                        continue;
                    }
                    $tables[] = $table;
                }
                $cache_tables = $tables;
                return $tables;
            }
        } else {
            if ("sqlite" == $db_driver) {
                if ($views_mode) {
                    $views = PDO_FetchAssoc("SELECT name FROM sqlite_master WHERE type = 'view' AND name NOT LIKE 'sqlite_%' ORDER BY name");
                    $cache_views = $views;
                    return $views;
                } else {
                    $tables = PDO_FetchAssoc("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name");
                    $cache_tables = $tables;
                    return $tables;
                }
            }
        }
    }
}
function table_types2($table)
{
    global $db_driver;
    if ('mysql' == $db_driver) {
        $types = array();
        $rows = @db_list("SHOW COLUMNS FROM `{$table}`");
        if (!($rows && count($rows))) {
            return false;
        }
        foreach ($rows as $row) {
            $type = $row['Type'];
            preg_match('#^[a-z]+#', $type, $match);
            $type = $match[0];
            $types[$row['Field']] = $type;
        }
    }
    if ('pgsql' == $db_driver) {
        $types = db_assoc("SELECT column_name, udt_name FROM information_schema.columns WHERE table_name ='{$table}' ORDER BY ordinal_position");
        if (!count($types)) {
            return false;
        }
        foreach ($types as $col => $type) {
            // "_" also in regexp - error when retrieving column info from "pg_class",
            // udt_name might be "_aclitem" / "_text".
            preg_match('#^[a-z_]+#', $type, $match);
            $type = $match[0];
            $types[$col] = $type;
        }
    }
    foreach ($types as $col => $type) {
        if ('varchar' == $type) {
            $type = 'char';
        }
        if ('integer' == $type) {
            $type = 'int';
        }
        if ('timestamp' == $type) {
            $type = 'time';
        }
        $types[$col] = $type;
    }
    return $types;
}
Example #4
0
     include "templates/footer.php";
 } else {
     fwrite($fh, $header);
     $tables = array('admin', 'alias', 'config', 'domain', 'domain_admins', 'fetchmail', 'log', 'mailbox', 'vacation', 'vacation_notification');
     for ($i = 0; $i < sizeof($tables); ++$i) {
         $result = db_query("SHOW CREATE TABLE " . table_by_key($tables[$i]));
         if ($result['rows'] > 0) {
             while ($row = db_array($result['result'])) {
                 fwrite($fh, "{$row['1']};\n\n");
             }
         }
     }
     for ($i = 0; $i < sizeof($tables); ++$i) {
         $result = db_query("SELECT * FROM " . table_by_key($tables[$i]));
         if ($result['rows'] > 0) {
             while ($row = db_assoc($result['result'])) {
                 foreach ($row as $key => $val) {
                     $fields[] = $key;
                     $values[] = $val;
                 }
                 fwrite($fh, "INSERT INTO " . $tables[$i] . " (" . implode(',', $fields) . ") VALUES ('" . implode('\',\'', $values) . "');\n");
                 $fields = "";
                 $values = "";
             }
         }
     }
 }
 header("Content-Type: text/plain");
 header("Content-Disposition: attachment; filename=\"{$filename}\"");
 header("Content-Transfer-Encoding: binary");
 header("Content-Length: " . filesize("{$backup}"));