xref: /PHP-7.3/ext/dba/tests/dba_handler.inc (revision 1c850bfc)
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.'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			if (dba_insert("key number 6", "The 6th value", $db_writer)) {
50				echo '"key number 6" written' . "\n";
51			} else {
52				echo 'Failed to write "key number 6"' . "\n";
53			}
54			if (dba_insert("key number 6", "The 6th value inserted again would be an error", $db_writer)) {
55				echo '"key number 6" written 2nd time' . "\n";
56			} else {
57				echo 'Failed to write "key number 6" 2nd time' . "\n";
58			}
59			dba_replace("key2", "Content 2 replaced 2nd time", $db_writer);
60			dba_delete("key4", $db_writer);
61			echo dba_fetch("key2", $db_writer)."\n";
62			echo dba_fetch("key number 6", $db_writer)."\n";
63			dba_close($db_writer); // when the writer is open at least db3 would fail because of buffered io.
64		} else {
65			die("Error reopening database\n");
66		}
67	}
68	if (($db_file = dba_open($db_filename, 'r'.$lock_flag, $handler))!==FALSE) {
69		$key = dba_firstkey($db_file);
70		$res = array();
71		while($key) {
72			$res[$key] = dba_fetch($key, $db_file);
73			$key = dba_nextkey($db_file);
74		}
75		ksort($res);
76		var_dump($res);
77		dba_close($db_file);
78	} else {
79		echo "Error reading database\n";
80	}
81	if (!empty($dba_reader)) {
82		dba_close($dba_reader);
83	}
84	if (($db_file = dba_popen($db_filename, 'r'.($lock_flag==''?'':'-'), $handler))!==FALSE) {
85		if ($handler == 'dbm' || $handler == "tcadb") {
86			dba_close($db_file);
87		}
88	}
89
90	if ($lock_flag == '') {
91		break;
92	} else {
93		echo "--NO-LOCK--\n";
94		$lock_flag = '';
95	}
96} while(1);
97
98?>
99