Обертка обертки. v0.1

  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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php //this is kernel of db_module
/*
-- Connect to database
fn_connect($databaseConfiguration)
-- Create table in selected database
fn_CreateTable($TableName, $rows)
[x] rows array look like:
[x] 'row name' => 'row type'
-- Delete table in selected database
fn_DeleteTable($TableName)
-- Post new data to table in selected database
fn_PostToTable($TableName, $dataToPostInTable)
[x] data array look like:
[x] 'row name' => 'safe row data'
-- Delete data from table in selected database
fn_DeletePost($TableName, $condition)
[x] condition is array, and look like:
[x] 'row name' => 'condition for row'
-- Select rows from table in selected database
fn_SelectFromTable($TableName, $condition)
[x] condition is array, and look like:
[x] 'row name' => 'condition for row'
[x] if condition array is empty, returned all data
*/
class mainDB
{
function fn_slashes($text) { return str_replace("'", "\\'", $text); }
function fn_connect($conf)
{
$link = mysql_connect
(
$conf['host'],
$conf['user'],
$conf['password']
);
return $link;
}
function fn_fetchSelectQuery($res)
{
$resd = array();
for ($i = 0; $i < count($res); $i++)
$resd[$i] = mysql_fetch_array($res);
return $resd;
}
function fn_getCondition($data)
{
$values = '';
if (count($data) == 1) {
foreach ($data as $key => $value)
$values = "(`$key` = '$value')";
} else {
$values = '';
foreach ($data as $key => $value)
$values .= "(`$key` = '$value') and ";
$values = substr_replace ( $values, '', strlen($values) - 4 );
}
if (count($data) == 0)
$values = "NULL";
return $values;
}
function fn_CreateTable($TableName, $data)
{
$safeTableName = $this -> fn_slashes($TableName);
$safeValues = "` (";
foreach ($data as $key => $value)
$safeValues .= "`$key` $value, ";
$safeValues = substr_replace ( $safeValues, ')', strlen($safeValues) - 2 );
$query = "CREATE TABLE `".$safeTableName.$safeValues;
var_dump($query);
mysql_query($query) or die(mysql_error());
}
function fn_DeleteTable($TableName)
{
$query = "DROP TABLE `$TableName`";
var_dump($query);
mysql_query($query) or die(mysql_error());
}
function fn_PostToTable($TableName, $data)
{
$safeTableName = $this -> fn_slashes($TableName);
$safeValues = "VALUES (";
foreach ($data as $key => $value)
$safeValues .= "'{$this -> fn_slashes($value)}', ";
$safeValues = substr_replace ( $safeValues, ')', strlen($safeValues) - 2 );
$query = "INSERT INTO `{$TableName}` ".$safeValues;
var_dump($query);
mysql_query($query) or die(mysql_error());
}
function fn_DeletePost($TableName, $data)
{
$query = "DELETE FROM `$TableName` WHERE ";
// if length of $data array more, then one
// 'where' block look like '(`name` = "fff") and (...) and (...)'
$values = $this -> fn_getCondition($data);
$query .= $values;
var_dump($query);
mysql_query($query) or die(mysql_error());
}
function fn_SelectFromTable($TableName, $data)
{
$rows = '';
if (count($data) == 0) {
$rows = "*";
} else {
foreach ($data as $key => $value)
$rows .= "`$key`,";
$rows = substr_replace ( $rows, '', strlen($rows) - 1 );
}
$query = "SELECT $rows FROM `$TableName` ";
// if length of $data array more, then one
// 'where' block look like '(`name` = "fff") and (...) and (...)'
$values = $this -> fn_getCondition($data);
if ($values != "NULL") {
$values = "WHERE ".$values;
} else {
$values = "";
}
$query .= $values;
var_dump($query);
$res = mysql_query($query) or die(mysql_error());
return $this -> fn_fetchSelectQuery($res);
}
}
include "config.php";
$db_worker = new mainDB;
$link = $db_worker -> fn_connect($db_configuration);
mysql_select_db($db_configuration['database'],$link) or die(mysql_error());
/* delete this comment for YOBA-DEMO-IN-CONSOLE
// #php file.php
$table = date('U');
$rows = array(
'NAME' => 'varchar(25)',
'CIPHER' => 'int'
);
$db_worker -> fn_CreateTable($table, $rows);
$dataToPostInTable = array(
'NAME' => 'Vasya',
'CIPHER' => '195322'
);
$db_worker -> fn_PostToTable($table, $dataToPostInTable);
$db_worker -> fn_PostToTable($table, $dataToPostInTable);
$condition = array();
$result = $db_worker -> fn_SelectFromTable($table, $condition);
var_dump($result);
$condition = array(
'NAME' => 'Vasya',
'CIPHER' => '195322'
);
$db_worker -> fn_DeletePost($table, $condition);
$db_worker -> fn_DeleteTable($table);
var_dump('Test of var_dump');
delete this comment */
?>