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