-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnanodb.php
217 lines (200 loc) · 5.46 KB
/
nanodb.php
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
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/************************************************
* NanoDB v.9.01
* Ultra Lightweight Text File Data Base
* PHP CRUD system.
* Copyright (c) 2009 Antonio Villamarin
* www.zeura.com
* License CC 3.0BY-SA
* http://creativecommons.org/licenses/by-sa/3.0/
***********************************************/
// Configuracion
define( 'ND_DIR', dirname( __FILE__ ).'/' );
define( 'ND_DEBUG', FALSE );
// Errores
define( 'ND_ERROR_CANNOT_CREATE', '(1) El fichero ya existe.' );
define( 'ND_ERROR_CONDITION', '(2) Condición errónea.' );
define( 'ND_FILE_NOT_EXISTS', '(3) El fichero no existe.' );
function nd_file( $name ) {
return( ND_DIR.$name.'.ndb' );
}
function nd_exists( $name ) {
return( file_exists( nd_file( $name ) ) );
}
function nd_create( $name ) {
if( file_exists( nd_file( $name ) ) ) {
nd_error( ND_ERROR_CANNOT_CREATE.' ['.nd_file( $name ).']' );
return( FALSE );
}
$json = array(
'name' => $name,
'data' => array(),
'count' => 0,
'last_modified' => time(),
'creation' => time(),
'high_id' => 0
);
nd_put( $name, $json );
}
function nd_drop( $name ) {
if( file_exists( nd_file( $name ) ) ) {
unlink( nd_file( $name ) );
} else {
nd_error( ND_FILE_NOT_EXISTS.' ['.nd_file( $name ).']' );
}
}
function nd_put( $name, $value ) {
$value['last_modified'] = time();
file_put_contents( nd_file( $name ), base64_encode( json_encode( $value ) ) );
chmod( nd_file( $name ), 0750 );
}
function nd_get( $name ) {
if( file_exists( nd_file( $name ) ) ) {
return( json_decode( base64_decode( file_get_contents( nd_file( $name ) ) ), TRUE ) );
} else {
nd_error( ND_FILE_NOT_EXISTS.' ['.nd_file( $name ).']' );
}
}
function nd_select( $name, $cond = NULL ) {
$json = nd_get( $name );
if( is_string( $cond ) ) {
$json = _nd_evaluate_condition( $json, $cond );
}
return( $json );
}
function nd_delete( $name, $cond ) {
$json = nd_get( $name );
$counter = $json['count'];
$json = _nd_evaluate_condition( $json, $cond, TRUE );
nd_put( $name, $json );
return( $counter - $json['count'] );
}
function nd_update( $name, $fields, $cond = NULL ) {
$json = nd_get( $name );
$results = $json;
if( is_string( $cond ) ) {
$results = _nd_evaluate_condition( $json, $cond );
}
$counter = 0;
if( $results['count'] > 0 ) {
foreach( $results['data'] as $key => $line ) {
foreach( $line as $fname => $fvalue ) {
$json['data'][$key][$fname] = (isset($fields[$fname])?$fields[$fname]:$json['data'][$key][$fname]);
}
$counter++;
}
}
nd_put( $name, $json );
return( $counter );
}
function nd_insert( $name, $fields ) {
$json = nd_get( $name );
if( is_array( $fields[0] ) ) {
foreach( $fields as $record ) {
$record['_id'] = $json['high_id']+1;
$json['data'][] = $record;
$json['count'] = count( $json['data'] );
$json['high_id']++;
}
} else {
$fields['_id'] = $json['high_id']+1;
$json['data'][] = $fields;
$json['count'] = count( $json['data'] );
$json['high_id']++;
}
nd_put( $name, $json );
}
function _nd_evaluate_condition( $json, $cond, $not = FALSE ) {
$c = _nd_evaluate_operation( $cond );
if( $c === FALSE ) {
return( $json );
}
if( count( $json['data'] ) > 0 ) {
$result = array();
foreach( $json['data'] as $key => $line ) {
switch( $c[2] ) {
case '==':
if( $line[$c[0]] == $c[1] && !$not ) {
$result[$key] = $line;
} elseif( $line[$c[0]] != $c[1] && $not ) {
$result[$key] = $line;
}
break;
case '!=':
if( $line[$c[0]] != $c[1] && !$not ) {
$result[$key] = $line;
} elseif( $line[$c[0]] == $c[1] && $not ) {
$result[$key] = $line;
}
break;
case '>=':
if( $line[$c[0]] >= $c[1] && !$not ) {
$result[$key] = $line;
} elseif( $line[$c[0]] < $c[1] && $not ) {
$result[$key] = $line;
}
break;
case '<=':
if( $line[$c[0]] <= $c[1] && !$not ) {
$result[$key] = $line;
} elseif( $line[$c[0]] > $c[1] && $not ) {
$result[$key] = $line;
}
break;
case '>':
if( $line[$c[0]] > $c[1] && !$not ) {
$result[$key] = $line;
} elseif( $line[$c[0]] <= $c[1] && $not ) {
$result[$key] = $line;
}
break;
case '<':
if( $line[$c[0]] < $c[1] && !$not ) {
$result[$key] = $line;
} elseif( $line[$c[0]] >= $c[1] && $not ) {
$result[$key] = $line;
}
break;
case '[]':
$preg = ( preg_match( $c[1], $line[$c[0]] ) > 0 );
if( !$preg XOR !$not ) {
$result[$key] = $line;
}
break;
}
}
$json['data'] = $result;
$json['count'] = count( $result );
}
return( $json );
}
function _nd_evaluate_operation( $cond ) {
$conds = array( '==', '!=', '>=', '<=', '>', '<', '[]' );
$j = FALSE;
$i = 0;
while( $j === FALSE && $i < count( $conds ) ) {
$j = strpos( $cond, $conds[$i] );
$i++;
}
$i--;
if( $j !== FALSE ) {
$r = explode( $conds[$i], $cond );
$r[0] = trim( $r[0] );
$r[1] = trim( $r[1] );
$r[2] = $conds[$i];
return( $r );
} else {
nd_error( ND_ERROR_CONDITION.' ['.$cond.']' );
return( FALSE );
}
}
function nd_error( $error = NULL ) {
static $errors = array();
if( $error != NULL ) {
$errors[] = $error;
if( ND_DEBUG ) echo( 'ND Error: '.$error.'<br/>'."\n" );
} else {
return( $errors );
}
}
?>