Browse Source

Bulk Loader: made it 'enable-able' in Drupal 7

Lacey Sanderson 11 years ago
parent
commit
be50855da2

+ 1 - 1
tripal_bulk_loader/api/tripal_bulk_loader.api.templates.inc

@@ -29,7 +29,7 @@ function tripal_bulk_loader_is_record_name_unique($new_record_name, $template_id
 
   // get the template array if it's not supplied
   if (empty($template_array)) {
-    $template = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $template_id));
+    $template = db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template", array(':template' => $template_id))->fetchObject();
     $template_array = unserialize($template->template_array);
     if (!is_array($template_array)) {
       watchdog(

+ 12 - 2
tripal_bulk_loader/includes/tripal_bulk_loader.admin.inc

@@ -71,13 +71,23 @@ function tripal_bulk_loader_admin_jobs() {
     array('data' => 'Progress', 'field' => 'progress'),
     '');
   $rows = array();
+  $query = db_select('tripal_bulk_loader', 'n')->extend('TableSort');
+  $query->join('tripal_bulk_loader_template', 't', 'cast(n.template_id as integer) = t.template_id');
+  $query->join('tripal_jobs', 'j', 'n.job_id = j.job_id');
+  $query->fields('n')
+    ->fields('t', array('name'))
+    ->fields('j', array('progress'))
+    ->orderByHeader($header);
+  $result = $query->execute();
+  /**
   $resource = pager_query("SELECT n.*, t.name as template_name, j.progress
     FROM {tripal_bulk_loader} n
     LEFT JOIN {tripal_bulk_loader_template} t ON cast(n.template_id as integer) = t.template_id
     LEFT JOIN {tripal_jobs} j ON n.job_id = j.job_id"
     . tablesort_sql($header),
     $num_jobs_per_page);
-  while ($n = db_fetch_object($resource)) {
+    */
+  foreach ($result as $n) {
     $rows[] = array(
       l($n->job_id, 'admin/tripal/tripal_jobs/view/' . $n->job_id),
       l($n->loader_name, 'node/' . $n->nid),
@@ -87,7 +97,7 @@ function tripal_bulk_loader_admin_jobs() {
       l('View', 'node/' . $n->nid) . ' | ' .  l('Edit', 'node/' . $n->nid . '/edit')
     );
   }
-  $output .= theme_table($header, $rows);
+  $output .= theme_table(array('header' => $header, 'rows' => $rows));
 
   $output .= theme('pager');
 

+ 18 - 18
tripal_bulk_loader/includes/tripal_bulk_loader.admin.templates.inc

@@ -26,8 +26,8 @@ function tripal_bulk_loader_modify_template_base_form($form_state = NULL, $mode)
       $form_state['storage']['template_id'] = $_GET['template_id'];
     }
 
-    $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
-    $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
+    $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
+    $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
     $form_state['storage']['template'] = unserialize($result->template_array);
     $form_state['storage']['template_name'] = $result->name;
 
@@ -65,7 +65,7 @@ function tripal_bulk_loader_modify_template_base_form($form_state = NULL, $mode)
       $resource = db_query($sql);
       $templates = array();
       $templates[''] = 'Select a Template';
-      while ($r = db_fetch_object($resource)) {
+      while ($r = $resource->fetchObject()) {
         $templates[$r->template_id] = $r->name;
       }
 
@@ -356,8 +356,8 @@ function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state
 
   $form_state['rebuild'] = TRUE;
   if ($form_state['storage']['template_id']) {
-    $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
-    $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
+    $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
+    $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
     $form_state['storage']['template'] = unserialize($result->template_array);
   }
 
@@ -373,8 +373,8 @@ function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state
     case 'Edit Template':
       $form_state['storage']['template_id'] = $form_state['values']['template_id'];
 
-      $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
-      $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
+      $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
+      $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
       $form_state['storage']['template'] = unserialize($result->template_array);
       $form_state['storage']['template_name'] = $result->name;
 
@@ -512,7 +512,7 @@ function tripal_bulk_loader_delete_template_base_form() {
   $resource = db_query($sql);
   $templates = array();
   $templates[''] = 'Select a Template';
-  while ($r = db_fetch_object($resource)) {
+  while ($r = $resource->fetchObject()) {
     $templates[$r->template_id] = $r->name;
   }
   $form['template_name'] = array(
@@ -543,8 +543,8 @@ function tripal_bulk_loader_delete_template_base_form() {
  * @ingroup tripal_bulk_loader
  */
 function tripal_bulk_loader_delete_template_base_form_submit($form, &$form_state) {
-  $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d";
-  db_query($sql, $form_state['values']['template_name']);
+  $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=:template";
+  db_query($sql, array(':template' => $form_state['values']['template_name']))->execute();
 }
 
 /**
@@ -590,7 +590,7 @@ function tripal_bulk_loader_import_export_template_form($form_state = NULL, $mod
     $resource = db_query($sql);
     $templates = array();
     $templates[''] = 'Select a Template';
-    while ($r = db_fetch_object($resource)) {
+    while ($r = $resource->fetchObject()) {
       $templates[$r->template_id] = $r->name;
     }
 
@@ -637,7 +637,7 @@ function tripal_bulk_loader_import_export_template_form($form_state = NULL, $mod
 function tripal_bulk_loader_import_export_template_form_submit($form, &$form_state) {
   switch ($form_state['values']['mode']) {
     case 'export':
-      $record = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $form_state['values']['template_id']));
+      $record = db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template", array(':template' => $form_state['values']['template_id']))->fetchObject();
       //$form_state['storage']['template_array'] = $record->template_array;
       $t = var_export(unserialize($record->template_array), TRUE);
       $t = preg_replace("/\n\s+array/", "array", $t); // move array( to previous line
@@ -699,8 +699,8 @@ function tripal_bulk_loader_edit_template_record_form(&$form_state = NULL) {
 
   // If this is the first load of the form (no form state) we need to initialize some variables
   if (!$form_state['storage']['template']) {
-    $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
-    $template = db_fetch_object(db_query($sql, $template_id));
+    $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
+    $template = db_query($sql, array(':template' => $template_id))->fetchObject();
     $form_state['storage']['template_array'] = unserialize($template->template_array);
     $form_state['storage']['template'] = $template;
 
@@ -1040,8 +1040,8 @@ function tripal_bulk_loader_add_template_field_form(&$form_state = NULL) {
 
    // If this is the first load of the form (no form state) we need to initialize some variables
   if (!$form_state['storage']['template']) {
-    $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
-    $template = db_fetch_object(db_query($sql, $template_id));
+    $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
+    $template = db_query($sql, array(':template' => $template_id))->fetchObject();
     $form_state['storage']['template_array'] = unserialize($template->template_array);
     $form_state['storage']['template'] = $template;
 
@@ -1780,8 +1780,8 @@ function tripal_bulk_loader_edit_template_field_form(&$form_state = NULL) {
 
   // If this is the first load of the form (no form state) we need to initialize some variables
   if (!$form_state['storage']['template']) {
-    $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
-    $template = db_fetch_object(db_query($sql, $template_id));
+    $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
+    $template = db_query($sql, array(':template' => $template_id))->fetchObject();
     $form_state['storage']['template_array'] = unserialize($template->template_array);
     $form_state['storage']['template'] = $template;
 

+ 10 - 9
tripal_bulk_loader/includes/tripal_bulk_loader.constants.inc

@@ -41,13 +41,14 @@ function tripal_bulk_loader_update_constant($nid, $group_id, $table, $field, $re
   );
 
   // Check to see if already exists
-  $exists = db_fetch_object(db_query(
-    "SELECT constant_id FROM {tripal_bulk_loader_constants} WHERE nid=%d AND record_id=%d AND field_id=%d AND group_id=%d",
-    $record['nid'],
-    $record['record_id'],
-    $record['field_id'],
-    $record['group_id']
-  ));
+  $exists = db_query(
+    "SELECT constant_id FROM {tripal_bulk_loader_constants} WHERE nid=:nid AND record_id=:record AND field_id=:field AND group_id=:group",
+    array(
+      ':nid' => $record['nid'],
+      ':record' => $record['record_id'],
+      ':field' => $record['field_id'],
+      ':group' => $record['group_id']
+  ))->fetchObject();
   if ($exists->constant_id) {
     $record['constant_id'] = $exists->constant_id;
     $status = drupal_write_record('tripal_bulk_loader_constants', $record, 'constant_id');
@@ -327,7 +328,7 @@ function tripal_bulk_loader_set_constants_form_submit($form, $form_state) {
 
   $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
   if (strcmp('Add Constant Set', $op) == 0) {
-      $max_group = db_fetch_object(db_query("SELECT max(group_id) as value FROM {tripal_bulk_loader_constants} WHERE nid=%d", $form_state['values']['nid']));
+      $max_group = db_query("SELECT max(group_id) as value FROM {tripal_bulk_loader_constants} WHERE nid=:nid", array(':nid' => $form_state['values']['nid']))->fetchObject();
       foreach ($indexes as $record_id => $array) {
         foreach ($array as $field_id) {
           tripal_bulk_loader_update_constant(
@@ -592,7 +593,7 @@ function tripal_bulk_loader_delete_constant_set_form_submit($form, $form_state)
   $group_id = $form_state['values']['group_id'];
   $nid = $form_state['values']['nid'];
   if ($nid && $form_state['values']['confirm']) {
-    db_query("DELETE FROM {tripal_bulk_loader_constants} WHERE nid=%d AND group_id=%d", $nid, $group_id);
+    db_query("DELETE FROM {tripal_bulk_loader_constants} WHERE nid=:nid AND group_id=:group", array(':nid' => $nid, ':group' => $group_id))->execute();
     drupal_set_message(t('Constant set successfully deleted.'));
   }
 

+ 23 - 22
tripal_bulk_loader/includes/tripal_bulk_loader.loader.inc

@@ -74,10 +74,10 @@ function tripal_bulk_loader_add_loader_job_form_submit($form, $form_state) {
       $job_id = tripal_add_job("Bulk Loading Job: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
 
       // add job_id to bulk_loader node
-      $success = db_query("UPDATE {tripal_bulk_loader} SET job_id=%d WHERE nid=%d", $job_id, $form_state['values']['nid']);
+      $success = db_query("UPDATE {tripal_bulk_loader} SET job_id=:job WHERE nid=:nid", array(':job' => $job_id, ':nid' => $form_state['values']['nid']))->execute();
 
       // change status
-      db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Submitted to Queue', $form_state['values']['nid']);
+      db_query("UPDATE {tripal_bulk_loader} SET job_status=:status' WHERE nid=:nid", array(':status' => 'Submitted to Queue', ':nid' => $form_state['values']['nid']))->execute();
     }
     else {
       drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $form_state['values']['file'])));
@@ -85,23 +85,23 @@ function tripal_bulk_loader_add_loader_job_form_submit($form, $form_state) {
   }
   elseif (preg_match('/Re-Submit Job/', $form_state['values']['op'])) {
     tripal_jobs_rerun($form_state['values']['job_id']);
-    db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Submitted to Queue', $form_state['values']['nid']);
+    db_query("UPDATE {tripal_bulk_loader} SET job_status=:status WHERE nid=:nid", array(':status' => 'Submitted to Queue', 'nid' => $form_state['values']['nid']))->execute();
   }
   elseif (preg_match('/Cancel Job/', $form_state['values']['op'])) {
-    db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Job Cancelled', $form_state['values']['nid']);
+    db_query("UPDATE {tripal_bulk_loader} SET job_status=:status WHERE nid=:nid", array(':status' => 'Job Cancelled', ':nid' => $form_state['values']['nid']))->execute();
     tripal_jobs_cancel($form_state['values']['job_id']);
   }
   elseif (preg_match('/Revert/', $form_state['values']['op'])) {
 
     // Remove the records from the database that were already inserted
-    $resource = db_query('SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=%d ORDER BY tripal_bulk_loader_inserted_id DESC', $form_state['values']['nid']);
-    while ($r = db_fetch_object($resource)) {
+    $resource = db_query('SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=:nid ORDER BY tripal_bulk_loader_inserted_id DESC', array(':nid' => $form_state['values']['nid']));
+    while ($r = $resource->fetchObject()) {
       $ids = preg_split('/,/', $r->ids_inserted);
-      db_query('DELETE FROM {%s} WHERE %s IN (%s)', $r->table_inserted_into, $r->table_primary_key, $r->ids_inserted);
-      $result = db_fetch_object(db_query('SELECT true as present FROM {%s} WHERE %s IN (%s)', $r->table_inserted_into, $r->table_primary_key, $r->ids_inserted));
+      db_query('DELETE FROM {'.$r->table_inserted_into.'} WHERE '.$r->table_primary_key.' IN ('.$r->ids_inserted.')')->execute();
+      $result = db_query('SELECT true as present FROM {'.$r->table_inserted_into.'} WHERE '.$r->table_primary_key.' IN ('.$r->ids_inserted.')')->fetchObject();
       if (!$result->present) {
         drupal_set_message(t('Successfully Removed data Inserted into the %tableto table.', array('%tableto' => $r->table_inserted_into)));
-        db_query('DELETE FROM {tripal_bulk_loader_inserted} WHERE tripal_bulk_loader_inserted_id=%d', $r->tripal_bulk_loader_inserted_id);
+        db_query('DELETE FROM {tripal_bulk_loader_inserted} WHERE tripal_bulk_loader_inserted_id=:id', array(':id' => $r->tripal_bulk_loader_inserted_id))->execute();
       }
       else {
         drupal_set_message(t('Unable to remove data Inserted into the %tableto table!', array('%tableto' => $r->table_inserted_into)), 'error');
@@ -109,7 +109,7 @@ function tripal_bulk_loader_add_loader_job_form_submit($form, $form_state) {
     }
 
     // reset status
-    db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Reverted -Data Deleted', $form_state['values']['nid']);
+    db_query("UPDATE {tripal_bulk_loader} SET job_status=:status WHERE nid=:nid", array(':status' => 'Reverted -Data Deleted', ':nid' => $form_state['values']['nid']))->execute();
   }
 
 }
@@ -131,10 +131,10 @@ function tripal_bulk_loader_add_loader_job_form_submit($form, $form_state) {
 function tripal_bulk_loader_load_data($nid, $job_id) {
 
   // ensure no timeout
-  set_time_limit(0);
+  drupal_set_time_limit(0);
 
   // set the status of the job (in the node not the tripal jobs)
-  db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Loading...', $nid);
+  db_query("UPDATE {tripal_bulk_loader} SET job_status=:status WHERE nid=:nid", array(':status' => 'Loading...', ':nid' => $nid))->execute();
 
 
   $node = node_load($nid);
@@ -344,7 +344,7 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
       $lockmode = variable_get('tripal_bulk_loader_lock', 'ROW EXCLUSIVE');
       foreach ($tables as $table) {
         print "\t\t\t$lockmode for $table\n";
-        chado_query("LOCK TABLE %s IN %s MODE", $table, $lockmode);
+        chado_query("LOCK TABLE ".$table." IN ".$lockmode." MODE");
       }
     }
 
@@ -460,7 +460,7 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
   else {
     $status = 'Errors Encountered';
   }
-  db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", $status, $nid);
+  db_query("UPDATE {tripal_bulk_loader} SET job_status=:status WHERE nid=:nid", array(':status' => $status, ':nid' => $nid))->execute();
 
 }
 
@@ -500,10 +500,10 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
   // get the table description
   $table_desc = tripal_core_get_chado_table_schema($table);
   if (!$table_desc) {
-    watchdog('T_bulk_loader', "Failure: Tripal does not know about the table named '%table'. If this is a custom table, 
+    watchdog('T_bulk_loader', "Failure: Tripal does not know about the table named '%table'. If this is a custom table,
       please define it first", array('%table' => $table), WATCHDOG_ERROR);
     $data[$priority]['error'] = TRUE;
-    return;  
+    return;
   }
 
   // Check that template required fields are present. if a required field is
@@ -600,7 +600,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
   // We check for "insert_unique" for backwards compatibilty but that mode no longer exists
   $data[$priority]['is_duplicate'] = 0;
   if (preg_match('/insert_unique/', $table_data['mode']) or
-     $table_data['select_if_duplicate'] == 1 or 
+     $table_data['select_if_duplicate'] == 1 or
      $table_data['update_if_duplicate'] == 1) {
     $options = array('is_duplicate' => TRUE);
     $duplicate = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, $options);
@@ -735,11 +735,12 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
 
       // add to tripal_bulk_loader_inserted
       if ($addt->node->keep_track_inserted) {
-        $insert_record = db_fetch_object(db_query(
-          "SELECT * FROM {tripal_bulk_loader_inserted} WHERE table_inserted_into='%s' AND nid=%d",
-          $table,
-          $addt->nid
-        ));
+        $insert_record = db_query(
+          "SELECT * FROM {tripal_bulk_loader_inserted} WHERE table_inserted_into=:table AND nid=:nid",
+          array(
+          ':table' => $table,
+          'nid' => $addt->nid
+        ))->fetchObject();
         if ($insert_record) {
           $insert_record->ids_inserted .= ',' . $record[$table_desc['primary key'][0] ];
           drupal_write_record('tripal_bulk_loader_inserted', $insert_record, 'tripal_bulk_loader_inserted_id');

+ 3 - 2
tripal_bulk_loader/tripal_bulk_loader.info

@@ -1,7 +1,8 @@
 name = Tripal Bulk Loader
 description = A module for uploading tab-delimit data into GMOD chado database using templates.
-core = 6.x
+core = 7.x
 project = tripal_bulk_loader
 package = Tripal
-version = 6.x-1.1
+version = 7.x-2.0-beta1
+
 dependencies[] = tripal_core

+ 5 - 25
tripal_bulk_loader/tripal_bulk_loader.install

@@ -4,21 +4,6 @@
  * @todo Add file header description
  */
 
-
-/**
- * Implements hook_install
- */
-function tripal_bulk_loader_install() {
-  drupal_install_schema('tripal_bulk_loader');
-}
-
-/**
- * Implements hook_uninstall
- */
-function tripal_bulk_loader_uninstall() {
-  drupal_uninstall_schema('tripal_bulk_loader');
-}
-
 /**
  * Implements hook_schema
  *
@@ -166,10 +151,9 @@ function tripal_bulk_loader_update_6150() {
 
   // Create tripal_bulk_loader_constants table
   $schema = tripal_bulk_loader_schema();
-  $ret = array();
-  db_create_table($ret, 'tripal_bulk_loader_constants', $schema['tripal_bulk_loader_constants']);
+  db_create_table('tripal_bulk_loader_constants', $schema['tripal_bulk_loader_constants']);
 
-  return $ret;
+  return 'Added support for loader-specific constants.';
 
 }
 
@@ -179,11 +163,9 @@ function tripal_bulk_loader_update_6150() {
  *   to allow multiple sets of constants per job
  */
 function tripal_bulk_loader_update_6151() {
-  $ret = array();
 
   $schema = tripal_bulk_loader_schema();
   db_add_field(
-    $ret,
     'tripal_bulk_loader_constants',
     'group_id',
     array(
@@ -194,14 +176,12 @@ function tripal_bulk_loader_update_6151() {
       )
     );
 
-  return $ret;
+  return 'Added support for multiple sets of loader-specific constants.';
 }
 
 function tripal_bulk_loader_update_6152() {
-  $ret = array();
 
   db_add_field(
-    $ret,
     'tripal_bulk_loader',
     'keep_track_inserted',
     array(
@@ -212,10 +192,10 @@ function tripal_bulk_loader_update_6152() {
     )
   );
 
-  return $ret;
+  return 'Added ability to rollback loading job based on storing loaded ids.';
 }
 /**
- * Implementation of hook_requirements(). 
+ * Implementation of hook_requirements().
   */
 function tripal_bulk_loader_requirements($phase) {
   $requirements = array();

+ 46 - 27
tripal_bulk_loader/tripal_bulk_loader.module

@@ -1,4 +1,5 @@
 <?php
+
 /**
  * @file
  * The functions for the Tripal bulk loader.
@@ -218,11 +219,11 @@ function tripal_bulk_loader_theme() {
  *  @return
  *  If the permission for the specified operation is not set then return FALSE. If the
  *  permission is set then return NULL as this allows other modules to disable
- *  access.  The only exception is when the $op == 'create'.  We will always 
+ *  access.  The only exception is when the $op == 'create'.  We will always
  *  return TRUE if the permission is set.
  * @ingroup tripal_bulk_loader
  */
-function tripal_bulk_loader_access($op, $node, $account) {
+function tripal_bulk_loader_node_access($node, $op, $account) {
   if ($op == 'create') {
     if (!user_access('create tripal_bulk_loader', $account)) {
       return FALSE;
@@ -252,7 +253,7 @@ function tripal_bulk_loader_access($op, $node, $account) {
  *
  * @ingroup tripal_bulk_loader
  */
-function tripal_bulk_loader_perm() {
+function tripal_bulk_loader_permission() {
   return array(
       'access tripal_bulk_loader',
       'create tripal_bulk_loader',
@@ -275,7 +276,7 @@ function tripal_bulk_loader_node_info() {
   $nodes = array();
   $nodes['tripal_bulk_loader'] = array(
       'name' => t('Bulk Loading Job'),
-      'module' => 'tripal_bulk_loader',
+      'base' => 'tripal_bulk_loader',
       'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
       'has_title' => TRUE,
       'has_body' => FALSE,
@@ -299,9 +300,9 @@ function tripal_bulk_loader_form($node, $form_state) {
   }
 
   $sql = "SELECT * FROM {tripal_bulk_loader_template}";
-  $results = db_query($sql);
+  $results = db_query($sql)->execute();
   $templates = array();
-  while ($template = db_fetch_object ($results)) {
+  foreach ($results as $template) {
     $templates [$template->template_id] = $template->name;
   }
 
@@ -376,27 +377,27 @@ function tripal_bulk_loader_form($node, $form_state) {
  * @ingroup tripal_bulk_loader
  */
 function tripal_bulk_loader_load($node) {
-  $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
-  $node = db_fetch_object(db_query($sql, $node->nid));
+  $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = :nid";
+  $node = db_query($sql, array(':nid' => $node->nid))->fetchObject();
 
   $node->title = 'Bulk Loading Job: ' . $node->loader_name;
 
   // Add job details
   $progress = tripal_bulk_loader_progess_file_get_progress($node->job_id);
-  $sql = "SELECT * FROM {tripal_jobs} WHERE job_id=%d";
-  $node->job = db_fetch_object(db_query($sql, $node->job_id));
+  $sql = "SELECT * FROM {tripal_jobs} WHERE job_id=:job_id";
+  $node->job = db_query($sql, array(':job_id' => $node->job_id))->fetchObject();
 
   // Add the loader template
-  $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
-  $results = db_fetch_object(db_query($sql, $node->template_id));
+  $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
+  $results = db_query($sql, array(':template' => $node->template_id))->fetchObject();
   $template = unserialize($results->template_array);
   $node->template = $results;
   $node->template->template_array = $template;
 
   // Add inserted records
-  $sql = 'SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=%d';
-  $resource = db_query($sql, $node->nid);
-  while ($r = db_fetch_object($resource)) {
+  $sql = 'SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=:nid';
+  $resource = db_query($sql, array(':nid' => $node->nid))->execute();
+  foreach ($resource as $r) {
     $r->num_inserted = sizeof(preg_split('/,/', $r->ids_inserted));
     $node->inserted_records->{$r->table_inserted_into} = $r;
   }
@@ -423,9 +424,9 @@ function tripal_bulk_loader_load($node) {
 
 
   // Add constants
-  $sql = 'SELECT * FROM {tripal_bulk_loader_constants} WHERE nid=%d ORDER BY group_id, record_id, field_id';
-  $resource = db_query($sql, $node->nid);
-  while ($r = db_fetch_object($resource)) {
+  $sql = 'SELECT * FROM {tripal_bulk_loader_constants} WHERE nid=:nid ORDER BY group_id, record_id, field_id';
+  $resource = db_query($sql, array(':nid' => $node->nid))->execute();
+  foreach ($resource as $r) {
     $node->constants[$r->group_id][$r->record_id][$r->field_id] = array(
       'constant_id' => $r->constant_id,
       'group_id' => $r->group_id,
@@ -452,8 +453,17 @@ function tripal_bulk_loader_load($node) {
 function tripal_bulk_loader_insert($node) {
 
   // Insert into tripal_bulk_loader
-  $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header, job_status, keep_track_inserted) VALUES (%d, '%s', %d, '%s', %d, '%s', %d)";
-  db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, 'Initialized', $node->keep_track_inserted);
+  $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header, job_status, keep_track_inserted) VALUES (:nid, :loader, :template, :file, :header, :status, :ids)";
+  db_query($sql,
+    array(':nid' => $node->nid,
+      ':loader' => $node->loader_name,
+      ':template' => $node->template_id,
+      ':file' => $node->file,
+      ':header' => $node->has_header,
+      ':status' => 'Initialized',
+      ':ids' => $node->keep_track_inserted
+    )
+  )->execute();
 
   // Update title
   $node->title =$node->loader_name;
@@ -471,8 +481,8 @@ function tripal_bulk_loader_insert($node) {
  * @ingroup tripal_bulk_loader
  */
 function tripal_bulk_loader_delete($node) {
-  $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
-  db_query($sql, $node->nid);
+  $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = :nid";
+  db_query($sql, array(':nid' => $node->nid))->execute();
 }
 
 /**
@@ -484,8 +494,17 @@ function tripal_bulk_loader_delete($node) {
 function tripal_bulk_loader_update($node) {
 
   // Update tripal_bulk_loader
-  $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s', file_has_header = '%s', keep_track_inserted = %d WHERE nid = %d";
-  db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, $node->keep_track_inserted, $node->nid);
+  $sql = "UPDATE {tripal_bulk_loader} SET nid = :nid, loader_name = :loader, template_id = :template, file = :file, file_has_header = :header, keep_track_inserted = :ids WHERE nid = :wherenid";
+  db_query($sql,
+    array(':nid' => $node->nid,
+      ':loader' => $node->loader_name,
+      ':template' => $node->template_id,
+      ':file' => $node->file,
+      ':header' => $node->has_header,
+      ':ids' => $node->keep_track_inserted,
+      ':wherenid' => $node->nid
+    )
+  )->execute();
 
   // Add a job if the user want to load the data
   global $user;
@@ -514,7 +533,7 @@ function tripal_bulk_loader_update($node) {
 function tripal_bulk_loader_preprocess_tripal_bulk_loader_template(&$variables) {
 
   $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
-  $template = db_fetch_object(db_query($sql, $variables['template_id']));
+  $template = db_query($sql, array(':template' => $variables['template_id']))->fetchObject();
   $template->template_array = unserialize($template->template_array);
   $variables['template'] = $template;
 
@@ -550,7 +569,7 @@ function tripal_bulk_loader_progess_file_get_progress($job_id, $update_progress
   $num_lines = trim(`wc --lines < $filename`);
   $num_records = trim(`grep -o "." $filename | wc --lines`);
 
-  $job = db_fetch_object(db_query("SELECT j.*, b.file, b.file_has_header, c.num as num_constant_sets
+  $job = db_query("SELECT j.*, b.file, b.file_has_header, c.num as num_constant_sets
                               FROM {tripal_jobs} j
                               LEFT JOIN {tripal_bulk_loader} b ON b.job_id=j.job_id
                               LEFT JOIN (
@@ -558,7 +577,7 @@ function tripal_bulk_loader_progess_file_get_progress($job_id, $update_progress
                                   FROM {tripal_bulk_loader_constants}
                                   GROUP BY nid
                                 ) c ON c.nid=b.nid
-                              WHERE j.job_id=%d", $job_id));
+                              WHERE j.job_id=:job", array(':job' =>$job_id))->execute();
   if ($job->num_constant_sets) {
     $num_constant_sets_loaded = round($job->progress / (100 / $job->num_constant_sets), 4);