X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2FRecord.pm;h=2540dd399f40eca00168f863cb83a125cd502209;hp=b950e306bfc847aaea3aaaeb1d2d6b28a1cbd2cd;hb=ec5603ae351d4ed8e4873dcd20bf71f8a4d549bb;hpb=729eaf2e7c8e110432a8f4953cba1e4b78e45db5 diff --git a/FS/FS/Record.pm b/FS/FS/Record.pm index b950e306b..2540dd399 100644 --- a/FS/FS/Record.pm +++ b/FS/FS/Record.pm @@ -1,35 +1,54 @@ package FS::Record; use strict; -use vars qw( $dbdef_file $dbdef $setup_hack $AUTOLOAD @ISA @EXPORT_OK $DEBUG - $me %dbdef_cache %virtual_fields_cache ); -use subs qw(reload_dbdef); +use vars qw( $AUTOLOAD @ISA @EXPORT_OK $DEBUG + $conf $me + %virtual_fields_cache + $nowarn_identical $no_update_diff $no_check_foreign + ); use Exporter; use Carp qw(carp cluck croak confess); +use Scalar::Util qw( blessed ); use File::CounterFile; use Locale::Country; use DBI qw(:sql_types); -use DBIx::DBSchema 0.21; +use DBIx::DBSchema 0.33; use FS::UID qw(dbh getotaker datasrc driver_name); +use FS::CurrentUser; +use FS::Schema qw(dbdef); use FS::SearchCache; use FS::Msgcat qw(gettext); +#use FS::Conf; #dependency loop bs, in install_callback below instead use FS::part_virtual_field; use Tie::IxHash; @ISA = qw(Exporter); -@EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch); + +#export dbdef for now... everything else expects to find it here +@EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch + str2time_sql str2time_sql_closing ); $DEBUG = 0; $me = '[FS::Record]'; -#ask FS::UID to run this stuff for us later -$FS::UID::callback{'FS::Record'} = sub { - $File::CounterFile::DEFAULT_DIR = "/usr/local/etc/freeside/counters.". datasrc; - $dbdef_file = "/usr/local/etc/freeside/dbdef.". datasrc; - &reload_dbdef unless $setup_hack; #$setup_hack needed now? -}; +$nowarn_identical = 0; +$no_update_diff = 0; +$no_check_foreign = 0; + +my $rsa_module; +my $rsa_loaded; +my $rsa_encrypt; +my $rsa_decrypt; + +FS::UID->install_callback( sub { + eval "use FS::Conf;"; + die $@ if $@; + $conf = FS::Conf->new; + $File::CounterFile::DEFAULT_DIR = $conf->base_dir . "/counters.". datasrc; +} ); + =head1 NAME @@ -38,7 +57,7 @@ FS::Record - Database record objects =head1 SYNOPSIS use FS::Record; - use FS::Record qw(dbh fields qsearch qsearchs dbdef); + use FS::Record qw(dbh fields qsearch qsearchs); $record = new FS::Record 'table', \%hash; $record = new FS::Record 'table', { 'column' => 'value', ... }; @@ -73,8 +92,11 @@ FS::Record - Database record objects $value = $record->unique('column'); $error = $record->ut_float('column'); + $error = $record->ut_floatn('column'); $error = $record->ut_number('column'); $error = $record->ut_numbern('column'); + $error = $record->ut_snumber('column'); + $error = $record->ut_snumbern('column'); $error = $record->ut_money('column'); $error = $record->ut_text('column'); $error = $record->ut_textn('column'); @@ -84,10 +106,6 @@ FS::Record - Database record objects $error = $record->ut_anything('column'); $error = $record->ut_name('column'); - $dbdef = reload_dbdef; - $dbdef = reload_dbdef "/non/standard/filename"; - $dbdef = dbdef; - $quoted_value = _quote($value,'table','field'); #deprecated @@ -131,14 +149,18 @@ sub new { $self->{'Table'} = shift; carp "warning: FS::Record::new called with table name ". $self->{'Table'}; } + + $self->{'Hash'} = shift; - my $hashref = $self->{'Hash'} = shift; - - foreach my $field ( grep !defined($hashref->{$_}), $self->fields ) { - $hashref->{$field}=''; + foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) { + $self->{'Hash'}{$field}=''; } - $self->_cache($hashref, shift) if $self->can('_cache') && @_; + $self->_rebless if $self->can('_rebless'); + + $self->{'modified'} = 0; + + $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_; $self; } @@ -176,13 +198,38 @@ sub create { } } -=item qsearch TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ +=item qsearch PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM Searches the database for all records matching (at least) the key/value pairs in HASHREF. Returns all the records found as `FS::TABLE' objects if that module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record objects. +The preferred usage is to pass a hash reference of named parameters: + + my @records = qsearch( { + 'table' => 'table_name', + 'hashref' => { 'field' => 'value' + 'field' => { 'op' => '<', + 'value' => '420', + }, + }, + + #these are optional... + 'select' => '*', + 'extra_sql' => 'AND field ', + 'order_by' => 'ORDER BY something', + #'cache_obj' => '', #optional + 'addl_from' => 'LEFT JOIN othtable USING ( field )', + 'debug' => 1, + } + ); + +Much code still uses old-style positional parameters, this is also probably +fine in the common case where there are only two parameters: + + my @records = qsearch( 'table', { 'field' => 'value' } ); + ###oops, argh, FS::Record::new only lets us create database fields. #Normal behaviour if SELECT is not specified is `*', as in #C