NoSQL.pm

filter: Statementstmt 100% 16 / 16 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 50.0% 1 / 2 totaltotal 95.6% 22 / 23 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« NoSQLite.pm ↑ Summary
1 package DBIx::NoSQL;
2 {
3 8 $DBIx::NoSQL::VERSION = '0.0020';
4 }
5 # ABSTRACT: NoSQL-ish overlay for an SQL database
6  
7 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use DBIx::NoSQL::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 3 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 3 return DBIx::NoSQL::Store->new( @_ );
15 }
16  
17 sub connect {
18 5 ▶ my $class = shift;
Subroutine connect: called CC=1 SCAR=0.0
Pod documented
19 5 return DBIx::NoSQL::Store->connect( @_ );
20 }
21  
22 8 1;
23  
24  
25  
26 =pod
27  
28 =head1 NAME
29  
30 DBIx::NoSQL - NoSQL-ish overlay for an SQL database
31  
32 =head1 VERSION
33  
34 version 0.0020
35  
36 =head1 SYNOPSIS
37  
38 use DBIx::NoSQL;
39  
40 my $store = DBIx::NoSQL->connect( 'store.sqlite' );
41  
42 $store->set( 'Artist' => 'Smashing Pumpkins' => {
43 name => 'Smashing Pumpkins',
44 genre => 'rock',
45 website => 'smashingpumpkins.com',
46 } );
47  
48 $store->exists( 'Artist' => 'Smashing Pumpkins' ); # 1
49  
50 $store->set( 'Artist' => 'Tool' => {
51 name => 'Tool',
52 genre => 'rock',
53 } );
54  
55 $store->search( 'Artist' )->count; # 2
56  
57 my $artist = $store->get( 'Artist' => 'Smashing Pumpkins' );
58  
59 # Set up a (searchable) index on the name field
60 $store->model( 'Artist' )->index( 'name' );
61 $store->model( 'Artist' )->reindex;
62  
63 for $artist ( $store->search( 'Artist' )->order_by( 'name DESC' )->all ) {
64 ...
65 }
66  
67 $store->model( 'Album' )->index( 'released' => ( isa => 'DateTime' ) );
68  
69 $store->set( 'Album' => 'Siamese Dream' => {
70 artist => 'Smashing Pumpkins',
71 released => DateTime->new( ... ),
72 } );
73  
74 my $album = $store->get( 'Album' => 'Siamese Dream' );
75 my $released = $album->{ released }; # The field is automatically inflated
76 print $release->strftime( ... );
77  
78 =head1 DESCRIPTION
79  
80 DBIx::NoSQL is a layer over DBI that presents a NoSQLish way to store and retrieve data. It does this by using a table called C<__Store__>. Once connected to a database, it will detect if this table is missing and create it if necessary
81  
82 When writing data to the store, the data (a HASH reference) is first serialized using L<JSON> and then inserted/updated via L<DBIx::Class> to (currently) an SQLite backend
83  
84 Retrieving data from the store is done by key lookup or by searching an SQL-based index. Once found, the data is deserialized via L<JSON> and returned
85  
86 The API is fairly sane, though still beta
87  
88 =head1 USAGE
89  
90 =head2 $store = DBIx::NoSQL->connect( $path )
91  
92 Returns a new DBIx::NoSQL store connected to the SQLite database located at C<$path>
93  
94 If the SQLite database file at C<$path> does not exist, it will be created
95  
96 =head2 $store->set( $model, $key, $value )
97  
98 Set C<$key> (a string) to C<$value> (a HASH reference) in C<$model>
99  
100 If C<$model> has index, this command will also update the index entry corresponding to C<$key>
101  
102 =head2 $value = $store->exists( $model, $key )
103  
104 Returns true if some data for C<$key> is present in C<$model>
105  
106 =head2 $value = $store->get( $model, $key )
107  
108 Get C<$value> matching C<$key> in C<$model>
109  
110 =head2 $value = $store->delete( $model, $key )
111  
112 Delete the entry matching C<$key> in C<$model>
113  
114 If C<$model> has index, this command will also delete the index entry corresponding to C<$key>
115  
116 =head2 $store->reindex
117  
118 Reindex the searchable/orderable data in C<$store>
119  
120 This method is smart, in that it won't reindex a model unless the schema for $store is different/has changed. That is, if the schema for C<$store> is the same as it is in the database, this call will do nothing
121  
122 Refer to "Model USAGE" below for more information
123  
124 =head2 $store->dbh
125  
126 Return the L<DBI> database handle for the store, if you need/want to do your own thing
127  
128 =head1 Search USAGE
129  
130 To search on a model, you must have installed an index on the field you want to search on
131  
132 Refer to "Model USAGE" for indexing information
133  
134 =head2 $search = $store->search( $model, [ $where ] )
135  
136 $search = $store->search( 'Artist' => { name => { -like => 'Smashing%' } } )
137  
138 Return a L<DBIx::NoSQL::Search> object for C<$model>, filtering on the optional C<$where>
139  
140 An index is required for the filtering columns
141  
142 Refer to L<SQL::Abstract> for the format of C<$where> (actually uses L<DBIx::Class::SQLMaker> under the hood)
143  
144 =head2 @all = $search->all
145  
146 Returns every result for C<$search> in a list
147  
148 Returns an empty list if nothing is found
149  
150 =head2 $result = $search->next
151  
152 Returns the next item found for C<$search> via C<< $search->cursor >>
153  
154 Returns undef if nothing is left for C<$search>
155  
156 =head2 $sth = $search->cursor->sth
157  
158 Returns the L<DBI> sth (statement handle) for C<$search>
159  
160 =head2 $search = $search->search( $where )
161  
162 Further refine the search in the same way C<< $search->where( ... ) >> does
163  
164 =head2 $search = $search->where( $where )
165  
166 $search = $search->where( { genre => 'rock' } )
167  
168 Further refine C<$search> with the given C<$where>
169  
170 A new object is cloned from the original (the original C<$search> is left untouched)
171  
172 An index is required for the filtering columns
173  
174 Refer to L<SQL::Abstract> for the format of C<$where> (actually uses L<DBIx::Class::SQLMaker> under the hood)
175  
176 =head2 $search = $search->order_by( $order_by )
177  
178 $search->order_by( 'name DESC' )
179  
180 $search->order_by([ 'name DESC', 'age' ])
181  
182 Return the results in the given order
183  
184 A new object is cloned from the original, which is left untouched
185  
186 An index is required for the ordering columns
187  
188 Refer to L<SQL::Abstract> for the format of C<$order_by> (actually uses L<DBIx::Class::SQLMaker> under the hood)
189  
190 =head1 Model USAGE
191  
192 =head2 $model = $store->model( $model_name )
193  
194 Retrieve or create the C<$model_name> model object
195  
196 =head2 $model->index( $field_name )
197  
198 $store->model( 'Artist' )->index( 'name' ) # 'name' is now searchable/orderable, etc.
199  
200 Index C<$field_name> on C<$model>
201  
202 Every time the store for c<$model> is written to, the index will be updated with the value of C<$field>
203  
204 =head2 $model->index( $field_name, isa => $type )
205  
206 $store->model( 'Artist' )->index( 'website', isa => 'URI' )
207 $store->model( 'Artist' )->index( 'founded', isa => 'DateTime' )
208  
209 Index C<$field_name> on C<$model> as a special type/object (e.g. L<DateTime> or L<URI>)
210  
211 Every time the store for c<$model> is written to, the index will be updated with the deflated value of C<$field> (since
212 L<JSON> can not trivially serialize blessed references)
213  
214 =head2 $model->reindex
215  
216 Reindex the C<$model> data in the store after making a field indexing change:
217  
218 1. Rebuild the DBIx::Class::ResultSource
219 2. Drop and recreate the search table for $model
220 3. Iterate through all the data for $model, repopulating the search table
221  
222 If C<$model> does not have an index, this method will simply return
223  
224 To rebuild the index for _every_ model (on startup, for example), you can do:
225  
226 $store->reindex
227  
228 =head1 In the future
229  
230 Create a better interface for stashing and document it
231  
232 Wrap things in transactions that need it
233  
234 More tests: Always. Be. Testing.
235  
236 =head1 SEE ALSO
237  
238 L<KiokuDB>
239  
240 L<DBIx::Class>
241  
242 L<DBD::SQLite>
243  
244 =head1 AUTHOR
245  
246 Robert Krimen <robertkrimen@gmail.com>
247  
248 =head1 COPYRIGHT AND LICENSE
249  
250 This software is copyright (c) 2013 by Robert Krimen.
251  
252 This is free software; you can redistribute it and/or modify it under
253 the same terms as the Perl 5 programming language system itself.
254  
255 =cut
256  
257  
258 __END__
« NoSQLite.pm ↑ Summary