function _db_open(&$database, $write, $do_init = false) { global $CONN_CACHE; if (!$database) { $database = _db_maindatabase(); } // use dynamic programming for connections if (isset($CONN_CACHE[$database][$write])) { return $CONN_CACHE[$database][$write]; } global $BASEDIR; global $SYNTAX_CONFIG; global $CONFIG; global $BASENAME; global $USER; global $PASSWD; global $ERROR; global $debug; if (!@$CONFIG["CONNECTIONS"]) { // use some reasonable default if (!$BASENAME) { $BASENAME = "main"; } $CONFIG["CONNECTIONS"] = array("DEFAULT" => array("MASTER" => "localhost", "BASENAME" => $BASENAME)); } $ERROR = db_check_syntax($CONFIG, $SYNTAX_CONFIG); if ($ERROR) { print_r($CONFIG); echo "<br>\n"; die("_db_open syntax error: {$ERROR}"); return null; } $driver = _db_get_driver($database); $host = @$CONFIG["CONNECTIONS"][$database]["MASTER"]; if (!$host) { $host = "localhost"; } $basename = @$CONFIG["CONNECTIONS"][$database]["BASENAME"]; if (!$basename) { $basename = @$CONFIG["CONNECTIONS"][$database]["BASE"]; } if (!$basename) { $basename = $BASENAME; } if ($do_init) { $basename = ""; } $user = @$CONFIG["CONNECTIONS"][$database]["USER"]; if (!$user) { $user = $USER; } $passwd = @$CONFIG["CONNECTIONS"][$database]["PASSWD"]; if (!$passwd) { $passwd = $PASSWD; } if (!$write && @$CONFIG["CONNECTIONS"][$database]["SLAVES"]) { // randomly determine a mirror for reading $max = count($CONFIG["CONNECTIONS"][$database]["SLAVES"]); $idx = crc32($USER) % $max; $host = $CONFIG["CONNECTIONS"][$database]["SLAVES"][$idx]; } if ($debug) { echo "_db_open driver={$driver} host={$host}, user={$user}, passwd={$passwd}, basename={$basename}<br>\n"; } $call = "{$driver}_do_open"; $connection = $call($host, $user, $passwd, $basename); if (!$connection) { return null; } $CONN_CACHE[$database][$write] = $connection; return $connection; }
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; }