• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /macosx-10.9.5/CPANInternal-140/Class-DBI-v3.0.17/lib/Class/

Lines Matching +refs:artist +refs:file +refs:to +refs:string

147 # Override transform_sql from Ima::DBI to provide some extra
248 # Careful to take copy
324 # If both the accessor and mutator are to have the same method name,
466 # If you override this method, you must use the same mechanism to log changes
472 # We increment instead of setting to 1 because it might be useful to
473 # someone to know how many times a value has changed between updates.
511 # time to clean up your room?
602 # Try to do this in a standard method. Fall back to MySQL/SQLite
605 # Note: I don't believe the last_insert_id can be zero. We need to
606 # switch to defined() checks if it can.
675 # Get the data, as a hash, but setting certain values to whatever
678 # to change.
687 $self->_croak("Need hash-ref to edit copied column values")
724 . "you should tell me quickly so I can abandon my plan to remove it.");
725 return $old_obj->_croak("Can't move to an unrelated class")
761 # Return the placeholder to be used in UPDATE and INSERT queries.
798 # delete columns that changed (in case adding to DB modifies them again)
818 $self->_carp("$class $self destroyed without saving changes to "
832 # We override the get() method from Class::Accessor to fetch the data for
834 # method. We also allow get to be called with a list of keys, instead of
865 # changes, and either write to the database now (if autoupdate is on),
909 # is valid for the object in relation to its current values
977 $class->_croak("Don't know how to constrain $col with $how");
1100 # version has been publicised as the way to do this. We need to
1262 Music::Artist->table('artist');
1269 Music::CD->columns(All => qw/cdid artist title year reldate/);
1271 Music::CD->has_a(artist => 'Music::Artist');
1286 my $artist = Music::Artist->insert({ artistid => 1, name => 'U2' });
1288 my $cd = $artist->add_to_cds({
1313 Class::DBI provides a convenient abstraction layer to a database.
1315 It not only provides a simple database to object mapping layer, but can
1316 be used to implement several higher order database functions (triggers,
1321 these (such as MySQL), or when you would like your code to be portable
1325 In short, Class::DBI aims to make it simple to introduce 'best
1328 =head2 How to set it up
1338 =item I<Set up a table for your objects to be stored in.>
1341 responsibility to have your database tables already set up. Automating that
1348 artist INTEGER, # references 'artist'
1355 It's usually wise to set up a "top level" class for your entire
1356 application to inherit from, rather than have each class inherit
1357 directly from Class::DBI. This gives you a convenient point to
1358 place system-wide overrides and enhancements to Class::DBI's behavior.
1365 Class::DBI needs to know how to access the database. It does this
1380 need to repeat the information on how to connect to the database.
1393 Music::CD->columns(All => qw/cdid artist title year/);
1396 declare them using a separate call to columns() like this:
1406 That's it! You now have a class with methods to L<"insert">,
1423 This uses L<Ima::DBI> to set up an inheritable connection (named Main). It is
1424 therefore usual to only set up a connection() in your application base class
1435 Class::DBI helps you along a bit to set up the database connection.
1454 and you should be very careful not to change this unless you know what
1459 It is sometimes desirable to generate your database connection information
1460 dynamically, for example, to allow multiple databases with the same
1461 schema to not have to duplicate an entire class hierarchy.
1463 The preferred method for doing this is to supply your own db_Main()
1467 with your own. Note, this handle *must* have its RootClass set to
1468 L<DBIx::ContextualFetch>, so it is usually not possible to just supply a
1482 An accessor to get/set the name of the database table in which this
1493 When Class::DBI constructs SQL, it aliases your table name to a name
1499 This can also be passed as a second argument to 'table':
1512 If you are using a database which supports sequences and you want to use
1513 a sequence to automatically supply values for the primary key of a table,
1519 Class::DBI will use the sequence to generate a primary key value when
1527 not need this, and any call to insert() without a primary key specified
1530 Sequence and auto-increment mechanisms only apply to tables that have
1532 you need to supply the key values manually.
1536 The following are methods provided for convenience to insert, retrieve
1538 might find it necessary to override them.
1544 This is a constructor to insert new data into the database and create an
1547 %data consists of the initial information to place in your object and
1553 artist => $artist,
1559 is not defined in %data, insert() will assume it is to be generated.
1562 AUTO_INCREMENT and attempt to use that.
1568 database is going to be providing the primary key value.
1570 For tables with multi-column primary keys you need to supply all
1571 the key values, either in the arguments to the insert() method, or
1575 has_a(), you can pass an object to insert() for the value of that key.
1589 my $cd = Music::CD->find_or_create({ artist => 'U2', title => 'Boy' });
1591 This checks if a CD can be found to match the information passed, and
1608 The C<before_delete> trigger is when an object instance is about to be
1617 It is not the goal of Class::DBI to replace the need for using SQL. Users
1618 are expected to write their own searches for more complex cases.
1648 equal to the values specified e.g.:
1656 @cds = Music::CD->search(year => 1990, { order_by=>'artist' });
1671 @cds = Music::CD->search_like(title => 'Hits%', artist => 'Various%');
1695 if your mapping is not a one-to-one, the results will probably not be
1702 You can also subclass the default iterator class to override its
1710 This is used to turn data from the database into objects, and should
1712 cheaply setting up lots of objects from data for without going back to
1715 For example, instead of doing one SELECT to get a bunch of IDs and then
1716 feeding those individually to retrieve() (and thus doing more SELECT
1717 calls), you can do one SELECT to get the essential data of many objects
1718 and feed that data to construct():
1734 sets any supplied column values and calls insert() to make a new
1759 For transferring objects from one class to another. Similar to copy(), an
1773 It is possible to set up triggers that will be called at various
1823 It is also possible to set up constraints on the values that can be set
1829 - The new value to be assigned
1830 - The object it will be assigned to
1837 The constraints are applied to all the columns being set before the
1838 object data is changed. Attempting to create or modify an object
1842 The exception thrown has its data set to a hashref of the column being
1843 changed and the value being changed to.
1847 the provided insert() or set() methods. It will always be possible to
1851 checked for column names included in the parameters to insert().
1852 This is probably a bug and is likely to change in future.
1860 Simple anonymous constraints can also be added to a column using the
1862 must match, a reference to a list of possible values, or a subref which
1863 will have $_ aliased to the value being set, and should return a
1867 constraint handler for the type of argument passed to constrain_column.
1876 a reference to a hash containing the column names and the new values
1877 which are to be assigned (after any validation and constraint checking,
1882 to normalize (edit) the data in any way you need. For example the values
1893 reference to a hash containing the column names and the new values which
1894 are to be assigned.
1903 is a reference to a hash which holds the column name and error text for
1908 custom _croak() method, and then caught and used to redisplay the
1909 form with error messages next to each field which failed validation.
1919 is simply to call Carp::croak($message).
1936 The _croak() method is expected to trigger an exception and not
1939 context. You should only return other values if you are prepared to
1944 That allows you to correctly propagate exception objects that may have
1957 The default behaviour is simply to call Carp::carp().
1965 the get() and set() methods provided by Accessor to automagically handle
1966 database reading and writing. (Note that as it doesn't make sense to
1991 It is possible for an object to not have all its column data in memory
1992 (due to lazy inflation). If the get() method is called for such a column
2000 It is possible to change the name of the accessor method created for a
2014 If you want to change the name of all your accessors, or all that match
2015 a certain pattern, you need to provide an accessor_name_for($col) method,
2016 which will convert a column name to a method name.
2018 e.g: if your local database naming convention was to prepend the word
2019 'customer' to each column in the 'customer' table, so that you had the
2021 your methods to just be $customer->name and $customer->age rather than
2030 Similarly, if you wanted to have distinct accessor and mutator methods,
2032 the name of the method to change the value:
2044 There are two modes for the accessors to work in: manual update and
2046 to make a change an UPDATE will immediately be sent to the database.
2052 # The calls to NumExplodingSheep() and Rating() will only make the
2054 # it writes to the database in one swell foop.
2064 # Each accessor call causes the new value to immediately be written.
2069 it provides the extra safety of a discard_changes() option to clear out all
2085 This is an accessor to the current style of auto-updating. When called
2106 If L<"autoupdate"> is not enabled then changes you make to your object are
2108 to call update() if there are no changes to be saved. (If autoupdate
2109 is on there'll never be anything to save.)
2112 L<"TRANSACTIONS"> below) you will also need to call dbi_commit(), as
2113 update() merely issues the UPDATE to the database).
2128 The trigger code can modify the discard_columns array to affect
2142 Take care to not delete a primary key column unless you know what
2146 had not changed and thus did not need to issue an UPDATE statement,
2157 Removes any changes you've made to this object since the last update.
2179 with inflated values reduced to their ids.
2187 routines, you'll want to manipulate data in a Class::DBI object without
2194 to make you think carefully about using them - they should not be a
2217 values in the object itself; changes will not be propagated to the
2226 the changes so that they are propagated to the database with the next
2245 By default, Class::DBI uses simple hash references to store object
2246 data, but all access is via these routines, so if you want to
2259 When a Class::DBI object reference is used in a string context it will,
2263 You can also specify the column(s) to be used for stringification via
2265 an auto-incremented primary key, you could use this to provide a more
2266 meaningful display string:
2270 If you need to do anything more complex, you can provide an stringify_self()
2291 field is later changed to be a foreign key to a new currency table then
2293 string. Without overloading the stringify operator the example would now
2295 be to change the code to add a call to id():
2300 to work as before, with no code changes needed.
2302 This makes it much simpler and safer to add relationships to existing
2308 for you to set up descriptions of your relationhips.
2317 Music::CD->has_a(artist => 'Music::Artist');
2318 print $cd->artist->name;
2320 'has_a' is most commonly used to supply lookup information for a foreign
2325 It is also possible to use has_a to inflate the column value to a non
2326 Class::DBI based. A common usage would be to inflate a date field to a
2344 When the object is being written to the database the object will be
2346 attempting to stringify the object. If the deflate method is a subref,
2349 *NOTE* You should not attempt to make your primary key column inflate
2373 to be constructed, taking care of the linking automatically. This method
2376 The add_to_tracks example above is exactly equivalent to:
2385 are examined to discover which of its columns reference our class. (Note
2387 in the same file, the class with the has_a() must be defined earlier than
2389 Class::DBI should usually be able to do the right things, as long as all
2392 If the foreign class has no has_a() declarations linking to this class,
2396 If this is not true you can pass an additional third argument to
2398 is the foreign key to this class.
2403 my @cds = $artist->cds(year => 1980);
2422 If the second argument to has_many is turned into a listref of the
2426 The above is exactly equivalent to:
2442 It is also possible to control what happens to the 'child' objects when
2443 the 'parent' object is deleted. By default this is set to 'Delete' - so,
2444 for example, when you delete an artist, you also delete all their CDs,
2445 leaving no orphaned records. However you could also set this to 'None',
2448 try to delete an artist that still has any CDs.
2454 which would set all related foreign keys to be NULL, and plug it into
2468 my $notes = $cd->notes; # equivalent to $cd->liner_notes->notes;
2470 might_have() is similar to has_many() for relationships that can have
2472 to which you want to add liner notes information, you might not want
2473 to add a 'liner_notes' column to your main CD table even though there
2478 But you don't want to have to keep writing methods to turn the the
2481 creates an accessor to fetch the single object back if it exists, and
2487 Making sure you don't have namespace clashes is up to you, as is correctly
2494 already been loaded. If it hasn't then they try to load the module of
2501 NOTE: The two classes in a relationship do not have to be in the same
2503 is quite acceptable for a table in a MySQL database to be connected to
2505 to work across these. This should assist greatly if you need to migrate
2510 Class::DBI does not currently support Many to Many relationships, per se.
2512 to set these up.
2522 Then, set up the Film and Actor classes to use this linking table:
2532 In each case the 'mapping method' variation of has_many() is used to
2535 Actor and Film objects, providing a cheap many-to-many relationship.
2537 In the case of Film, this is equivalent to the more long-winded:
2555 If the Role table were to contain extra information, such as the name
2556 of the character played, then you would usually need to skip these
2574 If is thus possible to add new relationship types, or modify the behaviour
2580 There are several main approaches to setting up your own SQL queries:
2582 For queries which could be used to create a list of matching objects
2586 For more complex queries you need to fall back on the underlying Ima::DBI
2588 you need to be careful to double any "wildcard" % signs in your queries).
2616 On occasions where you want to execute arbitrary SQL, but don't want
2617 to go to the trouble of setting up a constructor method, you can inline
2621 artist = 'Ozzy Osbourne' AND
2632 and prefers to use its style of dealing with statements, via set_sql().
2634 The Class::DBI set_sql() method defaults to using prepare_cached()
2660 For such 'SELECT' queries L<Ima::DBI>'s set_sql() method is extended to
2662 SQL fragment with 'search_'. Thus, the above call to set_sql() will
2677 The $sth which is used to return the objects here is a normal DBI-style
2679 it is still possible to call $sth->fetchrow_array etc and return whatever
2683 to add a query that returns the 10 Artists with the most CDs, you could
2687 SELECT artist.id, COUNT(cd.id) AS cds
2688 FROM artist, cd
2689 WHERE artist.id = cd.artist
2690 GROUP BY artist.id
2697 If you also need to access the 'cds' value returned from this query,
2698 the best approach is to declare 'cds' to be a TEMP column. (See
2704 artist => [ 'Ozzy', 'Kelly' ],
2709 plugin for Class::DBI that allows you to write arbitrarily complex
2738 This is used to implement the above Class->count_all():
2764 available columns and it would be a waste of memory to load all of them
2765 just to get at two, especially if you're dealing with large numbers of
2772 So for example, if we usually fetch the artist and title, but don't use
2776 Music::CD->columns(Essential => qw/artist title/);
2780 'artist' and 'title' fields. Fetching the 'year' will mean another visit
2781 to the database, but will bring back the 'runlength' whilst it's there.
2785 If you don't like this behavior, then just add all your columns to the
2790 Music::CD->columns(Essential => qw/cdid artist title year runlength/);
2813 B<'Essential'> are the minimal set of columns needed to load and use the
2815 is retrieve()'d. It is typically used to save memory on a class that
2817 used. It will automatically be set to B<'Primary'> if not explicitly set.
2821 methods are provided to return these. The primary_column() method should
2828 If you wish to have fields that act like columns in every other way, but
2839 This is most useful either in a boolean context to discover if the column
2840 exists, or to 'normalize' a user-entered column name to an actual Column.
2843 so you shouldn't really rely on anything internal to it.
2850 may not be expecting you to commit.
2852 However, as long as you are aware of this, and try to keep the scope
2854 method, you should be able to work with transactions with few problems.
2861 These are thin aliases through to the DBI's commit() and rollback()
2862 commands to commit or rollback all changes to this object.
2889 my $artist = Music::Artist->insert({ name => 'Pink Floyd' });
2890 my $cd = $artist->add_to_cds({
2906 Here's an example to illustrate:
2912 Now $artist1, $artist2, and $artist3 all point to the same object. If you
2920 The ability to perform this magic for you replies on your perl having
2921 access to the Scalar::Util::weaken function. Although this is part of
2938 $artist->remove_from_object_index();
2941 objects index. You can use this if you want to have multiple distinct
2958 but every so often dead references are cleaned out to prevent this. By
2974 The preferred method of interacting with Class::DBI is for you to write
2979 this also allows you to override default behaviour or add additional
2985 as being safe to override. If you find yourself needing to do this,
2988 a new means to do whatever you need to do.
2995 You could, however, write a Relationship plugin to do this, and the
3000 Altering your primary key column currently causes Bad Things to happen.
3007 to work with MySQL, PostgreSQL, Oracle and SQLite, each of which have
3014 For the most part it's been reported to work with Sybase, although there
3016 The Great Unknown(tm). If you have access to other databases, please
3043 and all the others who've helped, but that I've forgetten to mention.
3050 The general approach to releases will be that users who like a degree of
3053 to date with minor sub-version releases.
3057 to avoid doing this, and will aim to have a deprecation cycle of at least
3059 file, and have good tests in place. (This is good advice generally,
3069 du jour happens to be.
3072 these places, but don't expect anything to happen unless you actually
3080 probably bounce them on to there, unless you specifically ask me not to.
3082 means that if you ask me not to send your mail to RT, there's a much
3089 a completely stand-alone test that could be added to the Class::DBI
3092 needs to have a working database to show the problem, then this should
3093 preferably use SQLite, and come with all the code to set this up. The
3097 You don't need to include code for actually fixing the problem, but of
3098 course it's often nice if you can. I may choose to fix it in a different
3099 way, however, so it's often better to ask first whether I'd like a
3106 what version the patch is against. I tend to apply all patches manually,
3107 so I'm more interested in being able to see what you're doing than in
3108 being able to apply the patch cleanly. Code formatting isn't an issue,
3113 People often think that it's better for me to get one patch with a bunch
3116 is always better than one that needs a couple of hours to ponder and work
3120 I also like time to think about the wider implications of changes. Often
3121 a I<lot> of time. Feel free to remind me about things that I may have
3123 to them eventually.
3131 In general I am much more open to discussion on how best to provide the
3132 flexibility for you to make your Cool New Feature(tm) a plugin rather
3133 than adding it to Class::DBI itself.
3140 is about making life easier for people to write extensions - whether
3141 they're local to your own codebase or released for wider consumption.
3154 but isn't yet. (See above if you want to help to rectify this.)
3157 to contact me individually, then you'll have to track me down personally.
3167 The wiki has numerous references to other articles, presentations etc.
3170 of different approaches to database persistence, such as Class::DBI,
3182 much of the interface are easier to understand if you have an idea of