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; }
function _db_create_view($NEW, $alias, $qstruct) { global $SCHEMA; $oldschema = $SCHEMA; $SCHEMA = $NEW; $databases = array(); $q2 = _db_mangle_query($databases, $qstruct); if (count($databases) == 1) { $database = key($databases); $query = _db_make_query($database, $subqs, $q2); } else { $SCHEMA = $oldschema; return "/* cannot create distributed view '{$alias}' */\n"; } $SCHEMA = $oldschema; return "drop view if exists {$alias};\ncreate view {$alias} as {$query};\n\n"; }
function _db_read($qstruct) { global $ERROR; global $debug; if ($debug) { echo "_db_read raw data: "; print_r($qstruct); echo "<br>\n"; } $databases = array(); $q2 = _db_mangle_query($databases, $qstruct); // currently only 1 database supported $database = key($databases); $mainquery = _db_make_query($database, $subqs, $q2); $query = $mainquery; if ($subqs) { foreach ($subqs as $name => $tuple) { $joinfields = $tuple[0]; $subquery = $tuple[1]; if ($joinfields) { if (is_array($joinfields)) { $joinfields = implode(",", $joinfields); } $subquery = "select * from ({$mainquery}) mainquery join ({$subquery}) subquery using({$joinfields})"; } if ($debug) { $subquery = "/* alias='{$name}' joinfields='{$joinfields}' */ {$subquery}"; } $query .= "; {$subquery}"; } } $env = array("DB" => $database, "ARG" => $subqs, "CB_PROCESS" => "_db_cb_make_keys"); $ok = _db_multiquery($env, false, $query, array("_db_cb_process_data", "_db_cb_process_subdata")); if (!$ok) { if (!$ERROR) { $ERROR = "unknown retrieval error"; } if ($debug) { echo "oops............................ {$ERROR} <br>\n"; } return array(); } $res = array_shift($env["RES"]); if ($res) { $res = _db_do_datasplit($res, $env); if ($debug) { echo "got data.<br>\n"; } } return $res; }
function _db_pass_main($MYSCHEMA) { // main pass $RES = array(); $maindatabase = _db_maindatabase(); foreach ($MYSCHEMA as $table => $tdef) { $newtdef = $tdef; if (@$tdef["VIEW"]) { global $SCHEMA; $SCHEMA = $MYSCHEMA; $databases = array(); $q2 = _db_mangle_query($databases, $tdef["VIEW"]); $tdef["FIELDS"] = $q2["SCHEMA_FIELDS"]; $newtdef["ACCESS"] = "R"; $newtdef["TEMPORAL"] = false; $newtdef["TOOLS"] = array("tool_search" => true, "tool_page" => true); } if (!@$tdef["SCHEMA_CONTROL"]) { $newtdef["SCHEMA_CONTROL"] = true; } if (!@$tdef["REALNAME"]) { $newtdef["REALNAME"] = $table; } if (!($singular = @$tdef["SINGULAR"])) { $singular = _db_singular($table, $MYSCHEMA); $newtdef["SINGULAR"] = $singular; } if (!($primary = @$tdef["PRIMARY"])) { $primary = _db_primary($table, $MYSCHEMA); $newtdef["PRIMARY"] = $primary; } if (!@$tdef["DB"]) { $newtdef["DB"] = $maindatabase; } $newfields = array(); foreach ($tdef["FIELDS"] as $field => $fdef) { if (!@$fdef["REALNAME"]) { $fdef["REALNAME"] = $field; } if (isset($fdef["REFERENCES"])) { $assoc = $fdef["REFERENCES"]; foreach ($assoc as $foreign => $props) { $all = preg_split("/\\s*\\.\\s*/s", $foreign, 2); $ftable = $all[0]; $ffield = $all[1]; if (!isset($MYSCHEMA[$ftable])) { die("REFERENCES: foreign table '{$ftable}' does not exist"); } if (!isset($MYSCHEMA[$ftable]["FIELDS"][$ffield])) { die("REFERENCES: foreign field '{$ffield}' of table '{$ftable}' does not exist"); } // the follwing will not work for cyclic references, deliberately $RES[$ftable]["XREF"][$ffield][] = array($table, $field, $props); } } $newfields[$field] = $fdef; } //echo "<br>newfields: "; print_r($newfields); echo "<br>\n"; $newtdef["FIELDS"] = $newfields; $MYSCHEMA[$table] = $newtdef; $RES[$table] = $newtdef; } return $RES; }