Database (dbm-style) abstraction layer functionsdba
These functions build the foundation for accessing Berkeley DB
style databases.
This is a general abstraction layer for several file-based databases. As
such, functionality is limited to a subset of features modern databases
such as Sleepycat Software's DB2
support. (This is not to be confused with IBM's DB2 software, which is
supported through the ODBC functions.)
The behaviour of various aspects depend on the implementation of the
underlying database. Functions such as dba_optimize
and dba_sync will do what they promise for one
database and will do nothing for others.
To add support for any of the following handlers, add the specified --with
configure switch to your PHP configure line:
Dbm is the oldest (original) type of Berkeley DB style
databases. You should avoid it, if possible. We do not support
the compatibility functions built into DB2 and gdbm, because
they are only compatible on the source code level, but cannot
handle the original dbm format. (--with-dbm)
Ndbm is a newer type and more flexible than dbm. It still has
most of the arbitrary limits of dbm (therefore it is
deprecated). (--with-ndbm)
Gdbm is the GNU database
manager. (--with-gdbm)
DB2 is Sleepycat Software's
DB2. It is described as "a programmatic toolkit that
provides high-performance built-in database support for both
standalone and client/server applications." (--with-db2)
DB3 is Sleepycat Software's
DB3. (--with-db3)
Cdb is "a fast, reliable, lightweight package for creating and
reading constant databases." It is from the author of qmail and
can be found here. Since it is
constant, we support only reading operations. (--with-cdb)
DBA example
<?php
$id = dba_open ("/tmp/test.db", "n", "db2");
if (!$id) {
echo "dba_open failed\n";
exit;
}
dba_replace ("key", "This is an example!", $id);
if (dba_exists ("key", $id)) {
echo dba_fetch ("key", $id);
dba_delete ("key", $id);
}
dba_close ($id);
?>
DBA is binary safe and does not have any arbitrary limits. It inherits all
limits set by the underlying database implementation.
All file-based databases must provide a way of setting the file
mode of a new created database, if that is possible at all. The
file mode is commonly passed as the fourth argument to
dba_open or dba_popen.
You can access all entries of a database in a linear way by using the
dba_firstkey and dba_nextkey
functions. You may not change the database while traversing it.
Traversing a database
<?php
# ...open database...
$key = dba_firstkey ($id);
while ($key != false) {
if (...) { # remember the key to perform some action later
$handle_later[] = $key;
}
$key = dba_nextkey ($id);
}
for ($i = 0; $i < count($handle_later); $i++)
dba_delete ($handle_later[$i], $id);
?>
dba_closeClose databaseDescriptionvoid dba_closeint handleDba_close closes the established database
and frees all resources specified by
handle.
handle is a database handle returned by
dba_open.
Dba_close does not return any value.
See also: dba_open and
dba_popendba_deleteDelete entry specified by keyDescriptionstring dba_deletestring keyint handledba_delete deletes the entry specified by
key from the database specified with
handle.
key is the key of the entry which is
deleted.
handle is a database handle returned by
dba_open.
dba_delete returns true or false, if the entry is
deleted or not deleted, respectively.
See also: dba_exists,
dba_fetch, dba_insert,
and dba_replace.
dba_existsCheck whether key existsDescriptionbool dba_existsstring keyint handleDba_exists checks whether the specified
key exists in the database specified by
handle.
Key is the key the check is performed for.
Handle is a database handle returned by
dba_open.
Dba_exists returns true or false, if the key is found
or not found, respectively.
See also: dba_fetch,
dba_delete, dba_insert,
and dba_replace.
dba_fetchFetch data specified by keyDescriptionstring dba_fetchstring keyint handleDba_fetch fetches the data specified by
key from the database specified with
handle.
Key is the key the data is specified by.
Handle is a database handle returned by
dba_open.
Dba_fetch returns the associated string or false, if
the key/data pair is found or not found, respectively.
See also: dba_exists,
dba_delete, dba_insert,
and dba_replace.
dba_firstkeyFetch first keyDescriptionstring dba_firstkeyint handleDba_firstkey returns the first key of the
database specified by handle and resets
the internal key pointer. This permits a linear search through
the whole database.
Handle is a database handle returned by
dba_open.
Dba_firstkey returns the key or false
depending on whether it succeeds or fails, respectively.
See also:
Dba_nextkeydba_insertInsert entryDescriptionbool dba_insertstring keystring valueint handledba_insert inserts the entry described with
key and value into the
database specified by handle. It fails, if an entry
with the same key already exists.key is the key of the entry to be inserted.value is the value to be inserted.handle is a database handle returned by
dba_open.dba_insert returns true or false, depending on
whether it succeeds of fails, respectively.
See also:
dba_existsdba_deletedba_fetchdba_replacedba_nextkeyFetch next keyDescriptionstring dba_nextkeyint handledba_nextkey returns the next key of the database
specified by handle and increments the internal
key pointer.handle is a database handle returned by
dba_open.dba_nextkey returns the key or false depending on
whether it succeeds or fails, respectively.
See also:
dba_firstkeydba_popenOpen database persistentlyDescriptionint dba_popenstring pathstring modestring handler...dba_popen establishes a persistent database instance
for path with mode using
handler.path is commonly a regular path in your filesystem.mode is "r" for read access, "w" for read/write
access to an already existing database, "c" for read/write access
and database creation if it doesn't currently exist, and "n" for
create, truncate and read/write access.handler is the name of the handler which shall be
used for accessing path. It is passed all optional
parameters given to dba_popen and can act on behalf
of them.dba_popen returns a positive handler id or false, in
the case the open is successful or fails, respectively.
See also:
dba_opendba_closedba_openOpen databaseDescriptionint dba_openstring pathstring modestring handler...dba_open establishes a database instance for
path with mode using
handler.path is commonly a regular path in your filesystem.mode is "r" for read access, "w" for read/write
access to an already existing database, "c" for read/write access
and database creation if it doesn't currently exist, and "n" for
create, truncate and read/write access.handler is the name of the handler which shall be
used for accessing path. It is passed all optional
parameters given to dba_open and can act on behalf of
them.dba_open returns a positive handler id or false, in
the case the open is successful or fails, respectively.
See also:
dba_popendba_closedba_optimizeOptimize databaseDescriptionbool dba_optimizeint handledba_optimize optimizes the underlying database
specified by handle.handle is a database handle returned by
dba_open.dba_optimize returns true or false, if the
optimization succeeds or fails, respectively.
See also:
dba_syncdba_replaceReplace or insert entryDescriptionbool dba_replacestring keystring valueint handledba_replace replaces or inserts the entry described
with key and value into the
database specified by handle.key is the key of the entry to be inserted.value is the value to be inserted.handle is a database handle returned by
dba_open.dba_replace returns true or false, depending on
whether it succeeds of fails, respectively.
See also:
dba_existsdba_deletedba_fetchdba_insertdba_syncSynchronize databaseDescriptionbool dba_syncint handledba_sync synchronizes the database specified by
handle. This will probably trigger a physical write
to disk, if supported.handle is a database handle returned by
dba_open.dba_sync returns true or false, if the
synchronization succeeds or fails, respectively.
See also:
dba_optimize