| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (C) 2003-2004 Polytechnique.org |
| 4 | * http://opensource.polytechnique.org/ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * A class for describing database queries and breaking down the results |
| 24 | * into pages. |
| 25 | */ |
| 26 | class DiogenesQuery { |
| 27 | /** The number of arguments in the current query. */ |
| 28 | var $nArgs; |
| 29 | /** The current query string. */ |
| 30 | var $sQuery; |
| 31 | |
| 32 | /** The constructor. |
| 33 | */ |
| 34 | function DiogenesQuery($sQue = "") { |
| 35 | $this->sQuery = $sQue; |
| 36 | $this->nArgs = 0; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /** Add an argument to the query. |
| 41 | */ |
| 42 | function addArg($sArg, $sSep = "") { |
| 43 | if ($sSep && $this->nArgs) |
| 44 | $this->sQuery .= $sSep; |
| 45 | $this->sQuery .= $sArg; |
| 46 | $this->nArgs++; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /** Return an array holding the start and end point |
| 51 | * for the pages holding the results. |
| 52 | */ |
| 53 | function getPages($qcount,$hitmax=0) |
| 54 | { |
| 55 | if (!$hitmax) |
| 56 | return array(array(0,$qcount)); |
| 57 | |
| 58 | $pages = array(); |
| 59 | $nrest = $qcount % $hitmax; |
| 60 | $npages = ($qcount - $nrest) / $hitmax; |
| 61 | |
| 62 | // complete pages |
| 63 | for($i = 0; $i < $npages; $i++) |
| 64 | array_push($pages, array($i*$hitmax, ($i+1)*$hitmax)); |
| 65 | |
| 66 | // leftovers |
| 67 | if ($nrest) |
| 68 | array_push($pages, array($npages*$hitmax, $npages*$hitmax+$nrest)); |
| 69 | return $pages; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** Execute the query and return the result. |
| 74 | */ |
| 75 | function getResult(&$dbh,$hitmax=0,$start=0) |
| 76 | { |
| 77 | $query = $this->sQuery; |
| 78 | if ($hitmax) { |
| 79 | $query .= " LIMIT $start, $hitmax"; |
| 80 | } |
| 81 | |
| 82 | return $dbh->query($query); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** Accessor for the query string. |
| 87 | */ |
| 88 | function getQuery() { |
| 89 | return $this->sQuery; |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
| 94 | ?> |