Commit | Line | Data |
9c82c181 |
1 | =head1 NAME |
2 | |
3b44ccc6 |
3 | DBIx::Class::Manual::Intro - Introduction to DBIx::Class |
9c82c181 |
4 | |
40dbc108 |
5 | =head1 INTRODUCTION |
076652e8 |
6 | |
d53178fd |
7 | You're bored with SQL, and want a native Perl interface for your database? Or |
8 | you've been doing this for a while with L<Class::DBI>, and think there's a |
9 | better way? You've come to the right place. |
4b0779f4 |
10 | |
11 | =head1 THE DBIx::Class WAY |
12 | |
d53178fd |
13 | Here are a few simple tips that will help you get your bearings with |
d56c3191 |
14 | DBIx::Class. |
4b0779f4 |
15 | |
2f0790c4 |
16 | =head2 Tables become Result classes |
4b0779f4 |
17 | |
2f0790c4 |
18 | DBIx::Class needs to know what your Table structure looks like. You |
19 | do that by defining Result classes. Result classes are defined by |
20 | calling methods proxied to L<DBIx::Class::ResultSource>. Each Result |
21 | class defines one Table, which defines the Columns it has, along with |
22 | any Relationships it has to other tables. (And oh, so much more |
23 | besides) The important thing to understand: |
d53178fd |
24 | |
2f0790c4 |
25 | A Result class == Table |
d53178fd |
26 | |
4b0779f4 |
27 | (most of the time, but just bear with my simplification) |
28 | |
29 | =head2 It's all about the ResultSet |
30 | |
d53178fd |
31 | So, we've got some ResultSources defined. Now, we want to actually use those |
d56c3191 |
32 | definitions to help us translate the queries we need into handy perl objects! |
d53178fd |
33 | |
34 | Let's say we defined a ResultSource for an "album" table with three columns: |
35 | "albumid", "artist", and "title". Any time we want to query this table, we'll |
36 | be creating a L<DBIx::Class::ResultSet> from its ResultSource. For example, the |
37 | results of: |
4b0779f4 |
38 | |
d53178fd |
39 | SELECT albumid, artist, title FROM album; |
4b0779f4 |
40 | |
d53178fd |
41 | Would be retrieved by creating a ResultSet object from the album table's |
d56c3191 |
42 | ResultSource, likely by using the "search" method. |
4b0779f4 |
43 | |
d53178fd |
44 | DBIx::Class doesn't limit you to creating only simple ResultSets -- if you |
45 | wanted to do something like: |
46 | |
47 | SELECT title FROM album GROUP BY title; |
4b0779f4 |
48 | |
d56c3191 |
49 | You could easily achieve it. |
4b0779f4 |
50 | |
d56c3191 |
51 | The important thing to understand: |
4b0779f4 |
52 | |
d56c3191 |
53 | Any time you would reach for a SQL query in DBI, you are |
d53178fd |
54 | creating a DBIx::Class::ResultSet. |
4b0779f4 |
55 | |
56 | =head2 Search is like "prepare" |
57 | |
d53178fd |
58 | DBIx::Class tends to wait until it absolutely must fetch information from the |
59 | database. If you are returning a ResultSet, the query won't execute until you |
60 | use a method that wants to access the data. (Such as "next", or "first") |
4b0779f4 |
61 | |
62 | The important thing to understand: |
63 | |
d53178fd |
64 | Setting up a ResultSet does not execute the query; retrieving |
65 | the data does. |
4b0779f4 |
66 | |
2f0790c4 |
67 | =head2 Search results are returned as Rows |
68 | |
69 | Rows of the search from the database are blessed into |
70 | L<DBIx::Class::Row> objects. |
71 | |
4b0779f4 |
72 | =head1 SETTING UP DBIx::Class |
73 | |
d53178fd |
74 | Let's look at how you can set and use your first native L<DBIx::Class> tree. |
076652e8 |
75 | |
d53178fd |
76 | First we'll see how you can set up your classes yourself. If you want them to |
77 | be auto-discovered, just skip to the next section, which shows you how to use |
78 | L<DBIx::Class::Schema::Loader>. |
076652e8 |
79 | |
80 | =head2 Setting it up manually |
81 | |
5cc9fa32 |
82 | First, you should create your base schema class, which inherits from |
83 | L<DBIx::Class::Schema>: |
076652e8 |
84 | |
5cc9fa32 |
85 | package My::Schema; |
86 | use base qw/DBIx::Class::Schema/; |
87 | |
d53178fd |
88 | In this class you load your result_source ("table", "model") classes, which we |
da7372ac |
89 | will define later, using the load_namespaces() method: |
076652e8 |
90 | |
da7372ac |
91 | # load My::Schema::Result::* and their resultset classes |
92 | __PACKAGE__->load_namespaces(); |
076652e8 |
93 | |
da7372ac |
94 | By default this loads all the Result (Row) classes in the |
95 | My::Schema::Result:: namespace, and also any resultset classes in the |
96 | My::Schema::ResultSet:: namespace (if missing, the resultsets are |
97 | defaulted to be DBIx::Class::ResultSet objects). You can change the |
98 | result and resultset namespaces by using options to the |
99 | L<DBIx::Class::Schema/load_namespaces> call. |
076652e8 |
100 | |
da7372ac |
101 | It is also possible to do the same things manually by calling |
102 | C<load_classes> for the Row classes and defining in those classes any |
103 | required resultset classes. |
076652e8 |
104 | |
5cc9fa32 |
105 | Next, create each of the classes you want to load as specified above: |
076652e8 |
106 | |
da7372ac |
107 | package My::Schema::Result::Album; |
d88ecca6 |
108 | use base qw/DBIx::Class::Core/; |
35d4fe78 |
109 | |
d88ecca6 |
110 | Load any additional components you may need with the load_components() method, |
111 | and provide component configuration if required. For example, if you want |
112 | automatic row ordering: |
076652e8 |
113 | |
d88ecca6 |
114 | __PACKAGE__->load_components(qw/ Ordered /); |
115 | __PACKAGE__->position_column('rank'); |
076652e8 |
116 | |
5cc9fa32 |
117 | Set the table for your class: |
076652e8 |
118 | |
35d4fe78 |
119 | __PACKAGE__->table('album'); |
076652e8 |
120 | |
5cc9fa32 |
121 | Add columns to your class: |
122 | |
d88ecca6 |
123 | __PACKAGE__->add_columns(qw/ albumid artist title rank /); |
5cc9fa32 |
124 | |
d53178fd |
125 | Each column can also be set up with its own accessor, data_type and other pieces |
126 | of information that it may be useful to have -- just pass C<add_columns> a hash: |
5cc9fa32 |
127 | |
128 | __PACKAGE__->add_columns(albumid => |
129 | { accessor => 'album', |
130 | data_type => 'integer', |
131 | size => 16, |
132 | is_nullable => 0, |
133 | is_auto_increment => 1, |
134 | default_value => '', |
135 | }, |
136 | artist => |
137 | { data_type => 'integer', |
138 | size => 16, |
139 | is_nullable => 0, |
140 | is_auto_increment => 0, |
141 | default_value => '', |
142 | }, |
d56c3191 |
143 | title => |
5cc9fa32 |
144 | { data_type => 'varchar', |
145 | size => 256, |
146 | is_nullable => 0, |
147 | is_auto_increment => 0, |
148 | default_value => '', |
d88ecca6 |
149 | }, |
150 | rank => |
151 | { data_type => 'integer', |
152 | size => 16, |
153 | is_nullable => 0, |
154 | is_auto_increment => 0, |
155 | default_value => '', |
5cc9fa32 |
156 | } |
157 | ); |
158 | |
d53178fd |
159 | DBIx::Class doesn't directly use most of this data yet, but various related |
160 | modules such as L<DBIx::Class::WebForm> make use of it. Also it allows you to |
161 | create your database tables from your Schema, instead of the other way around. |
d88ecca6 |
162 | See L<DBIx::Class::Schema/deploy> for details. |
5cc9fa32 |
163 | |
164 | See L<DBIx::Class::ResultSource> for more details of the possible column |
165 | attributes. |
166 | |
da7372ac |
167 | Accessors are created for each column automatically, so My::Schema::Result::Album will |
5cc9fa32 |
168 | have albumid() (or album(), when using the accessor), artist() and title() |
169 | methods. |
170 | |
171 | Define a primary key for your class: |
076652e8 |
172 | |
5cc9fa32 |
173 | __PACKAGE__->set_primary_key('albumid'); |
076652e8 |
174 | |
5cc9fa32 |
175 | If you have a multi-column primary key, just pass a list instead: |
076652e8 |
176 | |
5cc9fa32 |
177 | __PACKAGE__->set_primary_key( qw/ albumid artistid / ); |
076652e8 |
178 | |
d53178fd |
179 | Define this class' relationships with other classes using either C<belongs_to> |
180 | to describe a column which contains an ID of another Table, or C<has_many> to |
181 | make a predefined accessor for fetching objects that contain this Table's |
182 | foreign key: |
5cc9fa32 |
183 | |
4ae94ded |
184 | # in My::Schema::Result::Artist |
185 | __PACKAGE__->has_many('albums', 'My::Schema::Result::Album', 'artist'); |
076652e8 |
186 | |
d53178fd |
187 | See L<DBIx::Class::Relationship> for more information about the various types of |
188 | available relationships and how you can design your own. |
076652e8 |
189 | |
5cc9fa32 |
190 | =head2 Using L<DBIx::Class::Schema::Loader> |
076652e8 |
191 | |
d53178fd |
192 | This is an external module, and not part of the L<DBIx::Class> distribution. |
193 | Like L<Class::DBI::Loader>, it inspects your database, and automatically creates |
194 | classes for all the tables in your database. Here's a simple setup: |
076652e8 |
195 | |
24d34a80 |
196 | package My::Schema; |
197 | use base qw/DBIx::Class::Schema::Loader/; |
40dbc108 |
198 | |
3f073ddf |
199 | __PACKAGE__->loader_options( relationships => 1 ); |
076652e8 |
200 | |
35d4fe78 |
201 | 1; |
076652e8 |
202 | |
d53178fd |
203 | The actual autoloading process will occur when you create a connected instance |
204 | of your schema below. |
3f073ddf |
205 | |
d53178fd |
206 | See the L<DBIx::Class::Schema::Loader> documentation for more information on its |
207 | many options. |
076652e8 |
208 | |
5cc9fa32 |
209 | =head2 Connecting |
210 | |
6ba55998 |
211 | To connect to your Schema, you need to provide the connection details or a |
212 | database handle. |
213 | |
214 | =head3 Via connection details |
215 | |
216 | The arguments are the same as for L<DBI/connect>: |
5cc9fa32 |
217 | |
218 | my $schema = My::Schema->connect('dbi:SQLite:/home/me/myapp/my.db'); |
219 | |
d53178fd |
220 | You can create as many different schema instances as you need. So if you have a |
221 | second database you want to access: |
5cc9fa32 |
222 | |
223 | my $other_schema = My::Schema->connect( $dsn, $user, $password, $attrs ); |
224 | |
d53178fd |
225 | Note that L<DBIx::Class::Schema> does not cache connections for you. If you use |
226 | multiple connections, you need to do this manually. |
5cc9fa32 |
227 | |
48580715 |
228 | To execute some SQL statements on every connect you can add them as an option in |
d53178fd |
229 | a special fifth argument to connect: |
3f073ddf |
230 | |
231 | my $another_schema = My::Schema->connect( |
232 | $dsn, |
233 | $user, |
234 | $password, |
235 | $attrs, |
236 | { on_connect_do => \@on_connect_sql_statments } |
237 | ); |
5cc9fa32 |
238 | |
d53178fd |
239 | See L<DBIx::Class::Schema::Storage::DBI/connect_info> for more information about |
240 | this and other special C<connect>-time options. |
5cc9fa32 |
241 | |
6ba55998 |
242 | =head3 Via a database handle |
243 | |
244 | The supplied coderef is expected to return a single connected database handle |
245 | (e.g. a L<DBI> C<$dbh>) |
246 | |
247 | my $schema = My::Schema->connect ( |
248 | sub { Some::DBH::Factory->connect }, |
249 | \%extra_attrs, |
250 | ); |
251 | |
40dbc108 |
252 | =head2 Basic usage |
076652e8 |
253 | |
35d4fe78 |
254 | Once you've defined the basic classes, either manually or using |
5cc9fa32 |
255 | L<DBIx::Class::Schema::Loader>, you can start interacting with your database. |
256 | |
d53178fd |
257 | To access your database using your $schema object, you can fetch a |
258 | L<DBIx::Class::Manual::Glossary/"ResultSet"> representing each of your tables by |
259 | calling the C<resultset> method. |
5cc9fa32 |
260 | |
35d4fe78 |
261 | The simplest way to get a record is by primary key: |
076652e8 |
262 | |
5cc9fa32 |
263 | my $album = $schema->resultset('Album')->find(14); |
076652e8 |
264 | |
d53178fd |
265 | This will run a C<SELECT> with C<albumid = 14> in the C<WHERE> clause, and |
da7372ac |
266 | return an instance of C<My::Schema::Result::Album> that represents this row. Once you |
d53178fd |
267 | have that row, you can access and update columns: |
076652e8 |
268 | |
35d4fe78 |
269 | $album->title('Physical Graffiti'); |
270 | my $title = $album->title; # $title holds 'Physical Graffiti' |
076652e8 |
271 | |
d53178fd |
272 | If you prefer, you can use the C<set_column> and C<get_column> accessors |
273 | instead: |
076652e8 |
274 | |
35d4fe78 |
275 | $album->set_column('title', 'Presence'); |
276 | $title = $album->get_column('title'); |
076652e8 |
277 | |
18bb9eca |
278 | Just like with L<Class::DBI>, you call C<update> to save your changes to the |
279 | database (by executing the actual C<UPDATE> statement): |
40dbc108 |
280 | |
35d4fe78 |
281 | $album->update; |
076652e8 |
282 | |
d53178fd |
283 | If needed, you can throw away your local changes: |
076652e8 |
284 | |
35d4fe78 |
285 | $album->discard_changes if $album->is_changed; |
076652e8 |
286 | |
d53178fd |
287 | As you can see, C<is_changed> allows you to check if there are local changes to |
288 | your object. |
076652e8 |
289 | |
40dbc108 |
290 | =head2 Adding and removing rows |
076652e8 |
291 | |
d53178fd |
292 | To create a new record in the database, you can use the C<create> method. It |
da7372ac |
293 | returns an instance of C<My::Schema::Result::Album> that can be used to access the data |
d53178fd |
294 | in the new record: |
076652e8 |
295 | |
d56c3191 |
296 | my $new_album = $schema->resultset('Album')->create({ |
35d4fe78 |
297 | title => 'Wish You Were Here', |
298 | artist => 'Pink Floyd' |
299 | }); |
dfeba824 |
300 | |
301 | Now you can add data to the new record: |
302 | |
35d4fe78 |
303 | $new_album->label('Capitol'); |
304 | $new_album->year('1975'); |
305 | $new_album->update; |
076652e8 |
306 | |
d53178fd |
307 | Likewise, you can remove it from the database: |
076652e8 |
308 | |
35d4fe78 |
309 | $new_album->delete; |
076652e8 |
310 | |
d53178fd |
311 | You can also remove records without retrieving them first, by calling delete |
312 | directly on a ResultSet object. |
076652e8 |
313 | |
35d4fe78 |
314 | # Delete all of Falco's albums |
5cc9fa32 |
315 | $schema->resultset('Album')->search({ artist => 'Falco' })->delete; |
076652e8 |
316 | |
40dbc108 |
317 | =head2 Finding your objects |
076652e8 |
318 | |
d53178fd |
319 | L<DBIx::Class> provides a few different ways to retrieve data from your |
320 | database. Here's one example: |
35d4fe78 |
321 | |
322 | # Find all of Santana's albums |
5cc9fa32 |
323 | my $rs = $schema->resultset('Album')->search({ artist => 'Santana' }); |
35d4fe78 |
324 | |
d53178fd |
325 | In scalar context, as above, C<search> returns a L<DBIx::Class::ResultSet> |
326 | object. It can be used to peek at the first album returned by the database: |
35d4fe78 |
327 | |
328 | my $album = $rs->first; |
329 | print $album->title; |
076652e8 |
330 | |
5cc9fa32 |
331 | You can loop over the albums and update each one: |
076652e8 |
332 | |
35d4fe78 |
333 | while (my $album = $rs->next) { |
334 | print $album->artist . ' - ' . $album->title; |
335 | $album->year(2001); |
336 | $album->update; |
337 | } |
a3c5e7e3 |
338 | |
5cc9fa32 |
339 | Or, you can update them all at once: |
340 | |
341 | $rs->update({ year => 2001 }); |
342 | |
d53178fd |
343 | In list context, the C<search> method returns all of the matching rows: |
a3c5e7e3 |
344 | |
35d4fe78 |
345 | # Fetch immediately all of Carlos Santana's albums |
5cc9fa32 |
346 | my @albums = $schema->resultset('Album')->search( |
347 | { artist => 'Carlos Santana' } |
348 | ); |
35d4fe78 |
349 | foreach my $album (@albums) { |
350 | print $album->artist . ' - ' . $album->title; |
351 | } |
076652e8 |
352 | |
40dbc108 |
353 | We also provide a handy shortcut for doing a C<LIKE> search: |
076652e8 |
354 | |
35d4fe78 |
355 | # Find albums whose artist starts with 'Jimi' |
24d34a80 |
356 | my $rs = $schema->resultset('Album')->search_like({ artist => 'Jimi%' }); |
076652e8 |
357 | |
d53178fd |
358 | Or you can provide your own C<WHERE> clause: |
35d4fe78 |
359 | |
360 | # Find Peter Frampton albums from the year 1986 |
361 | my $where = 'artist = ? AND year = ?'; |
362 | my @bind = ( 'Peter Frampton', 1986 ); |
5cc9fa32 |
363 | my $rs = $schema->resultset('Album')->search_literal( $where, @bind ); |
40dbc108 |
364 | |
d53178fd |
365 | The preferred way to generate complex queries is to provide a L<SQL::Abstract> |
366 | construct to C<search>: |
40dbc108 |
367 | |
5cc9fa32 |
368 | my $rs = $schema->resultset('Album')->search({ |
35d4fe78 |
369 | artist => { '!=', 'Janis Joplin' }, |
370 | year => { '<' => 1980 }, |
1aec4bac |
371 | albumid => { '-in' => [ 1, 14, 15, 65, 43 ] } |
35d4fe78 |
372 | }); |
373 | |
374 | This results in something like the following C<WHERE> clause: |
40dbc108 |
375 | |
35d4fe78 |
376 | WHERE artist != 'Janis Joplin' |
377 | AND year < 1980 |
378 | AND albumid IN (1, 14, 15, 65, 43) |
379 | |
d53178fd |
380 | For more examples of complex queries, see L<DBIx::Class::Manual::Cookbook>. |
40dbc108 |
381 | |
382 | The search can also be modified by passing another hash with |
383 | attributes: |
384 | |
24d34a80 |
385 | my @albums = My::Schema->resultset('Album')->search( |
35d4fe78 |
386 | { artist => 'Bob Marley' }, |
387 | { rows => 2, order_by => 'year DESC' } |
388 | ); |
389 | |
390 | C<@albums> then holds the two most recent Bob Marley albums. |
40dbc108 |
391 | |
d53178fd |
392 | For more information on what you can do with a L<DBIx::Class::ResultSet>, see |
393 | L<DBIx::Class::ResultSet/METHODS>. |
394 | |
40dbc108 |
395 | For a complete overview of the available attributes, see |
396 | L<DBIx::Class::ResultSet/ATTRIBUTES>. |
076652e8 |
397 | |
11736b4c |
398 | =head1 NOTES |
399 | |
400 | =head2 Problems on RHEL5/CentOS5 |
401 | |
dc253b77 |
402 | There used to be an issue with the system perl on Red Hat Enterprise |
403 | Linux 5, some versions of Fedora and derived systems. Further |
404 | information on this can be found in L<DBIx::Class::Manual::Troubleshooting> |
11736b4c |
405 | |
40dbc108 |
406 | =head1 SEE ALSO |
076652e8 |
407 | |
40dbc108 |
408 | =over 4 |
076652e8 |
409 | |
40dbc108 |
410 | =item * L<DBIx::Class::Manual::Cookbook> |
076652e8 |
411 | |
40dbc108 |
412 | =back |
076652e8 |
413 | |
414 | =cut |