File: db_peardb.php

package info (click to toggle)
phpreports 0.4.9-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 684 kB
  • ctags: 1,426
  • sloc: php: 3,377; xml: 203; makefile: 29; sql: 18; sh: 17; python: 10
file content (141 lines) | stat: -rwxr-xr-x 3,093 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

/*************************************************************************
 * File: db_peardb.php
 * Authors: Matthew Palmer <mpalmer@baileyroberts.com.au>
 *
 * A PEAR DB abstraction layer backend for PHPReports.  Acts a little bit
 * differently to other PHPReports backends; I don't necessarily think
 * that's a problem at my end.
 *
 * Easiest way to use this is to pass a DSN into the report via the server
 * parameter.  The rest of the DB parameters are ignored.
 *
 * Copyright (C) 2004 Bailey Roberts Group Pty Ltd.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation (version 2 of the License)
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 ****************************************************************************/
           
require_once 'DB.php';

class PHPR_PEARDB_Result
{
	var $index = 0;
	
	var $data = array();
	
	function PHPR_PEARDB_Result($res)
	{
		while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
		{
			$this->data[] = $row;
		}

		$this->index = 0;
	}

	function fetchNext()
	{
		if ($this->index >= count($this->data))
		{
			return NULL;
		}
		return $this->data[$this->index++];
	}
	
	function fieldNames()
	{
		return array_keys($this->data[0]);
	}

	function numCols()
	{
		return count($this->data[0]);
	}
}

// This database type is a little different to the average.  You can
// either specify a DSN to connect to via the 'server' parameter, or
// otherwise specify a global variable name in the 'database'
// parameter which contains the PEAR database object you want to
// use.  'server' trumps 'database' if both are supplied.
class PHPReportsDBI {
	function db_connect($cdata)
	{
		if ($cdata[2])
		{
			$DB = DB::Connect($cdata[2]);
		}
		else
		{
			global ${$cdata[3]};
			
			$DB = ${$cdata[3]};
		}

		if (DB::isError($DB))
		{
			die($DB->getMessage()."\n".$DB->getUserInfo()."\n");
		}
		
		return $DB;		
	}

	function db_select_db($sDatabase)
	{
		// You wanna DB?  Pick it in the DSN
		return;
	}

	function db_query(&$DB,$SQL)
	{
		$res = $DB->query($SQL);
		
		$rv = new PHPR_PEARDB_Result($res);
		$res->free();
		return $rv;
	}

	function db_colnum($res)
	{
		return $res->numCols();
	}

	function db_columnName($res,$i)
	{
		$names = $res->fieldNames();
		return $names[$i-1];
	}
		
	function db_columnType($res,$i)
	{
		// The type of a field should be irrelevant
		return 'INT';
	}

	// An auto-increment fetch
	function db_fetch(&$res)
	{
		$row = $res->fetchNext();
		return $row;
	}

	function db_free($res)
	{
		return 0;
	}

	function db_disconnect($DB)
	{
		$DB->disconnect();
	}
}