4 use vars qw( $AUTOLOAD @ISA @EXPORT_OK $DEBUG
5 $me %virtual_fields_cache $nowarn_identical );
7 use Carp qw(carp cluck croak confess);
10 use DBI qw(:sql_types);
11 use DBIx::DBSchema 0.25;
12 use FS::UID qw(dbh getotaker datasrc driver_name);
13 use FS::Schema qw(dbdef);
15 use FS::Msgcat qw(gettext);
18 use FS::part_virtual_field;
24 #export dbdef for now... everything else expects to find it here
25 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch);
30 $nowarn_identical = 0;
38 FS::UID->install_callback( sub {
40 $File::CounterFile::DEFAULT_DIR = "/usr/local/etc/freeside/counters.". datasrc;
45 FS::Record - Database record objects
50 use FS::Record qw(dbh fields qsearch qsearchs);
52 $record = new FS::Record 'table', \%hash;
53 $record = new FS::Record 'table', { 'column' => 'value', ... };
55 $record = qsearchs FS::Record 'table', \%hash;
56 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
57 @records = qsearch FS::Record 'table', \%hash;
58 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
60 $table = $record->table;
61 $dbdef_table = $record->dbdef_table;
63 $value = $record->get('column');
64 $value = $record->getfield('column');
65 $value = $record->column;
67 $record->set( 'column' => 'value' );
68 $record->setfield( 'column' => 'value' );
69 $record->column('value');
71 %hash = $record->hash;
73 $hashref = $record->hashref;
75 $error = $record->insert;
77 $error = $record->delete;
79 $error = $new_record->replace($old_record);
81 # external use deprecated - handled by the database (at least for Pg, mysql)
82 $value = $record->unique('column');
84 $error = $record->ut_float('column');
85 $error = $record->ut_number('column');
86 $error = $record->ut_numbern('column');
87 $error = $record->ut_money('column');
88 $error = $record->ut_text('column');
89 $error = $record->ut_textn('column');
90 $error = $record->ut_alpha('column');
91 $error = $record->ut_alphan('column');
92 $error = $record->ut_phonen('column');
93 $error = $record->ut_anything('column');
94 $error = $record->ut_name('column');
96 $quoted_value = _quote($value,'table','field');
99 $fields = hfields('table');
100 if ( $fields->{Field} ) { # etc.
102 @fields = fields 'table'; #as a subroutine
103 @fields = $record->fields; #as a method call
108 (Mostly) object-oriented interface to database records. Records are currently
109 implemented on top of DBI. FS::Record is intended as a base class for
110 table-specific classes to inherit from, i.e. FS::cust_main.
116 =item new [ TABLE, ] HASHREF
118 Creates a new record. It doesn't store it in the database, though. See
119 L<"insert"> for that.
121 Note that the object stores this hash reference, not a distinct copy of the
122 hash it points to. You can ask the object for a copy with the I<hash>
125 TABLE can only be omitted when a dervived class overrides the table method.
131 my $class = ref($proto) || $proto;
133 bless ($self, $class);
135 unless ( defined ( $self->table ) ) {
136 $self->{'Table'} = shift;
137 carp "warning: FS::Record::new called with table name ". $self->{'Table'};
140 $self->{'Hash'} = shift;
142 foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) {
143 $self->{'Hash'}{$field}='';
146 $self->_rebless if $self->can('_rebless');
148 $self->{'modified'} = 0;
150 $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
157 my $class = ref($proto) || $proto;
159 bless ($self, $class);
161 $self->{'Table'} = shift unless defined ( $self->table );
163 my $hashref = $self->{'Hash'} = shift;
165 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
166 my $obj = $cache->cache->{$hashref->{$cache->key}};
167 $obj->_cache($hashref, $cache) if $obj->can('_cache');
170 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
177 my $class = ref($proto) || $proto;
179 bless ($self, $class);
180 if ( defined $self->table ) {
181 cluck "create constructor is deprecated, use new!";
184 croak "FS::Record::create called (not from a subclass)!";
188 =item qsearch PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
190 Searches the database for all records matching (at least) the key/value pairs
191 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
192 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
195 The preferred usage is to pass a hash reference of named parameters:
197 my @records = qsearch( {
198 'table' => 'table_name',
199 'hashref' => { 'field' => 'value'
200 'field' => { 'op' => '<',
205 #these are optional...
207 'extra_sql' => 'AND field ',
208 #'cache_obj' => '', #optional
209 'addl_from' => 'LEFT JOIN othtable USING ( field )',
213 Much code still uses old-style positional parameters, this is also probably
214 fine in the common case where there are only two parameters:
216 my @records = qsearch( 'table', { 'field' => 'value' } );
218 ###oops, argh, FS::Record::new only lets us create database fields.
219 #Normal behaviour if SELECT is not specified is `*', as in
220 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
221 #feature where you can specify SELECT - remember, the objects returned,
222 #although blessed into the appropriate `FS::TABLE' package, will only have the
223 #fields you specify. This might have unwanted results if you then go calling
224 #regular FS::TABLE methods
230 my($stable, $record, $select, $extra_sql, $cache, $addl_from );
231 if ( ref($_[0]) ) { #hashref for now, eventually maybe accept a list too
233 $stable = $opt->{'table'} or die "table name is required";
234 $record = $opt->{'hashref'} || {};
235 $select = $opt->{'select'} || '*';
236 $extra_sql = $opt->{'extra_sql'} || '';
237 $cache = $opt->{'cache_obj'} || '';
238 $addl_from = $opt->{'addl_from'} || '';
240 ($stable, $record, $select, $extra_sql, $cache, $addl_from ) = @_;
244 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
246 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
250 my $table = $cache ? $cache->table : $stable;
251 my $dbdef_table = dbdef->table($table)
252 or die "No schema for table $table found - ".
253 "do you need to create it or run dbdef-create?";
254 my $pkey = $dbdef_table->primary_key;
256 my @real_fields = grep exists($record->{$_}), real_fields($table);
258 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
259 @virtual_fields = grep exists($record->{$_}), "FS::$table"->virtual_fields;
261 cluck "warning: FS::$table not loaded; virtual fields not searchable";
262 @virtual_fields = ();
265 my $statement = "SELECT $select FROM $stable";
266 $statement .= " $addl_from" if $addl_from;
267 if ( @real_fields or @virtual_fields ) {
268 $statement .= ' WHERE '. join(' AND ',
273 if ( ref($record->{$_}) ) {
274 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
275 #$op = 'LIKE' if $op =~ /^ILIKE$/i && driver_name ne 'Pg';
276 if ( uc($op) eq 'ILIKE' ) {
278 $record->{$_}{'value'} = lc($record->{$_}{'value'});
279 $column = "LOWER($_)";
281 $record->{$_} = $record->{$_}{'value'}
284 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
286 if ( driver_name eq 'Pg' ) {
287 my $type = dbdef->table($table)->column($column)->type;
288 if ( $type =~ /(int|serial)/i ) {
289 qq-( $column IS NULL )-;
291 qq-( $column IS NULL OR $column = '' )-;
294 qq-( $column IS NULL OR $column = "" )-;
296 } elsif ( $op eq '!=' ) {
297 if ( driver_name eq 'Pg' ) {
298 my $type = dbdef->table($table)->column($column)->type;
299 if ( $type =~ /(int|serial)/i ) {
300 qq-( $column IS NOT NULL )-;
302 qq-( $column IS NOT NULL AND $column != '' )-;
305 qq-( $column IS NOT NULL AND $column != "" )-;
308 if ( driver_name eq 'Pg' ) {
309 qq-( $column $op '' )-;
311 qq-( $column $op "" )-;
321 if ( ref($record->{$_}) ) {
322 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
323 if ( uc($op) eq 'ILIKE' ) {
325 $record->{$_}{'value'} = lc($record->{$_}{'value'});
326 $column = "LOWER($_)";
328 $record->{$_} = $record->{$_}{'value'};
331 # ... EXISTS ( SELECT name, value FROM part_virtual_field
333 # ON part_virtual_field.vfieldpart = virtual_field.vfieldpart
334 # WHERE recnum = svc_acct.svcnum
335 # AND (name, value) = ('egad', 'brain') )
337 my $value = $record->{$_};
341 $subq = ($value ? 'EXISTS ' : 'NOT EXISTS ') .
342 "( SELECT part_virtual_field.name, virtual_field.value ".
343 "FROM part_virtual_field JOIN virtual_field ".
344 "ON part_virtual_field.vfieldpart = virtual_field.vfieldpart ".
345 "WHERE virtual_field.recnum = ${table}.${pkey} ".
346 "AND part_virtual_field.name = '${column}'".
348 " AND virtual_field.value ${op} '${value}'"
352 } @virtual_fields ) );
356 $statement .= " $extra_sql" if defined($extra_sql);
358 warn "[debug]$me $statement\n" if $DEBUG > 1;
359 my $sth = $dbh->prepare($statement)
360 or croak "$dbh->errstr doing $statement";
365 grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields
367 if ( $record->{$field} =~ /^\d+(\.\d+)?$/
368 && dbdef->table($table)->column($field)->type =~ /(int|serial)/i
370 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_INTEGER } );
372 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_VARCHAR } );
376 # $sth->execute( map $record->{$_},
377 # grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
378 # ) or croak "Error executing \"$statement\": ". $sth->errstr;
380 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
382 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
383 @virtual_fields = "FS::$table"->virtual_fields;
385 cluck "warning: FS::$table not loaded; virtual fields not returned either";
386 @virtual_fields = ();
390 tie %result, "Tie::IxHash";
391 my @stuff = @{ $sth->fetchall_arrayref( {} ) };
393 %result = map { $_->{$pkey}, $_ } @stuff;
395 @result{@stuff} = @stuff;
400 if ( keys(%result) and @virtual_fields ) {
402 "SELECT virtual_field.recnum, part_virtual_field.name, ".
403 "virtual_field.value ".
404 "FROM part_virtual_field JOIN virtual_field USING (vfieldpart) ".
405 "WHERE part_virtual_field.dbtable = '$table' AND ".
406 "virtual_field.recnum IN (".
407 join(',', keys(%result)). ") AND part_virtual_field.name IN ('".
408 join(q!', '!, @virtual_fields) . "')";
409 warn "[debug]$me $statement\n" if $DEBUG > 1;
410 $sth = $dbh->prepare($statement) or croak "$dbh->errstr doing $statement";
411 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
413 foreach (@{ $sth->fetchall_arrayref({}) }) {
414 my $recnum = $_->{recnum};
415 my $name = $_->{name};
416 my $value = $_->{value};
417 if (exists($result{$recnum})) {
418 $result{$recnum}->{$name} = $value;
423 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
424 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
425 #derivied class didn't override new method, so this optimization is safe
428 new_or_cached( "FS::$table", { %{$_} }, $cache )
432 new( "FS::$table", { %{$_} } )
436 #okay, its been tested
437 # warn "untested code (class FS::$table uses custom new method)";
439 eval 'FS::'. $table. '->new( { %{$_} } )';
443 # Check for encrypted fields and decrypt them.
444 if ($conf->exists('encryption') && eval 'defined(@FS::'. $table . '::encrypted_fields)') {
445 foreach my $record (@return) {
446 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
447 # Set it directly... This may cause a problem in the future...
448 $record->setfield($field, $record->decrypt($record->getfield($field)));
453 cluck "warning: FS::$table not loaded; returning FS::Record objects";
455 FS::Record->new( $table, { %{$_} } );
461 =item by_key PRIMARY_KEY_VALUE
463 This is a class method that returns the record with the given primary key
464 value. This method is only useful in FS::Record subclasses. For example:
466 my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
470 my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
475 my ($class, $pkey_value) = @_;
477 my $table = $class->table
478 or croak "No table for $class found";
480 my $dbdef_table = dbdef->table($table)
481 or die "No schema for table $table found - ".
482 "do you need to create it or run dbdef-create?";
483 my $pkey = $dbdef_table->primary_key
484 or die "No primary key for table $table";
486 return qsearchs($table, { $pkey => $pkey_value });
489 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
491 Experimental JOINed search method. Using this method, you can execute a
492 single SELECT spanning multiple tables, and cache the results for subsequent
493 method calls. Interface will almost definately change in an incompatible
501 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
502 my $cache = FS::SearchCache->new( $ptable, $pkey );
505 grep { !$saw{$_->getfield($pkey)}++ }
506 qsearch($table, $record, $select, $extra_sql, $cache )
510 =item qsearchs PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
512 Same as qsearch, except that if more than one record matches, it B<carp>s but
513 returns the first. If this happens, you either made a logic error in asking
514 for a single item, or your data is corrupted.
518 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
520 my(@result) = qsearch(@_);
521 cluck "warning: Multiple records in scalar search ($table)"
522 if scalar(@result) > 1;
523 #should warn more vehemently if the search was on a primary key?
524 scalar(@result) ? ($result[0]) : ();
535 Returns the table name.
540 # cluck "warning: FS::Record::table deprecated; supply one in subclass!";
547 Returns the DBIx::DBSchema::Table object for the table.
553 my($table)=$self->table;
554 dbdef->table($table);
557 =item get, getfield COLUMN
559 Returns the value of the column/field/key COLUMN.
564 my($self,$field) = @_;
565 # to avoid "Use of unitialized value" errors
566 if ( defined ( $self->{Hash}->{$field} ) ) {
567 $self->{Hash}->{$field};
577 =item set, setfield COLUMN, VALUE
579 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
584 my($self,$field,$value) = @_;
585 $self->{'modified'} = 1;
586 $self->{'Hash'}->{$field} = $value;
593 =item AUTLOADED METHODS
595 $record->column is a synonym for $record->get('column');
597 $record->column('value') is a synonym for $record->set('column','value');
604 my($field)=$AUTOLOAD;
606 if ( defined($value) ) {
607 confess "errant AUTOLOAD $field for $self (arg $value)"
608 unless ref($self) && $self->can('setfield');
609 $self->setfield($field,$value);
611 confess "errant AUTOLOAD $field for $self (no args)"
612 unless ref($self) && $self->can('getfield');
613 $self->getfield($field);
619 # my $field = $AUTOLOAD;
621 # if ( defined($_[1]) ) {
622 # $_[0]->setfield($field, $_[1]);
624 # $_[0]->getfield($field);
630 Returns a list of the column/value pairs, usually for assigning to a new hash.
632 To make a distinct duplicate of an FS::Record object, you can do:
634 $new = new FS::Record ( $old->table, { $old->hash } );
640 confess $self. ' -> hash: Hash attribute is undefined'
641 unless defined($self->{'Hash'});
642 %{ $self->{'Hash'} };
647 Returns a reference to the column/value hash. This may be deprecated in the
648 future; if there's a reason you can't just use the autoloaded or get/set
660 Returns true if any of this object's values have been modified with set (or via
661 an autoloaded method). Doesn't yet recognize when you retreive a hashref and
673 Inserts this record to the database. If there is an error, returns the error,
674 otherwise returns false.
682 my $error = $self->check;
683 return $error if $error;
685 #single-field unique keys are given a value if false
686 #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
687 foreach ( $self->dbdef_table->unique->singles ) {
688 $self->unique($_) unless $self->getfield($_);
691 #and also the primary key, if the database isn't going to
692 my $primary_key = $self->dbdef_table->primary_key;
694 if ( $primary_key ) {
695 my $col = $self->dbdef_table->column($primary_key);
698 uc($col->type) eq 'SERIAL'
699 || ( driver_name eq 'Pg'
700 && defined($col->default)
701 && $col->default =~ /^nextval\(/i
703 || ( driver_name eq 'mysql'
704 && defined($col->local)
705 && $col->local =~ /AUTO_INCREMENT/i
707 $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
710 my $table = $self->table;
713 # Encrypt before the database
714 if ($conf->exists('encryption') && defined(eval '@FS::'. $table . 'encrypted_fields')) {
715 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
716 $self->{'saved'} = $self->getfield($field);
717 $self->setfield($field, $self->enrypt($self->getfield($field)));
722 #false laziness w/delete
724 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
727 my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
730 my $statement = "INSERT INTO $table ( ".
731 join( ', ', @real_fields ).
733 join( ', ', @values ).
736 warn "[debug]$me $statement\n" if $DEBUG > 1;
737 my $sth = dbh->prepare($statement) or return dbh->errstr;
739 local $SIG{HUP} = 'IGNORE';
740 local $SIG{INT} = 'IGNORE';
741 local $SIG{QUIT} = 'IGNORE';
742 local $SIG{TERM} = 'IGNORE';
743 local $SIG{TSTP} = 'IGNORE';
744 local $SIG{PIPE} = 'IGNORE';
746 $sth->execute or return $sth->errstr;
748 # get inserted id from the database, if applicable & needed
749 if ( $db_seq && ! $self->getfield($primary_key) ) {
750 warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
754 if ( driver_name eq 'Pg' ) {
756 #my $oid = $sth->{'pg_oid_status'};
757 #my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
759 my $default = $self->dbdef_table->column($primary_key)->default;
760 unless ( $default =~ /^nextval\('"?([\w\.]+)"?'/i ) {
761 dbh->rollback if $FS::UID::AutoCommit;
762 return "can't parse $table.$primary_key default value".
763 " for sequence name: $default";
767 my $i_sql = "SELECT currval('$sequence')";
768 my $i_sth = dbh->prepare($i_sql) or do {
769 dbh->rollback if $FS::UID::AutoCommit;
772 #$i_sth->execute($oid) or do {
773 $i_sth->execute() or do {
774 dbh->rollback if $FS::UID::AutoCommit;
775 return $i_sth->errstr;
777 $insertid = $i_sth->fetchrow_arrayref->[0];
779 } elsif ( driver_name eq 'mysql' ) {
781 $insertid = dbh->{'mysql_insertid'};
782 # work around mysql_insertid being null some of the time, ala RT :/
783 unless ( $insertid ) {
784 warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
785 "using SELECT LAST_INSERT_ID();";
786 my $i_sql = "SELECT LAST_INSERT_ID()";
787 my $i_sth = dbh->prepare($i_sql) or do {
788 dbh->rollback if $FS::UID::AutoCommit;
791 $i_sth->execute or do {
792 dbh->rollback if $FS::UID::AutoCommit;
793 return $i_sth->errstr;
795 $insertid = $i_sth->fetchrow_arrayref->[0];
800 dbh->rollback if $FS::UID::AutoCommit;
801 return "don't know how to retreive inserted ids from ". driver_name.
802 ", try using counterfiles (maybe run dbdef-create?)";
806 $self->setfield($primary_key, $insertid);
811 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
812 $self->virtual_fields;
813 if (@virtual_fields) {
814 my %v_values = map { $_, $self->getfield($_) } @virtual_fields;
816 my $vfieldpart = $self->vfieldpart_hashref;
818 my $v_statement = "INSERT INTO virtual_field(recnum, vfieldpart, value) ".
821 my $v_sth = dbh->prepare($v_statement) or do {
822 dbh->rollback if $FS::UID::AutoCommit;
826 foreach (keys(%v_values)) {
827 $v_sth->execute($self->getfield($primary_key),
831 dbh->rollback if $FS::UID::AutoCommit;
832 return $v_sth->errstr;
839 if ( defined dbdef->table('h_'. $table) ) {
840 my $h_statement = $self->_h_statement('insert');
841 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
842 $h_sth = dbh->prepare($h_statement) or do {
843 dbh->rollback if $FS::UID::AutoCommit;
849 $h_sth->execute or return $h_sth->errstr if $h_sth;
851 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
853 # Now that it has been saved, reset the encrypted fields so that $new
855 foreach my $field (keys %{$saved}) {
856 $self->setfield($field, $saved->{$field});
864 Depriciated (use insert instead).
869 cluck "warning: FS::Record::add deprecated!";
870 insert @_; #call method in this scope
875 Delete this record from the database. If there is an error, returns the error,
876 otherwise returns false.
883 my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
885 $self->getfield($_) eq ''
886 #? "( $_ IS NULL OR $_ = \"\" )"
887 ? ( driver_name eq 'Pg'
889 : "( $_ IS NULL OR $_ = \"\" )"
891 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
892 } ( $self->dbdef_table->primary_key )
893 ? ( $self->dbdef_table->primary_key)
894 : real_fields($self->table)
896 warn "[debug]$me $statement\n" if $DEBUG > 1;
897 my $sth = dbh->prepare($statement) or return dbh->errstr;
900 if ( defined dbdef->table('h_'. $self->table) ) {
901 my $h_statement = $self->_h_statement('delete');
902 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
903 $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
908 my $primary_key = $self->dbdef_table->primary_key;
911 my $vfp = $self->vfieldpart_hashref;
912 foreach($self->virtual_fields) {
913 next if $self->getfield($_) eq '';
914 unless(@del_vfields) {
915 my $st = "DELETE FROM virtual_field WHERE recnum = ? AND vfieldpart = ?";
916 $v_sth = dbh->prepare($st) or return dbh->errstr;
918 push @del_vfields, $_;
921 local $SIG{HUP} = 'IGNORE';
922 local $SIG{INT} = 'IGNORE';
923 local $SIG{QUIT} = 'IGNORE';
924 local $SIG{TERM} = 'IGNORE';
925 local $SIG{TSTP} = 'IGNORE';
926 local $SIG{PIPE} = 'IGNORE';
928 my $rc = $sth->execute or return $sth->errstr;
929 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
930 $h_sth->execute or return $h_sth->errstr if $h_sth;
931 $v_sth->execute($self->getfield($primary_key), $vfp->{$_})
932 or return $v_sth->errstr
933 foreach (@del_vfields);
935 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
937 #no need to needlessly destoy the data either (causes problems actually)
938 #undef $self; #no need to keep object!
945 Depriciated (use delete instead).
950 cluck "warning: FS::Record::del deprecated!";
951 &delete(@_); #call method in this scope
954 =item replace OLD_RECORD
956 Replace the OLD_RECORD with this one in the database. If there is an error,
957 returns the error, otherwise returns false.
965 if (!defined($old)) {
966 warn "[debug]$me replace called with no arguments; autoloading old record\n"
968 my $primary_key = $new->dbdef_table->primary_key;
969 if ( $primary_key ) {
970 $old = qsearchs($new->table, { $primary_key => $new->$primary_key() } )
971 or croak "can't find ". $new->table. ".$primary_key ".
972 $new->$primary_key();
974 croak $new->table. " has no primary key; pass old record as argument";
978 warn "[debug]$me $new ->replace $old\n" if $DEBUG;
980 return "Records not in same table!" unless $new->table eq $old->table;
982 my $primary_key = $old->dbdef_table->primary_key;
983 return "Can't change primary key $primary_key ".
984 'from '. $old->getfield($primary_key).
985 ' to ' . $new->getfield($primary_key)
987 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
989 my $error = $new->check;
990 return $error if $error;
992 # Encrypt for replace
994 if ($conf->exists('encryption') && defined(eval '@FS::'. $new->table . 'encrypted_fields')) {
995 foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
996 $saved->{$field} = $new->getfield($field);
997 $new->setfield($field, $new->encrypt($new->getfield($field)));
1001 #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
1002 my %diff = map { ($new->getfield($_) ne $old->getfield($_))
1003 ? ($_, $new->getfield($_)) : () } $old->fields;
1005 unless ( keys(%diff) ) {
1006 carp "[warning]$me $new -> replace $old: records identical"
1007 unless $nowarn_identical;
1011 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
1013 "$_ = ". _quote($new->getfield($_),$old->table,$_)
1014 } real_fields($old->table)
1019 if ( $old->getfield($_) eq '' ) {
1021 #false laziness w/qsearch
1022 if ( driver_name eq 'Pg' ) {
1023 my $type = $old->dbdef_table->column($_)->type;
1024 if ( $type =~ /(int|serial)/i ) {
1027 qq-( $_ IS NULL OR $_ = '' )-;
1030 qq-( $_ IS NULL OR $_ = "" )-;
1034 "$_ = ". _quote($old->getfield($_),$old->table,$_);
1037 } ( $primary_key ? ( $primary_key ) : real_fields($old->table) )
1040 warn "[debug]$me $statement\n" if $DEBUG > 1;
1041 my $sth = dbh->prepare($statement) or return dbh->errstr;
1044 if ( defined dbdef->table('h_'. $old->table) ) {
1045 my $h_old_statement = $old->_h_statement('replace_old');
1046 warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
1047 $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
1053 if ( defined dbdef->table('h_'. $new->table) ) {
1054 my $h_new_statement = $new->_h_statement('replace_new');
1055 warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
1056 $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
1061 # For virtual fields we have three cases with different SQL
1062 # statements: add, replace, delete
1066 my (@add_vfields, @rep_vfields, @del_vfields);
1067 my $vfp = $old->vfieldpart_hashref;
1068 foreach(grep { exists($diff{$_}) } $new->virtual_fields) {
1069 if($diff{$_} eq '') {
1071 unless(@del_vfields) {
1072 my $st = "DELETE FROM virtual_field WHERE recnum = ? ".
1073 "AND vfieldpart = ?";
1074 warn "[debug]$me $st\n" if $DEBUG > 2;
1075 $v_del_sth = dbh->prepare($st) or return dbh->errstr;
1077 push @del_vfields, $_;
1078 } elsif($old->getfield($_) eq '') {
1080 unless(@add_vfields) {
1081 my $st = "INSERT INTO virtual_field (value, recnum, vfieldpart) ".
1083 warn "[debug]$me $st\n" if $DEBUG > 2;
1084 $v_add_sth = dbh->prepare($st) or return dbh->errstr;
1086 push @add_vfields, $_;
1089 unless(@rep_vfields) {
1090 my $st = "UPDATE virtual_field SET value = ? ".
1091 "WHERE recnum = ? AND vfieldpart = ?";
1092 warn "[debug]$me $st\n" if $DEBUG > 2;
1093 $v_rep_sth = dbh->prepare($st) or return dbh->errstr;
1095 push @rep_vfields, $_;
1099 local $SIG{HUP} = 'IGNORE';
1100 local $SIG{INT} = 'IGNORE';
1101 local $SIG{QUIT} = 'IGNORE';
1102 local $SIG{TERM} = 'IGNORE';
1103 local $SIG{TSTP} = 'IGNORE';
1104 local $SIG{PIPE} = 'IGNORE';
1106 my $rc = $sth->execute or return $sth->errstr;
1107 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1108 $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1109 $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1111 $v_del_sth->execute($old->getfield($primary_key),
1113 or return $v_del_sth->errstr
1114 foreach(@del_vfields);
1116 $v_add_sth->execute($new->getfield($_),
1117 $old->getfield($primary_key),
1119 or return $v_add_sth->errstr
1120 foreach(@add_vfields);
1122 $v_rep_sth->execute($new->getfield($_),
1123 $old->getfield($primary_key),
1125 or return $v_rep_sth->errstr
1126 foreach(@rep_vfields);
1128 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1130 # Now that it has been saved, reset the encrypted fields so that $new
1131 # can still be used.
1132 foreach my $field (keys %{$saved}) {
1133 $new->setfield($field, $saved->{$field});
1142 Depriciated (use replace instead).
1147 cluck "warning: FS::Record::rep deprecated!";
1148 replace @_; #call method in this scope
1153 Checks virtual fields (using check_blocks). Subclasses should still provide
1154 a check method to validate real fields, foreign keys, etc., and call this
1155 method via $self->SUPER::check.
1157 (FIXME: Should this method try to make sure that it I<is> being called from
1158 a subclass's check method, to keep the current semantics as far as possible?)
1163 #confess "FS::Record::check not implemented; supply one in subclass!";
1166 foreach my $field ($self->virtual_fields) {
1167 for ($self->getfield($field)) {
1168 # See notes on check_block in FS::part_virtual_field.
1169 eval $self->pvf($field)->check_block;
1171 #this is bad, probably want to follow the stack backtrace up and see
1173 my $err = "Fatal error checking $field for $self";
1175 return "$err (see log for backtrace): $@";
1178 $self->setfield($field, $_);
1185 my( $self, $action, $time ) = @_;
1190 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
1191 real_fields($self->table);
1193 my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
1195 "INSERT INTO h_". $self->table. " ( ".
1196 join(', ', qw(history_date history_user history_action), @fields ).
1198 join(', ', $time, dbh->quote(getotaker()), dbh->quote($action), @values).
1205 B<Warning>: External use is B<deprecated>.
1207 Replaces COLUMN in record with a unique number, using counters in the
1208 filesystem. Used by the B<insert> method on single-field unique columns
1209 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
1210 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
1212 Returns the new value.
1217 my($self,$field) = @_;
1218 my($table)=$self->table;
1220 croak "Unique called on field $field, but it is ",
1221 $self->getfield($field),
1223 if $self->getfield($field);
1225 #warn "table $table is tainted" if is_tainted($table);
1226 #warn "field $field is tainted" if is_tainted($field);
1228 my($counter) = new File::CounterFile "$table.$field",0;
1230 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
1232 # my($counter) = new File::CounterFile "$user/$table.$field",0;
1235 my $index = $counter->inc;
1236 $index = $counter->inc while qsearchs($table, { $field=>$index } );
1238 $index =~ /^(\d*)$/;
1241 $self->setfield($field,$index);
1245 =item ut_float COLUMN
1247 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
1248 null. If there is an error, returns the error, otherwise returns false.
1253 my($self,$field)=@_ ;
1254 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
1255 $self->getfield($field) =~ /^(\d+)$/ ||
1256 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
1257 $self->getfield($field) =~ /^(\d+e\d+)$/)
1258 or return "Illegal or empty (float) $field: ". $self->getfield($field);
1259 $self->setfield($field,$1);
1263 =item ut_snumber COLUMN
1265 Check/untaint signed numeric data (whole numbers). May not be null. If there
1266 is an error, returns the error, otherwise returns false.
1271 my($self, $field) = @_;
1272 $self->getfield($field) =~ /^(-?)\s*(\d+)$/
1273 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1274 $self->setfield($field, "$1$2");
1278 =item ut_number COLUMN
1280 Check/untaint simple numeric data (whole numbers). May not be null. If there
1281 is an error, returns the error, otherwise returns false.
1286 my($self,$field)=@_;
1287 $self->getfield($field) =~ /^(\d+)$/
1288 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1289 $self->setfield($field,$1);
1293 =item ut_numbern COLUMN
1295 Check/untaint simple numeric data (whole numbers). May be null. If there is
1296 an error, returns the error, otherwise returns false.
1301 my($self,$field)=@_;
1302 $self->getfield($field) =~ /^(\d*)$/
1303 or return "Illegal (numeric) $field: ". $self->getfield($field);
1304 $self->setfield($field,$1);
1308 =item ut_money COLUMN
1310 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
1311 is an error, returns the error, otherwise returns false.
1316 my($self,$field)=@_;
1317 $self->setfield($field, 0) if $self->getfield($field) eq '';
1318 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
1319 or return "Illegal (money) $field: ". $self->getfield($field);
1320 #$self->setfield($field, "$1$2$3" || 0);
1321 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
1325 =item ut_text COLUMN
1327 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1328 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / =
1329 May not be null. If there is an error, returns the error, otherwise returns
1335 my($self,$field)=@_;
1336 #warn "msgcat ". \&msgcat. "\n";
1337 #warn "notexist ". \¬exist. "\n";
1338 #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
1339 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]+)$/
1340 or return gettext('illegal_or_empty_text'). " $field: ".
1341 $self->getfield($field);
1342 $self->setfield($field,$1);
1346 =item ut_textn COLUMN
1348 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1349 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
1350 May be null. If there is an error, returns the error, otherwise returns false.
1355 my($self,$field)=@_;
1356 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
1357 or return gettext('illegal_text'). " $field: ". $self->getfield($field);
1358 $self->setfield($field,$1);
1362 =item ut_alpha COLUMN
1364 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
1365 an error, returns the error, otherwise returns false.
1370 my($self,$field)=@_;
1371 $self->getfield($field) =~ /^(\w+)$/
1372 or return "Illegal or empty (alphanumeric) $field: ".
1373 $self->getfield($field);
1374 $self->setfield($field,$1);
1378 =item ut_alpha COLUMN
1380 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
1381 error, returns the error, otherwise returns false.
1386 my($self,$field)=@_;
1387 $self->getfield($field) =~ /^(\w*)$/
1388 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
1389 $self->setfield($field,$1);
1393 =item ut_phonen COLUMN [ COUNTRY ]
1395 Check/untaint phone numbers. May be null. If there is an error, returns
1396 the error, otherwise returns false.
1398 Takes an optional two-letter ISO country code; without it or with unsupported
1399 countries, ut_phonen simply calls ut_alphan.
1404 my( $self, $field, $country ) = @_;
1405 return $self->ut_alphan($field) unless defined $country;
1406 my $phonen = $self->getfield($field);
1407 if ( $phonen eq '' ) {
1408 $self->setfield($field,'');
1409 } elsif ( $country eq 'US' || $country eq 'CA' ) {
1411 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
1412 or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
1413 $phonen = "$1-$2-$3";
1414 $phonen .= " x$4" if $4;
1415 $self->setfield($field,$phonen);
1417 warn "warning: don't know how to check phone numbers for country $country";
1418 return $self->ut_textn($field);
1425 Check/untaint ip addresses. IPv4 only for now.
1430 my( $self, $field ) = @_;
1431 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
1432 or return "Illegal (IP address) $field: ". $self->getfield($field);
1433 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
1434 $self->setfield($field, "$1.$2.$3.$4");
1440 Check/untaint ip addresses. IPv4 only for now. May be null.
1445 my( $self, $field ) = @_;
1446 if ( $self->getfield($field) =~ /^()$/ ) {
1447 $self->setfield($field,'');
1450 $self->ut_ip($field);
1454 =item ut_domain COLUMN
1456 Check/untaint host and domain names.
1461 my( $self, $field ) = @_;
1462 #$self->getfield($field) =~/^(\w+\.)*\w+$/
1463 $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
1464 or return "Illegal (domain) $field: ". $self->getfield($field);
1465 $self->setfield($field,$1);
1469 =item ut_name COLUMN
1471 Check/untaint proper names; allows alphanumerics, spaces and the following
1472 punctuation: , . - '
1479 my( $self, $field ) = @_;
1480 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
1481 or return gettext('illegal_name'). " $field: ". $self->getfield($field);
1482 $self->setfield($field,$1);
1488 Check/untaint zip codes.
1492 my @zip_reqd_countries = qw( CA ); #US implicit...
1495 my( $self, $field, $country ) = @_;
1496 if ( $country eq 'US' ) {
1497 $self->getfield($field) =~ /\s*(\d{5}(\-\d{4})?)\s*$/
1498 or return gettext('illegal_zip'). " $field for country $country: ".
1499 $self->getfield($field);
1500 $self->setfield($field,$1);
1502 if ( $self->getfield($field) =~ /^\s*$/
1503 && ( !$country || ! grep { $_ eq $country } @zip_reqd_countries )
1506 $self->setfield($field,'');
1508 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
1509 or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
1510 $self->setfield($field,$1);
1516 =item ut_country COLUMN
1518 Check/untaint country codes. Country names are changed to codes, if possible -
1519 see L<Locale::Country>.
1524 my( $self, $field ) = @_;
1525 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
1526 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
1527 && country2code($1) ) {
1528 $self->setfield($field,uc(country2code($1)));
1531 $self->getfield($field) =~ /^(\w\w)$/
1532 or return "Illegal (country) $field: ". $self->getfield($field);
1533 $self->setfield($field,uc($1));
1537 =item ut_anything COLUMN
1539 Untaints arbitrary data. Be careful.
1544 my( $self, $field ) = @_;
1545 $self->getfield($field) =~ /^(.*)$/s
1546 or return "Illegal $field: ". $self->getfield($field);
1547 $self->setfield($field,$1);
1551 =item ut_enum COLUMN CHOICES_ARRAYREF
1553 Check/untaint a column, supplying all possible choices, like the "enum" type.
1558 my( $self, $field, $choices ) = @_;
1559 foreach my $choice ( @$choices ) {
1560 if ( $self->getfield($field) eq $choice ) {
1561 $self->setfield($choice);
1565 return "Illegal (enum) field $field: ". $self->getfield($field);
1568 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1570 Check/untaint a foreign column key. Call a regular ut_ method (like ut_number)
1571 on the column first.
1575 sub ut_foreign_key {
1576 my( $self, $field, $table, $foreign ) = @_;
1577 qsearchs($table, { $foreign => $self->getfield($field) })
1578 or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
1579 " in $table.$foreign";
1583 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1585 Like ut_foreign_key, except the null value is also allowed.
1589 sub ut_foreign_keyn {
1590 my( $self, $field, $table, $foreign ) = @_;
1591 $self->getfield($field)
1592 ? $self->ut_foreign_key($field, $table, $foreign)
1597 =item virtual_fields [ TABLE ]
1599 Returns a list of virtual fields defined for the table. This should not
1600 be exported, and should only be called as an instance or class method.
1604 sub virtual_fields {
1607 $table = $self->table or confess "virtual_fields called on non-table";
1609 confess "Unknown table $table" unless dbdef->table($table);
1611 return () unless dbdef->table('part_virtual_field');
1613 unless ( $virtual_fields_cache{$table} ) {
1614 my $query = 'SELECT name from part_virtual_field ' .
1615 "WHERE dbtable = '$table'";
1617 my $result = $dbh->selectcol_arrayref($query);
1618 confess $dbh->errstr if $dbh->err;
1619 $virtual_fields_cache{$table} = $result;
1622 @{$virtual_fields_cache{$table}};
1627 =item fields [ TABLE ]
1629 This is a wrapper for real_fields and virtual_fields. Code that called
1630 fields before should probably continue to call fields.
1635 my $something = shift;
1637 if($something->isa('FS::Record')) {
1638 $table = $something->table;
1640 $table = $something;
1641 $something = "FS::$table";
1643 return (real_fields($table), $something->virtual_fields());
1648 =item pvf FIELD_NAME
1650 Returns the FS::part_virtual_field object corresponding to a field in the
1651 record (specified by FIELD_NAME).
1656 my ($self, $name) = (shift, shift);
1658 if(grep /^$name$/, $self->virtual_fields) {
1659 return qsearchs('part_virtual_field', { dbtable => $self->table,
1669 =item real_fields [ TABLE ]
1671 Returns a list of the real columns in the specified table. Called only by
1672 fields() and other subroutines elsewhere in FS::Record.
1679 my($table_obj) = dbdef->table($table);
1680 confess "Unknown table $table" unless $table_obj;
1681 $table_obj->columns;
1684 =item _quote VALUE, TABLE, COLUMN
1686 This is an internal function used to construct SQL statements. It returns
1687 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
1688 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
1693 my($value, $table, $column) = @_;
1694 my $column_obj = dbdef->table($table)->column($column);
1695 my $column_type = $column_obj->type;
1696 my $nullable = $column_obj->null;
1698 warn " $table.$column: $value ($column_type".
1699 ( $nullable ? ' NULL' : ' NOT NULL' ).
1700 ")\n" if $DEBUG > 2;
1702 if ( $value eq '' && $column_type =~ /^int/ ) {
1706 cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
1710 } elsif ( $value =~ /^\d+(\.\d+)?$/ &&
1711 ! $column_type =~ /(char|binary|text)$/i ) {
1718 =item vfieldpart_hashref TABLE
1720 Returns a hashref of virtual field names and vfieldparts applicable to the given
1725 sub vfieldpart_hashref {
1727 my $table = $self->table;
1729 return {} unless dbdef->table('part_virtual_field');
1732 my $statement = "SELECT vfieldpart, name FROM part_virtual_field WHERE ".
1733 "dbtable = '$table'";
1734 my $sth = $dbh->prepare($statement);
1735 $sth->execute or croak "Execution of '$statement' failed: ".$dbh->errstr;
1736 return { map { $_->{name}, $_->{vfieldpart} }
1737 @{$sth->fetchall_arrayref({})} };
1744 This is deprecated. Don't use it.
1746 It returns a hash-type list with the fields of this record's table set true.
1751 carp "warning: hfields is deprecated";
1754 foreach (fields($table)) {
1763 "$_: ". $self->getfield($_). "|"
1764 } (fields($self->table)) );
1768 my ($self, $value) = @_;
1771 if ($conf->exists('encryption')) {
1772 if ($self->is_encrypted($value)) {
1773 # Return the original value if it isn't plaintext.
1774 $encrypted = $value;
1777 if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
1778 # RSA doesn't like the empty string so let's pack it up
1779 # The database doesn't like the RSA data so uuencode it
1780 my $length = length($value)+1;
1781 $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
1783 die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
1791 my ($self, $value) = @_;
1792 # Possible Bug - Some work may be required here....
1794 if (length($value) > 80) {
1802 my ($self,$value) = @_;
1803 my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
1804 if ($conf->exists('encryption') && $self->is_encrypted($value)) {
1806 if (ref($rsa_decrypt) =~ /::RSA/) {
1807 my $encrypted = unpack ("u*", $value);
1808 $decrypted = unpack("Z*", $rsa_decrypt->decrypt($encrypted));
1816 #Initialize the Module
1817 $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
1819 if ($conf->exists('encryptionmodule') && $conf->config('encryptionmodule') ne '') {
1820 $rsa_module = $conf->config('encryptionmodule');
1824 eval ("require $rsa_module"); # No need to import the namespace
1827 # Initialize Encryption
1828 if ($conf->exists('encryptionpublickey') && $conf->config('encryptionpublickey') ne '') {
1829 my $public_key = join("\n",$conf->config('encryptionpublickey'));
1830 $rsa_encrypt = $rsa_module->new_public_key($public_key);
1833 # Intitalize Decryption
1834 if ($conf->exists('encryptionprivatekey') && $conf->config('encryptionprivatekey') ne '') {
1835 my $private_key = join("\n",$conf->config('encryptionprivatekey'));
1836 $rsa_decrypt = $rsa_module->new_private_key($private_key);
1840 sub DESTROY { return; }
1844 # #use Carp qw(cluck);
1845 # #cluck "DESTROYING $self";
1846 # warn "DESTROYING $self";
1850 # return ! eval { join('',@_), kill 0; 1; };
1857 This module should probably be renamed, since much of the functionality is
1858 of general use. It is not completely unlike Adapter::DBI (see below).
1860 Exported qsearch and qsearchs should be deprecated in favor of method calls
1861 (against an FS::Record object like the old search and searchs that qsearch
1862 and qsearchs were on top of.)
1864 The whole fields / hfields mess should be removed.
1866 The various WHERE clauses should be subroutined.
1868 table string should be deprecated in favor of DBIx::DBSchema::Table.
1870 No doubt we could benefit from a Tied hash. Documenting how exists / defined
1871 true maps to the database (and WHERE clauses) would also help.
1873 The ut_ methods should ask the dbdef for a default length.
1875 ut_sqltype (like ut_varchar) should all be defined
1877 A fallback check method should be provided which uses the dbdef.
1879 The ut_money method assumes money has two decimal digits.
1881 The Pg money kludge in the new method only strips `$'.
1883 The ut_phonen method only checks US-style phone numbers.
1885 The _quote function should probably use ut_float instead of a regex.
1887 All the subroutines probably should be methods, here or elsewhere.
1889 Probably should borrow/use some dbdef methods where appropriate (like sub
1892 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
1893 or allow it to be set. Working around it is ugly any way around - DBI should
1894 be fixed. (only affects RDBMS which return uppercase column names)
1896 ut_zip should take an optional country like ut_phone.
1900 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
1902 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.