1 |
dpavlin |
604 |
<?php |
2 |
|
|
/* |
3 |
|
|
The Malete project - the Z39.2/Z39.50 database framework of OpenIsis. |
4 |
|
|
Version 0.9.x (patchlevel see file Version) |
5 |
|
|
Copyright (C) 2001-2004 by Erik Grziwotz, erik@openisis.org |
6 |
|
|
|
7 |
|
|
This library is free software; you can redistribute it and/or |
8 |
|
|
modify it under the terms of the GNU Lesser General Public |
9 |
|
|
License as published by the Free Software Foundation; either |
10 |
|
|
version 2.1 of the License, or (at your option) any later version. |
11 |
|
|
|
12 |
|
|
This library is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
Lesser General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU Lesser General Public |
18 |
|
|
License along with this library; if not, write to the Free Software |
19 |
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
|
21 |
|
|
see README for more information |
22 |
|
|
EOH */ |
23 |
|
|
|
24 |
|
|
// $Id: Db.php,v 1.2 2004/11/02 13:44:24 kripke Exp $ |
25 |
|
|
|
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
This class represents a "database". |
29 |
|
|
|
30 |
|
|
@version $Revision: 1.2 $ |
31 |
|
|
@license LGPL |
32 |
|
|
@package Isis |
33 |
|
|
*/ |
34 |
|
|
class Isis_Db { |
35 |
|
|
/** "field definiton table" |
36 |
|
|
simple name -> number hash (where name should be an "identifier") |
37 |
|
|
may contain some additional entries using non-identifier chars ... |
38 |
|
|
*/ |
39 |
|
|
var $fdt; |
40 |
|
|
var $name; |
41 |
|
|
var $srv; |
42 |
|
|
|
43 |
|
|
function Isis_Db ( $fdt = null, $name = null, $server = null ) |
44 |
|
|
{ |
45 |
|
|
$this->fdt = $fdt; |
46 |
|
|
$this->name = $name; |
47 |
|
|
$this->srv = $server; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
/* |
52 |
|
|
internal helper to construct and send a request |
53 |
|
|
set $this->srv->dbg to true to get full listing of what's going on |
54 |
|
|
*/ |
55 |
|
|
function req ( $type, $arg, $emb = null, $lst = null ) |
56 |
|
|
{ |
57 |
|
|
$req = new Isis_Rec(); |
58 |
|
|
$req->head = $this->name.'.'.$type; |
59 |
|
|
if ( $arg ) $req->head .= "\t$arg"; |
60 |
|
|
if ( $emb ) foreach ($emb as $r) $req->embed($r); |
61 |
|
|
if ( $lst ) foreach ($lst as $l) $req->append(0,$l); |
62 |
|
|
return $this->srv->request( $req ); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
/* |
67 |
|
|
query |
68 |
|
|
with $expr = null, fetch more results from previous query |
69 |
|
|
with $recs, fetch an array of records, else of mfns |
70 |
|
|
*/ |
71 |
|
|
function query ( $expr = null, $recs = true ) |
72 |
|
|
{ |
73 |
|
|
if ( $expr && $recs && false === strpos($expr, '?') ) |
74 |
|
|
$expr .= '?'; // force fetch records |
75 |
|
|
$ret = $this->req('Q', $expr); |
76 |
|
|
return $recs ? $ret->recs($this) : $ret->get(0); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
/* |
80 |
|
|
read one or an array of mfns |
81 |
|
|
return one or an array of records |
82 |
|
|
*/ |
83 |
|
|
function read ( $mfn ) |
84 |
|
|
{ |
85 |
|
|
if ( is_array($mfn) ) { |
86 |
|
|
$ret = $this->req('R', null, null, $mfn); |
87 |
|
|
return $ret->recs($this); |
88 |
|
|
} |
89 |
|
|
$ret = $this->req('R', null, null, array($mfn)); |
90 |
|
|
$recs = $ret->recs($this); |
91 |
|
|
return $recs[0]; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
/* |
95 |
|
|
read array of terms |
96 |
|
|
return one or an array of records |
97 |
|
|
*/ |
98 |
|
|
function terms ( $from, $to = null ) |
99 |
|
|
{ |
100 |
|
|
if (!is_null($to)) $from .= "\t$to"; |
101 |
|
|
$ret = $this->req('T', $from); |
102 |
|
|
return $ret->get(0); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
/* |
106 |
|
|
write one or an array of records |
107 |
|
|
return an array of mfns written |
108 |
|
|
*/ |
109 |
|
|
function write ( $rec ) |
110 |
|
|
{ |
111 |
|
|
if ( !is_array($rec) ) $rec = array($rec); |
112 |
|
|
$ret = $this->req('W', null, $rec); |
113 |
|
|
return $ret->get(0); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/* |
117 |
|
|
unlike the other methods, this expects $req to be a prepared X request |
118 |
|
|
however, $name.X is prepended |
119 |
|
|
return res->head, which should be a comment |
120 |
|
|
*/ |
121 |
|
|
function index ( $req ) |
122 |
|
|
{ |
123 |
|
|
$pfx = $this->name.'.X'; |
124 |
|
|
if ( $req->head ) |
125 |
|
|
$req->head = $pfx."\t".$req->head; |
126 |
|
|
else |
127 |
|
|
$req->head = $pfx; |
128 |
|
|
$res = $this->srv->request( $req ); |
129 |
|
|
return $res->head; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
} // class Isis_Db |
133 |
|
|
?> |