Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
 
3
/***************************************************************************
4
 *   Copyright (C) 2009-2011 by Geo Varghese(www.seopanel.in)  	   *
5
 *   sendtogeo@gmail.com   												   *
6
 *                                                                         *
7
 *   This program is free software; you can redistribute it and/or modify  *
8
 *   it under the terms of the GNU General Public License as published by  *
9
 *   the Free Software Foundation; either version 2 of the License, or     *
10
 *   (at your option) any later version.                                   *
11
 *                                                                         *
12
 *   This program is distributed in the hope that it will be useful,       *
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 *   GNU General Public License for more details.                          *
16
 *                                                                         *
17
 *   You should have received a copy of the GNU General Public License     *
18
 *   along with this program; if not, write to the                         *
19
 *   Free Software Foundation, Inc.,                                       *
20
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21
 ***************************************************************************/
22
 
23
# class defines all database functions
24
class Database{
25
 
26
	var $dbEngine;
27
	var $dbConObj;
28
 
29
    // constructor
30
    function __construct($dbEngine='mysql'){
31
 
32
    	// if db engine is mysql
33
    	if ($dbEngine == 'mysql') {
34
    		$this->dbEngine = function_exists('mysqli_query') ? "mysqlihelper" : $dbEngine;
35
    	} else {
36
    		$this->dbEngine = $dbEngine;
37
    	}
38
 
39
    }
40
 
41
	# func to connect db enine
42
    function dbConnect(){
43
    	include_once(SP_LIBPATH."/".$this->dbEngine."/".$this->dbEngine.".class.php");
44
    	$this->dbConObj = New $this->dbEngine(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, SP_DEBUG);
45
    	return $this->dbConObj;
46
    }
47
 
48
    /**
49
     * function get a db table row information
50
     * @param String $table			The name of the table
51
     * @param String $whereCond		The where condition string to get the results
52
     * @param String $cols 			The db columns needs to be retrieved
53
     */
54
    public function getRow($table, $whereCond = '1=1', $cols = '*') {
55
    	$whereCond = !empty($whereCond) ? $whereCond : '1=1';
56
    	$sql = "SELECT $cols FROM $table WHERE $whereCond";
57
    	$rowInfo = $this->dbConObj->select($sql, true);
58
    	return $rowInfo;
59
    }
60
 
61
    /**
62
     * function get all db table rows according to where condition
63
     * @param String $table			The name of the table
64
     * @param String $whereCond		The where condition string to get the results
65
     * @param String $cols 			The db columns needs to be retrieved
66
     */
67
    public function getAllRows($table, $whereCond = '1=1', $cols = '*') {
68
    	$whereCond = !empty($whereCond) ? $whereCond : '1=1';
69
    	$sql = "SELECT $cols FROM $table WHERE $whereCond";
70
    	$rowList = $this->dbConObj->select($sql);
71
    	return $rowList;
72
    }
73
 
74
    /**
75
     * Function to delete db table row according to the where condition
76
     * @param String $table			The name of the table
77
     * @param String $whereCond		The where condition string to get the results
78
     */
79
    public function deleteRows($table, $whereCond = '1=1') {
80
 
81
    	$whereCond = !empty($whereCond) ? $whereCond : '1=1';
82
    	$sql = "Delete FROM $table WHERE $whereCond";
83
 
84
    	// if no error occured
85
    	if ($this->dbConObj->query($sql)) {
86
    		return TRUE;
87
    	}
88
 
89
    	return FALSE;
90
    }
91
 
92
    /**
93
     * function to insert row to a db table
94
     * @param string $table			The name of the table
95
     * @param Array $dataList		The data list array with key as db column and value as db column insert value
96
     * 								Eg: array('name' => 'Tom', 'status' => 1)
97
     */
98
    public function insertRow($table, $dataList) {
99
 
100
    	$dataList = self::escapeValue($dataList);
101
    	$colList = array_keys($dataList);
102
    	$valueList = array_values($dataList);
103
    	$sql = "INSERT into $table(" . implode(',', $colList) . ") values('" . implode("', '", $valueList) . "')";
104
 
105
    	// if no error occured
106
    	if ($this->dbConObj->query($sql)) {
107
    		return TRUE;
108
    	}
109
 
110
    	return FALSE;
111
    }
112
 
113
    /**
114
     * function to update row of a db table
115
     * @param string $table			The name of the table
116
     * @param Array $dataList		The data list array with key as db column and value as db column insert value
117
     * 								Eg: array('name' => 'Tom', 'status' => 1)
118
     * @param String $whereCond		The where condition string to get the results
119
     */
120
    public function updateRow($table, $dataList, $whereCond = '1=1') {
121
 
122
    	$dataList = self::escapeValue($dataList);
123
    	$whereCond = !empty($whereCond) ? $whereCond : '1=1';
124
    	$sql = "Update $table SET ";
125
 
126
    	// for loop through values
127
    	foreach ($dataList as $key => $value) {
128
    		$sql .= "$key='$value',";
129
    	}
130
 
131
    	$sql = rtrim($sql, ",");
132
		$sql .= " WHERE $whereCond";
133
 
134
    	// if no error occured
135
    	if ($this->dbConObj->query($sql)) {
136
    		return TRUE;
137
    	}
138
 
139
    	return FALSE;
140
    }
141
 
142
    /**
143
     * function to escape db values to be inserted
144
     * @param Mixed $dataList		the array contains valus as key => $value
145
     */
146
    public static function escapeValue($dataList) {
147
    	$escDataList = array();
148
 
149
    	// loop through the data list
150
    	foreach ($dataList as $key => $value) {
151
 
152
	    	// check whether type passed
153
	    	if (stristr($key, '|')) {
154
	    		list($key, $type) = explode("|", $key);
155
	    	} else {
156
	    		$type = "string";
157
	    	}
158
 
159
	    	switch ($type) {
160
 
161
	    		case "float":
162
	    			$value = floatval($value);
163
	    			break;
164
 
165
	    		case "number":
166
	    		case "integer":
167
	    		case "int":
168
	    			$value = intval($value);
169
	    			break;
170
 
171
	    		case "text":
172
	    		case "string":
173
	    		default:
174
	    			$value = addslashes($value);
175
	    			break;
176
 
177
	    	}
178
 
179
	    	$escDataList[$key] = $value;
180
    	}
181
 
182
    	return $escDataList;
183
 
184
    }
185
 
186
    /**
187
     * function to escape data
188
     * @param mixed $value		To be escaped
189
     * @param string $type		The data type[float|number|integer|int|text|string]
190
     */
191
    public static function escapeData($value, $type) {
192
    	$dataList = array("data|$type" => $value);
193
    	$dataList = self::escapeValue($dataList);
194
    	return $dataList['data'];
195
    }
196
 
197
}
198
?>