Add MySQL code to Appendix 2 (thanks to Jim Howard)
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Manual / Tutorial / Appendices.pod
CommitLineData
4d583dd8 1=head1 NAME
2
64ccd8a8 3Catalyst::Manual::Tutorial::Appendices - Catalyst Tutorial - Part 9: Appendices
4d583dd8 4
5
6=head1 OVERVIEW
7
64ccd8a8 8This is B<Part 9 of 9> of the Catalyst tutorial.
4d583dd8 9
64ccd8a8 10L<Tutorial Overview|Catalyst::Manual::Tutorial>
4d583dd8 11
12=over 4
13
14=item 1
15
16L<Introduction|Catalyst::Manual::Tutorial::Intro>
17
18=item 2
19
20L<Catalyst Basics|Catalyst::Manual::Tutorial::CatalystBasics>
21
22=item 3
23
64ccd8a8 24L<Basic CRUD|Catalyst::Manual::Tutorial_BasicCRUD>
4d583dd8 25
26=item 4
27
28L<Authentication|Catalyst::Manual::Tutorial::Authentication>
29
30=item 5
31
32L<Authorization|Catalyst::Manual::Tutorial::Authorization>
33
34=item 6
35
36L<Debugging|Catalyst::Manual::Tutorial::Debugging>
37
38=item 7
39
40L<Testing|Catalyst::Manual::Tutorial::Testing>
41
42=item 8
43
44L<AdvancedCRUD|Catalyst::Manual::Tutorial::AdvancedCRUD>
45
46=item 9
47
3c098c71 48B<Appendices>
4d583dd8 49
50=back
51
4d583dd8 52=head1 DESCRIPTION
53
64ccd8a8 54This part of the tutorial provides supporting information relevant to
55the Catalyst tutorial.
4d583dd8 56
4d583dd8 57=head1 APPENDIX 1: CUT AND PASTE FOR POD-BASED EXAMPLES
58
64ccd8a8 59You may notice that Pod indents example code with four spaces. This
60section provides some quick advice to "un-indent" this text in common
61editors.
4d583dd8 62
4d583dd8 63=head2 "Un-indenting" with Vi/Vim
64
64ccd8a8 65When cutting and pasting multi-line text from Pod-based documents, the
66following vi/vim regexs can be helpful to "un-indent" the inserted text
67(do NOT type the quotes, they are only included to show spaces in the
68regex patterns). I<Note that all 3 of the regexs end in 4 spaces>:
4d583dd8 69
70=over 4
71
72=item *
73
74":0,$s/^ "
75
64ccd8a8 76Removes four leading spaces from the entire file (from the first line,
77C<0>, to the last line, C<$>).
4d583dd8 78
79=item *
80
cc548726 81"%$s/^ "
82
83A shortcut for the previous item (C<%> specifies the entire file; so
84this removes four leading spaces from every line).
85
86=item *
87
4d583dd8 88":.,$s/^ "
89
64ccd8a8 90Removes the first four spaces from the line the cursor is on at the time
91the regex command is executed (".") to the last line of the file.
4d583dd8 92
93=item *
94
95":.,44s/^ "
96
64ccd8a8 97Removes four leading space from the current line through line 44
98(obviously adjust the C<44> to the appropriate value in your example).
4d583dd8 99
100=back
101
4d583dd8 102=head2 "Un-indenting" with Emacs
103
64ccd8a8 104B<TODO>
4d583dd8 105
4d583dd8 106=head1 APPENDIX 2: USING MYSQL AND POSTGRESQL
107
64ccd8a8 108The main database used in this tutorial is the very simple yet powerful
109SQLite. This section provides information that can be used to "convert"
110the tutorial to use MySQL and PostgreSQL. However, note that part of
111the beauty of the MVC architecture is that very little database-specific
112code is spread throughout the system (at least when MVC is "done
113right"). Consequently, converting from one database to another is
114relatively painless with most Catalyst applications. In general, you
115just need to adapt the schema definition C<.sql> file you use to
116initialize your database and adjust a few configuration parameters.
4d583dd8 117
64ccd8a8 118Also note that the purpose of the data definition statements for this
119section are not designed to take maximum advantage of the various
120features in each database for issues such as referential integrity and
121field types/constraints.
4d583dd8 122
4d583dd8 123=head2 MySQL
124
642d4547 125Use the following steps to adapt the tutorial to MySQL. Thanks to Jim
126Howard for the help.
127
128=over 4
129
130=item *
131
132Part 2: Catalyst Basics
133
134=over 4
135
136=item *
137
138Install the required software:
139
140=over 4
141
142=item *
143
144The MySQL database server and client utility.
145
146=item *
147
148The Perl C<DBD::MySQL> module
149
150=back
151
152For CentOS users (see
153L<Catalyst::Manual::Installation::CentOS4|Catalyst::Manual::Installation::CentOS4>),
154you can use the following commands to install the software and start the MySQL
155daemon:
156
157 yum -y install mysql mysql-server
158 service mysqld start
159
160=item *
161
162Create the database and set the permissions:
163
164 $ mysql
165 Welcome to the MySQL monitor. Commands end with ; or \g.
166 Your MySQL connection id is 2 to server version: 4.1.20
167
168 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
169
170 mysql> create database myapp;
171 Query OK, 1 row affected (0.01 sec)
172
173 mysql> grant all on myapp.* to tutorial@'localhost';
174 Query OK, 0 rows affected (0.00 sec)
175
176 mysql> flush privileges;
177 Query OK, 0 rows affected (0.00 sec)
178
179 mysql> quit
180 Bye
181
182=item *
183
184Create the C<.sql> file and load the data:
185
186=over 4
187
188=item *
189
190Open the C<myapp01_mysql.sql> in your editor and enter:
191
192 --
193 -- Create a very simple database to hold book and author information
194 --
195 DROP TABLE IF EXISTS books;
196 DROP TABLE IF EXISTS book_authors;
197 DROP TABLE IF EXISTS authors;
198 CREATE TABLE books (
199 id INT(11) PRIMARY KEY,
200 title TEXT ,
201 rating INT(11)
202 );
203 -- 'book_authors' is a many-to-many join table between books & authors
204 CREATE TABLE book_authors (
205 book_id INT(11),
206 author_id INT(11),
207 PRIMARY KEY (book_id, author_id)
208 );
209 CREATE TABLE authors (
210 id INT(11) PRIMARY KEY,
211 first_name TEXT,
212 last_name TEXT
213 );
214 ---
215 --- Load some sample data
216 ---
217 INSERT INTO books VALUES (1, 'CCSP SNRS Exam Certification Guide', 5);
218 INSERT INTO books VALUES (2, 'TCP/IP Illustrated, Volume 1', 5);
219 INSERT INTO books VALUES (3, 'Internetworking with TCP/IP Vol.1', 4);
220 INSERT INTO books VALUES (4, 'Perl Cookbook', 5);
221 INSERT INTO books VALUES (5, 'Designing with Web Standards', 5);
222 INSERT INTO authors VALUES (1, 'Greg', 'Bastien');
223 INSERT INTO authors VALUES (2, 'Sara', 'Nasseh');
224 INSERT INTO authors VALUES (3, 'Christian', 'Degu');
225 INSERT INTO authors VALUES (4, 'Richard', 'Stevens');
226 INSERT INTO authors VALUES (5, 'Douglas', 'Comer');
227 INSERT INTO authors VALUES (6, 'Tom', 'Christiansen');
228 INSERT INTO authors VALUES (7, ' Nathan', 'Torkington');
229 INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman');
230 INSERT INTO book_authors VALUES (1, 1);
231 INSERT INTO book_authors VALUES (1, 2);
232 INSERT INTO book_authors VALUES (1, 3);
233 INSERT INTO book_authors VALUES (2, 4);
234 INSERT INTO book_authors VALUES (3, 5);
235 INSERT INTO book_authors VALUES (4, 6);
236 INSERT INTO book_authors VALUES (4, 7);
237 INSERT INTO book_authors VALUES (5, 8);
238
239=item *
240
241Load the data:
242
243 mysql -ututorial myapp < myapp01_mysql.sql
244
245=item *
246
247Make sure the data loaded correctly:
248
249 $ mysql -ututorial myapp
250 Reading table information for completion of table and column names
251 You can turn off this feature to get a quicker startup with -A
252
253 Welcome to the MySQL monitor. Commands end with ; or \g.
254 Your MySQL connection id is 4 to server version: 4.1.20
255
256 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
257
258 mysql> show tables;
259 +-----------------+
260 | Tables_in_myapp |
261 +-----------------+
262 | authors |
263 | book_authors |
264 | books |
265 +-----------------+
266 3 rows in set (0.00 sec)
267
268 mysql> select * from books;
269 +----+------------------------------------+--------+
270 | id | title | rating |
271 +----+------------------------------------+--------+
272 | 1 | CCSP SNRS Exam Certification Guide | 5 |
273 | 2 | TCP/IP Illustrated, Volume 1 | 5 |
274 | 3 | Internetworking with TCP/IP Vol.1 | 4 |
275 | 4 | Perl Cookbook | 5 |
276 | 5 | Designing with Web Standards | 5 |
277 +----+------------------------------------+--------+
278 5 rows in set (0.00 sec)
279
280 mysql>
281
282=back
283
284=item *
285
286Update the model:
287
288=over 4
289
290=item *
291
292Delete the existing model:
293
294 rm lib/MyApp/Model/MyAppDB.pm
295
296=item *
297
298Regenerate the model using the Catalyst "_create.pl" script:
299
300 script/myapp_create.pl model MyAppDB DBIC::Schema MyAppDB dbi:mysql:myapp 'tutorial' '' '{ AutoCommit => 1 }'
301
302=back
303
304=back
305
306=item *
307
308Part 4: Authentication
309
310=over 4
311
312=item *
313
314Create the C<.sql> file for the user/roles data:
315
316Open C<myapp02_mysql.sql> in your editor and enter:
317
318 --
319 -- Add users and roles tables, along with a many-to-many join table
320 --
321 CREATE TABLE users (
322 id INT(11) PRIMARY KEY,
323 username TEXT,
324 password TEXT,
325 email_address TEXT,
326 first_name TEXT,
327 last_name TEXT,
328 active INT(11)
329 );
330 CREATE TABLE roles (
331 id INTEGER PRIMARY KEY,
332 role TEXT
333 );
334 CREATE TABLE user_roles (
335 user_id INT(11),
336 role_id INT(11),
337 PRIMARY KEY (user_id, role_id)
338 );
339 --
340 -- Load up some initial test data
341 --
342 INSERT INTO users VALUES (1, 'test01', 'mypass', 't01@na.com', 'Joe', 'Blow', 1);
343 INSERT INTO users VALUES (2, 'test02', 'mypass', 't02@na.com', 'Jane', 'Doe', 1);
344 INSERT INTO users VALUES (3, 'test03', 'mypass', 't03@na.com', 'No', 'Go', 0);
345 INSERT INTO roles VALUES (1, 'user');
346 INSERT INTO roles VALUES (2, 'admin');
347 INSERT INTO user_roles VALUES (1, 1);
348 INSERT INTO user_roles VALUES (1, 2);
349 INSERT INTO user_roles VALUES (2, 1);
350 INSERT INTO user_roles VALUES (3, 1);
351
352=item *
353
354Load the user/roles data:
355
356 mysql -ututorial myapp < myapp02_mysql.sql
357
358=item *
359
360Create the C<.sql> file for the hashed password data:
361
362Open C<myapp03_mysql.sql> in your editor and enter:
363
364 --
365 -- Convert passwords to SHA-1 hashes
366 --
367 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 1;
368 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 2;
369 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 3;
370
371=item *
372
373Load the user/roles data:
374
375 mysql -ututorial myapp < myapp03_mysql.sql
376
377=back
378
379=back
4d583dd8 380
381=head2 PostgreSQL
382
64ccd8a8 383B<TODO>
4d583dd8 384
cc548726 385
386=head1 APPENDIX 3: IMPROVED HASHING SCRIPT
387
388Here is an improved SHA-1 hashing script from Gavin Henry that does
389not expose the passwords to "capture" on the command line.
390
391 #!/usr/bin/perl -w
392 #===============================================================================
393 #
394 # FILE: enc_pass.pl
395 #
396 # USAGE: ./enc_pass.pl
397 #
398 # DESCRIPTION: Encrypt a Password using SHA-1
399 #
400 # OPTIONS: ---
401 # REQUIREMENTS: ---
402 # BUGS: ---
403 # NOTES: ---
404 # AUTHOR: Gavin Henry (GH), <ghenry@suretecsystems.com>
405 # COMPANY: Suretec Systems Ltd.
406 # VERSION: 1.0
407 # CREATED: 26/06/2006
408 # REVISION: ---
409 # COPYRIGHT: http://search.cpan.org/dist/perl/pod/perlgpl.pod
410 #===============================================================================
411
412 use strict;
413 use warnings;
414 use Digest::SHA1;
415 use Term::ReadKey;
416
417 sub get_pass {
418 ReadMode 'noecho';
419 chomp( my $pw = ReadLine 0 );
420 ReadMode 'normal';
421 return $pw;
422 }
423
424 print "Enter the password to be encrypted: ";
425 my $pass = get_pass();
426
427 print "\nConfirm the password: ";
428 my $verify = get_pass();
429
430 if ( $pass eq $verify ) {
431 my $sha1_enc = Digest::SHA1->new;
432 $sha1_enc->add($pass);
433
434 print "\nYour encrypted password is: "
435 . $sha1_enc->hexdigest . "\n"
436 . "Paste this into your SQL INSERT/COPY Data.\n";
437 }
438 else {
439 print "\nPasswords do not match!\n";
440 }
441
442
443
444=head1 AUTHOR
445
446Kennedy Clark, C<hkclark@gmail.com>
447
eed93301 448Please report any errors, issues or suggestions to the author. The
449most recent version of the Catlayst Tutorial can be found at
450L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/Catalyst-Runtime/lib/Catalyst/Manual/Tutorial/>.
cc548726 451
452Copyright 2006, Kennedy Clark, under Creative Commons License
453(L<http://creativecommons.org/licenses/by-nc-sa/2.5/>).