3 <TITLE>FS::Record - Database record objects</TITLE>
4 <LINK REV="made" HREF="mailto:ivan@rootwood.sisd.com">
13 <LI><A HREF="#NAME">NAME</A>
14 <LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
15 <LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
16 <LI><A HREF="#CONSTRUCTORS">CONSTRUCTORS</A>
17 <LI><A HREF="#METHODS">METHODS</A>
18 <LI><A HREF="#SUBROUTINES">SUBROUTINES</A>
19 <LI><A HREF="#VERSION">VERSION</A>
20 <LI><A HREF="#BUGS">BUGS</A>
21 <LI><A HREF="#SEE_ALSO">SEE ALSO</A>
27 <H1><A NAME="NAME">NAME</A></H1>
29 FS::Record - Database record objects
33 <H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
36 use FS::Record qw(dbh fields qsearch qsearchs dbdef);
39 <PRE> $record = new FS::Record 'table', \%hash;
40 $record = new FS::Record 'table', { 'column' => 'value', ... };
43 <PRE> $record = qsearchs FS::Record 'table', \%hash;
44 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
45 @records = qsearch FS::Record 'table', \%hash;
46 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
49 <PRE> $table = $record->table;
50 $dbdef_table = $record->dbdef_table;
53 <PRE> $value = $record->get('column');
54 $value = $record->getfield('column');
55 $value = $record->column;
58 <PRE> $record->set( 'column' => 'value' );
59 $record->setfield( 'column' => 'value' );
60 $record->column('value');
63 <PRE> %hash = $record->hash;
66 <PRE> $hashref = $record->hashref;
69 <PRE> $error = $record->insert;
70 #$error = $record->add; #depriciated
73 <PRE> $error = $record->delete;
74 #$error = $record->del; #depriciated
77 <PRE> $error = $new_record->replace($old_record);
78 #$error = $new_record->rep($old_record); #depriciated
81 <PRE> $value = $record->unique('column');
84 <PRE> $value = $record->ut_float('column');
85 $value = $record->ut_number('column');
86 $value = $record->ut_numbern('column');
87 $value = $record->ut_money('column');
88 $value = $record->ut_text('column');
89 $value = $record->ut_textn('column');
90 $value = $record->ut_alpha('column');
91 $value = $record->ut_alphan('column');
92 $value = $record->ut_phonen('column');
93 $value = $record->ut_anythingn('column');
96 <PRE> $dbdef = reload_dbdef;
97 $dbdef = reload_dbdef "/non/standard/filename";
101 <PRE> $quoted_value = _quote($value,'table','field');
105 $fields = hfields('table');
106 if ( $fields->{Field} ) { # etc.
109 <PRE> @fields = fields 'table'; #as a subroutine
110 @fields = $record->fields; #as a method call
114 <H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
116 (Mostly) object-oriented interface to database records. Records are
117 currently implemented on top of DBI. FS::Record is intended as a base class
118 for table-specific classes to inherit from, i.e. FS::cust_main.
122 <H1><A NAME="CONSTRUCTORS">CONSTRUCTORS</A></H1>
124 <DT><STRONG><A NAME="item_new">new [ TABLE, ] HASHREF</A></STRONG><DD>
126 Creates a new record. It doesn't store it in the database, though. See
127 <A HREF="#insert">insert</A> for that.
130 Note that the object stores this hash reference, not a distinct copy of the
131 hash it points to. You can ask the object for a copy with the <EM>hash</EM>
135 TABLE can only be omitted when a dervived class overrides the table method.
137 <DT><STRONG><A NAME="item_qsearch">qsearch TABLE, HASHREF</A></STRONG><DD>
139 Searches the database for all records matching (at least) the key/value
140 pairs in HASHREF. Returns all the records found as `FS::TABLE' objects if
141 that module is loaded (i.e. via `use FS::cust_main;'), otherwise returns
144 <DT><STRONG><A NAME="item_qsearchs">qsearchs TABLE, HASHREF</A></STRONG><DD>
146 Same as qsearch, except that if more than one record matches, it <STRONG>carp</STRONG>s but returns the first. If this happens, you either made a logic error in
147 asking for a single item, or your data is corrupted.
152 <H1><A NAME="METHODS">METHODS</A></H1>
154 <DT><STRONG><A NAME="item_table">table</A></STRONG><DD>
156 Returns the table name.
158 <DT><STRONG><A NAME="item_dbdef_table">dbdef_table</A></STRONG><DD>
160 Returns the FS::dbdef_table object for the table.
162 <DT><STRONG><A NAME="item_get">get, getfield COLUMN</A></STRONG><DD>
164 Returns the value of the column/field/key COLUMN.
166 <DT><STRONG><A NAME="item_set">set, setfield COLUMN, VALUE</A></STRONG><DD>
168 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
170 <DT><STRONG><A NAME="item_AUTLOADED">AUTLOADED METHODS</A></STRONG><DD>
172 $record->column is a synonym for $record->get('column');
175 $record->column('value') is a synonym for
176 $record->set('column','value');
178 <DT><STRONG><A NAME="item_hash">hash</A></STRONG><DD>
180 Returns a list of the column/value pairs, usually for assigning to a new
184 To make a distinct duplicate of an FS::Record object, you can do:
187 <PRE> $new = new FS::Record ( $old->table, { $old->hash } );
189 <DT><STRONG><A NAME="item_hashref">hashref</A></STRONG><DD>
191 Returns a reference to the column/value hash.
193 <DT><STRONG><A NAME="item_insert">insert</A></STRONG><DD>
195 Inserts this record to the database. If there is an error, returns the
196 error, otherwise returns false.
198 <DT><STRONG><A NAME="item_add">add</A></STRONG><DD>
200 Depriciated (use insert instead).
202 <DT><STRONG><A NAME="item_delete">delete</A></STRONG><DD>
204 Delete this record from the database. If there is an error, returns the
205 error, otherwise returns false.
207 <DT><STRONG><A NAME="item_del">del</A></STRONG><DD>
209 Depriciated (use delete instead).
211 <DT><STRONG><A NAME="item_replace">replace OLD_RECORD</A></STRONG><DD>
213 Replace the OLD_RECORD with this one in the database. If there is an error,
214 returns the error, otherwise returns false.
216 <DT><STRONG><A NAME="item_rep">rep</A></STRONG><DD>
218 Depriciated (use replace instead).
220 <DT><STRONG><A NAME="item_check">check</A></STRONG><DD>
222 Not yet implemented, croaks. Derived classes should provide a check method.
224 <DT><STRONG><A NAME="item_unique">unique COLUMN</A></STRONG><DD>
226 Replaces COLUMN in record with a unique number. Called by the <STRONG>add</STRONG> method on primary keys and single-field unique columns (see <A HREF="../FS/dbdef_table.html">FS::dbdef_table</A>). Returns the new value.
228 <DT><STRONG><A NAME="item_ut_float">ut_float COLUMN</A></STRONG><DD>
230 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
231 null. If there is an error, returns the error, otherwise returns false.
233 <DT><STRONG><A NAME="item_ut_number">ut_number COLUMN</A></STRONG><DD>
235 Check/untaint simple numeric data (whole numbers). May not be null. If
236 there is an error, returns the error, otherwise returns false.
238 <DT><STRONG><A NAME="item_ut_numbern">ut_numbern COLUMN</A></STRONG><DD>
240 Check/untaint simple numeric data (whole numbers). May be null. If there is
241 an error, returns the error, otherwise returns false.
243 <DT><STRONG><A NAME="item_ut_money">ut_money COLUMN</A></STRONG><DD>
245 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
246 is an error, returns the error, otherwise returns false.
248 <DT><STRONG><A NAME="item_ut_text">ut_text COLUMN</A></STRONG><DD>
250 Check/untaint text. Alphanumerics, spaces, and the following punctuation
251 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' `` , . ? /
252 May not be null. If there is an error, returns the error, otherwise returns
255 <DT><STRONG><A NAME="item_ut_textn">ut_textn COLUMN</A></STRONG><DD>
257 Check/untaint text. Alphanumerics, spaces, and the following punctuation
258 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' `` , . ? /
259 May be null. If there is an error, returns the error, otherwise returns
262 <DT><STRONG><A NAME="item_ut_alpha">ut_alpha COLUMN</A></STRONG><DD>
264 Check/untaint alphanumeric strings (no spaces). May not be null. If there
265 is an error, returns the error, otherwise returns false.
267 <DT><STRONG>ut_alpha COLUMN</STRONG><DD>
269 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
270 error, returns the error, otherwise returns false.
272 <DT><STRONG><A NAME="item_ut_phonen">ut_phonen COLUMN</A></STRONG><DD>
274 Check/untaint phone numbers. May be null. If there is an error, returns the
275 error, otherwise returns false.
277 <DT><STRONG><A NAME="item_ut_anything">ut_anything COLUMN</A></STRONG><DD>
279 Untaints arbitrary data. Be careful.
281 <DT><STRONG><A NAME="item_fields">fields [ TABLE ]</A></STRONG><DD>
283 This can be used as both a subroutine and a method call. It returns a list
284 of the columns in this record's table, or an explicitly specified table.
285 (See <A HREF="../FS/dbdef_table.html">FS::dbdef_table</A>).
287 <H1><A NAME="SUBROUTINES">SUBROUTINES</A></H1>
289 <DT><STRONG><A NAME="item_reload_dbdef">reload_dbdef([FILENAME])</A></STRONG><DD>
291 Load a database definition (see <A HREF="../FS/dbdef.html">FS::dbdef</A>), optionally from a non-default filename. This command is executed at
293 <EM>$FS::Record::setup_hack</EM> is true. Returns a FS::dbdef object.
295 <DT><STRONG><A NAME="item_dbdef">dbdef</A></STRONG><DD>
297 Returns the current database definition. See <A HREF="../FS/dbdef.html">FS::dbdef</A>.
299 <DT><STRONG><A NAME="item__quote">_quote VALUE, TABLE, COLUMN</A></STRONG><DD>
301 This is an internal function used to construct SQL statements. It returns
302 VALUE DBI-quoted (see <EM>DBI</EM>) unless VALUE is a number and the column type (see <A HREF="../FS/dbdef_column.html">FS::dbdef_column</A>) does not end in `char' or `binary'.
304 <DT><STRONG><A NAME="item_hfields">hfields TABLE</A></STRONG><DD>
306 This is depriciated. Don't use it.
309 It returns a hash-type list with the fields of this record's table set
313 <H1><A NAME="VERSION">VERSION</A></H1>
315 $Id: Record.html,v 1.1 1999-08-04 12:13:27 ivan Exp $
317 <H1><A NAME="BUGS">BUGS</A></H1>
319 This module should probably be renamed, since much of the functionality is
320 of general use. It is not completely unlike Adapter::DBI (see below).
323 Exported qsearch and qsearchs should be depriciated in favor of method
324 calls (against an FS::Record object like the old search and searchs that
325 qsearch and qsearchs were on top of.)
328 The whole fields / hfields mess should be removed.
331 The various WHERE clauses should be subroutined.
334 table string should be depriciated in favor of FS::dbdef_table.
337 No doubt we could benefit from a Tied hash. Documenting how exists /
338 defined true maps to the database (and WHERE clauses) would also help.
341 The ut_ methods should ask the dbdef for a default length.
344 ut_sqltype (like ut_varchar) should all be defined
347 A fallback check method should be provided whith uses the dbdef.
350 The ut_money method assumes money has two decimal digits.
353 The Pg money kludge in the new method only strips `$'.
356 The ut_phonen method assumes US-style phone numbers.
359 The _quote function should probably use ut_float instead of a regex.
362 All the subroutines probably should be methods, here or elsewhere.
365 Probably should borrow/use some dbdef methods where appropriate (like sub
368 <H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
370 <A HREF="../FS/dbdef.html">FS::dbdef</A>, <A HREF="../FS/UID.html">FS::UID</A>, <EM>DBI</EM>
375 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.