Ejemplo n.º 1
0
Archivo: db.php Proyecto: refirio/levis
/**
 * Export SQL to the file.
 *
 * @param string|null $file
 * @param string|null $target
 * @param bool        $combined
 */
function db_export($file = null, $target = null, $combined = true)
{
    $resource = db_query(db_sql('table_list'));
    $results = db_result($resource);
    $tables = array();
    foreach ($results as $result) {
        $tables[] = array_shift($result);
    }
    $text = '-- Database: ' . DATABASE_NAME . ' (' . DATABASE_TYPE . ")\n";
    $text .= '-- Datetime: ' . localdate('Y-m-d H:i:s') . "\n";
    $text .= '-- Host: ' . gethostbyaddr(clientip()) . "\n";
    $text .= "\n";
    foreach ($tables as $table) {
        if ($target === null || $target === $table) {
            $resource = db_query(db_sql('table_create', $table));
            $results = db_result($resource);
            if (DATABASE_TYPE === 'pdo_mysql' || DATABASE_TYPE === 'mysql') {
                $text .= "DROP TABLE IF EXISTS " . $table . ";\n";
                $text .= $results[0]['Create Table'] . ";\n";
                $text .= "\n";
            } elseif (DATABASE_TYPE === 'pdo_pgsql' || DATABASE_TYPE === 'pgsql') {
                $text .= "DROP TABLE IF EXISTS " . $table . ";\n";
                $text .= $results[0]['case'] . ";\n";
                $text .= "\n";
            } elseif (DATABASE_TYPE === 'pdo_sqlite' || DATABASE_TYPE === 'pdo_sqlite2' || DATABASE_TYPE === 'sqlite') {
                $text .= "DROP TABLE IF EXISTS " . $table . ";\n";
                $text .= $results[0]['sql'] . ";\n";
                $text .= "\n";
            }
            $resource = db_query('SELECT * FROM ' . $table . ';');
            $results = db_result($resource);
            $values = array();
            $i = 0;
            foreach ($results as $result) {
                $inserts = array();
                foreach ($result as $data) {
                    if ($data === null) {
                        $inserts[] = 'NULL';
                    } else {
                        $inserts[] = db_escape($data);
                    }
                }
                if ($combined === true) {
                    $values[intval($i / 50)][] = '(' . implode(', ', $inserts) . ')';
                } else {
                    $text .= "INSERT INTO " . $table . " VALUES(" . implode(', ', $inserts) . ");\n";
                }
                $i++;
            }
            if ($combined === true && !empty($values)) {
                foreach ($values as $value) {
                    $text .= "INSERT INTO " . $table . " VALUES\n";
                    $text .= implode(",\n", $value);
                    $text .= ";\n";
                }
            }
            $text .= "\n";
        }
    }
    if ($file === null) {
        if ($target === null) {
            $filename = DATABASE_NAME . '.sql';
        } else {
            $filename = DATABASE_NAME . '-' . $target . '.sql';
        }
        header('Content-Type: text/plain');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        echo $text;
        exit;
    } else {
        if (file_put_contents($file, $text) === false) {
            error('db: Export file can\'t write');
        }
    }
    return;
}
Ejemplo n.º 2
0
     if ($populated != TRUE) {
         $missing_salsa[] = $f;
     }
 }
 foreach ($salsa_fields as $s) {
     unset($populated);
     foreach ($fields as $f) {
         if ($f == $s['Field']) {
             $populated = TRUE;
         }
     }
     if ($populated != TRUE) {
         $missing_db[] = $s['Field'];
         $db_sql[] = db_sql($m, $s);
     }
     $db_sql[] = db_sql($m, $s);
 }
 $x = 0;
 foreach ($missing_salsa as $i) {
     if ($i == 'organization_KEY') {
         unset($missing_salsa[$x]);
     }
     if ($i == 'chapter_KEY') {
         unset($missing_salsa[$x]);
     }
     $x++;
 }
 echo '<h3>' . $m::$table_name . '</h3>';
 if ($missing_salsa) {
     echo '<b>missing from salsa:</b><br><br>';
     #echo 'public static $custom_fields = array(<br>';