xref: /PHP-5.4/ext/dba/tests/dba_handler.inc (revision 25de19a2)
1<?php
2
3echo "database handler: $handler\n";
4
5do {
6	if (($db_file = dba_open($db_filename, 'n'.$lock_flag, $handler))!==FALSE) {
7		dba_insert("key1", "Content String 1", $db_file);
8		dba_insert("key2", "Content String 2", $db_file);
9		dba_insert("key3", "Third Content String", $db_file);
10		dba_insert("key4", "Another Content String", $db_file);
11		dba_insert("key5", "The last content string", $db_file);
12		if ($handler != 'cdb') {
13			dba_delete("key3", $db_file);
14			dba_delete("key1", $db_file);
15		} else {
16			dba_close($db_file);
17			if (($db_file = dba_open($db_filename, 'r'.$lock_flag, $handler))===FALSE) {
18				echo "Error reopening database\n";
19			}
20		}
21		$a = dba_firstkey($db_file);
22		$i=0;
23		while($a) {
24			$a = dba_nextkey($db_file);
25			$i++;
26		}
27		echo $i;
28		for ($i=1; $i<6; $i++) {
29			echo dba_exists("key$i", $db_file) ? "Y" : "N";
30		}
31		echo "\n";
32		echo dba_fetch("key2", $db_file)."\n";
33		if ($handler != 'cdb') {
34			dba_replace("key2", "Content 2 replaced", $db_file);
35			echo dba_fetch("key2", $db_file)."\n";
36		}
37		dba_close($db_file);
38	} else {
39		echo "Error creating database\n";
40	}
41	if ($handler != 'cdb') {
42		$db_writer = dba_open($db_filename, 'c'.$lock_flag, $handler);
43		if (($dba_reader = @dba_open($db_filename, 'r'.$lock_flag.($lock_flag ? 't' : ''), $handler))===false) {
44			echo "Read during write: not allowed\n";
45		} else {
46			echo "Read during write: allowed\n";
47		}
48		if ($db_writer!==FALSE) {
49			dba_insert("key number 6", "The 6th value", $db_writer);
50			@dba_insert("key number 6", "The 6th value inserted again would be an error", $db_writer);
51			dba_replace("key2", "Content 2 replaced 2nd time", $db_writer);
52			dba_delete("key4", $db_writer);
53			echo dba_fetch("key2", $db_writer)."\n";
54			echo dba_fetch("key number 6", $db_writer)."\n";
55			dba_close($db_writer); // when the writer is open at least db3 would fail because of buffered io.
56		} else {
57			die("Error reopening database\n");
58		}
59	}
60	if (($db_file = dba_open($db_filename, 'r'.$lock_flag, $handler))!==FALSE) {
61		$key = dba_firstkey($db_file);
62		$res = array();
63		while($key) {
64			$res[$key] = dba_fetch($key, $db_file);
65			$key = dba_nextkey($db_file);
66		}
67		ksort($res);
68		var_dump($res);
69		dba_close($db_file);
70	} else {
71		echo "Error reading database\n";
72	}
73	if (!empty($dba_reader)) {
74		dba_close($dba_reader);
75	}
76	if (($db_file = dba_popen($db_filename, 'r'.($lock_flag==''?'':'-'), $handler))!==FALSE) {
77		if ($handler == 'dbm') {
78			dba_close($db_file);
79		}
80	}
81
82	if ($lock_flag == '') {
83		break;
84	} else {
85		echo "--NO-LOCK--\n";
86		$lock_flag = '';
87	}
88} while(1);
89
90?>