- web interface for hourly account charges!
[freeside.git] / httemplate / docs / man / FS / Record.html
1 <HTML>
2 <HEAD>
3 <TITLE>FS::Record - Database record objects</TITLE>
4 <LINK REV="made" HREF="mailto:perl@packages.debian.org">
5 </HEAD>
6
7 <BODY>
8
9 <A NAME="__index__"></A>
10 <!-- INDEX BEGIN -->
11
12 <UL>
13
14         <LI><A HREF="#name">NAME</A></LI>
15         <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
16         <LI><A HREF="#description">DESCRIPTION</A></LI>
17         <LI><A HREF="#constructors">CONSTRUCTORS</A></LI>
18         <LI><A HREF="#methods">METHODS</A></LI>
19         <LI><A HREF="#subroutines">SUBROUTINES</A></LI>
20         <LI><A HREF="#bugs">BUGS</A></LI>
21         <LI><A HREF="#see also">SEE ALSO</A></LI>
22 </UL>
23 <!-- INDEX END -->
24
25 <HR>
26 <P>
27 <H1><A NAME="name">NAME</A></H1>
28 <P>FS::Record - Database record objects</P>
29 <P>
30 <HR>
31 <H1><A NAME="synopsis">SYNOPSIS</A></H1>
32 <PRE>
33     use FS::Record;
34     use FS::Record qw(dbh fields qsearch qsearchs dbdef);</PRE>
35 <PRE>
36     $record = new FS::Record 'table', \%hash;
37     $record = new FS::Record 'table', { 'column' =&gt; 'value', ... };</PRE>
38 <PRE>
39     $record  = qsearchs FS::Record 'table', \%hash;
40     $record  = qsearchs FS::Record 'table', { 'column' =&gt; 'value', ... };
41     @records = qsearch  FS::Record 'table', \%hash; 
42     @records = qsearch  FS::Record 'table', { 'column' =&gt; 'value', ... };</PRE>
43 <PRE>
44     $table = $record-&gt;table;
45     $dbdef_table = $record-&gt;dbdef_table;</PRE>
46 <PRE>
47     $value = $record-&gt;get('column');
48     $value = $record-&gt;getfield('column');
49     $value = $record-&gt;column;</PRE>
50 <PRE>
51     $record-&gt;set( 'column' =&gt; 'value' );
52     $record-&gt;setfield( 'column' =&gt; 'value' );
53     $record-&gt;column('value');</PRE>
54 <PRE>
55     %hash = $record-&gt;hash;</PRE>
56 <PRE>
57     $hashref = $record-&gt;hashref;</PRE>
58 <PRE>
59     $error = $record-&gt;insert;
60     #$error = $record-&gt;add; #depriciated</PRE>
61 <PRE>
62     $error = $record-&gt;delete;
63     #$error = $record-&gt;del; #depriciated</PRE>
64 <PRE>
65     $error = $new_record-&gt;replace($old_record);
66     #$error = $new_record-&gt;rep($old_record); #depriciated</PRE>
67 <PRE>
68     $value = $record-&gt;unique('column');</PRE>
69 <PRE>
70     $error = $record-&gt;ut_float('column');
71     $error = $record-&gt;ut_number('column');
72     $error = $record-&gt;ut_numbern('column');
73     $error = $record-&gt;ut_money('column');
74     $error = $record-&gt;ut_text('column');
75     $error = $record-&gt;ut_textn('column');
76     $error = $record-&gt;ut_alpha('column');
77     $error = $record-&gt;ut_alphan('column');
78     $error = $record-&gt;ut_phonen('column');
79     $error = $record-&gt;ut_anything('column');
80     $error = $record-&gt;ut_name('column');</PRE>
81 <PRE>
82     $dbdef = reload_dbdef;
83     $dbdef = reload_dbdef &quot;/non/standard/filename&quot;;
84     $dbdef = dbdef;</PRE>
85 <PRE>
86     $quoted_value = _quote($value,'table','field');</PRE>
87 <PRE>
88     #depriciated
89     $fields = hfields('table');
90     if ( $fields-&gt;{Field} ) { # etc.</PRE>
91 <PRE>
92     @fields = fields 'table'; #as a subroutine
93     @fields = $record-&gt;fields; #as a method call</PRE>
94 <P>
95 <HR>
96 <H1><A NAME="description">DESCRIPTION</A></H1>
97 <P>(Mostly) object-oriented interface to database records.  Records are currently
98 implemented on top of DBI.  FS::Record is intended as a base class for
99 table-specific classes to inherit from, i.e. FS::cust_main.</P>
100 <P>
101 <HR>
102 <H1><A NAME="constructors">CONSTRUCTORS</A></H1>
103 <DL>
104 <DT><STRONG><A NAME="item_new_%5B_TABLE%2C_%5D_HASHREF">new [ TABLE, ] HASHREF</A></STRONG><BR>
105 <DD>
106 Creates a new record.  It doesn't store it in the database, though.  See
107 <A HREF="#insert">insert</A> for that.
108 <P>Note that the object stores this hash reference, not a distinct copy of the
109 hash it points to.  You can ask the object for a copy with the <EM>hash</EM> 
110 method.</P>
111 <P>TABLE can only be omitted when a dervived class overrides the table method.</P>
112 <P></P>
113 <DT><STRONG><A NAME="item_qsearch_TABLE%2C_HASHREF%2C_SELECT%2C_EXTRA_SQL">qsearch TABLE, HASHREF, SELECT, EXTRA_SQL</A></STRONG><BR>
114 <DD>
115 Searches the database for all records matching (at least) the key/value pairs
116 in HASHREF.  Returns all the records found as `FS::TABLE' objects if that
117 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
118 objects.
119 <P>###oops, argh, FS::Record::new only lets us create database fields.
120 #Normal behaviour if SELECT is not specified is `*', as in
121 #<CODE>SELECT * FROM table WHERE ...</CODE>.  However, there is an experimental new
122 #feature where you can specify SELECT - remember, the objects returned,
123 #although blessed into the appropriate `FS::TABLE' package, will only have the
124 #fields you specify.  This might have unwanted results if you then go calling
125 #regular FS::TABLE methods
126 #on it.</P>
127 <P></P>
128 <DT><STRONG><A NAME="item_jsearch_TABLE%2C_HASHREF%2C_SELECT%2C_EXTRA_SQL%2C">jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY</A></STRONG><BR>
129 <DD>
130 Experimental JOINed search method.  Using this method, you can execute a
131 single SELECT spanning multiple tables, and cache the results for subsequent
132 method calls.  Interface will almost definately change in an incompatible
133 fashion.
134 <P>Arguments:</P>
135 <P></P>
136 <DT><STRONG><A NAME="item_qsearchs">qsearchs TABLE, HASHREF</A></STRONG><BR>
137 <DD>
138 Same as qsearch, except that if more than one record matches, it <STRONG>carp</STRONG>s but
139 returns the first.  If this happens, you either made a logic error in asking
140 for a single item, or your data is corrupted.
141 <P></P></DL>
142 <P>
143 <HR>
144 <H1><A NAME="methods">METHODS</A></H1>
145 <DL>
146 <DT><STRONG><A NAME="item_table">table</A></STRONG><BR>
147 <DD>
148 Returns the table name.
149 <P></P>
150 <DT><STRONG><A NAME="item_dbdef_table">dbdef_table</A></STRONG><BR>
151 <DD>
152 Returns the FS::dbdef_table object for the table.
153 <P></P>
154 <DT><STRONG><A NAME="item_get%2C_getfield_COLUMN">get, getfield COLUMN</A></STRONG><BR>
155 <DD>
156 Returns the value of the column/field/key COLUMN.
157 <P></P>
158 <DT><STRONG><A NAME="item_set%2C_setfield_COLUMN%2C_VALUE">set, setfield COLUMN, VALUE</A></STRONG><BR>
159 <DD>
160 Sets the value of the column/field/key COLUMN to VALUE.  Returns VALUE.
161 <P></P>
162 <DT><STRONG><A NAME="item_AUTLOADED_METHODS">AUTLOADED METHODS</A></STRONG><BR>
163 <DD>
164 $record-&gt;column is a synonym for $record-&gt;get('column');
165 <P>$record-&gt;<CODE>column('value')</CODE> is a synonym for $record-&gt;set('column','value');</P>
166 <P></P>
167 <DT><STRONG><A NAME="item_hash">hash</A></STRONG><BR>
168 <DD>
169 Returns a list of the column/value pairs, usually for assigning to a new hash.
170 <P>To make a distinct duplicate of an FS::Record object, you can do:</P>
171 <PRE>
172     $new = new FS::Record ( $old-&gt;table, { $old-&gt;hash } );</PRE>
173 <P></P>
174 <DT><STRONG><A NAME="item_hashref">hashref</A></STRONG><BR>
175 <DD>
176 Returns a reference to the column/value hash.
177 <P></P>
178 <DT><STRONG><A NAME="item_insert">insert</A></STRONG><BR>
179 <DD>
180 Inserts this record to the database.  If there is an error, returns the error,
181 otherwise returns false.
182 <P></P>
183 <DT><STRONG><A NAME="item_add">add</A></STRONG><BR>
184 <DD>
185 Depriciated (use insert instead).
186 <P></P>
187 <DT><STRONG><A NAME="item_delete">delete</A></STRONG><BR>
188 <DD>
189 Delete this record from the database.  If there is an error, returns the error,
190 otherwise returns false.
191 <P></P>
192 <DT><STRONG><A NAME="item_del">del</A></STRONG><BR>
193 <DD>
194 Depriciated (use delete instead).
195 <P></P>
196 <DT><STRONG><A NAME="item_replace_OLD_RECORD">replace OLD_RECORD</A></STRONG><BR>
197 <DD>
198 Replace the OLD_RECORD with this one in the database.  If there is an error,
199 returns the error, otherwise returns false.
200 <P></P>
201 <DT><STRONG><A NAME="item_rep">rep</A></STRONG><BR>
202 <DD>
203 Depriciated (use replace instead).
204 <P></P>
205 <DT><STRONG><A NAME="item_check">check</A></STRONG><BR>
206 <DD>
207 Not yet implemented, croaks.  Derived classes should provide a check method.
208 <P></P>
209 <DT><STRONG><A NAME="item_unique">unique COLUMN</A></STRONG><BR>
210 <DD>
211 Replaces COLUMN in record with a unique number.  Called by the <STRONG>add</STRONG> method
212 on primary keys and single-field unique columns (see <A HREF="../DBIx/DBSchema/Table.html">the DBIx::DBSchema::Table manpage</A>).
213 Returns the new value.
214 <P></P>
215 <DT><STRONG><A NAME="item_ut_float_COLUMN">ut_float COLUMN</A></STRONG><BR>
216 <DD>
217 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10.  May not be
218 null.  If there is an error, returns the error, otherwise returns false.
219 <P></P>
220 <DT><STRONG><A NAME="item_ut_number_COLUMN">ut_number COLUMN</A></STRONG><BR>
221 <DD>
222 Check/untaint simple numeric data (whole numbers).  May not be null.  If there
223 is an error, returns the error, otherwise returns false.
224 <P></P>
225 <DT><STRONG><A NAME="item_ut_numbern_COLUMN">ut_numbern COLUMN</A></STRONG><BR>
226 <DD>
227 Check/untaint simple numeric data (whole numbers).  May be null.  If there is
228 an error, returns the error, otherwise returns false.
229 <P></P>
230 <DT><STRONG><A NAME="item_ut_money_COLUMN">ut_money COLUMN</A></STRONG><BR>
231 <DD>
232 Check/untaint monetary numbers.  May be negative.  Set to 0 if null.  If there
233 is an error, returns the error, otherwise returns false.
234 <P></P>
235 <DT><STRONG><A NAME="item_ut_text_COLUMN">ut_text COLUMN</A></STRONG><BR>
236 <DD>
237 Check/untaint text.  Alphanumerics, spaces, and the following punctuation
238 symbols are currently permitted: ! @ # $ % &amp; ( ) - + ; : ' `` , . ? /
239 May not be null.  If there is an error, returns the error, otherwise returns
240 false.
241 <P></P>
242 <DT><STRONG><A NAME="item_ut_textn_COLUMN">ut_textn COLUMN</A></STRONG><BR>
243 <DD>
244 Check/untaint text.  Alphanumerics, spaces, and the following punctuation
245 symbols are currently permitted: ! @ # $ % &amp; ( ) - + ; : ' `` , . ? /
246 May be null.  If there is an error, returns the error, otherwise returns false.
247 <P></P>
248 <DT><STRONG><A NAME="item_ut_alpha_COLUMN">ut_alpha COLUMN</A></STRONG><BR>
249 <DD>
250 Check/untaint alphanumeric strings (no spaces).  May not be null.  If there is
251 an error, returns the error, otherwise returns false.
252 <P></P>
253 <DT><STRONG>ut_alpha COLUMN</STRONG><BR>
254 <DD>
255 Check/untaint alphanumeric strings (no spaces).  May be null.  If there is an
256 error, returns the error, otherwise returns false.
257 <P></P>
258 <DT><STRONG><A NAME="item_ut_phonen_COLUMN_%5B_COUNTRY_%5D">ut_phonen COLUMN [ COUNTRY ]</A></STRONG><BR>
259 <DD>
260 Check/untaint phone numbers.  May be null.  If there is an error, returns
261 the error, otherwise returns false.
262 <P>Takes an optional two-letter ISO country code; without it or with unsupported
263 countries, ut_phonen simply calls ut_alphan.</P>
264 <P></P>
265 <DT><STRONG><A NAME="item_ut_ip_COLUMN">ut_ip COLUMN</A></STRONG><BR>
266 <DD>
267 Check/untaint ip addresses.  IPv4 only for now.
268 <P></P>
269 <DT><STRONG><A NAME="item_ut_ipn_COLUMN">ut_ipn COLUMN</A></STRONG><BR>
270 <DD>
271 Check/untaint ip addresses.  IPv4 only for now.  May be null.
272 <P></P>
273 <DT><STRONG><A NAME="item_ut_domain_COLUMN">ut_domain COLUMN</A></STRONG><BR>
274 <DD>
275 Check/untaint host and domain names.
276 <P></P>
277 <DT><STRONG><A NAME="item_ut_name_COLUMN">ut_name COLUMN</A></STRONG><BR>
278 <DD>
279 Check/untaint proper names; allows alphanumerics, spaces and the following
280 punctuation: , . - '
281 <P>May not be null.</P>
282 <P></P>
283 <DT><STRONG><A NAME="item_ut_zip_COLUMN">ut_zip COLUMN</A></STRONG><BR>
284 <DD>
285 Check/untaint zip codes.
286 <P></P>
287 <DT><STRONG><A NAME="item_ut_country_COLUMN">ut_country COLUMN</A></STRONG><BR>
288 <DD>
289 Check/untaint country codes.  Country names are changed to codes, if possible -
290 see <A HREF="../Locale/Country.html">the Locale::Country manpage</A>.
291 <P></P>
292 <DT><STRONG><A NAME="item_ut_anything_COLUMN">ut_anything COLUMN</A></STRONG><BR>
293 <DD>
294 Untaints arbitrary data.  Be careful.
295 <P></P>
296 <DT><STRONG><A NAME="item_ut_enum_COLUMN_CHOICES_ARRAYREF">ut_enum COLUMN CHOICES_ARRAYREF</A></STRONG><BR>
297 <DD>
298 Check/untaint a column, supplying all possible choices, like the ``enum'' type.
299 <P></P>
300 <DT><STRONG><A NAME="item_fields_%5B_TABLE_%5D">fields [ TABLE ]</A></STRONG><BR>
301 <DD>
302 This can be used as both a subroutine and a method call.  It returns a list
303 of the columns in this record's table, or an explicitly specified table.
304 (See <A HREF="../DBIx/DBSchema/Table.html">the DBIx::DBSchema::Table manpage</A>).
305 <P></P></DL>
306 <P>
307 <HR>
308 <H1><A NAME="subroutines">SUBROUTINES</A></H1>
309 <DL>
310 <DT><STRONG><A NAME="item_reload_dbdef"><CODE>reload_dbdef([FILENAME])</CODE></A></STRONG><BR>
311 <DD>
312 Load a database definition (see <A HREF="../DBIx/DBSchema.html">the DBIx::DBSchema manpage</A>), optionally from a
313 non-default filename.  This command is executed at startup unless
314 <EM>$FS::Record::setup_hack</EM> is true.  Returns a DBIx::DBSchema object.
315 <P></P>
316 <DT><STRONG><A NAME="item_dbdef">dbdef</A></STRONG><BR>
317 <DD>
318 Returns the current database definition.  See <A HREF="../FS/dbdef.html">the FS::dbdef manpage</A>.
319 <P></P>
320 <DT><STRONG><A NAME="item__quote_VALUE%2C_TABLE%2C_COLUMN">_quote VALUE, TABLE, COLUMN</A></STRONG><BR>
321 <DD>
322 This is an internal function used to construct SQL statements.  It returns
323 VALUE DBI-quoted (see <EM>DBI/``quote''</EM>) unless VALUE is a number and the column
324 type (see <A HREF="../FS/dbdef_column.html">the FS::dbdef_column manpage</A>) does not end in `char' or `binary'.
325 <P></P>
326 <DT><STRONG><A NAME="item_hfields">hfields TABLE</A></STRONG><BR>
327 <DD>
328 This is depriciated.  Don't use it.
329 <P>It returns a hash-type list with the fields of this record's table set true.</P>
330 <P></P></DL>
331 <P>
332 <HR>
333 <H1><A NAME="bugs">BUGS</A></H1>
334 <P>This module should probably be renamed, since much of the functionality is
335 of general use.  It is not completely unlike Adapter::DBI (see below).</P>
336 <P>Exported qsearch and qsearchs should be depriciated in favor of method calls
337 (against an FS::Record object like the old search and searchs that qsearch
338 and qsearchs were on top of.)</P>
339 <P>The whole fields / hfields mess should be removed.</P>
340 <P>The various WHERE clauses should be subroutined.</P>
341 <P>table string should be depriciated in favor of FS::dbdef_table.</P>
342 <P>No doubt we could benefit from a Tied hash.  Documenting how exists / defined
343 true maps to the database (and WHERE clauses) would also help.</P>
344 <P>The ut_ methods should ask the dbdef for a default length.</P>
345 <P>ut_sqltype (like ut_varchar) should all be defined</P>
346 <P>A fallback check method should be provided which uses the dbdef.</P>
347 <P>The ut_money method assumes money has two decimal digits.</P>
348 <P>The Pg money kludge in the new method only strips `$'.</P>
349 <P>The ut_phonen method only checks US-style phone numbers.</P>
350 <P>The _quote function should probably use ut_float instead of a regex.</P>
351 <P>All the subroutines probably should be methods, here or elsewhere.</P>
352 <P>Probably should borrow/use some dbdef methods where appropriate (like sub
353 fields)</P>
354 <P>As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
355 or allow it to be set.  Working around it is ugly any way around - DBI should
356 be fixed.  (only affects RDBMS which return uppercase column names)</P>
357 <P>ut_zip should take an optional country like ut_phone.</P>
358 <P>
359 <HR>
360 <H1><A NAME="see also">SEE ALSO</A></H1>
361 <P><A HREF="../DBIx/DBSchema.html">the DBIx::DBSchema manpage</A>, <A HREF=".././FS/UID.html">the FS::UID manpage</A>, <EM>DBI</EM></P>
362 <P>Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.</P>
363
364 </BODY>
365
366 </HTML>