Welcome Guest, Not a member yet? Register   Sign In
Core: Database raw sql
#1

(This post was last modified: 01-22-2022, 09:56 AM by iRedds.)

Sometimes it is necessary to use a raw sql query as part of the main query.
I already suggested this, but as part of other changes. And now I would like to know your opinion on the implementation of this as an independent tool.

This solution will help bypass the unwanted raw sql processing and use it as is.

PHP Code:
class SQLExpression
{
    protected $query '';

    /**
    * @param string $query
    */
    public function __construct(string $query)
    {
        $this->query $query;
    }

    public function __toString(): string
    
{
        return $this->query;
    }
}

function 
sql(string $query): SQLExpression
{
    return new SQLExpression($query);


Usage
PHP Code:
$db->select(sql('a + b AS c'));
$db->from(sql('SELECT * FROM ....'));    
// etc 


// Raw sql detection
PHP Code:
public function from($frombool $overwrite false)
{
    if ($overwrite === true) {
        $this->QBFrom = [];
        $this->db->setAliasedTables([]);
    }

    // here detection

    if ($from instanceof SQLExpression)
    {
        $this->QBFrom[] = $from;
        
        
return $this;
    }
    
    
// ....

Reply


Messages In This Thread
Core: Database raw sql - by iRedds - 12-08-2021, 09:23 PM
RE: Core: Database raw sql - by includebeer - 12-09-2021, 06:07 PM
RE: Core: Database raw sql - by kenjis - 04-07-2022, 03:20 AM
RE: Core: Database raw sql - by iRedds - 04-13-2022, 10:15 PM
RE: Core: Database raw sql - by InsiteFX - 04-14-2022, 12:47 AM
RE: Core: Database raw sql - by kenjis - 04-14-2022, 01:51 AM
RE: Core: Database raw sql - by kenjis - 05-24-2022, 11:30 PM
RE: Core: Database raw sql - by iRedds - 05-24-2022, 11:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB