Raw object
Medoo provided a way to use raw expressions for more complex and customizable queries. It also supports data placeholders for preventing SQL injection and optimized syntax without carrying about the quotation.
query [string]
The raw SQL expression.
map (optional) [array]
The array of input parameters value for the prepared statement.
Syntax
The raw SQL expression provides a shortcut for quoting column names, so you don't have to care about the quotation. All you have to do is use <name>
to explain this is the column name. Medoo will handle the rest.
Medoo::raw("AVG(<weight>)")
For Column
You can use the raw object to select() the column name parameter. It will be aliased as the key name.
$data = $database->get('account', [ 'user_name', 'score' => Medoo::raw('SUM(<age> + <experience>)') ], [ 'user_id' => 100 ]);
SELECT "user_name", SUM("age" + "experience") AS "score" FROM "WP_account" WHERE "user_id" > 100
For Update and Insert Statement
$data = $database->insert('account', [ 'user_name' => 'apple', 'user_id' => Medoo::raw('UUID()') ]); $data = $database->update('account', [ 'user_name' => 'apple', 'user_id' => Medoo::raw('UUID()') ], [ 'age[>]' => 10 ]);
For Where Statement
$data = $database->select('account', [ 'user_name', 'user_id', ], [ 'datetime' => Medoo::raw('NOW()'), 'ORDER' => Medoo::raw('RAND()'), 'LIMIT' => Medoo::raw('AVG(<age>)') ]);
With Prepared Statement
If you want to use some values from other variables or user post dates for the raw object, you can use a prepared statement to prevent SQL injection.
$today = "2017-05-01"; $database->select('account', [ 'user_id', 'user_name' ], [ 'datetime' => Medoo::raw('DATE_ADD(:today, INTERVAL 10 DAY)', [ ':today' => $today ]) ]);
As Where Clause
You can also use raw objects as the where clause for building complex clauses.
$data = $database->select('account', [ 'user_id', 'email' ], Medoo::raw('WHERE LENGTH(<user_name>) > 5 ORDER BY RAND() LIMIT 10 ') );