예제 #1
0
 /**
  * Performs the actual task of table population.
  */
 public static function populate_cache_table($db, $table, $last_run_date)
 {
     $queries = kohana::config("cache_builder.{$table}");
     try {
         $count = cache_builder::get_changelist($db, $table, $queries, $last_run_date);
         if ($count > 0) {
             cache_builder::do_delete($db, $table, $queries);
             // preprocess some of the tags in the queries
             if (is_array($queries['update'])) {
                 foreach ($queries['update'] as $key => &$sql) {
                     $sql = str_replace('#join_needs_update#', $queries['join_needs_update'], $sql);
                 }
             } else {
                 $queries['update'] = str_replace('#join_needs_update#', $queries['join_needs_update'], $queries['update']);
             }
             cache_builder::run_statement($db, $table, $queries['update'], 'update');
             // preprocess some of the tags in the queries
             if (is_array($queries['insert'])) {
                 foreach ($queries['insert'] as $key => &$sql) {
                     $sql = str_replace('#join_needs_update#', $queries['join_needs_update'] . ' and (nu.deleted=false or nu.deleted is null)', $sql);
                 }
             } else {
                 $queries['insert'] = str_replace('#join_needs_update#', $queries['join_needs_update'] . ' and (nu.deleted=false or nu.deleted is null)', $queries['insert']);
             }
             cache_builder::run_statement($db, $table, $queries['insert'], 'insert');
             if (isset($queries['extra_multi_record_updates'])) {
                 cache_builder::run_statement($db, $table, $queries['extra_multi_record_updates'], 'final update');
             }
             if (!variable::get("populated-{$table}")) {
                 $cacheQuery = $db->query("select count(*) from cache_{$table}")->result_array(false);
                 if (isset($queries['count'])) {
                     $totalQuery = $db->query($queries['count'])->result_array(false);
                 } else {
                     $totalQuery = $db->query("select count(*) from {$table} where deleted='f'")->result_array(false);
                 }
                 $percent = round($cacheQuery[0]['count'] * 100 / $totalQuery[0]['count']);
                 echo "{$table} population in progress - {$percent}% done";
             }
             echo '<br/>';
         }
         $db->query("drop table needs_update_{$table}");
     } catch (Exception $e) {
         error::log_error('Building cache', $e);
         echo $e->getMessage();
         $db->query("drop table needs_update_{$table}");
     }
 }