1 package FS::cdr::Import;
4 use Date::Format 'time2str';
5 use FS::UID qw(adminsuidsetup dbh);
10 use vars qw( $DEBUG );
15 FS::cdr::Import - CDR importing
21 FS::cdr::Import->dbi_import(
22 'dbd' => 'Pg', #mysql, Sybase, etc.
23 'database' => 'DATABASE_NAME',
24 'table' => 'TABLE_NAME',,
25 'status_table' => 'STATUS_TABLE_NAME', # if using a table rather than field in main table
26 'primary_key' => 'BILLING_ID',
27 'primary_key_info' => 'BIGINT', # defaults to bigint
28 'status_column' => 'STATUS_COLUMN_NAME', # defaults to freesidestatus
29 'status_column_info' => 'varchar(32)', # defaults to varchar(32)
30 'column_map' => { #freeside => remote_db
31 'freeside_column' => 'remote_db_column',
32 'freeside_column' => sub { my $row = shift; $row->{remote_db_column}; },
34 'batch_name' => 'batch_name', # cdr_batch name -import-date gets appended.
49 my %args = @_; #args are specifed by the script using this sub
51 my %opt; #opt is specified for each install / run of the script
52 getopts('H:U:P:D:T:c:L:S:', \%opt);
54 my $user = shift(@ARGV) or die $class->cli_usage;
55 my $database = $opt{D} || $args{database};
56 my $table = $opt{T} || $args{table};
57 my $pkey = $args{primary_key};
58 my $pkey_info = $args{primary_key_info} ? $args{primary_key_info} : 'BIGINT';
59 my $status_table = $opt{S} || $args{status_table};
60 my $dbd_type = $args{'dbd'} ? $args{'dbd'} : 'Pg';
61 my $status_column = $args{status_column} ? $args{status_column} : 'freesidestatus';
62 my $status_column_info = $args{status_column_info} ? $args{status_column} : 'VARCHAR(32)';
64 my $queries = get_queries({
68 'status_column' => $status_column,
69 'status_column_info' => $status_column_info,
70 'status_table' => $status_table,
71 'primary_key' => $pkey,
72 'primary_key_info' => $pkey_info,
75 my $dsn = 'dbi:'. $dbd_type . $queries->{connect_type};
76 $dsn .= ";database=$database" if $database;
78 my $dbi = DBI->connect($dsn, $opt{U}, $opt{P})
83 ## check for status table if using. if not there create it.
85 my $status = $dbi->selectall_arrayref( $queries->{check_statustable} );
87 print "Adding status table $status_table ...\n";
88 $dbi->do( $queries->{create_statustable} )
92 ## check for column freeside status if not using status table and create it if not there.
94 my $status = $dbi->selectall_arrayref( $queries->{check_statuscolumn} );
96 print "Adding $status_column column...\n";
97 $dbi->do( $queries->{create_statuscolumn} )
102 #my @cols = values %{ $args{column_map} };
103 my $sql = "SELECT $table.* FROM $table "; # join(',', @cols). " FROM $table ".
104 $sql .= "LEFT JOIN $status_table ON ( $table.$pkey = $status_table.$pkey ) "
106 $sql .= "WHERE $status_column IS NULL ";
108 #$sql .= ' LIMIT '. $opt{L} if $opt{L};
109 my $sth = $dbi->prepare($sql);
110 $sth->execute or die $sth->errstr. " executing $sql";
112 my $cdr_batch = new FS::cdr_batch({
113 'cdrbatch' => $args{batch_name} . '-import-'. time2str('%Y/%m/%d-%T',time),
115 my $error = $cdr_batch->insert;
116 die $error if $error;
117 my $cdrbatchnum = $cdr_batch->cdrbatchnum;
121 while ( $row = $sth->fetchrow_hashref ) {
123 my %hash = ( 'cdrbatchnum' => $cdrbatchnum );
124 foreach my $field ( keys %{ $args{column_map} } ) {
125 my $col_or_coderef = $args{column_map}->{$field};
126 if ( ref($col_or_coderef) eq 'CODE' ) {
127 $hash{$field} = &{ $col_or_coderef }( $row );
129 $hash{$field} = $row->{ $col_or_coderef };
131 $hash{$field} = '' if $hash{$field} =~ /^\s+$/; #IVR (MSSQL?) bs
134 my $cdr = FS::cdr->new(\%hash);
136 $cdr->cdrtypenum($opt{c}) if $opt{c};
138 my $pkey_value = $row->{$pkey};
140 #print "$pkey_value\n" if $opt{v};
141 my $error = $cdr->insert;
145 #die "$pkey_value: failed import: $error\n";
146 print "$pkey_value: failed import: $error\n";
153 if ( $args{status_table} ) {
156 'INSERT INTO '. $status_table. " ( $pkey, $status_column ) ".
157 " VALUES ( ?, 'done' )";
161 $st_sql = "UPDATE $table SET $status_column = 'done' WHERE $pkey = ?";
165 my $updated = $dbi->do($st_sql, undef, $pkey_value );
166 #$updates += $updated;
167 die "failed to set status: ".$dbi->errstr."\n" unless $updated;
171 if ( $opt{L} && $imported >= $opt{L} ) {
178 print "Imported $imported CDRs.\n" if $imported;
185 "Usage: \n $0\n\t-H hostname\n\t[ -D database ]\n\t-U user\n\t-P password\n\t[ -c cdrtypenum ]\n\t[ -L num_cdrs_limit ]\n\t[ -T table ]\n\t[ -S status table ]\n\tfreesideuser\n";
189 #my ($dbd, $host, $table, $column, $column_create_info, $status_table, $primary_key, $primary_key_info) = @_;
192 #get host and port information.
193 my ($host, $port) = split /:/, $info->{host};
194 $host ||= 'localhost';
195 $port ||= '5000'; # check for pg default 5000 is sybase.
197 my %dbi_connect_types = (
198 'Sybase' => ':host='.$host.';port='.$port,
199 'Pg' => ':host='.$info->{host},
202 #Check for freeside status table
203 my %dbi_check_statustable = (
204 'Sybase' => "SELECT * FROM sysobjects WHERE name = '$info->{status_table}'",
205 'Pg' => "SELECT * FROM information_schema.columns WHERE table_schema = 'public' AND table_name = '$info->{status_table}' AND column_name = '$info->{status_column}'",
208 #Create freeside status table
209 my %dbi_create_statustable = (
210 'Sybase' => "CREATE TABLE $info->{status_table} ( $info->{primary_key} $info->{primary_key_info}, $info->{status_column} $info->{status_column_info} )",
211 'Pg' => "CREATE TABLE $info->{status_table} ( $info->{primary_key} $info->{primary_key_info}, $info->{status_column} $info->{status_column_info} )",
214 #Check for freeside status column
215 my %dbi_check_statuscolumn = (
216 'Sybase' => "SELECT syscolumns.name FROM sysobjects
217 JOIN syscolumns ON sysobjects.id = syscolumns.id
218 WHERE sysobjects.name LIKE '$info->{table}' AND syscolumns.name = '$info->{status_column}'",
219 'Pg' => "SELECT * FROM information_schema.columns WHERE table_schema = 'public' AND table_name = '$info->{table}' AND column_name = '$info->{status_column}' ",
222 #Create freeside status column
223 my %dbi_create_statuscolumn = (
224 'Sybase' => "ALTER TABLE $info->{table} ADD $info->{status_column} $info->{status_column_info} NULL",
225 'Pg' => "ALTER TABLE $info->{table} ADD COLUMN $info->{status_column} $info->{status_column_info}",
229 'connect_type' => $dbi_connect_types{$info->{dbd}},
230 'check_statustable' => $dbi_check_statustable{$info->{dbd}},
231 'create_statustable' => $dbi_create_statustable{$info->{dbd}},
232 'check_statuscolumn' => $dbi_check_statuscolumn{$info->{dbd}},
233 'create_statuscolumn' => $dbi_create_statuscolumn{$info->{dbd}},
241 currently works with Pg(Postgresql) and Sybase(Sybase AES)
243 Sparse documentation.