Browse Source

Fixed a few more memory leaks on the bulk loader

Stephen Ficklin 12 years ago
parent
commit
ec689d00a5
2 changed files with 53 additions and 42 deletions
  1. 31 30
      tripal_bulk_loader/tripal_bulk_loader.loader.inc
  2. 22 12
      tripal_core/api/tripal_core.api.inc

+ 31 - 30
tripal_bulk_loader/tripal_bulk_loader.loader.inc

@@ -493,27 +493,6 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
   // get the table description
   $table_desc = tripal_core_get_chado_table_schema($table);  
 
-  // for an insert, check that all database required fields are present in the values array
-  // we check for 'optional' in the mode for backwards compatibility. The 'optional' 
-  // mode used to be a type of insert
-  if (preg_match('/insert/', $table_data['mode']) or 
-      preg_match('/optional/', $table_data['mode'])) {
-    // Check all database table required fields are set
-    $fields = $table_desc['fields'];
-    foreach ($fields as $field => $def) { 	
-      // a field is considered missing if it cannot be null and there is no default
-      // value for it or it is not of type 'serial'
-      if (array_key_exists('not null', $def) and $def['not null'] == 1 and 
-          !array_key_exists($field, $values) and 
-          (!array_key_exists('default', $def) or !isset($def['default'])) and
-          strcmp($def['type'], 'serial') != 0) { 
-        $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') Missing Database Required Value: ' . $table . '.' . $field;
-        watchdog('T_bulk_loader', $msg, array(), WATCHDOG_NOTICE);
-        $data[$priority]['error'] = TRUE;
-      } 
-    } 
-  } 
-
   // Check that template required fields are present. if a required field is 
   // missing and this
   // is an optional record then just return. otherwise raise an error
@@ -543,6 +522,28 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
       }
     }
   }
+
+  // for an insert, check that all database required fields are present in the values array
+  // we check for 'optional' in the mode for backwards compatibility. The 'optional' 
+  // mode used to be a type of insert
+  if (!$skip_optional and (preg_match('/insert/', $table_data['mode']) or 
+       preg_match('/optional/', $table_data['mode']))) {
+    // Check all database table required fields are set
+    $fields = $table_desc['fields'];
+    foreach ($fields as $field => $def) { 	
+      // a field is considered missing if it cannot be null and there is no default
+      // value for it or it is not of type 'serial'
+      if (array_key_exists('not null', $def) and $def['not null'] == 1 and   // field must have a value
+          !array_key_exists($field, $values) and                             // there is not a value for it
+          !array_key_exists('default', $def) and                             // there is no default for it
+          strcmp($def['type'], 'serial') != 0) {                             // it is not a 'serial' type column
+        $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . 
+               ' (' . $table_data['mode'] . ') Missing Database Required Value: ' . $table . '.' . $field;
+        watchdog('T_bulk_loader', $msg, array(), WATCHDOG_ERROR); 
+        $data[$priority]['error'] = TRUE;
+      } 
+    } 
+  } 
   
   // add updated values array into the data array
   $data[$priority]['values_array'] = $values;
@@ -688,15 +689,15 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
     if(is_array($values) and count($values) > 0){
       $matches = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values);
     }
-/*$time_end = microtime(true);
-$time = $time_end - $time_start;
-printf("time: %04f seconds\n",$time);
-if($time > 0.05){
-  print "$table\n";
-  print_r(array_keys($table_desc['fields']));
-  print_r($values);
-  exit;
-} */   
+//$time_end = microtime(true);
+//$time = $time_end - $time_start;
+//printf("time: %04f seconds\n",$time);
+//if($time > 0.05){
+//  print "$table\n";
+//  print_r(array_keys($table_desc['fields']));
+//  print_r($values);
+//  exit;
+//}
     // if the record doesn't exists and it's not optional then generate an error
     if (count($matches) == 0){
       // No record on select

+ 22 - 12
tripal_core/api/tripal_core.api.inc

@@ -200,7 +200,10 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
   if ($validate) {
 
     // check for violation of any unique constraints
-    $ukeys = $table_desc['unique keys'];
+    $ukeys = array();
+    if(array_key_exists('unique keys', $table_desc)){
+      $ukeys = $table_desc['unique keys'];
+    }
     $ukselect_cols = array();
     $ukselect_vals = array();
     if ($ukeys) {
@@ -209,9 +212,9 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
           // build the arrays for performing a select that will check the contraint
           $ukselect_cols[] = $field;
           if (!array_key_exists($field, $insert_values)) {
-          	if (array_key_exists('default', $table_desc['fields'][$field])) {
+            if (array_key_exists('default', $table_desc['fields'][$field])) {
               $ukselect_vals[$field] = $table_desc['fields'][$field]['default'];
-          	}
+            }
           }
           else {
             $ukselect_vals[$field] = $insert_values[$field];
@@ -223,19 +226,22 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
           $coptions = array('statement_name' => 'uqsel_' . $table . '_' . $name);
         }
         if (tripal_core_chado_select($table, $ukselect_cols, $ukselect_vals, $coptions)) {
-          watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate record into $table table: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
+          watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate record into $table table: " . 
+            print_r($values, 1), array(), 'WATCHDOG_ERROR');
           return FALSE;
         }
       }
     }
 
     // if trying to insert a field that is the primary key, make sure it also is unique
-    $pkey = $table_desc['primary key'][0];
-    if ($insert_values[$pkey]) {
-      $coptions = array('statement_name' => 'pqsel_' . $table . '_' . $pkey);
-      if (tripal_core_chado_select($table, array($pkey), array($pkey => $insert_values[$pkey]), $coptions)) {
-        watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate primary key into $table table: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
-        return FALSE;
+    if (array_key_exists('primary key', $table_desc)) {
+      $pkey = $table_desc['primary key'][0];
+      if (array_key_exists($pkey, $insert_values)) {
+        $coptions = array('statement_name' => 'pqsel_' . $table . '_' . $pkey);
+        if (tripal_core_chado_select($table, array($pkey), array($pkey => $insert_values[$pkey]), $coptions)) {
+          watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate primary key into $table table: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
+          return FALSE;
+        }
       }
     }
 
@@ -247,8 +253,12 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
     foreach ($table_desc['fields'] as $field => $def) {
       // a field is considered missing if it cannot be NULL and there is no default
       // value for it or it is of type 'serial'
-      if ($def['not NULL'] == 1 and !array_key_exists($field, $insert_values) and !isset($def['default']) and strcmp($def['type'], serial) != 0) {
-        watchdog('tripal_core', "tripal_core_chado_insert: Field $table.$field cannot be NULL: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
+      if (array_key_exists('NOT NULL', $def) and 
+          !array_key_exists($field, $insert_values) and 
+          !array_key_exists('default', $def) and
+          strcmp($def['type'], serial) != 0) {
+        watchdog('tripal_core', "tripal_core_chado_insert: Field $table.$field cannot be NULL: " . 
+          print_r($values, 1), array(), 'WATCHDOG_ERROR');
         return FALSE;
       }
     }