[LON-CAPA-cvs] cvs: loncom /interface lonmysql.pm

matthew lon-capa-cvs@mail.lon-capa.org
Mon, 26 Jul 2004 19:36:21 -0000


matthew		Mon Jul 26 15:36:21 2004 EDT

  Modified files:              
    /loncom/interface	lonmysql.pm 
  Log:
  Added &bulk_store_rows, to take advantage of MySQLs ability to store multiple
  rows at a time.  Currently untested.
  
  
Index: loncom/interface/lonmysql.pm
diff -u loncom/interface/lonmysql.pm:1.22 loncom/interface/lonmysql.pm:1.23
--- loncom/interface/lonmysql.pm:1.22	Wed Jul 21 17:14:36 2004
+++ loncom/interface/lonmysql.pm	Mon Jul 26 15:36:21 2004
@@ -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 @@
     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