2 rcs_id('$Id: ADODB.php,v 1.2 2002-02-02 22:50:24 lakka Exp $');
4 /*This file is part of PhpWiki.
6 PhpWiki is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 PhpWiki is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with PhpWiki; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 // Based on PearDB.php
24 // 1) ADODB's GetRow() is slightly different from that in PEAR. It does not accept a fetchmode parameter
25 // That doesn't matter too much here, since we only ever use FETCHMODE_ASSOC
27 // 2) No need for ''s arond strings in sprintf arguments - qstr puts them there automatically
29 // 3) ADODB has a version of GetOne, but it is difficult to use it when FETCH_ASSOC is in effect.
30 // Instead, use $rs = Execute($query); $value = $rs->fields["$colname"]
31 // 4) No error handling yet - could use ADOConnection->raiseErrorFn
33 //require_once('DB.php');
34 require_once('lib/WikiDB/backend.php');
35 //require_once('lib/FileFinder.php');
36 //require_once('lib/ErrorManager.php');
37 // Error handling - calls trigger_error. NB - does not close the connection. Does it need to?
38 include_once('lib/WikiDB/adodb/adodb-errorhandler.inc.php');
39 // include the main adodb file
40 require_once('lib/WikiDB/adodb/adodb.inc.php');
42 class WikiDB_backend_ADODB
43 extends WikiDB_backend
45 function WikiDB_backend_ADODB ($dbparams) {
46 // Find and include PEAR's DB.php.
47 //$pearFinder = new PearFileFinder;
48 //$pearFinder->includeOnce('DB.php');
50 // Install filter to handle bogus error notices from buggy DB.php's.
51 // global $ErrorManager;
52 // $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_pear_notice_filter'));
54 // Open connection to database
55 // $this->_dsn = $dbparams['dsn'];
56 /* $dboptions = array('persistent' => true,
58 $this->_dbh = DB::connect($this->_dsn, $dboptions);
60 if (DB::isError($dbh)) {
61 trigger_error(sprintf("Can't connect to database: %s",
62 $this->_pear_error_message($dbh)),
65 $dbh->setErrorHandling(PEAR_ERROR_CALLBACK,
66 array($this, '_pear_error_callback'));
67 $dbh->setFetchMode(ADODB_FETCH_ASSOC);
69 $parsed = parseDSN($dbparams['dsn']);
70 $this->_dbh = &ADONewConnection($parsed['phptype']); // Probably only MySql works just now
71 $conn = $this->_dbh->PConnect($parsed['hostspec'],$parsed['username'],
72 $parsed['password'], $parsed['database']);
74 // Error handling not needed here -all dealt with by adodb-errorhandler.inc.php
75 /* if ($conn === false) {
76 trigger_error(sprintf("Can't connect to database: %s",
77 $this->_pear_error_message($conn)),
82 // Uncomment the following line to enable debugging output (not very pretty!)
83 // $this->_dbh->debug = true;
85 $GLOBALS['ADODB_FETCH_MODE'] = ADODB_FETCH_ASSOC;
87 // The next line should speed up queries if enabled, but:
88 // 1) It only works with PHP >= 4.0.6; and
89 // 2) At the moment, I haven't figured out why thw wrong results are returned'
90 // $GLOBALS['ADODB_COUNTRECS'] = false;
92 $prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
95 = array('page_tbl' => $prefix . 'page',
96 'version_tbl' => $prefix . 'version',
97 'link_tbl' => $prefix . 'link',
98 'recent_tbl' => $prefix . 'recent',
99 'nonempty_tbl' => $prefix . 'nonempty');
102 = array('maxmajor' => "MAX(CASE WHEN minor_edit=0 THEN version END)",
103 'maxminor' => "MAX(CASE WHEN minor_edit<>0 THEN version END)",
104 'maxversion' => "MAX(version)");
106 $this->_lock_count = 0;
110 * Close database connection.
115 if ($this->_lock_count) {
116 trigger_error( "WARNING: database still locked " . '(lock_count = $this->_lock_count)' . "\n<br />",
119 // $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT); // prevent recursive loops.
120 $this->unlock('force');
122 $this->_dbh->close();
128 * Test fast wikipage.
130 function is_wiki_page($pagename) {
132 extract($this->_table_names);
133 $rs = $dbh->Execute(sprintf("SELECT $page_tbl.id AS id"
134 . " FROM $nonempty_tbl, $page_tbl"
135 . " WHERE $nonempty_tbl.id=$page_tbl.id"
136 . " AND pagename=%s",
137 $dbh->qstr($pagename)));
140 $result = $rs->fields["id"];
151 function get_all_pagenames() {
153 extract($this->_table_names);
154 //return $dbh->getCol("SELECT pagename"
155 // . " FROM $nonempty_tbl, $page_tbl"
156 // . " WHERE $nonempty_tbl.id=$page_tbl.id");
159 //Original code (above) return the column in an indexed array - 0 based
160 //So, hopefully, does this
161 $result = $dbh->Execute("SELECT pagename"
162 . " FROM $nonempty_tbl, $page_tbl"
163 . " WHERE $nonempty_tbl.id=$page_tbl.id");
164 return $result->GetArray();
168 * Read page information from database.
170 function get_pagedata($pagename) {
172 $page_tbl = $this->_table_names['page_tbl'];
174 //trigger_error("GET_PAGEDATA $pagename", E_USER_NOTICE);
176 $result = $dbh->GetRow(sprintf("SELECT * FROM $page_tbl WHERE pagename=%s",
177 $dbh->qstr($pagename)));
180 return $this->_extract_page_data($result);
183 function _extract_page_data(&$query_result) {
184 extract($query_result);
185 $data = empty($pagedata) ? array() : unserialize($pagedata);
186 $data['hits'] = $hits;
190 function update_pagedata($pagename, $newdata) {
192 $page_tbl = $this->_table_names['page_tbl'];
194 // Hits is the only thing we can update in a fast manner.
195 if (count($newdata) == 1 && isset($newdata['hits'])) {
196 // Note that this will fail silently if the page does not
197 // have a record in the page table. Since it's just the
198 // hit count, who cares?
199 $dbh->Execute(sprintf("UPDATE $page_tbl SET hits=%d WHERE pagename=%s",
200 $newdata['hits'], $dbh->qstr($pagename)));
205 $data = $this->get_pagedata($pagename);
208 $this->_get_pageid($pagename, true); // Creates page record
211 @$hits = (int)$data['hits'];
212 unset($data['hits']);
214 foreach ($newdata as $key => $val) {
217 else if (empty($val))
223 $dbh->Execute(sprintf("UPDATE $page_tbl"
224 . " SET hits=%d, pagedata=%s"
225 . " WHERE pagename=%s",
227 $dbh->qstr(serialize($data)),
228 $dbh->qstr($pagename)));
233 function _get_pageid($pagename, $create_if_missing = false) {
236 $page_tbl = $this->_table_names['page_tbl'];
238 $query = sprintf("SELECT id FROM $page_tbl WHERE pagename=%s",
239 $dbh->qstr($pagename));
241 if (!$create_if_missing)
243 $rs = $dbh->Execute($query);
244 return $rs->fields['id'];
247 $rs = $dbh->Execute($query);
248 $id = $rs->fields['id'];
250 // kludge necessary because an assoc array is returned with a reserved name as the key
251 $rs = $dbh->Execute("SELECT MAX(id) AS M FROM $page_tbl");
252 $id = $rs->fields['M'] + 1;
253 $dbh->Execute(sprintf("INSERT INTO $page_tbl"
254 . " (id,pagename,hits)"
255 . " VALUES (%d,%s,0)",
256 $id, $dbh->qstr($pagename)));
262 function get_latest_version($pagename) {
264 extract($this->_table_names);
265 $rs = $dbh->Execute(sprintf("SELECT latestversion"
266 . " FROM $page_tbl, $recent_tbl"
267 . " WHERE $page_tbl.id=$recent_tbl.id"
268 . " AND pagename=%s",
269 $dbh->qstr($pagename)));
270 return (int)$rs->fields['latestversion'];
273 function get_previous_version($pagename, $version) {
275 extract($this->_table_names);
276 //Use SELECTLIMIT for maximum portability
277 $rs = $dbh->SelectLimit(sprintf("SELECT version"
278 . " FROM $version_tbl, $page_tbl"
279 . " WHERE $version_tbl.id=$page_tbl.id"
281 . " AND version < %d"
282 . " ORDER BY version DESC"
283 ,$dbh->qstr($pagename),
286 return (int)$rs->fields['version'];
292 * @param $version int Which version to get.
294 * @return hash The version data, or false if specified version does not
297 function get_versiondata($pagename, $version, $want_content = false) {
299 extract($this->_table_names);
301 assert(!empty($pagename));
302 assert($version > 0);
304 //trigger_error("GET_REVISION $pagename $version $want_content", E_USER_NOTICE);
305 // FIXME: optimization: sometimes don't get page data?
311 $fields = ("$page_tbl.*,"
312 . "mtime,minor_edit,versiondata,"
313 . "content<>'' AS have_content");
315 // removed ref to FETCH_MODE in next line
316 $result = $dbh->GetRow(sprintf("SELECT $fields"
317 . " FROM $page_tbl, $version_tbl"
318 . " WHERE $page_tbl.id=$version_tbl.id"
321 $dbh->qstr($pagename), $version));
323 return $this->_extract_version_data($result);
326 function _extract_version_data(&$query_result) {
330 extract($query_result);
331 $data = empty($versiondata) ? array() : unserialize($versiondata);
333 $data['mtime'] = $mtime;
334 $data['is_minor_edit'] = !empty($minor_edit);
337 $data['%content'] = $content;
338 elseif ($have_content)
339 $data['%content'] = true;
341 $data['%content'] = '';
343 // FIXME: this is ugly.
344 if (isset($pagename)) {
345 // Query also includes page data.
346 // We might as well send that back too...
347 $data['%pagedata'] = $this->_extract_page_data($query_result);
355 * Create a new revision of a page.
357 function set_versiondata($pagename, $version, $data) {
359 $version_tbl = $this->_table_names['version_tbl'];
361 $minor_edit = (int) !empty($data['is_minor_edit']);
362 unset($data['is_minor_edit']);
364 $mtime = (int)$data['mtime'];
365 unset($data['mtime']);
366 assert(!empty($mtime));
368 @$content = (string) $data['%content'];
369 unset($data['%content']);
371 unset($data['%pagedata']);
374 $id = $this->_get_pageid($pagename, true);
376 // FIXME: optimize: mysql can do this with one REPLACE INTO (I think).
377 $dbh->Execute(sprintf("DELETE FROM $version_tbl"
378 . " WHERE id=%d AND version=%d",
381 $dbh->Execute(sprintf("INSERT INTO $version_tbl"
382 . " (id,version,mtime,minor_edit,content,versiondata)"
383 . " VALUES(%d,%d,%d,%d,%s,%s)",
384 $id, $version, $mtime, $minor_edit,
385 $dbh->qstr($content),
386 $dbh->qstr(serialize($data))));
388 $this->_update_recent_table($id);
389 $this->_update_nonempty_table($id);
395 * Delete an old revision of a page.
397 function delete_versiondata($pagename, $version) {
399 extract($this->_table_names);
402 if ( ($id = $this->_get_pageid($pagename)) ) {
403 $dbh->Execute("DELETE FROM $version_tbl"
404 . " WHERE id=$id AND version=$version");
405 $this->_update_recent_table($id);
406 // This shouldn't be needed (as long as the latestversion
407 // never gets deleted.) But, let's be safe.
408 $this->_update_nonempty_table($id);
414 * Delete page from the database.
416 function delete_page($pagename) {
418 extract($this->_table_names);
421 if ( ($id = $this->_get_pageid($pagename, 'id')) ) {
422 $dbh->Execute("DELETE FROM $version_tbl WHERE id=$id");
423 $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id");
424 $dbh->Execute("DELETE FROM $nonempty_tbl WHERE id=$id");
425 $dbh->Execute("DELETE FROM $link_tbl WHERE linkfrom=$id");
426 $rs = $dbh->Execute("SELECT COUNT(*) AS C FROM $link_tbl WHERE linkto=$id");
427 $nlinks = $rs->fields['C'];
429 // We're still in the link table (dangling link) so we can't delete this
431 $dbh->Execute("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
434 $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id");
436 $this->_update_recent_table();
437 $this->_update_nonempty_table();
443 // The only thing we might be interested in updating which we can
444 // do fast in the flags (minor_edit). I think the default
445 // update_versiondata will work fine...
446 //function update_versiondata($pagename, $version, $data) {
449 function set_links($pagename, $links) {
450 // Update link table.
451 // FIXME: optimize: mysql can do this all in one big INSERT.
454 extract($this->_table_names);
457 $pageid = $this->_get_pageid($pagename, true);
459 $dbh->Execute("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
462 foreach($links as $link) {
463 if (isset($linkseen[$link]))
465 $linkseen[$link] = true;
466 $linkid = $this->_get_pageid($link, true);
467 $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto)"
468 . " VALUES ($pageid, $linkid)");
475 * Find pages which link to or are linked from a page.
477 function get_links($pagename, $reversed = true) {
479 extract($this->_table_names);
482 list($have,$want) = array('linkee', 'linker');
484 list($have,$want) = array('linker', 'linkee');
486 $qpagename = $dbh->qstr($pagename);
487 // removed ref to FETCH_MODE in next line
488 $result = $dbh->Execute("SELECT $want.*"
489 . " FROM $link_tbl, $page_tbl AS linker, $page_tbl AS linkee"
490 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
491 . " AND $have.pagename='$qpagename'"
492 //. " GROUP BY $want.id"
493 . " ORDER BY $want.pagename");
495 return new WikiDB_backend_ADODB_iter($this, $result);
498 function get_all_pages($include_deleted) {
500 extract($this->_table_names);
502 if ($include_deleted) {
503 $result = $dbh->Execute("SELECT * FROM $page_tbl ORDER BY pagename");
506 $result = $dbh->Execute("SELECT $page_tbl.*"
507 . " FROM $nonempty_tbl, $page_tbl"
508 . " WHERE $nonempty_tbl.id=$page_tbl.id"
509 . " ORDER BY pagename");
512 return new WikiDB_backend_ADODB_iter($this, $result);
518 function text_search($search = '', $fullsearch = false) {
520 extract($this->_table_names);
522 $table = "$nonempty_tbl, $page_tbl";
523 $join_clause = "$nonempty_tbl.id=$page_tbl.id";
524 $fields = "$page_tbl.*";
525 $callback = new WikiMethodCb($this, '_sql_match_clause');
528 $table .= ", $recent_tbl";
529 $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
531 $table .= ", $version_tbl";
532 $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
534 $fields .= ",$version_tbl.*";
535 $callback = new WikiMethodCb($this, '_fullsearch_sql_match_clause');
538 $search_clause = $search->makeSqlClause($callback);
540 $result = $dbh->Execute("SELECT $fields FROM $table"
541 . " WHERE $join_clause"
542 . " AND ($search_clause)"
543 . " ORDER BY pagename");
545 return new WikiDB_backend_ADODB_iter($this, $result);
548 function _sql_match_clause($word) {
549 $word = preg_replace('/(?=[%_\\\\])/', "\\", $word); //not sure if we need this. ADODB may do it for us
550 $word = $this->_dbh->qstr($word, get_magic_quotes_runtime());
551 return "LOWER(pagename) LIKE %$word%";
554 function _fullsearch_sql_match_clause($word) {
555 $word = preg_replace('/(?=[%_\\\\])/', "\\", $word); //not sure if we need this
556 $word = $this->_dbh->qstr($word, get_magic_quotes_runtime());
557 return "LOWER(pagename) LIKE %$word% OR content LIKE %$word%";
561 * Find highest hit counts.
563 function most_popular($limit) {
565 extract($this->_table_names);
567 $limit = $limit ? $limit : -1;
568 $result = $dbh->SelectLimit("SELECT $page_tbl.*"
569 . " FROM $nonempty_tbl, $page_tbl"
570 . " WHERE $nonempty_tbl.id=$page_tbl.id"
571 . " ORDER BY hits DESC"
574 return new WikiDB_backend_ADODB_iter($this, $result);
578 * Find recent changes.
580 function most_recent($params) {
583 $include_minor_revisions = false;
584 $exclude_major_revisions = false;
585 $include_all_revisions = false;
589 extract($this->_table_names);
593 $pick[] = "mtime >= $since";
595 if ($include_all_revisions) {
596 // Include all revisions of each page.
597 $table = "$page_tbl, $version_tbl";
598 $join_clause = "$page_tbl.id=$version_tbl.id";
600 if ($exclude_major_revisions) {
601 // Include only minor revisions
602 $pick[] = "minor_edit <> 0";
604 elseif (!$include_minor_revisions) {
605 // Include only major revisions
606 $pick[] = "minor_edit = 0";
610 $table = "$page_tbl, $recent_tbl";
611 $join_clause = "$page_tbl.id=$recent_tbl.id";
612 $table .= ", $version_tbl";
613 $join_clause .= " AND $version_tbl.id=$page_tbl.id";
615 if ($exclude_major_revisions) {
616 // Include only most recent minor revision
617 $pick[] = 'version=latestminor';
619 elseif (!$include_minor_revisions) {
620 // Include only most recent major revision
621 $pick[] = 'version=latestmajor';
624 // Include only the latest revision (whether major or minor).
625 $pick[] ='version=latestversion';
629 $limit = $limit ? $limit : -1;
630 $where_clause = $join_clause;
632 $where_clause .= " AND " . join(" AND ", $pick);
634 // FIXME: use SQL_BUFFER_RESULT for mysql?
635 //Use SELECTLIMIT for portability
636 $result = $dbh->SelectLimit("SELECT $page_tbl.*,$version_tbl.*"
638 . " WHERE $where_clause"
639 . " ORDER BY mtime DESC",
642 return new WikiDB_backend_ADODB_iter($this, $result);
645 function _update_recent_table($pageid = false) {
647 extract($this->_table_names);
648 extract($this->_expressions);
650 $pageid = (int)$pageid;
654 $dbh->Execute("DELETE FROM $recent_tbl"
655 . ( $pageid ? " WHERE id=$pageid" : ""));
657 $dbh->Execute( "INSERT INTO $recent_tbl"
658 . " (id, latestversion, latestmajor, latestminor)"
659 . " SELECT id, $maxversion, $maxmajor, $maxminor"
660 . " FROM $version_tbl"
661 . ( $pageid ? " WHERE id=$pageid" : "")
666 function _update_nonempty_table($pageid = false) {
668 extract($this->_table_names);
670 $pageid = (int)$pageid;
674 $dbh->Execute("DELETE FROM $nonempty_tbl"
675 . ( $pageid ? " WHERE id=$pageid" : ""));
677 $dbh->Execute("INSERT INTO $nonempty_tbl (id)"
678 . " SELECT $recent_tbl.id"
679 . " FROM $recent_tbl, $version_tbl"
680 . " WHERE $recent_tbl.id=$version_tbl.id"
681 . " AND version=latestversion"
683 . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
690 * Grab a write lock on the tables in the SQL database.
692 * Calls can be nested. The tables won't be unlocked until
693 * _unlock_database() is called as many times as _lock_database().
697 function lock($write_lock = true) {
698 if ($this->_lock_count++ == 0)
699 $this->_lock_tables($write_lock);
703 * Actually lock the required tables.
705 function _lock_tables($write_lock) {
706 trigger_error("virtual", E_USER_ERROR);
710 * Release a write lock on the tables in the SQL database.
714 * @param $force boolean Unlock even if not every call to lock() has been matched
715 * by a call to unlock().
717 * @see _lock_database
719 function unlock($force = false) {
720 if ($this->_lock_count == 0)
722 if (--$this->_lock_count <= 0 || $force) {
723 $this->_unlock_tables();
724 $this->_lock_count = 0;
729 * Actually unlock the required tables.
731 function _unlock_tables($write_lock) {
732 trigger_error("virtual", E_USER_ERROR);
736 * Callback for PEAR (DB) errors.
740 * @param A PEAR_error object.
742 /* function _pear_error_callback($error) {
743 if ($this->_is_false_error($error))
746 $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT); // prevent recursive loops.
748 trigger_error($this->_pear_error_message($error), E_USER_ERROR);
752 * Detect false errors messages from PEAR DB.
754 * The version of PEAR DB which ships with PHP 4.0.6 has a bug in that
755 * it doesn't recognize "LOCK" and "UNLOCK" as SQL commands which don't
756 * return any data. (So when a "LOCK" command doesn't return any data,
757 * DB reports it as an error, when in fact, it's not.)
760 * @return bool True iff error is not really an error.
762 /* function _is_false_error($error) {
763 if ($error->getCode() != DB_ERROR)
766 $query = $this->_dbh->last_query;
768 if (! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE'
769 . '|DROP|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s/', $query)) {
770 // Last query was not of the sort which doesn't return any data.
771 //" <--kludge for brain-dead syntax coloring
775 if (! in_array('ismanip', get_class_methods('DB'))) {
776 // Pear shipped with PHP 4.0.4pl1 (and before, presumably)
777 // does not have the DB::isManip method.
781 if (DB::isManip($query)) {
782 // If Pear thinks it's an isManip then it wouldn't have thrown
783 // the error we're testing for....
790 /* function _pear_error_message($error) {
791 $class = get_class($this);
792 $message = "$class: fatal database error\n"
793 . "\t" . $error->getMessage() . "\n"
794 . "\t(" . $error->getDebugInfo() . ")\n";
796 // Prevent password from being exposed during a connection error
797 $safe_dsn = preg_replace('| ( :// .*? ) : .* (?=@) |xs',
798 '\\1:XXXXXXXX', $this->_dsn);
799 return str_replace($this->_dsn, $safe_dsn, $message);
803 * Filter PHP errors notices from PEAR DB code.
805 * The PEAR DB code which ships with PHP 4.0.6 produces spurious
806 * errors and notices. This is an error callback (for use with
807 * ErrorManager which will filter out those spurious messages.)
808 * @see _is_false_error, ErrorManager
811 /* function _pear_notice_filter($err) {
812 return ( $err->isNotice()
813 && preg_match('|DB[/\\\\]common.php$|', $err->errfile)
814 && $err->errline == 126
815 && preg_match('/Undefined offset: +0\b/', $err->errstr) );
820 class WikiDB_backend_ADODB_iter
821 extends WikiDB_backend_iterator
823 function WikiDB_backend_ADODB_iter(&$backend, &$query_result) {
824 // ADODB equivalent of this? May not matter, since we should never get here
825 /* if (DB::isError($query_result)) {
826 // This shouldn't happen, I thought.
827 $backend->_pear_error_callback($query_result);
830 $this->_backend = &$backend;
831 $this->_result = $query_result;
835 $backend = &$this->_backend;
839 // $record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
840 $record = $this->_result->fields;
841 $this->_result->MoveNext();
842 if ($this->_result->EOF) {
847 $pagedata = $backend->_extract_page_data($record);
848 $rec = array('pagename' => $record['pagename'],
849 'pagedata' => $pagedata);
851 if (!empty($record['version'])) {
852 $rec['versiondata'] = $backend->_extract_version_data($record);
853 $rec['version'] = $record['version'];
860 if ($this->_result) {
861 // $this->_result->free();
862 $this->_result->Close();
863 $this->_result = false;
868 // Following function taken from adodb-pear.inc.php. Eventually, change index.php to provide the relevant information
871 * Parse a data source name
873 * @param $dsn string Data Source Name to be parsed
875 * @return array an associative array with the following keys:
877 * phptype: Database backend used in PHP (mysql, odbc etc.)
878 * dbsyntax: Database used with regards to SQL syntax etc.
879 * protocol: Communication protocol to use (tcp, unix etc.)
880 * hostspec: Host specification (hostname[:port])
881 * database: Database to use on the DBMS server
882 * username: User name for login
883 * password: Password for login
885 * The format of the supplied DSN is in its fullest form:
887 * phptype(dbsyntax)://username:password@protocol+hostspec/database
889 * Most variations are allowed:
891 * phptype://username:password@protocol+hostspec:110//usr/db_file.db
892 * phptype://username:password@hostspec/database_name
893 * phptype://username:password@hostspec
894 * phptype://username@hostspec
895 * phptype://hostspec/database
900 * @author Tomas V.V.Cox <cox@idecnet.com>
902 function parseDSN($dsn)
904 if (is_array($dsn)) {
918 // Find phptype and dbsyntax
919 if (($pos = strpos($dsn, '://')) !== false) {
920 $str = substr($dsn, 0, $pos);
921 $dsn = substr($dsn, $pos + 3);
927 // Get phptype and dbsyntax
928 // $str => phptype(dbsyntax)
929 if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
930 $parsed['phptype'] = $arr[1];
931 $parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
933 $parsed['phptype'] = $str;
934 $parsed['dbsyntax'] = $str;
941 // Get (if found): username and password
942 // $dsn => username:password@protocol+hostspec/database
943 if (($at = strpos($dsn,'@')) !== false) {
944 $str = substr($dsn, 0, $at);
945 $dsn = substr($dsn, $at + 1);
946 if (($pos = strpos($str, ':')) !== false) {
947 $parsed['username'] = urldecode(substr($str, 0, $pos));
948 $parsed['password'] = urldecode(substr($str, $pos + 1));
950 $parsed['username'] = urldecode($str);
954 // Find protocol and hostspec
955 // $dsn => protocol+hostspec/database
956 if (($pos=strpos($dsn, '/')) !== false) {
957 $str = substr($dsn, 0, $pos);
958 $dsn = substr($dsn, $pos + 1);
964 // Get protocol + hostspec
965 // $str => protocol+hostspec
966 if (($pos=strpos($str, '+')) !== false) {
967 $parsed['protocol'] = substr($str, 0, $pos);
968 $parsed['hostspec'] = urldecode(substr($str, $pos + 1));
970 $parsed['hostspec'] = urldecode($str);
976 $parsed['database'] = $dsn;
984 // (c-file-style: "gnu")
989 // c-hanging-comment-ender-p: nil
990 // indent-tabs-mode: nil