changes from 1.2.3 release, fixes from webdemo
[freeside.git] / htdocs / docs / man / FS / Record.html
1 <HTML>
2 <HEAD>
3 <TITLE>FS::Record - Database record objects</TITLE>
4 <LINK REV="made" HREF="mailto:none">
5 </HEAD>
6
7 <BODY>
8
9 <!-- INDEX BEGIN -->
10
11 <UL>
12
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>
22 </UL>
23 <!-- INDEX END -->
24
25 <HR>
26 <P>
27 <H1><A NAME="NAME">NAME</A></H1>
28 <P>
29 FS::Record - Database record objects
30
31 <P>
32 <HR>
33 <H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
34 <P>
35 <PRE>    use FS::Record;
36     use FS::Record qw(dbh fields qsearch qsearchs dbdef);
37 </PRE>
38 <P>
39 <PRE>    $record = new FS::Record 'table', \%hash;
40     $record = new FS::Record 'table', { 'column' =&gt; 'value', ... };
41 </PRE>
42 <P>
43 <PRE>    $record  = qsearchs FS::Record 'table', \%hash;
44     $record  = qsearchs FS::Record 'table', { 'column' =&gt; 'value', ... };
45     @records = qsearch  FS::Record 'table', \%hash; 
46     @records = qsearch  FS::Record 'table', { 'column' =&gt; 'value', ... };
47 </PRE>
48 <P>
49 <PRE>    $table = $record-&gt;table;
50     $dbdef_table = $record-&gt;dbdef_table;
51 </PRE>
52 <P>
53 <PRE>    $value = $record-&gt;get('column');
54     $value = $record-&gt;getfield('column');
55     $value = $record-&gt;column;
56 </PRE>
57 <P>
58 <PRE>    $record-&gt;set( 'column' =&gt; 'value' );
59     $record-&gt;setfield( 'column' =&gt; 'value' );
60     $record-&gt;column('value');
61 </PRE>
62 <P>
63 <PRE>    %hash = $record-&gt;hash;
64 </PRE>
65 <P>
66 <PRE>    $hashref = $record-&gt;hashref;
67 </PRE>
68 <P>
69 <PRE>    $error = $record-&gt;insert;
70     #$error = $record-&gt;add; #depriciated
71 </PRE>
72 <P>
73 <PRE>    $error = $record-&gt;delete;
74     #$error = $record-&gt;del; #depriciated
75 </PRE>
76 <P>
77 <PRE>    $error = $new_record-&gt;replace($old_record);
78     #$error = $new_record-&gt;rep($old_record); #depriciated
79 </PRE>
80 <P>
81 <PRE>    $value = $record-&gt;unique('column');
82 </PRE>
83 <P>
84 <PRE>    $value = $record-&gt;ut_float('column');
85     $value = $record-&gt;ut_number('column');
86     $value = $record-&gt;ut_numbern('column');
87     $value = $record-&gt;ut_money('column');
88     $value = $record-&gt;ut_text('column');
89     $value = $record-&gt;ut_textn('column');
90     $value = $record-&gt;ut_alpha('column');
91     $value = $record-&gt;ut_alphan('column');
92     $value = $record-&gt;ut_phonen('column');
93     $value = $record-&gt;ut_anythingn('column');
94 </PRE>
95 <P>
96 <PRE>    $dbdef = reload_dbdef;
97     $dbdef = reload_dbdef &quot;/non/standard/filename&quot;;
98     $dbdef = dbdef;
99 </PRE>
100 <P>
101 <PRE>    $quoted_value = _quote($value,'table','field');
102 </PRE>
103 <P>
104 <PRE>    #depriciated
105     $fields = hfields('table');
106     if ( $fields-&gt;{Field} ) { # etc.
107 </PRE>
108 <P>
109 <PRE>    @fields = fields 'table'; #as a subroutine
110     @fields = $record-&gt;fields; #as a method call
111 </PRE>
112 <P>
113 <HR>
114 <H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
115 <P>
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.
119
120 <P>
121 <HR>
122 <H1><A NAME="CONSTRUCTORS">CONSTRUCTORS</A></H1>
123 <DL>
124 <DT><STRONG><A NAME="item_new">new [ TABLE, ] HASHREF</A></STRONG><DD>
125 <P>
126 Creates a new record. It doesn't store it in the database, though. See
127 <A HREF="#insert">insert</A> for that.
128
129 <P>
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> 
132 method.
133
134 <P>
135 TABLE can only be omitted when a dervived class overrides the table method.
136
137 <DT><STRONG><A NAME="item_qsearch">qsearch TABLE, HASHREF</A></STRONG><DD>
138 <P>
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
142 FS::Record objects.
143
144 <DT><STRONG><A NAME="item_qsearchs">qsearchs TABLE, HASHREF</A></STRONG><DD>
145 <P>
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.
148
149 </DL>
150 <P>
151 <HR>
152 <H1><A NAME="METHODS">METHODS</A></H1>
153 <DL>
154 <DT><STRONG><A NAME="item_table">table</A></STRONG><DD>
155 <P>
156 Returns the table name.
157
158 <DT><STRONG><A NAME="item_dbdef_table">dbdef_table</A></STRONG><DD>
159 <P>
160 Returns the FS::dbdef_table object for the table.
161
162 <DT><STRONG><A NAME="item_get">get, getfield COLUMN</A></STRONG><DD>
163 <P>
164 Returns the value of the column/field/key COLUMN.
165
166 <DT><STRONG><A NAME="item_set">set, setfield COLUMN, VALUE</A></STRONG><DD>
167 <P>
168 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
169
170 <DT><STRONG><A NAME="item_AUTLOADED">AUTLOADED METHODS</A></STRONG><DD>
171 <P>
172 $record-&gt;column is a synonym for $record-&gt;get('column');
173
174 <P>
175 $record-&gt;column('value') is a synonym for
176 $record-&gt;set('column','value');
177
178 <DT><STRONG><A NAME="item_hash">hash</A></STRONG><DD>
179 <P>
180 Returns a list of the column/value pairs, usually for assigning to a new
181 hash.
182
183 <P>
184 To make a distinct duplicate of an FS::Record object, you can do:
185
186 <P>
187 <PRE>    $new = new FS::Record ( $old-&gt;table, { $old-&gt;hash } );
188 </PRE>
189 <DT><STRONG><A NAME="item_hashref">hashref</A></STRONG><DD>
190 <P>
191 Returns a reference to the column/value hash.
192
193 <DT><STRONG><A NAME="item_insert">insert</A></STRONG><DD>
194 <P>
195 Inserts this record to the database. If there is an error, returns the
196 error, otherwise returns false.
197
198 <DT><STRONG><A NAME="item_add">add</A></STRONG><DD>
199 <P>
200 Depriciated (use insert instead).
201
202 <DT><STRONG><A NAME="item_delete">delete</A></STRONG><DD>
203 <P>
204 Delete this record from the database. If there is an error, returns the
205 error, otherwise returns false.
206
207 <DT><STRONG><A NAME="item_del">del</A></STRONG><DD>
208 <P>
209 Depriciated (use delete instead).
210
211 <DT><STRONG><A NAME="item_replace">replace OLD_RECORD</A></STRONG><DD>
212 <P>
213 Replace the OLD_RECORD with this one in the database. If there is an error,
214 returns the error, otherwise returns false.
215
216 <DT><STRONG><A NAME="item_rep">rep</A></STRONG><DD>
217 <P>
218 Depriciated (use replace instead).
219
220 <DT><STRONG><A NAME="item_check">check</A></STRONG><DD>
221 <P>
222 Not yet implemented, croaks. Derived classes should provide a check method.
223
224 <DT><STRONG><A NAME="item_unique">unique COLUMN</A></STRONG><DD>
225 <P>
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.
227
228 <DT><STRONG><A NAME="item_ut_float">ut_float COLUMN</A></STRONG><DD>
229 <P>
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.
232
233 <DT><STRONG><A NAME="item_ut_number">ut_number COLUMN</A></STRONG><DD>
234 <P>
235 Check/untaint simple numeric data (whole numbers). May not be null. If
236 there is an error, returns the error, otherwise returns false.
237
238 <DT><STRONG><A NAME="item_ut_numbern">ut_numbern COLUMN</A></STRONG><DD>
239 <P>
240 Check/untaint simple numeric data (whole numbers). May be null. If there is
241 an error, returns the error, otherwise returns false.
242
243 <DT><STRONG><A NAME="item_ut_money">ut_money COLUMN</A></STRONG><DD>
244 <P>
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.
247
248 <DT><STRONG><A NAME="item_ut_text">ut_text COLUMN</A></STRONG><DD>
249 <P>
250 Check/untaint text. Alphanumerics, spaces, and the following punctuation
251 symbols are currently permitted: ! @ # $ % &amp; ( ) - + ; : ' `` , . ? /
252 May not be null. If there is an error, returns the error, otherwise returns
253 false.
254
255 <DT><STRONG><A NAME="item_ut_textn">ut_textn COLUMN</A></STRONG><DD>
256 <P>
257 Check/untaint text. Alphanumerics, spaces, and the following punctuation
258 symbols are currently permitted: ! @ # $ % &amp; ( ) - + ; : ' `` , . ? /
259 May be null. If there is an error, returns the error, otherwise returns
260 false.
261
262 <DT><STRONG><A NAME="item_ut_alpha">ut_alpha COLUMN</A></STRONG><DD>
263 <P>
264 Check/untaint alphanumeric strings (no spaces). May not be null. If there
265 is an error, returns the error, otherwise returns false.
266
267 <DT><STRONG>ut_alpha COLUMN</STRONG><DD>
268 <P>
269 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
270 error, returns the error, otherwise returns false.
271
272 <DT><STRONG><A NAME="item_ut_phonen">ut_phonen COLUMN</A></STRONG><DD>
273 <P>
274 Check/untaint phone numbers. May be null. If there is an error, returns the
275 error, otherwise returns false.
276
277 <DT><STRONG><A NAME="item_ut_anything">ut_anything COLUMN</A></STRONG><DD>
278 <P>
279 Untaints arbitrary data. Be careful.
280
281 <DT><STRONG><A NAME="item_fields">fields [ TABLE ]</A></STRONG><DD>
282 <P>
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>).
286
287 <H1><A NAME="SUBROUTINES">SUBROUTINES</A></H1>
288 <DL>
289 <DT><STRONG><A NAME="item_reload_dbdef">reload_dbdef([FILENAME])</A></STRONG><DD>
290 <P>
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
292 startup unless
293 <EM>$FS::Record::setup_hack</EM> is true. Returns a FS::dbdef object.
294
295 <DT><STRONG><A NAME="item_dbdef">dbdef</A></STRONG><DD>
296 <P>
297 Returns the current database definition. See <A HREF="../FS/dbdef.html">FS::dbdef</A>.
298
299 <DT><STRONG><A NAME="item__quote">_quote VALUE, TABLE, COLUMN</A></STRONG><DD>
300 <P>
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'.
303
304 <DT><STRONG><A NAME="item_hfields">hfields TABLE</A></STRONG><DD>
305 <P>
306 This is depriciated. Don't use it.
307
308 <P>
309 It returns a hash-type list with the fields of this record's table set
310 true.
311
312 </DL>
313 <H1><A NAME="VERSION">VERSION</A></H1>
314 <P>
315 $Id: Record.html,v 1.2 2000-03-03 18:22:42 ivan Exp $
316
317 <H1><A NAME="BUGS">BUGS</A></H1>
318 <P>
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).
321
322 <P>
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.)
326
327 <P>
328 The whole fields / hfields mess should be removed.
329
330 <P>
331 The various WHERE clauses should be subroutined.
332
333 <P>
334 table string should be depriciated in favor of FS::dbdef_table.
335
336 <P>
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.
339
340 <P>
341 The ut_ methods should ask the dbdef for a default length.
342
343 <P>
344 ut_sqltype (like ut_varchar) should all be defined
345
346 <P>
347 A fallback check method should be provided whith uses the dbdef.
348
349 <P>
350 The ut_money method assumes money has two decimal digits.
351
352 <P>
353 The Pg money kludge in the new method only strips `$'.
354
355 <P>
356 The ut_phonen method assumes US-style phone numbers.
357
358 <P>
359 The _quote function should probably use ut_float instead of a regex.
360
361 <P>
362 All the subroutines probably should be methods, here or elsewhere.
363
364 <P>
365 Probably should borrow/use some dbdef methods where appropriate (like sub
366 fields)
367
368 <H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
369 <P>
370 <A HREF="../FS/dbdef.html">FS::dbdef</A>, <A HREF="../FS/UID.html">FS::UID</A>, <EM>DBI</EM>
371
372
373
374 <P>
375 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.
376
377 </DL>
378 </BODY>
379
380 </HTML>