Annotation of FreeBSD/tinderbox/webui/core/TinderboxDS.php, revision 1.1.1.1.2.3
1.1 as 1: <?php
2: #-
3: # Copyright (c) 2004-2005 FreeBSD GNOME Team <freebsd-gnome@FreeBSD.org>
4: # All rights reserved.
5: #
6: # Redistribution and use in source and binary forms, with or without
7: # modification, are permitted provided that the following conditions
8: # are met:
9: # 1. Redistributions of source code must retain the above copyright
10: # notice, this list of conditions and the following disclaimer.
11: # 2. Redistributions in binary form must reproduce the above copyright
12: # notice, this list of conditions and the following disclaimer in the
13: # documentation and/or other materials provided with the distribution.
14: #
15: # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16: # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17: # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18: # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19: # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20: # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21: # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22: # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23: # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24: # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25: # SUCH DAMAGE.
26: #
1.1.1.1.2.3! as 27: # $Paefchen: FreeBSD/tinderbox/webui/core/TinderboxDS.php,v 1.1.1.1.2.2 2007/11/13 07:52:29 as Exp $
1.1 as 28: # $MCom: portstools/tinderbox/webui/core/TinderboxDS.php,v 1.36 2007/10/13 02:28:47 ade Exp $
29: #
30:
1.1.1.1.2.1 as 31: require_once 'DB.php';
32:
33: class TinderboxDS {
1.1.1.1.2.3! as 34: static private $instance = false;
1.1.1.1.2.2 as 35: private $objectMap = array(
1.1.1.1.2.1 as 36: 'Build' => 'builds',
37: 'BuildPortsQueue' => 'build_ports_queue',
38: 'Config' => 'config',
39: 'Jail' => 'jails',
40: 'Port' => 'ports',
41: 'PortsTree' => 'ports_trees',
42: 'PortFailReason' => 'port_fail_reasons',
43: 'User' => 'users');
1.1.1.1.2.3! as 44: private $db;
1.1.1.1.2.1 as 45: private $error = null;
1.1.1.1.2.3! as 46: private $packageSuffixCache; /* in use by getPackageSuffix() */
1.1.1.1.2.1 as 47:
1.1.1.1.2.3! as 48: private function __construct() {
1.1.1.1.2.1 as 49: global $DB_HOST, $DB_DRIVER, $DB_NAME, $DB_USER, $DB_PASS;
50:
51: # XXX: backwards compatibility
52: if (empty($DB_DRIVER))
53: $DB_DRIVER = 'mysql';
54:
55: $dsn = "$DB_DRIVER://$DB_USER:$DB_PASS@$DB_HOST/$DB_NAME";
56:
57: $this->db = DB::connect($dsn);
58:
59: if (DB::isError($this->db))
1.1.1.1.2.3! as 60: die (sprintf("Error => %s: Unable to initialize datastore: %s\n",
! 61: __CLASS__, $this->db->getMessage()));
1.1.1.1.2.1 as 62:
63: $this->db->setFetchMode(DB_FETCHMODE_ASSOC);
64: $this->db->setOption('persistent', true);
65: }
66:
1.1.1.1.2.3! as 67: static public function instance() {
! 68: if (! self::$instance)
! 69: self::$instance = new TinderboxDS;
! 70: return self::$instance;
! 71: }
! 72:
! 73: public function __destruct() {
! 74: @$this->db->disconnect();
1.1.1.1.2.1 as 75: $this->error = null;
76: }
77:
78:
79: public function start_transaction() {
80: $this->db->autoCommit( false );
81: }
82:
83: public function commit_transaction() {
84: $this->db->commit();
85: $this->db->autoCommit( true );
86: }
87:
88: public function rollback_transaction() {
89: $this->db->rollback();
90: $this->db->autoCommit( true );
91: }
92:
93: public function getAllMaintainers() {
94: $query = '
95: SELECT DISTINCT LOWER(port_maintainer) AS port_maintainer
96: FROM ports
97: WHERE port_maintainer IS NOT NULL
98: ORDER BY LOWER(port_maintainer)';
99: if (! $this->_doQueryHashRef($query, $results, array()))
100: return array();
101:
102: foreach($results as $result)
103: $data[] = $result['port_maintainer'];
104:
105: return $data;
106: }
107:
108: public function getAllPortsByPortID($portid) {
109: $query = sprintf('
110: SELECT p.*,
111: bp.build_id,
112: bp.last_built,
113: bp.last_status,
114: bp.last_successful_built,
115: bp.last_failed_dependency,
116: bp.last_run_duration,
117: bp.last_built_version,
118: CASE bp.last_fail_reason
119: WHEN \'__nofail__\' THEN \'\'
120: ELSE bp.last_fail_reason
1.1 as 121: END
1.1.1.1.2.1 as 122: AS last_fail_reason
123: FROM ports p, build_ports bp
124: WHERE p.port_id = bp.port_id
125: AND bp.port_id = %d',
126: $portid);
127:
1.1.1.1.2.2 as 128: return $this->_doQueryHashRef($query, $results, array())
1.1.1.1.2.1 as 129: ? $this->_newFromArray('Port', $results)
130: : null;
131: }
132:
133:
134: public function addUser($user) {
135: $query = '
136: INSERT INTO users (
137: user_name, user_email, user_password, user_www_enabled
138: ) VALUES (?, ?, ?, ?)';
139:
140: return $this->_doQuery($query,
141: array($user->getName(),
142: $user->getEmail(),
143: $user->getPassword(),
144: $user->getWwwEnabled()),
145: $res);
146: }
1.1 as 147:
1.1.1.1.2.1 as 148: public function deleteUser($user) {
149: if(! $user->getId() || $this->deleteUserPermissions($user, '') ) {
150: if ($user->getId())
151: $this->deleteBuildPortsQueueByUserId($user);
1.1 as 152:
1.1.1.1.2.1 as 153: $query = 'DELETE FROM users WHERE user_name = ?';
1.1 as 154:
1.1.1.1.2.1 as 155: if (! $this->_doQuery($query, array($user->getName()), $res))
156: return false;
1.1 as 157:
158: return true;
159: }
1.1.1.1.2.1 as 160:
161: return false;
162: }
163:
164: public function updateUser($user) {
165: $query = '
166: UPDATE users
167: SET user_name = ?,
168: user_email = ?,
169: user_password = ?,
170: user_www_enabled = ?
171: WHERE user_id = ?';
172:
173: return $this->_doQuery($query,
174: array($user->getName(),
175: $user->getEmail(),
176: $user->getPassword(),
177: $user->getWwwEnabled(),
178: $user->getId()),
179: $res);
180: }
181:
182: public function getUserByLogin($username, $password) {
183: $query = '
184: SELECT user_id, user_name, user_email, user_password, user_www_enabled
185: FROM users
186: WHERE user_name = ?
187: AND user_password = ?';
188:
189: if (! $this->_doQueryHashRef($query, $results, array($username, md5($password))))
190: return null;
191:
192: list($user) = $this->_newFromArray('User', $results);
193:
194: return $user;
195: }
196:
197: public function getUserPermissions($user_id, $object_type, $object_id) {
198: $query = '
199: SELECT
200: CASE user_permission
201: WHEN 1 THEN \'IS_WWW_ADMIN\'
202: WHEN 2 THEN \'PERM_ADD_QUEUE\'
203: WHEN 3 THEN \'PERM_MODIFY_OWN_QUEUE\'
204: WHEN 4 THEN \'PERM_DELETE_OWN_QUEUE\'
205: WHEN 5 THEN \'PERM_PRIO_LOWER_5\'
206: WHEN 6 THEN \'PERM_MODIFY_OTHER_QUEUE\'
207: WHEN 7 THEN \'PERM_DELETE_OTHER_QUEUE\'
208: ELSE \'PERM_UNKNOWN\'
209: END AS user_permission
210: FROM user_permissions
211: WHERE user_id = ?
212: AND user_permission_object_type = ?
213: AND user_permission_object_id = ?';
214:
215: return $this->_doQueryHashRef($query, $results, array($user_id, $object_type, $object_id))
216: ? $results
217: : null;
218: }
219:
220: public function deleteUserPermissions($user, $object_type) {
221: $query = 'DELETE FROM user_permissions WHERE user_id = ?';
222:
223: if( $object_type )
224: $query .= sprintf(" AND user_permission_object_type = '%s'", $object_type);
225:
226: return $this->_doQuery($query, array($user->getId()), $res);
227: }
228:
229: public function addUserPermission($user_id, $object_type, $object_id, $permission) {
230: switch( $permission ) {
231: // case 'IS_WWW_ADMIN': $permission = 1; break; /* only configureable via shell */
232: case 'PERM_ADD_QUEUE': $permission = 2; break;
233: case 'PERM_MODIFY_OWN_QUEUE': $permission = 3; break;
234: case 'PERM_DELETE_OWN_QUEUE': $permission = 4; break;
235: case 'PERM_PRIO_LOWER_5': $permission = 5; break;
236: case 'PERM_MODIFY_OTHER_QUEUE': $permission = 6; break;
237: case 'PERM_DELETE_OTHER_QUEUE': $permission = 7; break;
238: default: return false;
239: }
240:
241: $query = '
242: INSERT INTO user_permissions (
243: user_id, user_permission_object_type, user_permission_object_id, user_permission
244: ) VALUES (?, ?, ?, ?)';
245:
246: return $this->_doQuery($query,
247: array($user_id, $object_type, $object_id, $permission),
248: $res);
249: }
250:
251: public function getBuildPortsQueueEntries($build_id) {
252: $query = '
253: SELECT build_ports_queue.*, builds.build_name AS build_name, users.user_name AS user_name
254: FROM build_ports_queue, builds, users
255: WHERE build_ports_queue.build_id = ?
256: AND builds.build_id = build_ports_queue.build_id
257: AND users.user_id = build_ports_queue.user_id
258: ORDER BY priority ASC, build_ports_queue_id ASC';
259:
260: return $this->_doQueryHashRef($query, $results, array($build_id))
261: ? $this->_newFromArray('BuildPortsQueue', $results)
262: : null;
263: }
264:
265: public function deleteBuildPortsQueueEntry($entry_id) {
266: $query = 'DELETE FROM build_ports_queue WHERE build_ports_queue_id = ?';
267:
268: return $this->_doQuery($query, $entry_id, $res);
269: }
270:
271: public function deleteBuildPortsQueueByUserId($user) {
272: $query = 'DELETE FROM build_ports_queue WHERE user_id = ?';
273:
274: return $this->_doQuery($query, $user->getId(), $res);
275: }
276:
277: public function createBuildPortsQueueEntry($build_id, $priority,
278: $port_directory, $user_id, $email_on_completion) {
279:
280: switch( $email_on_completion ) {
281: case '1': $email_on_completion = 1; break;
282: default: $email_on_completion = 0; break;
283: }
284:
285: $entries[] = array(
286: 'build_id' => $build_id,
287: 'priority' => $priority,
288: 'port_directory' => $port_directory,
289: 'user_id' => $user_id,
290: 'enqueue_date' => date('Y-m-d H:i:s'),
291: 'email_on_completion' => $email_on_completion,
292: 'status' => 'ENQUEUED');
293:
294: list($result) = $this->_newFromArray('BuildPortsQueue', $entries);
295:
296: return $result;
297: }
298:
299: public function updateBuildPortsQueueEntry($entry) {
300: $query = '
301: UPDATE build_ports_queue
302: SET build_id = ?, priority = ?, email_on_completion = ?, status = ?
303: WHERE build_ports_queue_id = ?';
304:
305: return $this->_doQuery($query,
306: array($entry->getBuildId(),
307: $entry->getPriority(),
308: $entry->getEmailOnCompletion(),
309: $entry->getStatus(),
310: $entry->getId()),
311: $res);
312: }
313:
314: public function addBuildPortsQueueEntry($entry) {
315: $query = '
316: INSERT INTO build_ports_queue (
317: enqueue_date, build_id, priority, port_directory, user_id, email_on_completion, status
318: ) VALUES (?, ?, ?, ?, ?, ?, ?)';
319:
320: return $this->_doQuery($query,
321: array($entry->getEnqueueDate(),
322: $entry->getBuildId(),
323: $entry->getPriority(),
324: $entry->getPortDirectory(),
325: $entry->getUserId(),
326: $entry->getEmailOnCompletion(),
327: $entry->getStatus()),
328: $res);
1.1 as 329: }
330:
1.1.1.1.2.1 as 331: public function getBuildPortsQueueEntryById($id) {
332: $results = $this->getBuildPortsQueue(
333: array( 'build_ports_queue_id' => $id ));
334:
335: if (is_null($results))
336: return null;
337:
338: return $results[0];
339: }
340:
341: public function getPortsForBuild($build, $sortby = 'port_directory') {
342: switch ($sortby) {
343: case '':
344: $sortby = 'port_directory';
345: $sortbytable = 'p';
346: break;
347: case 'port_directory':
348: case 'port_maintainer':
349: $sortbytable = 'p';
350: break;
351: default:
352: $sortbytable = 'bp';
353: }
1.1 as 354:
1.1.1.1.2.1 as 355: $query = sprintf('
356: SELECT p.*,
357: bp.last_built,
358: bp.last_status,
359: bp.last_successful_built,
360: bp.last_built_version,
361: bp.last_failed_dependency,
362: bp.last_run_duration,
363: CASE bp.last_fail_reason
364: WHEN \'__nofail__\' THEN \'\'
365: ELSE bp.last_fail_reason
366: END AS last_fail_reason
367: FROM ports p, build_ports bp
368: WHERE p.port_id = bp.port_id
369: AND bp.build_id = ?
370: ORDER BY %s.%s',
371: $sortbytable, $sortby);
372:
373: return $this->_doQueryHashRef($query, $results, $build->getId())
374: ? $this->_newFromArray('Port', $results)
375: : null;
376: }
377:
378: public function getLatestPorts($build_id, $limit = false) {
379: $query = '
380: SELECT p.*,
381: bp.build_id,
382: bp.last_built,
383: bp.last_status,
384: bp.last_successful_built,
385: bp.last_built_version,
386: bp.last_failed_dependency,
387: bp.last_run_duration,
388: CASE bp.last_fail_reason
389: WHEN \'__nofail__\' THEN \'\'
390: ELSE bp.last_fail_reason
391: END AS last_fail_reason
392: FROM ports p, build_ports bp
393: WHERE p.port_id = bp.port_id
394: AND bp.last_built IS NOT NULL';
395:
396: if($build_id)
397: $query .= ' AND bp.build_id = ' . $build_id;
398:
399: $query .= ' ORDER BY bp.last_built DESC';
400:
401: if($limit)
402: $query .= ' LIMIT ' . $limit;
403:
404: return $this->_doQueryHashRef($query, $results, array())
405: ? $this->_newFromArray('Port', $results)
406: : null;
407: }
408:
409: public function getBuildPorts($port_id, $build_id) {
410: $query = '
411: SELECT p.*,
412: bp.last_built,
413: bp.last_status,
414: bp.last_successful_built,
415: bp.last_failed_dependency,
416: bp.last_run_duration,
417: CASE bp.last_fail_reason
418: WHEN \'__nofail__\' THEN \'\'
419: ELSE bp.last_fail_reason
420: END
421: AS last_fail_reason
422: FROM ports p, build_ports bp
423: WHERE p.port_id = bp.port_id
424: AND bp.build_id = ?
425: AND bp.port_id = ?';
1.1 as 426:
1.1.1.1.2.1 as 427: if (! $this->_doQueryHashRef($query, $results, array($build_id, $port_id)))
428: return null;
1.1 as 429:
1.1.1.1.2.1 as 430: list($ports) = $this->_newFromArray('Port', $results);
431: return $ports;
1.1 as 432: }
433:
434:
1.1.1.1.2.1 as 435: public function getPortsByStatus($build_id, $maintainer, $status, $notstatus) {
436: $query = '
437: SELECT p.*,
438: bp.build_id,
439: bp.last_built,
440: bp.last_status,
441: bp.last_successful_built,
442: bp.last_built_version,
443: bp.last_failed_dependency,
444: bp.last_run_duration,
445: CASE bp.last_fail_reason
446: WHEN \'__nofail__\' THEN \'\'
447: ELSE bp.last_fail_reason
448: END AS last_fail_reason
449: FROM ports p, build_ports bp
450: WHERE p.port_id = bp.port_id';
451:
452: if($build_id)
453: $query .= ' AND bp.build_id = ' . $build_id;
454:
455: if($status <> '')
456: $query .= sprintf(" AND bp.last_status='%s'", $status);
457:
458: if($notstatus <> '')
459: $query .= sprintf(" AND bp.last_status <> '%s' AND bp.last_status <> 'UNKNOWN'",
460: $notstatus);
461:
462: if($maintainer)
463: $query .= sprintf(" AND p.port_maintainer='%s'", $maintainer);
464:
465: $query .= ' ORDER BY bp.last_built DESC';
466:
467: return $this->_doQueryHashRef($query, $results, array())
1.1.1.1.2.2 as 468: ? $this->_newFromArray('Port', $results)
469: : null;
1.1.1.1.2.1 as 470: }
471:
472: public function getBuildStatsWithStatus($build_id) {
473: $query = '
474: SELECT last_status,COUNT(*) AS c
475: FROM build_ports
476: WHERE build_id = ?
477: GROUP BY last_status';
478:
1.1.1.1.2.2 as 479: return $this->_doQueryHashRef($query, $results, $build_id)
1.1.1.1.2.1 as 480: ? $results
481: : null;
482: }
1.1 as 483:
484:
1.1.1.1.2.1 as 485: public function getBuildStats($build_id) {
486: $query = '
487: SELECT COUNT(*) AS fails
488: FROM build_ports
489: WHERE last_status = \'FAIL\'
490: AND build_id = ?';
491:
1.1.1.1.2.2 as 492: return $this->_doQueryHashRef($query, $results, $build_id)
1.1.1.1.2.1 as 493: ? $results[0]
494: : null;
495: }
496:
497: public function getPortById($id) {
498: $results = $this->getPorts(array( 'port_id' => $id ));
499:
500: return is_null($results)
501: ? null
502: : $results[0];
503: }
1.1 as 504:
1.1.1.1.2.1 as 505: public function getPortByDirectory($dir) {
1.1 as 506: $results = $this->getPorts(array( 'port_directory' => $dir ));
507:
1.1.1.1.2.1 as 508: return is_null($results)
509: ? null
510: : $results[0];
1.1 as 511: }
512:
1.1.1.1.2.1 as 513: public function getCurrentPortForBuild($build_id) {
514: $query = '
515: SELECT port_id AS id
516: FROM build_ports
517: WHERE build_id = ?
518: AND currently_building = \'1\'';
519:
520: return $this->_doQueryHashRef($query, $results, array($build_id))
521: ? $this->getPortById($results[0]['id'])
522: : null;
1.1 as 523: }
524:
1.1.1.1.2.1 as 525: public function getObjects($type, $params = array()) {
526: if (! isset($this->objectMap[$type]))
527: die("Unknown object type, $type\n");
528:
529: $table = $this->objectMap[$type];
530: $condition = "";
531:
532: $values = array();
533: $conds = array();
534:
535: foreach ($params as $field => $param) {
536: # Each parameter makes up and OR portion of a query. Within
537: # each parameter can be a hash reference that make up the AND
538: # portion of the query.
539: if (is_array($param)) {
540: $ands = array();
541: foreach ($param as $andcond => $value) {
542: array_push($ands, "$andcond = ?");
543: array_push($values, $value);
1.1 as 544: }
1.1.1.1.2.1 as 545: array_push($conds, '(' . (implode(' AND ', $ands)) . ')');
546: }
547: else {
548: array_push($conds, '(' . $field . '=?)');
549: array_push($values, $param);
550: }
551: }
552:
553: $condition = implode(' OR ', $conds);
554:
1.1.1.1.2.2 as 555: $query = empty($condition)
1.1.1.1.2.1 as 556: ? "SELECT * FROM $table"
557: : "SELECT * FROM $table WHERE $condition";
558:
559: $results = array();
560: return $this->_doQueryHashRef($query, $results, $values)
561: ? $this->_newFromArray($type, $results)
562: : null;
563: }
564:
565: public function getBuildByName($name) {
566: $results = $this->getBuilds(array( 'build_name' => $name ));
567:
568: return is_null($results)
569: ? null
570: : $results[0];
571: }
572:
573: public function getBuildById($id) {
574: $results = $this->getBuilds(array( 'build_id' => $id ));
575:
576: return is_null($results)
577: ? null
578: : $results[0];
579: }
580:
581: public function getJailById($id) {
582: $results = $this->getJails(array( 'jail_id' => $id ));
583:
584: return is_null($results)
585: ? null
586: : $results[0];
587: }
588:
589: public function getPortsTreeForBuild($build) {
590: return $this->getPortsTreeById($build->getPortsTreeId());
591: }
592:
593: public function getPortsTreeByName($name) {
594: $results = $this->getPortsTrees(array( 'ports_tree_name' => $name ));
595:
596: return is_null($results)
597: ? null
598: : $results[0];
599: }
600:
601: public function getPortsTreeById($id) {
602: $results = $this->getPortsTrees(array( 'ports_tree_id' => $id ));
603:
604: return is_null($results)
605: ? null
606: : $results[0];
607: }
608:
609: public function getUserById($id) {
610: $results = $this->getUsers(array( 'user_id' => $id ));
611:
612: return is_null($results)
613: ? null
614: : $results[0];
615: }
616:
617: public function getUserByName($name) {
618: $results = $this->getUsers(array( 'user_name' => $name ));
619:
620: return is_null($results)
621: ? null
622: : $results[0];
623: }
624:
625: public function getConfig($params = array()) {
626: return $this->getObjects('Config', $params);
627: }
1.1 as 628:
1.1.1.1.2.1 as 629: public function getBuildPortsQueue($params = array()) {
630: return $this->getObjects('BuildPortsQueue', $params);
1.1 as 631: }
632:
1.1.1.1.2.1 as 633: public function getBuilds($params = array()) {
634: return $this->getObjects('Build', $params);
635: }
1.1 as 636:
1.1.1.1.2.1 as 637: public function getPorts($params = array()) {
638: return $this->getObjects('Port', $params);
1.1 as 639: }
640:
1.1.1.1.2.1 as 641: public function getJails($params = array()) {
642: return $this->getObjects('Jail', $params);
643: }
644:
645: public function getPortFailReasons($params = array()) {
646: return $this->getObjects('PortFailReason', $params);
647: }
648:
649: public function getPortsTrees($params = array()) {
650: return $this->getObjects('PortsTree', $params);
651: }
652:
653: public function getUsers($params = array()) {
654: return $this->getObjects('User', $params);
655: }
656:
657: public function getAllConfig() {
658: return $this->getConfig();
659: }
1.1 as 660:
1.1.1.1.2.1 as 661: public function getAllBuilds() {
662: return $this->getBuilds();
663: }
1.1 as 664:
1.1.1.1.2.1 as 665: public function getAllJails() {
666: return $this->getJails();
667: }
1.1 as 668:
1.1.1.1.2.1 as 669: public function getAllPortFailReasons() {
670: return $this->getPortFailReasons();
671: }
1.1 as 672:
1.1.1.1.2.1 as 673: public function getAllPortsTrees() {
674: return $this->getPortsTrees();
675: }
1.1 as 676:
1.1.1.1.2.1 as 677: public function getAllUsers() {
678: return $this->getUsers();
679: }
1.1 as 680:
1.1.1.1.2.1 as 681: public function addError($error) {
682: return $this->error[] = $error;
683: }
1.1 as 684:
1.1.1.1.2.1 as 685: public function getErrors() {
686: return $this->error;
687: }
1.1 as 688:
1.1.1.1.2.1 as 689: private function _doQueryNumRows($query, $params = array()) {
690: $rows = 0;
691: if (! $this->_doQuery($query, $params, $res))
692: return -1;
1.1 as 693:
1.1.1.1.2.1 as 694: if ($res->numRows() > -1) {
695: $rows = $res->numRows();
1.1 as 696: }
1.1.1.1.2.1 as 697: else {
698: while($res->fetchRow()) {
699: $rows++;
700: }
1.1 as 701: }
702:
1.1.1.1.2.1 as 703: $res->free();
1.1 as 704:
1.1.1.1.2.1 as 705: return $rows;
706: }
1.1 as 707:
1.1.1.1.2.1 as 708: private function _doQueryHashRef($query, &$results, $params = array()) {
1.1.1.1.2.2 as 709: if (! $this->_doQuery($query, $params, $res)) {
1.1.1.1.2.1 as 710: $results = null;
711: return 0;
1.1 as 712: }
713:
1.1.1.1.2.1 as 714: $results = array();
715: while ($row = $res->fetchRow())
716: array_push($results, $row);
1.1 as 717:
1.1.1.1.2.1 as 718: $res->free();
1.1 as 719:
1.1.1.1.2.1 as 720: return 1;
721: }
1.1 as 722:
1.1.1.1.2.1 as 723: private function _doQuery($query, $params, &$res) {
724: $sth = $this->db->prepare($query);
1.1 as 725:
1.1.1.1.2.1 as 726: if (DB::isError($this->db)) {
727: $this->addError($this->db->getMessage());
728: return 0;
1.1 as 729: }
730:
1.1.1.1.2.2 as 731: $_res = count($params)
1.1.1.1.2.1 as 732: ? $this->db->execute($sth, $params)
733: : $this->db->execute($sth);
1.1 as 734:
1.1.1.1.2.1 as 735: if (DB::isError($_res)) {
736: $this->addError($_res->getMessage());
737: return 0;
1.1 as 738: }
739:
1.1.1.1.2.1 as 740: if (! is_null($_res))
741: $res = $_res;
742: else
743: $res->free();
1.1 as 744:
1.1.1.1.2.1 as 745: return 1;
746: }
1.1 as 747:
1.1.1.1.2.1 as 748: private function _newFromArray($type, $arr) {
749: $objects = array();
1.1 as 750:
1.1.1.1.2.1 as 751: foreach ($arr as $item) {
752: $obj = new $type($item);
753: // eval('$obj = new $type($item);');
754: if (! is_a($obj, $type))
1.1 as 755: return null;
756:
1.1.1.1.2.1 as 757: array_push($objects, $obj);
1.1 as 758: }
759:
1.1.1.1.2.1 as 760: return $objects;
761: }
1.1 as 762:
1.1.1.1.2.1 as 763: public function getPackageSuffix($jail_id) {
764: if (empty($jail_id))
765: return '';
766:
767: /* Use caching to avoid a lot of SQL queries */
768: if (isset($this->packageSuffixCache[$jail_id])) {
769: return $this->packageSuffixCache[$jail_id];
770: }
771: else {
772: $jail = $this->getJailById($jail_id);
773: return $this->packageSuffixCache[$jail_id] =
774: substr($jail->getName(), 0, 1) <= '4'
775: ? '.tgz'
776: : '.tbz';
1.1 as 777: }
1.1.1.1.2.1 as 778: }
779: }
780:
1.1 as 781: ?>