1 <?php rcs_id('$Id: DbaListSet.php,v 1.2 2001-09-19 03:24:36 wainstead Exp $');
5 function DbaListSet(&$dbh) {
9 function create_sequence($seq) {
12 if (!$dbh->exists('max_key')) {
13 // echo "initializing DbaListSet";
14 // FIXME: check to see if it's really empty?
15 $dbh->insert('max_key', 0);
18 $key = "s" . urlencode($seq);
19 assert(intval($key) == 0 && !strstr($key, ':'));
20 if (!$dbh->exists($key))
21 $dbh->insert($key, "$key:$key:");
24 function delete_sequence($seq) {
25 $key = "s" . urlencode($seq);
26 for ($i = $this->firstkey($seq); $i; $i = $next) {
27 $next = $this->next($i);
30 $this->_dbh->delete($key);
33 function firstkey($seq) {
34 $key = "s" . urlencode($seq);
35 list(, $next) = explode(':', $this->_dbh->fetch($key), 3);
39 function lastkey($seq) {
40 $key = "s" . urlencode($seq);
41 list($prev) = explode(':', $this->_dbh->fetch($key), 3);
47 list( , $next, ) = explode(':', $this->_dbh->fetch(intval($i)), 3);
52 list( $prev , , ) = explode(':', $this->_dbh->fetch(intval($i)), 3);
58 return $i && $this->_dbh->exists($i);
62 list(, , $data) = explode(':', $this->_dbh->fetch(intval($i)), 3);
66 function replace($i, $data) {
68 list($prev, $next,) = explode(':', $dbh->fetch(intval($i)), 3);
69 $dbh->replace($i, "$prev:$next:$data");
72 function insert_before($i, $data) {
74 return $this->_insert_before_nc($i, $data);
77 function insert_after($i, $data) {
79 return $this->_insert_after_nc($i, $data);
82 function append($seq, $data) {
83 $key = "s" . urlencode($seq);
84 $this->_insert_before_nc($key, $data);
87 function prepend($seq, $data) {
88 $key = "s" . urlencode($seq);
89 $this->_insert_after_nc($key, $data);
92 function _insert_before_nc($i, &$data) {
93 $newkey = $this->_new_key();
94 $old_prev = $this->_setprev($i, $newkey);
95 $this->_setnext($old_prev, $newkey);
96 $this->_dbh->insert($newkey, "$old_prev:$i:$data");
100 function _insert_after_nc($i, &$data) {
101 $newkey = $this->_new_key();
102 $old_next = $this->_setnext($i, $newkey);
103 $this->_setprev($old_next, $newkey);
104 $this->_dbh->insert($newkey, "$i:$old_next:$data");
108 function delete($i) {
110 list($prev, $next) = explode(':', $dbh->fetch(intval($i)), 3);
111 $this->_setnext($prev, $next);
112 $this->_setprev($next, $prev);
113 $dbh->delete(intval($i));
116 function _new_key() {
118 $new_key = $dbh->fetch('max_key') + 1;
119 $dbh->replace('max_key', $new_key);
123 function _setprev($i, $new_prev) {
125 list($old_prev, $next, $data) = explode(':', $dbh->fetch($i), 3);
126 $dbh->replace($i, "$new_prev:$next:$data");
130 function _setnext($i, $new_next) {
132 list($prev, $old_next, $data) = explode(':', $dbh->fetch($i), 3);
133 $dbh->replace($i, "$prev:$new_next:$data");
144 // c-hanging-comment-ender-p: nil
145 // indent-tabs-mode: nil