PHP 中的批处理的实现

2019-04-11 01:14:56刘景俊



第一个表 processing_items 包含作业处理程序调用的函数。第二个表 processing_args 包含要发送给函数的参数,采用的形式是由键/值对组成的 hash 表。

与 mailouts 表一样,这两个表也由 PHP 类包装,这个类称为 ProcessingItems。

清单 6. generic.php
    <?phprequire_once('DB.php');class ProcessingItems{  public static function get_db() { ... }  public static function delete( $id )  {    $db = ProcessingItems::get_db();    $sth = $db->prepare( 'DELETE FROM processing_args WHERE item_id=?' );    $db->execute( $sth, $id );    $sth = $db->prepare( 'DELETE FROM processing_items WHERE id=?' );    $db->execute( $sth, $id );    return true;  }  public static function add( $function, $args )  {    $db = ProcessingItems::get_db();    $sth = $db->prepare( 'INSERT INTO processing_items VALUES (null,?)' );    $db->execute( $sth, array( $function ) );    $res = $db->query( "SELECT last_insert_id()" );    $id = null;    while( $res->fetchInto( $row ) ) { $id = $row[0]; }    foreach( $args as $key => $value )    {        $sth = $db->prepare( 'INSERT INTO processing_args  VALUES (null,?,?,?)' );        $db->execute( $sth, array( $id, $key, $value ) );    }    return true;  }  public static function get_all()  {    $db = ProcessingItems::get_db();    $res = $db->query( "SELECT * FROM processing_items" );    $rows = array();    while( $res->fetchInto( $row ) )    {        $item = array();        $item['id'] = $row[0];        $item['function'] = $row[1];        $item['args'] = array();        $ares = $db->query( "SELECT key_name, value FROM   processing_args WHERE item_id=?", $item['id'] );        while( $ares->fetchInto( $arow ) )            $item['args'][ $arow[0] ] = $arow[1];        $rows []= $item;    }    return $rows;  }}?>
相关文章 大家在看