Изменения

Funq

2737 байтов добавлено, 10:42, 15 июля 2009
Нет описания правки
* Возможности программного последовательного выполнения подзапросов — сначала внутреннего, а потом внешнего, с программной передачей результата первого во второй (в MySQL бывают ситуации когда это ускоряет запрос В РАЗЫ);
* Нет ORM’а, и даже не обдуман. А возможно, стоит. А возможно, и не стоит.
 
== Примеры ==
 
==== Объектное формирование запросов ====
 
Вот, например, запрос из начала статьи на Perl:
 
<source lang="perl">
$query = $dbh
->query(album2album => "t1")->where("t1.parent=? and t1.pid=?pid")
->join($dbh->query(albums => "t0")->where("t0.id=t1.child and t0.?t0where"))
->leftjoin($dbh->query(album2album => "t3")->where("t3.parent=t1.parent and t3.pid=?pid"))
->join($dbh->query(album2album => "t4")->where("t4.parent=t3.child and t4.child=t1.child and t4.pid=?pid"))
->select("t0.*", "(t3.parent is null) as t1._direct")
->hint({calc-found-rows => 1})
->group("t0.id")
->where("t1._direct=1")
->order("t0.ord desc, t0.name")
->limit("?offset", "?limit")
</source>
 
Вот пример его выполнения:
 
<source lang="perl">
$rows = $query->hasharray({
parent => $parent,
pid => $pid,
t0where => "config=?config",
config => 1,
offset => 0,
limit => 20,
});
</source>
 
А вот он же, но с вызовами join у объекта запроса, а не у $dbh:
 
<source lang="perl">
$dbh
->query(album2album => "t1")->where("t1.parent=? and t1.pid=?pid")
->join($dbh->query(albums => "t0")->where("t0.id=t1.child and t0.?t0where"))
->join(left => $dbh->query(album2album => "t3")->where("t3.parent=t1.parent and t3.pid=?pid"))
->join($dbh->query(album2album => "t4")->where("t4.parent=t3.child and t4.child=t1.child and t4.pid=?pid"))
->select("t0.*", "(t3.parent is null) as t1._direct")
->hint({calc-found-rows => 1})
->group("t0.id")
->where("t1._direct=1")
->order("t0.ord desc, t0.name")
->limit("?offset", "?limit")
</source>
 
Запрос попроще:
 
<source lang="perl">
$dbh->join(
$dbh->query(tag2entity_cl => "t1")->where("t1.pid=? and t1.tag=?"),
$dbh->query(entity => "t0")->where("t0.id=t1.eid and t0.config=? and t0.?t0where")
)->select("t0.*")
->order("t0.?t0order")
->limit("?offset", "?limit")
</source>
 
Или совсем простой:
 
<source lang="perl">
$dbh->query("exif")->where("file=?")
</source>
 
А вот подзапрос:
 
<source lang="perl">
$dbh
->query(entity => "t0")
->where("t0.?t0where and ?subq in ?tagcount")
->select("t0.*, ?subq as t0.tag_count", subq => $dbh
->query(tag2entity => "t1")
->where("t1.pid=t0.pid and t1.eid=t0.eid")
->count->value
)
</source>
 
Или даже два вложенных подзапроса:
 
<source lang="perl">
$dbh
->query(entity => "t0")
->where("t0.?t0where and ?subq in ?tagcount")
->select("t0.*, ?subq as t0.tag_count", subq => $dbh
->query(tag2entity => "t1")
->where("t1.pid=t0.pid and t1.eid=t0.eid")
->where("t1.config in ?list", list => $dbh->query("configs"))
->count->value
)
</source>