| 1 | package BerkeleyDB::Easy::Cursor; | ||
| 2 | our @ISA = qw(BerkeleyDB::Cursor); | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use BerkeleyDB::Easy::Common; | ||
| 8 | use BerkeleyDB::Easy::Error; | ||
| 9 | use BerkeleyDB qw( | ||
| 10 | DB_FIRST | ||
| 11 | DB_LAST | ||
| 12 | DB_NEXT | ||
| 13 | DB_NEXT_DUP | ||
| 14 | DB_NEXT_NODUP | ||
| 15 | DB_PREV | ||
| 16 | DB_PREV_NODUP | ||
| 17 | DB_CURRENT | ||
| 18 | DB_SET | ||
| 19 | DB_SET_RANGE | ||
| 20 | DB_GET_BOTH | ||
| 21 | DB_GET_RECNO | ||
| 22 | DB_SET_RECNO | ||
| 23 | DB_AFTER | ||
| 24 | DB_BEFORE | ||
| 25 | DB_CURRENT | ||
| 26 | DB_KEYFIRST | ||
| 27 | DB_KEYLAST | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub _handle { shift->[1] } | ||
| 31 | |||
| 32 | # Each hash elem in %subs defines a wrapper specification. Look at Common.pm | ||
| 33 | # for how these work. Briefly, the key is our wrapper's name, and the value | ||
| 34 | # is an array ref with the following fields: | ||
| 35 | # | ||
| 36 | # 0 FUNC : the underlying BerkeleyDB.pm function we are wrapping | ||
| 37 | # 1 RECV : parameters to our wrapper, passed by the end user | ||
| 38 | # 2 SEND : arguments we call FUNC with, often carried thru from RECV | ||
| 39 | # 3 SUCC : what to return on success | ||
| 40 | # 4 FAIL : what to return on failure | ||
| 41 | # 5 OPTI : integer specifying optimization level | ||
| 42 | # 6 FLAG : default flag to FUNC | ||
| 43 | # | ||
| 44 | # Single-letter aliases expand as: | ||
| 45 | # | ||
| 46 | # K $key | R $return | X $x | ||
| 47 | # V $value | S $status | Y $y | ||
| 48 | # F $flags | T 1 ('True') | Z $z | ||
| 49 | # A @_ ('All') | N '' ('Nope') | U undef | ||
| 50 | |||
| 51 | my %subs = ( | ||
| 52 | c_get => ['c_get' ,[ ],[A ],[S ],[S],0, ], | ||
| 53 | c_put => ['c_put' ,[ ],[A ],[S ],[S],0, ], | ||
| 54 | c_del => ['c_del' ,[ ],[A ],[S ],[S],0, ], | ||
| 55 | c_count => ['c_count',[ ],[A ],[S ],[S],0, ], | ||
| 56 | c_close => ['c_close',[ ],[A ],[S ],[S],0, ], | ||
| 57 | first => ['c_get' ,[F ],[K,V,F],[K,V],[ ],0,DB_FIRST ], | ||
| 58 | last => ['c_get' ,[F ],[K,V,F],[K,V],[ ],0,DB_LAST ], | ||
| 59 | next => ['c_get' ,[F ],[K,V,F],[K,V],[ ],0,DB_NEXT ], | ||
| 60 | next_dup => ['c_get' ,[F ],[K,V,F],[K,V],[ ],0,DB_NEXT_DUP ], | ||
| 61 | next_nodup => ['c_get' ,[F ],[K,V,F],[K,V],[ ],0,DB_NEXT_NODUP], | ||
| 62 | prev => ['c_get' ,[F ],[K,V,F],[K,V],[ ],0,DB_PREV ], | ||
| 63 | prev_nodup => ['c_get' ,[F ],[K,V,F],[K,V],[ ],0,DB_PREV_NODUP], | ||
| 64 | current => ['c_get' ,[F ],[K,V,F],[K,V],[ ],0,DB_CURRENT ], | ||
| 65 | set => ['c_get' ,[K,F ],[K,V,F],[K,V],[ ],0,DB_SET ], | ||
| 66 | set_range => ['c_get' ,[K,F ],[K,V,F],[K,V],[ ],0,DB_SET_RANGE ], | ||
| 67 | both => ['c_get' ,[K,V,F],[K,V,F],[K,V],[ ],0,DB_GET_BOTH ], | ||
| 68 | get_recno => ['c_get' ,[F ],[K,V,F],[K,V],[ ],0,DB_GET_RECNO ], | ||
| 69 | set_recno => ['c_get' ,[K,F ],[K,V,F],[K,V],[ ],0,DB_SET_RECNO ], | ||
| 70 | after => ['c_put' ,[V,F ],[K,V,F],[T ],[ ],0,DB_AFTER ], | ||
| 71 | before => ['c_put' ,[V,F ],[K,V,F],[T ],[ ],0,DB_BEFORE ], | ||
| 72 | replace => ['c_put' ,[V,F ],[K,V,F],[T ],[ ],0,DB_CURRENT ], | ||
| 73 | keyfirst => ['c_put' ,[K,V,F],[K,V,F],[T ],[ ],0,DB_KEYFIRST ], | ||
| 74 | keylast => ['c_put' ,[K,V,F],[K,V,F],[T ],[ ],0,DB_KEYLAST ], | ||
| 75 | del => ['c_del' ,[F ],[F ],[T ],[ ],0, ], | ||
| 76 | count => ['c_count',[F ],[X,F ],[X ],[ ],0, ], | ||
| 77 | close => ['c_close',[ ],[ ],[T ],[ ],0, ], | ||
| 78 | ); | ||
| 79 | |||
| 80 | # Install the stubs | ||
| 81 | while (my ($name, $spec) = each %subs) { | ||
| 82 | __PACKAGE__->_install($name, $spec); | ||
| 83 | } | ||
| 84 | |||
| 85 | # Method aliases for naming consistency | ||
| 86 | *get_both = \&both; | ||
| 87 | *put_after = \&after; | ||
| 88 | *put_before = \&before; | ||
| 89 | *put_current = \&replace; | ||
| 90 | *put_first = \&keyfirst; | ||
| 91 | *put_last = \&keylast; | ||
| 92 | *delete = \&del; | ||
| 93 | |||
| 94 | INFO and __PACKAGE__->_info(q(Cursor.pm finished loading)); | ||
| 95 | |||
| 96 | 1; | ||
| 97 | |||
| 98 | =encoding utf8 | ||
| 99 | |||
| 100 | =head1 NAME | ||
| 101 | |||
| 102 | BerkeleyDB::Easy::Cursor - Cursor to database handle | ||
| 103 | |||
| 104 | =head1 METHODS | ||
| 105 | |||
| 106 | Most of the functionaly for BerkeleyDB cursors are crammed into a few | ||
| 107 | underlying functions, with the behavior specified by a flag. For example, to | ||
| 108 | get the next record, you call C<c_get> and provide the C<DB_NEXT> flag. In | ||
| 109 | this module, these are split out into individual wrapper methods, and the | ||
| 110 | required flag is provided for you. You can specify additional flags | ||
| 111 | and they will be OR'ed together with the default. | ||
| 112 | |||
| 113 | =head2 first | ||
| 114 | |||
| 115 | Get the first record. | ||
| 116 | |||
| 117 | ($key, $val) = $cursor->first(); | ||
| 118 | |||
| 119 | =head2 last | ||
| 120 | |||
| 121 | Get the last record. | ||
| 122 | |||
| 123 | ($key, $val) = $cursor->last(); | ||
| 124 | |||
| 125 | =head2 next | ||
| 126 | |||
| 127 | Get the next record. | ||
| 128 | |||
| 129 | ($key, $val) = $cursor->next(); | ||
| 130 | |||
| 131 | =head2 prev | ||
| 132 | |||
| 133 | Move the previous record. | ||
| 134 | |||
| 135 | ($key, $val) = $cursor->prev(); | ||
| 136 | |||
| 137 | =head2 current | ||
| 138 | |||
| 139 | Get the record at the current position. | ||
| 140 | |||
| 141 | ($key, $val) = $cursor->current(); | ||
| 142 | |||
| 143 | =head2 set | ||
| 144 | |||
| 145 | Position the cursor to the specified key. | ||
| 146 | |||
| 147 | ($key, $val) = $cursor->set($key); | ||
| 148 | |||
| 149 | =head2 after | ||
| 150 | |||
| 151 | Set the next record to the specified value. | ||
| 152 | Returns true on success. | ||
| 153 | |||
| 154 | $bool = $cursor->after($val); | ||
| 155 | |||
| 156 | =head2 before | ||
| 157 | |||
| 158 | Set the previous record to the specified value. | ||
| 159 | |||
| 160 | $bool = $cursor->before($val); | ||
| 161 | |||
| 162 | =head2 replace | ||
| 163 | |||
| 164 | Replace the record at the current position. | ||
| 165 | |||
| 166 | $bool = $cursor->replace($val); | ||
| 167 | |||
| 168 | =head2 del | ||
| 169 | |||
| 170 | Delete the current record. | ||
| 171 | |||
| 172 | $bool = $cursor->del(); | ||
| 173 | |||
| 174 | =head2 close | ||
| 175 | |||
| 176 | Close the cursor. | ||
| 177 | |||
| 178 | $bool = $cursor->close(); | ||
| 179 | |||
| 180 | =head1 BUGS | ||
| 181 | |||
| 182 | This module is functional but unfinished. | ||
| 183 | |||
| 184 | =head1 AUTHOR | ||
| 185 | |||
| 186 | Rob Schaber, C<< <robschaber at gmail.com> >> | ||
| 187 | |||
| 188 | =head1 LICENSE | ||
| 189 | |||
| 190 | Copyright 2013 Rob Schaber. | ||
| 191 | |||
| 192 | This program is free software; you can redistribute it and/or modify it | ||
| 193 | under the terms of either: the GNU General Public License as published | ||
| 194 | by the Free Software Foundation; or the Artistic License. | ||
| 195 | |||
| 196 | See L<http://dev.perl.org/licenses/> for more information. |