1<?php
2 	/**
3	* Database class for MSQL
4	* @author SH Dienst GmbH Boris Erdmann, Kristian Koehntopp
5    * @author Dan Kuykendall, Dave Hall and others
6	* @copyright Copyright (C) 1998-2000 SH Dienst GmbH Boris Erdmann, Kristian Koehntopp
7	* @copyright Portions Copyright (C) 2001-2004 Free Software Foundation, Inc. http://www.fsf.org/
8	* @license http://www.fsf.org/licenses/lgpl.html GNU Lesser General Public License
9	* @link http://www.sanisoft.com/phplib/manual/DB_sql.php
10	* @package phpgwapi
11	* @subpackage database
12	* @version $Id: class.db_msql.inc.php 15462 2004-11-06 15:34:27Z powerstat $
13	*/
14
15	/**
16	* Database class for MSQL
17	*
18	* @package phpgwapi
19	* @subpackage database
20	* @ignore
21	*/
22	class db
23	{
24		function connect($Database = '', $Host = '', $User = '', $Password = '')
25		{
26			/* Handle defaults */
27			if ($Database == '')
28			{
29				$Database = $this->Database;
30			}
31			if ($Host == '')
32			{
33				$Host     = $this->Host;
34			}
35			if ($User == '')
36			{
37				$User     = $this->User;
38			}
39			if ($Password == '')
40			{
41				$Password = $this->Password;
42			}
43
44			// Not connected? Then connect?
45			if (! $this->Link_ID)
46			{
47				// Check for local connect
48				$this->Link_ID = empty($Host)?
49					msql_pconnect() :
50					msql_pconnect($Host);
51			}
52
53			// Still not connected? Raise error.
54			if (! $this->Link_ID )
55			{
56				$this->halt('Link-ID == false, pconnect failed');
57			}
58
59			// Select current database
60			if (!msql_select_db($Database, $this->Link_ID))
61			{
62				$this->halt('cannot use database '.$Database);
63			}
64		}
65
66		function query($Query_String)
67		{
68			$this->connect();
69
70			#   printf("Debug: query = %s<br />\n", $Query_String);
71
72			$this->Query_ID = msql_query($Query_String,$this->Link_ID);
73			$this->Row   = 0;
74			$this->Error = msql_error();
75			if (!$this->Query_ID)
76			{
77				$this->halt('Invalid SQL: '.$Query_String);
78			}
79			return $this->Query_ID;
80		}
81
82		function next_record()
83		{
84			$this->Record = msql_fetch_array($this->Query_ID);
85			$this->Row   += 1;
86			$this->Error = msql_error();
87
88			$stat = is_array($this->Record);
89			if (!$stat && $this->Auto_Free)
90			{
91				msql_free_result($this->Query_ID);
92				$this->Query_ID = 0;
93			}
94			return $stat;
95		}
96
97		function seek($pos)
98		{
99			$status = msql_data_seek($this->Query_ID, $pos);
100			if ($status)
101			{
102				$this->Row = $pos;
103			}
104			return;
105		}
106
107		function metadata($table)
108		{
109			$count = 0;
110			$id    = 0;
111			$res   = array();
112
113			$this->connect();
114			$id = @msql_list_fields($this->Database, $table);
115			if ($id < 0)
116			{
117				$this->Error = msql_error();
118				$this->halt('Metadata query failed.');
119			}
120			$count = msql_num_fields($id);
121
122			for ($i=0; $i<$count; $i++)
123			{
124				$res[$i]['table'] = msql_fieldtable ($id, $i);
125				$res[$i]['name']  = msql_fieldname  ($id, $i);
126				$res[$i]['type']  = msql_fieldtype  ($id, $i);
127				$res[$i]['len']   = msql_fieldlen   ($id, $i);
128				$res[$i]['flags'] = msql_fieldflags ($id, $i);
129				$res['meta'][$res[$i]['name']] = $i;
130				$res['num_fields']= $count;
131			}
132
133			msql_free_result($id);
134			return $res;
135		}
136
137		function affected_rows()
138		{
139			return msql_affected_rows($this->Query_ID);
140		}
141
142		function num_rows()
143		{
144			return msql_num_rows($this->Query_ID);
145		}
146
147		function num_fields()
148		{
149			return msql_num_fields($this->Query_ID);
150		}
151
152		function halt($msg)
153		{
154			printf("<b>Database error:</b> %s<br />\n", $msg);
155			printf("<b>MSQL Error</b>: %s<br />\n", $this->Error);
156			die('Session halted.');
157		}
158	}
159?>
160