2
/////////////////////////////////////////////////////////
4
// include/data_manager.MySQL.inc
6
// (C)Copyright 2003 Ryo Chijiiwa <Ryo@IlohaMail.org>
8
// This file is part of IlohaMail. IlohaMail is free software released
9
// under the GPL license. See enclosed file COPYING for details, or
10
// see http://www.fsf.org/copyleft/gpl.html
12
/////////////////////////////////////////////////////////
14
/********************************************************
17
Data handling abstraction class.
19
$DB_USERS_TABLE - Name of table cotaining users
22
For DB based backend, none of the data will be stored
23
in memory, and will be accessed through DB in real time.
25
********************************************************/
27
include_once("../include/array2sql.inc");
29
class DataManager_obj{
32
var $table; //directory for FS backend, table nmae for DB
33
var $dataID; //file name for FS backend, user's ID for DB backend
34
var $data; //actually contains
38
function initialize($user, $host, $table, $backend){
39
global $DB_USERS_TABLE;
42
$this->error = "DB table name or ID is empty\n";
46
$this->db = new idba_obj;
47
if (!$this->db->connect()) return false;
49
$sql = "SELECT * FROM $DB_USERS_TABLE WHERE login='$user' and host='$host'";
50
$result = $this->db->query($sql);
51
if (($result) && ($this->db->num_rows($result)>0)){
52
$dataID = $this->db->result($result, 0, "id");
54
$this->error = $error;
58
$this->error.="User not found in database\n";
62
$this->backend = $backend;
63
$this->table = $table;
64
$this->dataID = $dataID;
65
$this->data = array();
73
$sql = "SELECT * FROM ".$this->table." WHERE owner='".$this->dataID."'";
74
$result = $this->db->query($sql);
75
if (($result) && ($this->db->num_rows($result)>0)){
76
while ($a = $this->db->fetch_row($result)){
81
$this->error .= $error;
90
//everything's done in real time anyway
96
$sql = "DELETE FROM ".$this->table;
97
$sql.= " WHERE id='".$id."' and owner='".$this->dataID."'";
98
return $this->db->query($sql);
102
function insert($array){
103
//get list of fields in table
104
$backend_fields = $this->db->list_fields($this->table);
105
if (!is_array($backend_fields)){
106
$this->error .= "Failed to fetch fields\n";
107
$this->error .= $error;
111
//pick out relevant fields
112
$insert_data = array();
113
while ( list($k,$field) = each($backend_fields) ){
114
if (!empty($array[$field])){
115
$insert_data[$field] = $array[$field];
118
if (empty($insert_data["owner"])) $insert_data["owner"] = $this->dataID;
120
//$this->error .= "Inserting: ".implode("," $insert_data)."\n";
123
$sql = Array2SQL($this->table, $insert_data, "INSERT");
124
$backend_result = $this->db->query($sql);
126
$this->error = $error;
128
return $backend_result;
131
function update($id, $array){
132
//get list of fields in table
133
$backend_fields = $this->db->list_fields($this->table);
134
if (!is_array($backend_fields)){
135
$this->error .= "Failed to fetch fields\n";
136
$this->error .= $error;
140
//pick out relevant fields
141
$insert_data = array();
142
while ( list($k,$field) = each($backend_fields) ){
143
if (isset($array[$field]))
144
$insert_data[$field] = $array[$field];
148
$sql = Array2SQL($this->table, $insert_data, "UPDATE");
149
$sql.= " WHERE id='$id' and owner='".$this->dataID."'";
150
$this->db->query($sql);
152
//echo "updating: $sql<br>\n";
153
$backend_result = $this->db->query($sql);
154
$this->error .= $this->db->error();
156
return $backend_result;
160
function sort($field, $order){
163
$backend_query = "SELECT * FROM ".$this->table;
164
$backend_query.=" WHERE owner='".$this->dataID."'";
165
$backend_query.=" ORDER BY $field $order";
167
$backend_result = $this->db->query($backend_query);
169
if (($backend_result) && ($this->db->num_rows($backend_result)>0)){
170
while ($a = $this->db->fetch_row($backend_result)){
174
$this->error .= $this->db->error();
182
function getDistinct($field, $order){
185
$backend_query = "SELECT distinct $field FROM ".$this->table;
186
$backend_query.=" WHERE owner='".$this->dataID."'";
187
$backend_query.=" ORDER BY $field $order";
189
$backend_result = $this->db->query($backend_query);
191
if (($backend_result) && ($this->db->num_rows($backend_result)>0)){
192
while ($a = $this->db->fetch_row($backend_result)){
193
$data[] = $a[$field];
196
$this->error .= $this->db->error();
204
function search($array){
207
function showError(){
208
echo nl2br($this->error);
b'\\ No newline at end of file'