Esempio n. 1
0
function _db_update(&$qstruct)
{
    global $ERROR;
    global $SCHEMA;
    global $debug;
    if ($debug) {
        echo "_db_update: ";
        print_r($qstruct);
        echo "<br>\n";
    }
    $ERROR = "";
    $tp_table = $qstruct["TABLE"];
    // robustness against wrong calling with *_tp
    if (_db_temporal($tp_table, $table)) {
        $qstruct["TABLE"] = $table;
    }
    // handle callback hooks
    foreach (array("CB_BEFORE", "CB_BEFORE_" . $qstruct["MODE"]) as $cb) {
        if ($test = @$SCHEMA[$table][$cb]) {
            if ($debug) {
                echo "CALLBACK {$cb} -> {$test}<br>\n";
            }
            $qstruct["DATA"] = $test($table, $qstruct["DATA"]);
            if ($ERROR) {
                return null;
            }
        }
    }
    $database = _db_database($table);
    if (!@$qstruct["RAW_MODE"]) {
        $qstruct["DATA"] = _db_prepare_data($table, $qstruct["DATA"]);
    }
    $query = _db_make_update($database, $qstruct, $cb_list);
    if (!$query && !$ERROR) {
        $ERROR = "internal error: cannot create SQL statements";
    }
    if ($ERROR) {
        return null;
    }
    if ($debug) {
        echo "-----------QUERY: {$query};<br>\n";
    }
    $env = array("DB" => $database, "CB" => "_db_cb_process_data");
    $ok = _db_multiquery($env, true, $query, $cb_list);
    if (!$ok || $ERROR) {
        if (!$ERROR) {
            $ERROR = "internal error: cannot execute SQL statements";
        }
        return null;
    }
    // handle callback hooks
    foreach (array("CB_AFTER", "CB_AFTER_" . $qstruct["MODE"]) as $cb) {
        if ($test = @$SCHEMA[$table][$cb]) {
            if ($debug) {
                echo "CALLBACK {$cb} -> {$test}<br>\n";
            }
            $qstruct["DATA"] = $test($table, $qstruct["DATA"]);
            if ($ERROR) {
                return null;
            }
        }
    }
    return true;
}
function _query($database, $query)
{
    global $ERROR;
    $env = array("DB" => $database);
    $res = _db_multiquery($env, true, $query, array("_db_cb_update"));
    if ($ERROR) {
        die("Query error:<br>\n" . $query . "<br>\nError: " . $ERROR);
    }
    return $res;
}
Esempio n. 3
0
function sync_table($src, $dst, $cond = array(), $transl = "", $modes = array("INSERT", "UPDATE", "DELETE"))
{
    global $SYNC_STATUS;
    global $SCHEMA;
    global $ERROR;
    global $debug;
    $tdef = $SCHEMA[$src];
    $version = _db_extfield($src, "version");
    $limit = @$SYNC_STATUS[$src][$dst];
    if ($limit) {
        $cond["{$version} >"] = $limit;
    }
    $qstruct = array("TABLE" => $src, "FIELD" => array(), "COND" => $cond, "ORDER" => "", "START" => 0, "COUNT" => 0);
    $databases = array();
    $q2 = _db_mangle_query($databases, $qstruct);
    // currently only 1 database supported
    $database = key($databases);
    $query = _db_make_query($database, $subqs, $q2);
    $env = array("DB" => $database, "ARG" => $subqs, "CB_PROCESS" => "_sync_cb_table", "SRC" => $src, "DST" => $dst, "TRANSL" => $transl, "MODES" => $modes, "version" => $version, "MAXTIME" => $limit);
    $ok = _db_multiquery($env, false, $query, array("_db_cb_process_data"));
    if (!$ok) {
        if (!$ERROR) {
            $ERROR = "unknown sync error";
        }
        if ($debug) {
            echo "sync oops............................ {$ERROR} <br>\n";
        }
        return false;
    }
    $SYNC_STATUS[$src][$dst] = $env["MAXTIME"];
    return true;
}