}
/**
* Create a new [Database_Query_Builder_Delete].
*
* // DELETE FROM users
* $query = DB::delete('users');
*
* @param string table to delete from
* @return Database_Query_Builder_Delete
*/
public static function delete($table = NULL)
{
return new Database_Query_Builder_Delete($table);
}
/**
* Create a new [Database_Expression] which is not escaped. An expression
* is the only way to use SQL functions within query builders.
*
* $expression = DB::expr('COUNT(users.id)');
*
* @param string expression
* @return Database_Expression
*/
public static function expr($string){
return new Database_Expression($string);
}
/*
* Gettting paginated page
*/
public static function getPage($_sql,&$page,$orderBy ='updated desc', $dataPro='data',$pagePro = 'pagination',
$config = NULL,$db = 'default',$as_object= true){
$_csql = 'select count(1) as c from ('.$_sql.') st';
$_c = DB::query(DB::SELECT,$_csql)->execute($db)->get('c');
if($config){
$config['total_items'] = $_c;
$_pagination = new Pagination($config);
}else{
$config = array();
$config['total_items'] = $_c;
$_pagination = new Pagination($config);
}
$_sql .= ' order by '.$orderBy;
if($_pagination->offset){
$_sql .= ' offset '.$_pagination->offset;
}
$_sql .= ' limit '.$_pagination->items_per_page;
$_data = DB::query(DB::SELECT,$_sql,$as_object)->execute($db)->as_array();
if(!$_data){
$page->{$dataPro} = false;
$page->{$pagePro} = false;
return false;
}
$page->{$dataPro} = $_data;
$page->{$pagePro} = $_pagination;
return true;
}
/*
* Get All roles
* level to control the statrt
*/
public static function getRoleTreeSql($role_id,$quote = false,$role_table,$level=0,$db='default'){
$_sql = 'select id from ('.self::getTree($role_table,true,$role_id,'id','pid','id',
0, //Maxdepth
$level, //Level
';',$db).') utree';
if(!$quote) return $_sql;
else return '('.$_sql.')';
}
/*
* Return sub query on Objects authorization
* Child role objects and owned objects
* Parent control
*/
public static function getCURTreeSql($role_id,$user_id,$role_table,$quote = true,
$role='role_id',$owner = 'owner_id' ,$db='default'){
$_sql = ' '.$role.' in '.self::getRoleTreeSql($role_id,true,$role_table,
1, //Level start with 1
$db). ' or '.$owner.'='.DB::quote($user_id);
if(!$quote) return $_sql;
else return '('.$_sql.')';
}
}