function MetaForeignKeys($table, $owner = false, $upper = false)
 {
     $sql = "\n\t  SELECT fum.ftblname AS lookup_table, split_part(fum.rf, ')'::text, 1) AS lookup_field,\n\t     fum.ltable AS dep_table, split_part(fum.lf, ')'::text, 1) AS dep_field\n\t  FROM (\n\t  SELECT fee.ltable, fee.ftblname, fee.consrc, split_part(fee.consrc,'('::text, 2) AS lf, \n\t    split_part(fee.consrc, '('::text, 3) AS rf\n\t  FROM (\n\t      SELECT foo.relname AS ltable, foo.ftblname,\n\t          pg_get_constraintdef(foo.oid) AS consrc\n\t      FROM (\n\t          SELECT c.oid, c.conname AS name, t.relname, ft.relname AS ftblname\n\t          FROM pg_constraint c \n\t          JOIN pg_class t ON (t.oid = c.conrelid) \n\t          JOIN pg_class ft ON (ft.oid = c.confrelid)\n\t          JOIN pg_namespace nft ON (nft.oid = ft.relnamespace)\n\t          LEFT JOIN pg_description ds ON (ds.objoid = c.oid)\n\t          JOIN pg_namespace n ON (n.oid = t.relnamespace)\n\t          WHERE c.contype = 'f'::\"char\"\n\t          ORDER BY t.relname, n.nspname, c.conname, c.oid\n\t          ) foo\n\t      ) fee) fum\n\t  WHERE fum.ltable='" . strtolower($table) . "'\n\t  ORDER BY fum.ftblname, fum.ltable, split_part(fum.lf, ')'::text, 1)\n\t  ";
     $rs = $this->Execute($sql);
     if (!$rs || $rs->EOF) {
         return false;
     }
     $a = array();
     while (!$rs->EOF) {
         if ($upper) {
             $a[strtoupper($rs->Fields('lookup_table'))][] = strtoupper(ereg_replace('"', '', $rs->Fields('dep_field') . '=' . $rs->Fields('lookup_field')));
         } else {
             $a[$rs->Fields('lookup_table')][] = ereg_replace('"', '', $rs->Fields('dep_field') . '=' . $rs->Fields('lookup_field'));
         }
         adodb_movenext($rs);
     }
     return $a;
 }
Example #2
0
 /**
  * Random access to a specific row in the recordset. Some databases do not support
  * access to previous rows in the databases (no scrolling backwards).
  *
  * @param rowNumber is the row to move to (0-based)
  *
  * @return true if there still rows available, or false if there are no more rows (EOF).
  */
 function Move($rowNumber = 0)
 {
     $this->EOF = false;
     if ($rowNumber == $this->_currentRow) {
         return true;
     }
     if ($rowNumber >= $this->_numOfRows) {
         if ($this->_numOfRows != -1) {
             $rowNumber = $this->_numOfRows - 2;
         }
     }
     if ($this->canSeek) {
         if ($this->_seek($rowNumber)) {
             $this->_currentRow = $rowNumber;
             if ($this->_fetch()) {
                 return true;
             }
         } else {
             $this->EOF = true;
             return false;
         }
     } else {
         if ($rowNumber < $this->_currentRow) {
             return false;
         }
         global $ADODB_EXTENSION;
         if ($ADODB_EXTENSION) {
             while (!$this->EOF && $this->_currentRow < $rowNumber) {
                 adodb_movenext($this);
             }
         } else {
             while (!$this->EOF && $this->_currentRow < $rowNumber) {
                 $this->_currentRow++;
                 if (!$this->_fetch()) {
                     $this->EOF = true;
                 }
             }
         }
         return !$this->EOF;
     }
     $this->fields = false;
     $this->EOF = true;
     return false;
 }
Example #3
0
function _adodb_getcount(&$zthis, $sql, $inputarr = false, $secs2cache = 0)
{
    $qryRecs = 0;
    if (!empty($zthis->_nestedSQL) || preg_match("/^\\s*SELECT\\s+DISTINCT/is", $sql) || preg_match('/\\s+GROUP\\s+BY\\s+/is', $sql) || preg_match('/\\s+UNION\\s+/is', $sql)) {
        $rewritesql = adodb_strip_order_by($sql);
        // ok, has SELECT DISTINCT or GROUP BY so see if we can use a table alias
        // but this is only supported by oracle and postgresql...
        if ($zthis->dataProvider == 'oci8') {
            // Allow Oracle hints to be used for query optimization, Chris Wrye
            if (preg_match('#/\\*+.*?\\*\\/#', $sql, $hint)) {
                $rewritesql = "SELECT " . $hint[0] . " COUNT(*) FROM (" . $rewritesql . ")";
            } else {
                $rewritesql = "SELECT COUNT(*) FROM (" . $rewritesql . ")";
            }
        } else {
            if (strncmp($zthis->databaseType, 'postgres', 8) == 0 || strncmp($zthis->databaseType, 'mysql', 5) == 0) {
                $rewritesql = "SELECT COUNT(*) FROM ({$rewritesql}) _ADODB_ALIAS_";
            } else {
                $rewritesql = "SELECT COUNT(*) FROM ({$rewritesql})";
            }
        }
    } else {
        // now replace SELECT ... FROM with SELECT COUNT(*) FROM
        if (strpos($sql, '_ADODB_COUNT') !== FALSE) {
            $rewritesql = preg_replace('/^\\s*?SELECT\\s+_ADODB_COUNT(.*)_ADODB_COUNT\\s/is', 'SELECT COUNT(*) ', $sql);
        } else {
            $rewritesql = preg_replace('/^\\s*?SELECT\\s.*?\\s+(.*?)\\s+FROM\\s/is', 'SELECT COUNT(*) FROM ', $sql);
        }
        // fix by alexander zhukov, alex#unipack.ru, because count(*) and 'order by' fails
        // with mssql, access and postgresql. Also a good speedup optimization - skips sorting!
        // also see http://phplens.com/lens/lensforum/msgs.php?id=12752
        $rewritesql = adodb_strip_order_by($rewritesql);
    }
    if (isset($rewritesql) && $rewritesql != $sql) {
        if (preg_match('/\\sLIMIT\\s+[0-9]+/i', $sql, $limitarr)) {
            $rewritesql .= $limitarr[0];
        }
        if ($secs2cache) {
            // we only use half the time of secs2cache because the count can quickly
            // become inaccurate if new records are added
            $qryRecs = $zthis->CacheGetOne($secs2cache / 2, $rewritesql, $inputarr);
        } else {
            $qryRecs = $zthis->GetOne($rewritesql, $inputarr);
        }
        if ($qryRecs !== false) {
            return $qryRecs;
        }
    }
    //--------------------------------------------
    // query rewrite failed - so try slower way...
    // strip off unneeded ORDER BY if no UNION
    if (preg_match('/\\s*UNION\\s*/is', $sql)) {
        $rewritesql = $sql;
    } else {
        $rewritesql = $rewritesql = adodb_strip_order_by($sql);
    }
    if (preg_match('/\\sLIMIT\\s+[0-9]+/i', $sql, $limitarr)) {
        $rewritesql .= $limitarr[0];
    }
    if ($secs2cache) {
        $rstest = $zthis->CacheExecute($secs2cache, $rewritesql, $inputarr);
        if (!$rstest) {
            $rstest = $zthis->CacheExecute($secs2cache, $sql, $inputarr);
        }
    } else {
        $rstest = $zthis->Execute($rewritesql, $inputarr);
        if (!$rstest) {
            $rstest = $zthis->Execute($sql, $inputarr);
        }
    }
    if ($rstest) {
        $qryRecs = $rstest->RecordCount();
        if ($qryRecs == -1) {
            global $ADODB_EXTENSION;
            // some databases will return -1 on MoveLast() - change to MoveNext()
            if ($ADODB_EXTENSION) {
                while (!$rstest->EOF) {
                    adodb_movenext($rstest);
                }
            } else {
                while (!$rstest->EOF) {
                    $rstest->MoveNext();
                }
            }
            $qryRecs = $rstest->_currentRow;
        }
        $rstest->Close();
        if ($qryRecs == -1) {
            return 0;
        }
    }
    return $qryRecs;
}
	function MoveNext()
	{
		return adodb_movenext($this);
	}
Example #5
0
function _adodb_getcount(&$zthis, $sql, $inputarr = false, $secs2cache = 0)
{
    $qryRecs = 0;
    if (preg_match("/^\\s*SELECT\\s+DISTINCT/is", $sql) || preg_match('/\\s+GROUP\\s+BY\\s+/is', $sql)) {
        // ok, has SELECT DISTINCT or GROUP BY so see if we can use a table alias
        // but this is only supported by oracle and postgresql...
        if ($zthis->dataProvider == 'oci8') {
            $rewritesql = preg_replace('/(\\sORDER\\s+BY\\s.*)/is', '', $sql);
            $rewritesql = "SELECT COUNT(*) FROM ({$rewritesql})";
        } else {
            if ($zthis->databaseType == 'postgres' || $zthis->databaseType == 'postgres7') {
                $info = $zthis->ServerInfo();
                if (substr($info['version'], 0, 3) >= 7.1) {
                    // good till version 999
                    $rewritesql = preg_replace('/(\\sORDER\\s+BY\\s.*)/is', '', $sql);
                    $rewritesql = "SELECT COUNT(*) FROM ({$rewritesql}) _ADODB_ALIAS_";
                }
            }
        }
    } else {
        // now replace SELECT ... FROM with SELECT COUNT(*) FROM
        $rewritesql = preg_replace('/^\\s*SELECT\\s.*\\s+FROM\\s/Uis', 'SELECT COUNT(*) FROM ', $sql);
        // fix by alexander zhukov, alex#unipack.ru, because count(*) and 'order by' fails
        // with mssql, access and postgresql. Also a good speedup optimization - skips sorting!
        $rewritesql = preg_replace('/(\\sORDER\\s+BY\\s.*)/is', '', $rewritesql);
    }
    if (isset($rewritesql) && $rewritesql != $sql) {
        if ($secs2cache) {
            // we only use half the time of secs2cache because the count can quickly
            // become inaccurate if new records are added
            $qryRecs = $zthis->CacheGetOne($secs2cache / 2, $rewritesql, $inputarr);
        } else {
            $qryRecs = $zthis->GetOne($rewritesql, $inputarr);
        }
        if ($qryRecs !== false) {
            return $qryRecs;
        }
    }
    //--------------------------------------------
    // query rewrite failed - so try slower way...
    // strip off unneeded ORDER BY if no UNION
    if (preg_match('/\\s*UNION\\s*/is', $sql)) {
        $rewritesql = $sql;
    } else {
        $rewritesql = preg_replace('/(\\sORDER\\s+BY\\s.*)/is', '', $sql);
    }
    $rstest =& $zthis->Execute($rewritesql, $inputarr);
    if ($rstest) {
        $qryRecs = $rstest->RecordCount();
        if ($qryRecs == -1) {
            global $ADODB_EXTENSION;
            // some databases will return -1 on MoveLast() - change to MoveNext()
            if ($ADODB_EXTENSION) {
                while (!$rstest->EOF) {
                    adodb_movenext($rstest);
                }
            } else {
                while (!$rstest->EOF) {
                    $rstest->MoveNext();
                }
            }
            $qryRecs = $rstest->_currentRow;
        }
        $rstest->Close();
        if ($qryRecs == -1) {
            return 0;
        }
    }
    return $qryRecs;
}
<?php

dl('adodb.so');
include_once 'phplens/adodb/adodb.inc.php';
$db = ADONewConnection('mysql');
$server = 'jaguar';
$user = '******';
$password = $_GET['pwd'];
$database = 'northwind';
$db->Connect($server, $user, $pwd, $database);
$rs = $db->Execute('select productname,unitsinstock from products');
while (!$rs->EOF) {
    print_r($rs->fields);
    print HTML_BR;
    adodb_movenext($rs);
    ## ADODB extension function
}
$rs->Close();
echo '<hr/>';
$rs = $db->Execute("select productname, unitsinstock from products where productid < 5");
$arr = adodb_getall($rs);
## ADOdb extension function
echo '<pre>';
print_r($arr);
echo '</pre>';
Example #7
0
 public function MoveNext()
 {
     return @adodb_movenext($this);
 }