示例#1
0
/**
 * Provedení sql dotazu a předání nalezených záznamů v požadovaném tvaru
 *
 * <b>Příklady:</b><br>
 * první políčko výsledku<br>
 * <code>_executeSql('select', $sql, array('single'));</code>
 * celá tabulka do indexovaného pole<br>
 * <code>_executeSql('select', $sql, array('all'));</code>
 * celá tabulka do asociativního pole a klíčem je políčko ‚recno‘
 * <code>_executeSql('select', array($pom['sql']), array('assoc', 'recno,#,='));</code>
 * asociativního pole klíč ⇒ hodnota
 * <code>_executeSql('select', array($pom['sql']), array('pairs', 'recno', 'xmldata'));</code>
 * update, insert, delete
 * <code>_executeSql('update', $sql, array());</code>
 *
 * @param string $type 'select', 'update', 'insert', 'delete'
 * @param array $sql
 * @param array $fetch 'single', 'all', 'assoc', 'pairs'
 */
function _executeSql($type, $sql, $fetch, $upper = TRUE)
{
    //setMickaLog("SQL TYPE=$type, fetch=" . $fetch[0], 'FUNCTION', 'micka_lib_db._executeSql.start');
    $rs = FALSE;
    if (count($sql) == 0) {
        setMickaLog("empty SQL", 'ERROR', 'micka_lib_db._executeSql');
        return $sql;
    }
    if (is_array($sql) && is_array($fetch) && $type == 'select') {
        $result = dibi::query($sql);
        setMickaLog(preg_replace("/\\s{2,}/", ' ', dibi::$sql), 'SQL', 'micka_lib_db._executeSql.sql');
        //Debugger::log('[micka_lib_db._executeSql.select] ' . dibi::$sql, 'INFO');
        //Debugger::log('[micka_lib_db._executeSql.select] ' . print_r($sql, true), 'INFO');
        if ($fetch[0] == 'single') {
            $rs = trim($result->fetchSingle());
        } elseif ($fetch[0] == 'all') {
            //setMickaLog("FETCH ALL START", 'DEBUG', 'micka_lib_db._executeSql');
            $blob = 'OCI-Lob';
            //if (count($result) > 0) { // FIXME: ORACLE - nefunguje
            foreach ($result as $n => $row) {
                foreach ($row as $key => $value) {
                    if (DB_DRIVER == 'oracle') {
                        if ($value instanceof $blob) {
                            $rs[$n][$key] = $value->load();
                        } else {
                            $rs[$n][$key] = is_string($value) ? rtrim($value) : $value;
                        }
                    } else {
                        $rs[$n][strtoupper($key)] = is_string($value) ? rtrim($value) : $value;
                    }
                }
            }
            //}
            //setMickaLog("FETCH ALL END", 'DEBUG', 'micka_lib_db._executeSql');
        } elseif ($fetch[0] == 'assoc') {
            $rs = setUpperColsName($result->fetchAssoc($fetch[1]));
        } elseif ($fetch[0] == 'pairs' && count($fetch) == 3) {
            if (DB_DRIVER == 'oracle') {
                $rs = $upper ? setUpperColsName($result->fetchPairs(strtoupper($fetch[1]), strtoupper($fetch[2]))) : $result->fetchPairs(strtoupper($fetch[1]), strtoupper($fetch[2]));
            } else {
                $rs = $upper ? setUpperColsName($result->fetchPairs($fetch[1], $fetch[2])) : $result->fetchPairs($fetch[1], $fetch[2]);
            }
        }
    } elseif (is_array($sql) && ($type == 'update' || $type == 'insert' || $type == 'delete')) {
        $rs = dibi::query($sql);
        setMickaLog(preg_replace("/\\s{2,}/", ' ', dibi::$sql), 'SQL', 'micka_lib_db._executeSql.sql');
        //Debugger::log('[micka_lib_db._executeSql.update] ' . dibi::$sql, 'INFO');
    } elseif (is_array($sql) && $type == 'pxml') {
        //Debugger::log('[micka_lib_db._executeSql.pxml] ' . $sql[0], 'INFO');
        $rs = dibi::query($sql[0]);
        //Debugger::log('[micka_lib_db._executeSql.pxml] OK', 'INFO');
    } else {
        setMickaLog("unknow TYPE SQL", 'ERROR', 'micka_lib_db._executeSql');
    }
    unset($result);
    setMickaLog("", 'FUNCTION', 'micka_lib_db._executeSql.end');
    return $rs;
}
示例#2
0
 public function getData($in)
 {
     setMickaLog($in, 'FUNCTION', "MdExport.getData.start");
     $rs = array();
     $rs['paginator']['records'] = 0;
     $this->xml_from = 'summary';
     $this->setQueryIn($in);
     $in = $this->query_in;
     $pom = $this->setQuery($in);
     if ($pom == -1) {
         setMickaLog('SQL == -1', 'ERROR', 'MdExport.getData');
         // TODO: návrat chyby
         /*
         if ($this->query_status === FALSE) {
         	//my_print_r($this->query_error);
         }
         */
         return -1;
     } elseif ($pom['paginator']['records'] > 0) {
         $result = dibi::query($pom['sql']);
         $rs['data'] = DB_DRIVER == 'oracle' ? setUpperColsName($result->fetchAssoc('RECNO,#,=')) : setUpperColsName($result->fetchAssoc('recno,#,='));
         $rs['paginator'] = $pom['paginator'];
     }
     if (isset($rs['data']) && count($rs['data']) > 0) {
         $rs['data'] = $this->setData2Micka($rs['data']);
     }
     setMickaLog($rs, 'DEBUG', "MdExport.getData.return");
     return $rs;
 }
示例#3
0
 function compare($resource)
 {
     $startPosition = 1;
     $result = array();
     $this->log("SEARCHING DELETED");
     $filter = trim($resource['filter']);
     do {
         $this->clientCSW->setParams("debug=0|ElementSetName=brief|typeNames=gmd:MD_Metadata|maxRecords=25|startPosition={$startPosition}");
         $fromDate = "1900-01-01";
         $query = "modified >= '" . $fromDate . "'";
         if ($filter) {
             $query .= " AND " . $filter;
         }
         $this->clientCSW->prepareRequest($query);
         do {
             $s = $this->clientCSW->runRequest($resource['source'], "harvest", "", "", "");
             $this->log();
             $this->log('QUERY TO DELETE: ' . $query, 1);
             $this->log('Source XML:', 2);
             $this->log($s, 2);
             libxml_clear_errors();
             $this->serverCSW->xml->loadXML($s);
             $err = libxml_get_errors();
             if (count($err)) {
                 $this->log('Record saving error:');
                 $this->log($err);
                 $this->log($s);
                 if ($tryCount > 2) {
                     return array('error' => array(3, 'Source', "Invalid source at " . htmlentities($resource['source'])));
                 }
                 $tryCount++;
                 $this->log("trying to resend request {$tryCount} ...");
                 sleep(1);
             }
         } while (count($err));
         //$s = $this->clientCSW->runRequest($resource['source'], "harvest", "", "", "");
         //$this->serverCSW->xml->loadXML($s);
         $ids = $this->serverCSW->xml->getElementsByTagNameNS('http://www.isotc211.org/2005/gmd', 'fileIdentifier');
         foreach ($ids as $id) {
             //echo "nod='". trim($id->nodeValue), "'\n";
             $result[] = trim($id->nodeValue);
         }
         $res = $this->serverCSW->xml->getElementsByTagNameNS($this->serverCSW->schemas['csw'], "SearchResults");
         //$startPosition = intval($res->item(0)->getAttribute("nextRecord"));
         //$numberOfRecordsMatched = intval($res->item(0)->getAttribute("numberOfRecordsMatched"));
         //if($startPosition > $numberOfRecordsMatched) $startPosition = 0;
         if ($res->length == 0) {
             $this->log('csw:SearchResults element not found.');
             return array('error' => array(3, 'csw:SearchResults', "Invalid CSW response " . htmlentities($resource['source'])));
         }
         $numResults = intval($res->item(0)->getAttribute("numberOfRecordsMatched"));
         $numReturned = intval($res->item(0)->getAttribute("numberOfRecordsReturned"));
         if (!$numResults) {
             return $result;
         }
         if ($numReturned > 0) {
             $startPosition += $numReturned;
         }
         if ($startPosition > $numResults) {
             $startPosition = 0;
         }
         if ($this->loglevel > 1) {
             $this->log();
             $this->log('Zdrojove XML pro smazani:');
             $this->log($s);
         }
         libxml_clear_errors();
         //if(!$startPosition) exit('Nenalezen atribut: nextRecord');
     } while ($startPosition > 1);
     $this->log("vet=" . count($result));
     // --- dotaz do databaze ---
     $sql = "SELECT uuid, title FROM md WHERE create_user='******' AND server_name=%s";
     $ids = array();
     try {
         $rs = setUpperColsName(dibi::query($sql, $resource['name']));
         $records = $rs->fetchAll();
         foreach ($records as $record) {
             $uuid = trim($record['UUID']);
             $ids[] = $uuid;
             $titles[$uuid] = trim($record['TITLE']);
         }
     } catch (DibiException $e) {
         return array('error' => array(6, "Database", "{$sql} is not valid SQL request"));
     }
     // --- porovnani ---
     $diff = array_diff($result, $ids);
     $diff2 = implode("\n", $diff);
     $this->log("Chybejici vety " . count($diff) . " :");
     $this->log($diff2);
     $diff = array_diff($ids, $result);
     if (count($diff) == 0) {
         return array();
     }
     $diff1 = "'" . implode("','", $diff) . "'";
     //echo "vet=".count($result);
     //echo "db=".count($ids);
     $this->log($result, 2);
     $this->log($diff, 2);
     //var_dump($diff);
     //exit;
     //--- vymazani ---
     try {
         $sql = "DELETE FROM md_values WHERE recno IN (SELECT recno FROM md WHERE create_user='******' AND uuid IN ({$diff1}))";
         _executeSql('delete', array($sql), array('all'));
         $sql = "DELETE FROM md WHERE create_user = '******' AND uuid IN ({$diff1})";
         _executeSql('delete', array($sql), array('all'));
         //$rs = setUpperColsName(dibi::query($sql));
         $result = array();
         foreach ($diff as $uuid) {
             $result[] = array("uuid" => $uuid, "title" => $titles[$uuid], "ok" => 2);
         }
         $this->log('DELETED');
         $this->log($result);
         return $result;
     } catch (DibiException $e) {
         return array('error' => array(6, "Database", "{$sql} is not valid SQL request"));
     }
 }