--- loncom/interface/lonmysql.pm 2004/07/21 21:14:36 1.22 +++ loncom/interface/lonmysql.pm 2004/07/26 19:36:21 1.23 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # MySQL utility functions # -# $Id: lonmysql.pm,v 1.22 2004/07/21 21:14:36 matthew Exp $ +# $Id: lonmysql.pm,v 1.23 2004/07/26 19:36:21 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -901,6 +901,58 @@ sub store_row { return 1; } + +############################### + +=pod + +=item &bulk_store_rows() + +Inputs: table id, [columns],[[row data1].[row data2],...] + +returns undef on error, 1 on success. + +=cut + +############################### +sub bulk_store_rows { + my ($table_id,$columns,$rows) = @_; + # + return undef if (! defined(&connect_to_db())); + my $dbh = &get_dbh(); + return undef if (! defined($dbh)); + my $table_status = &check_table($table_id); + return undef if (! defined($table_status)); + if (! $table_status) { + $errorstring = "table $table_id does not exist."; + return undef; + } + # + my $tablename = &translate_id($table_id); + # + my $request = 'INSERT IGNORE INTO '.$tablename.' '; + if (defined($columns) && ref($columns) eq 'ARRAY') { + $request .= join(',',@$columns).' '; + } + if (! defined($rows) || ref($rows) ne 'ARRAY') { + $errorstring = "no input rows given."; + return undef; + } + $request .= 'VALUES '; + foreach my $row (@$rows) { + # avoid doing row stuff here... + $request .= '('.join(',',@$row).'),'; + } + $request =~ s/,$//; + $dbh->do($request); + if ($dbh->err) { + $errorstring = 'Attempted '.$/.$request.$/.'Got error '.$dbh->errstr(); + return undef; + } + return 1; +} + + ############################### =pod