日期:2011-10-25  浏览次数:20468 次

    包含CRUD,事务,树递归查询,分页等. 演示简单的魅力. 所有的参数都escape了,不存在注入问题.

  /*

  * Anthony.chen

  * 2010 reserved

  */

  class DB {

  // Query types

  const SELECT = 1;

  const INSERT = 2;

  const UPDATE = 3;

  const DELETE = 4;

  const T = 't';

  const F = 'f';

  /*

  * Supporting return multiple data;

  */

  public static function insert_table($table_name,$data,$return_id='id',$db='default'){

  if (!is_array($data)){

  return false;

  }

  if (is_null($return_id)){

  self::query(self::SELECT,self::insert($table_name,array_keys($data))->values(array_values($data)))->execute($db);

  return true;

  }

  if (is_string($return_id)){

  $id = self::query(self::SELECT,self::insert($table_name,array_keys($data))->values(array_values($data))." returning ".$return_id)->execute($db)->get($return_id);

  return $id;

  }else{

  if (is_array($return_id)){

  $ids = implode(',',$return_id);

  $r_ids = self::query(self::SELECT,self::insert($table_name,array_keys($data))->values(array_values($data))." returning ".$ids)->execute($db)->current();

  return $r_ids;

  }

  }

  return false;

  }

  public static function update_table($table_name,$id,$data,$id_name='id',$db='default'){

  if (!is_array($data)){

  return false;

  }

  return self::update($table_name)->set($data)->where($id_name ,'=',$id)->execute($db);

  }

  public static function quote($s,$db='default'){

  if(!is_array($s)){

  return Database::instance($db)->quote($s);