-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaffdb-php.php
973 lines (911 loc) · 24.2 KB
/
yaffdb-php.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
<?php
/**
* Yaffdb-php (Yet another flat file database for PHP)
*
* PHP version 5
*
* @category Database
* @package Yaffdb
* @author Houzuo(Howard) Guo <[email protected]>
* @license GPL v3
* @link https://github.com/HouzuoGuo/yaffdb-php
*/
/**
* Global variables.
*/
$slash = '/';
$table_exts = array('dat', 'def', 'exc');
$table_dirs = array('shr');
$db_columns = array('~del'=>1);
$lock_timeout = 10; // timeout of any table lock in seconds
/**
* Overwrites or appends the lines into the file.
*
* @param string $path the file's path
* @param array $lines lines to be written
* @param bool $append whether to append to the file or overwrite
*
* @return void
*/
function writeFile($path, $lines=array(), $append=false)
{
$fh = fopen($path, $append ? 'abt' : 'wbt');
foreach ($lines as $line) {
fwrite($fh, "$line\n");
}
fclose($fh);
}
/**
* Truncates or trims the string to the desired size.
*
* @param string $str the string to trim
* @param int $size the desired size of the string
*
* @return string the trimmed string
*/
function trimSize($str, $size)
{
if ($size - strlen($str) > 0) {
return str_pad($str, $size);
} else {
return substr($str, 0, $size);
}
}
/**
* Searches for a value in an array and unsets it.
*
* @param array &$array reference to the array
* @param mix $val the value to search for
*
* @return void
*/
function findUnset(&$array, $val)
{
foreach ($array as $i=>$v) {
if ($v == $val) {
unset($array[$i]);
}
}
}
/**
* Searches for a value in an array and replaces it.
*
* @param array &$array reference to the array
* @param mix $val the value to search for
* @param mix $replace the value used to replace
*
* @return void
*/
function findReplace(&$array, $val, $replace)
{
foreach ($array as $i=>$v) {
if ($v == $val) {
$array[$i] = $replace;
}
}
}
/**
* Tests if the string ends with the ending.
* Written by MrHus@StackOverflow.
*
* @param string $str the string to be tested
* @param string $ending the desired ending
*
* @return bool true if the test is successful, otherwise false
*/
function endWith($str, $ending)
{
$length = strlen($ending);
$start = $length * -1;
return (substr($str, $start) === $ending);
}
/**
* Returns the value if it is numeric, otherwise halts.
*
* @param mixed $var the value
*
* @return mixed the value
*/
function ensureNumeric($var)
{
is_numeric($var) or die("$var is not a number");
return $var;
}
/**
* Checks if a name may be used as an identifier in database.
* It gets trimmed as well.
*
* @param string &$name the identifier
*
* @return string the identifier with spaces being trimmed from both ends
*/
function dbID(&$name)
{
preg_match('/[~_\-a-z0-9]+/', $name) or die("$name is an invalid name");
$name = trim($name);
}
/**
* Recursively removes all files in a directory, and the directory itself.
* Written by holger1 at NOSPAMzentralplan dot de.
*
* @param string $dir path to the directory
*
* @return void
*/
function rrmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != '.' && $object != '..') {
$file = "$dir$slash$object";
if (filetype($file) == 'dir') {
rrmdir($file);
} else {
unlink($file);
}
}
}
reset($objects);
rmdir($dir);
}
}
/**
* Simple function to replicate PHP 5 behaviour.
*
* @return float milliseconds since Epoch
*/
function microtimeFloat()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/**
* Table column definition operations.
*
* Column definition uses the following format: (column name),(column size)\n
* E.g.
* NAME,10
* GENDER,1
*
* @category Database
* @package Yaffdb
* @author Houzuo(Howard) Guo <[email protected]>
* @license GPL v3
* @link https://github.com/HouzuoGuo/yaffdb-php
*/
class Column
{
/**
* Constructor.
*
* @param string $name column name
* @param int $size the fixed size of the column
* @param int $offset position of the column data in a table row
*/
public function __construct($name, $size, $offset=0)
{
$this->_name = $name;
$this->_offset = $offset;
$this->_size = $size;
}
/**
* Constructs an instance of Column given a column definition.
*
* @param string $definition the string definition of the column
*
* @return Column a constructed Column instance
*/
public static function fromDefinition($definition)
{
list($name, $size) = explode(',', $definition);
return new Column(dbID($name), trim(ensureNumeric($size)));
}
/**
* Returns the column name.
*
* @return string the column name
*/
public function getName()
{
return $this->_name;
}
/**
* Sets the column name.
*
* @param string $name the new column name
*
* @return void
*/
public function setName($name)
{
$this->_name = $name;
}
/**
* Returns the position of the column data in a table row (offset).
*
* @return int the offset
*/
public function getOffset()
{
return $this->_offset;
}
/**
* Sets the the position of the column data in a table row (offset).
*
* @param int $offset the new offset
*
* @return void
*/
public function setOffset($offset)
{
$this->_offset = $offset;
}
/**
* Returns size of the column.
*
* @return int the size
*/
public function getSize()
{
return $this->_size;
}
/**
* Constructs a column definition string from the attributes that this
* instance has.
*
* @return string a column definition string.
*/
public function getDefinition()
{
return "{$this->_name},{$this->_size}";
}
private $_name, $_offset, $_size;
}
/**
* Low-level table operations.
*
* A table has the following files in its database' directory:
* .dat - Data file (spreadsheet)
* .def - Column definitions
* .exc - Exclusive lock, content is the transaction ID
* .shr - (Directory) shared locks, each file is named by the transaction ID
*
* An example of .dat and .def files:
* (t1.def)
* NAME,10
* GENDER,1
*
* (t1.dat)
* Howard m
* Vanya f
* Steve m
* Sandra f
*
* @category Database
* @package Yaffdb
* @author Houzuo(Howard) Guo <[email protected]>
* @license GPL v3
* @link https://github.com/HouzuoGuo/yaffdb-php
*/
class Table
{
/**
* Constructor.
*
* @param Database $db reference to the database where the table belongs to
* @param string $name name of the table
*/
public function __construct($db, $name)
{
$this->_db = $db;
$this->_name = $name;
$this->open();
$this->readDefinition();
}
/**
* Returns the table name.
*
* @return string table name
*/
public function getName()
{
return $this->_name;
}
/**
* Sets the table name.
*
* @param string $name the new table name
*
* @return void
*/
public function setName($name)
{
$this->_name = $name;
}
/**
* Returns path to the table's .dat file.
*
* @return string the file path
*/
public function getDatPath()
{
return $this->_db->getDir()."{$this->_name}.dat";
}
/**
* Returns path to the table's .def file.
*
* @return string the file path
*/
public function getDefPath()
{
return $this->_db->getDir()."{$this->_name}.def";
}
/**
* Returns path to the table's .exc file.
*
* @return string the file path
*/
public function getExcPath()
{
return $this->_db->getDir()."{$this->_name}.exc";
}
/**
* Returns path to the table's .shr directory.
*
* @return string the directory path
*/
public function getShrPath()
{
return $this->_db->getDir()."{$this->_name}.shr";
}
/**
* Returns the table's line size (size of a row + 1).
*
* @return string file path
*/
public function getLineSize()
{
return $this->_lineSize;
}
/**
* Opens file handle(s).
*
* @return void
*/
public function open()
{
$this->_datFH = fopen($this->getDatPath(), 'rb+');
stream_set_write_buffer($this->_datFH, 0);
}
/**
* Reads column definitions from .def file.
*
* @return void
*/
public function readDefinition()
{
$this->_sequence = array(); // the sequence (order) of columns
$this->_columns = array(); // individual column definition
$this->_lineSize = 0; // size of a line (includes \n)
foreach (file($this->getDefPath()) as $definition) {
$column = Column::fromDefinition($definition);
$column->setOffset($this->_lineSize);
$this->_sequence[] = $column->getName();
$this->_lineSize += $column->getSize();
$this->_columns[$column->getName()] = $column;
}
++$this->_lineSize; // the \n counts
}
/**
* Counts the number of rows in table.
* Non-integer value indicates corrupted table data.
*
* @return int number of rows in table
*/
public function count()
{
clearstatcache();
$lines = filesize($this->getDatPath()) / $this->getLineSize();
return $lines;
}
/**
* Adds a new column.
*
* @param string $name new column's name
* @param int $size new column's size
*
* @return void
*/
public function add($name, $size)
{
dbID($name);
array_key_exists($name, $this->_columns)
and die("Column $name already exists");
// pad each row to leave space for the new column
if ($this->count() > 0) {
$this->_pad($size);
}
$column = new Column($name, $size, $this->_lineSize - 1);
$this->_sequence[] = $name;
$this->_columns[$name] = $column;
$this->_lineSize += $size;
// write the definition of the column into the .def file
writeFile($this->getDefPath(), array($column->getDefinition()), true);
}
/**
* Removes a column.
*
* @param string $name the column's name
*
* @return void
*/
public function remove($name)
{
array_key_exists($name, $this->_columns) or die("Column $name is not found");
// find and remove the column definition from .def file
$lines = array();
$definition = $this->_columns[$name]->getDefinition();
foreach (file($this->getDefPath()) as $line) {
$line = trim($line);
if ($line != $definition) {
$lines[] = $line;
}
}
writeFile($this->getDefPath(), $lines);
$column = $this->_columns[$name];
findUnset($this->_sequence, $name);
unset($this->_columns[$name]);
// remove the data of the column
if ($this->count() > 0) {
$column_ending = $column->getOffset() + $column->getSize();
$this->_cut($column->getOffset(), $column_ending);
}
$this->_lineSize -= $column->getSize();
}
/**
* Renames a column.
*
* @param string $old_name the column's original name
* @param string $new_name the column's new name
*
* @return void
*/
public function alter($old_name, $new_name)
{
dbID($new_name);
array_key_exists($old_name, $this->_columns)
or die("Column $old_name is not found");
array_key_exists($new_name, $this->_columns)
and die("Column $new_name already exists");
// find and re-write the definition in .def file
$lines = array();
$column = $this->_columns[$old_name];
$definition = $column->getDefinition();
foreach (file($this->getDefPath()) as $line) {
$line = trim($line);
if ($line == $definition) {
$size = $this->_columns[$old_name]->getSize();
$lines[] = "$new_name,$size";
} else {
$lines[] = $line;
}
}
writeFile($this->getDefPath(), $lines);
$column->setName($new_name);
findReplace($this->_sequence, $old_name, $new_name);
unset($this->_columns[$old_name]);
$this->_columns[$new_name] = $column;
}
/**
* Seeks the .dat file handle to the beginning of the row.
*
* @param int $number the row number to seek
*
* @return void
*/
public function seek($number)
{
$number < $this->count() or die("Row number $number is out of boundary");
fseek($this->_datFH, $number * $this->_lineSize, 0);
}
/**
* Reads a row, returns an array.
*
* @param int $number the row number
*
* @return array the row data
*/
public function read($number)
{
$this->seek($number);
$line = fgets($this->_datFH);
$row = array();
foreach ($this->_sequence as $name) {
$column = $this->_columns[$name];
$row[$name] = substr($line, $column->getOffset(), $column->getSize());
}
return $row;
}
/**
* Seeks the .dat file handle to the row and beginning of the column.
*
* @param int $number the row number
* @param string $name the column's name
*
* @return void
*/
public function seekColumn($number, $name)
{
array_key_exists($name, $this->_columns) or die("Column $name is not found");
$this->seek($number);
fseek($this->_datFH, $this->_columns[$name]->getOffset(), 1);
}
/**
* Inserts a row.
*
* @param array $row the row array (e.g. ('NAME'=>'Howard', 'GENDER'=>'m')
*
* @return void
*/
public function insert($row)
{
$line = '';
foreach ($this->_sequence as $name) {
$column = $this->_columns[$name];
$value = array_key_exists($name, $row) ? $row[$name] : '';
$line .= trimSize($value, $column->getSize());
}
$line .= "\n";
fseek($this->_datFH, 0, 2);
fwrite($this->_datFH, $line);
}
/**
* Updates the row at the row number.
*
* @param int $number the row number
* @param array $row the row array (e.g. ('NAME'=>'Howard', 'GENDER'=>'m')
*
* @return void
*/
public function update($number, $row)
{
foreach ($row as $name => $val) {
if (!array_key_exists($name, $this->_columns)) {
continue;
}
$column = $this->_columns[$name];
$this->seekColumn($number, $name);
fwrite($this->_datFH, trimSize($val, $column->getSize()));
}
}
/**
* Deletes a row.
*
* @param int $number the row number
*
* @return void
*/
public function delete($number)
{
array_key_exists('~del', $this->_columns)
or die("Column defition of ~del is missing");
// set ~del to y
$this->seekColumn($number, '~del');
fwrite($this->_datFH, trimSize('y', $this->_columns['~del']->getSize()));
}
/**
* Closes all opened file handles.
*
* @return void
*/
public function close()
{
clearstatcache();
fclose($this->_datFH);
}
/**
* Pads spaces to all rows in the table to leave space for a new column.
*
* @param int $padding number of padded spaces
*
* @return void
*/
private function _pad($padding)
{
$desired_size = $this->_lineSize + $padding - 1;
$lines = array();
foreach (file($this->getDatPath()) as $line) {
$lines[] = str_pad(rtrim($line), $desired_size);
}
writeFile($this->getDatPath(), $lines);
}
/**
* Removes data of a column.
*
* @param int $begin the beginning position of the column data in a row
* @param int $end the ending position of the column data in a row
*
* @return void
*/
private function _cut($begin, $end)
{
$lines = array();
foreach (file($this->getDatPath()) as $line) {
$before = substr($line, 0, $begin);
$after = substr($line, $end, strlen($line) - $begin - 1);
$lines[] = rtrim($before.$after, "\n");
}
writeFile($this->getDatPath(), $lines);
}
/**
* Destructor.
*/
public function __destruct()
{
$this->close();
}
private $_db, $_name, $_sequence, $_columns, $_datFH, $_lineSize;
}
/**
* Database logics.
* Database is stored as a directory in file system with all its tables' files.
*
* @category Database
* @package Yaffdb
* @author Houzuo(Howard) Guo <[email protected]>
* @license GPL v3
* @link https://github.com/HouzuoGuo/yaffdb-php
*/
class Database
{
/**
* Constructor.
*
* @param string $dir path to the database directory
*/
public function __construct($dir)
{
global $slash;
$this->_tables = array();
if (!endWith($dir, $slash)) {
$dir .= $slash;
}
$this->_dir = $dir;
foreach (scandir($dir) as $path) {
$path_parts = pathinfo($path);
if ($path_parts['extension'] && $path_parts['extension'] == 'dat') {
$table = new Table($this, $path_parts['filename']);
$this->_tables[$table->getName()] = $table;
}
}
}
/**
* Returns the database directory path.
*
* @return string the directory path
*/
public function getDir()
{
return $this->_dir;
}
/**
* Returns the table instance given a table name.
*
* @param string $name the table's name
*
* @return Table the table instance
*/
public function table($name)
{
array_key_exists($name, $this->_tables) or die("Table $name does not exist");
return $this->_tables[$name];
}
/**
* Creates a table.
*
* @param string $name the new table's name
*
* @return void
*/
public function create($name)
{
dbID($name);
global $slash, $table_exts, $table_dirs, $db_columns;
array_key_exists($name, $this->_tables)
and die("Table $name already exists");
// create the files and directories for the new table
foreach ($table_exts as $ext) {
writeFile("{$this->_dir}$slash$name.$ext");
}
foreach ($table_dirs as $dir) {
mkdir("{$this->_dir}$slash$name.$dir");
}
$table = $this->_tables[$name] = new Table($this, $name);
// put default columns in
foreach ($db_columns as $column=>$size) {
$table->add($column, $size);
}
return $table;
}
/**
* Renames a table.
* It is unnecessary to abandone using any reference to the table before
* renaming.
*
* @param string $old_name the table's original name
* @param string $new_name the new name
*
* @return void
*/
public function rename($old_name, $new_name)
{
global $slash;
dbID($new_name);
array_key_exists($old_name, $this->_tables)
or die("Table $old_name does not exist");
array_key_exists($new_name, $this->_tables)
and die("Table $new_name already exists");
$tab = $this->_tables[$old_name];
$tab->close();
sleep(1); // Martin Pelletier 05-Feb-2011 06:01
// rename the table files and directories
foreach (scandir($this->_dir) as $path) {
if ($path != '.' && $path != '..') {
$path_parts = pathinfo($path);
$new_path = $this->_dir.$new_name.'.'.$path_parts['extension'];
rename("{$this->_dir}$path", $new_path);
}
}
$tab->setName($new_name);
$tab->open();
$this->_tables[$new_name] = $tab;
unset($this->_tables[$old_name]);
}
/**
* Drops a table.
*
* @param string $name the table's name
*
* @return void
*/
public function drop($name)
{
array_key_exists($name, $this->_tables) or die("Table $name does not exist");
$tab = $this->_tables[$name];
$tab->close();
// delete the table's files and directories
foreach (scandir($this->_dir) as $path) {
$path_parts = pathinfo($path);
if ($path_parts['filename'] == $name) {
$path = "{$this->_dir}$path";
if (is_file($path)) {
unlink($path);
} else {
rrmdir($path);
}
}
}
}
private $_dir, $_tables;
}
/**
* Transaction, table locking logics.
* Transaction ID is allocated as the system time in milliseconds. Thus it is
* unsafe under heavy concurrent operations.
*
* @category Database
* @package Yaffdb
* @author Houzuo(Howard) Guo <[email protected]>
* @license GPL v3
* @link https://github.com/HouzuoGuo/yaffdb-php
*/
class Transaction
{
/**
* The constructor.
*
* @param Database $db the database which the transaction works with
*/
public function __construct($db)
{
$this->_db = $db;
$this->_id = microtimeFloat();
$this->_locked = array(); // locked tables
$this->_history = array(); // insert/update/delete history
}
/**
* Locks a table in exclusive mode.
* Fails when another transaction is holding exclusive lock on it.
*
* @param Table $table the table to lock
*
* @return bool true if successful, otherwise false
*/
public function exclusiveLock($table)
{
}
/**
* Locks a table in shared mode.
* Fails when another transaction is holding exclusive lock on it.
*
* @param Table $table the table to lock
*
* @return bool true if successful, otherwise false
*/
public function sharedLock($table)
{
}
/**
* This transaction tries to unlock a table.
* Fails when the table cannot be unlocked by this transaction.
*
* @param Table $table the table to unlock
*
* @return bool if successful, otherwise false
*/
public function unlock($table)
{
}
/**
* Commits the transaction, gets a new transaction ID and releases all locked
* resources.
*
* @return void
*/
public function commit()
{
}
/**
* Rolls back the transaction, undo insert/update/delete operations, gets a
* new transaction ID and releases all locked resources.
*
* @return void
*/
public function rollback()
{
}
/**
* Returns locks on a table in the following structure:
* array('exclusive'=>101, 'shared'=>array(102, 103, 104))
*
* @param Table $table the table
*
* @return array locks on the table
*/
public static function locksOf($table)
{
global $lock_timeout;
$ret = array('exclusive'=>null, 'shared'=>array());
$exclusive_lock = file_get_contents($table->getExcPath());
if ($exclusive_lock) {
$lock_id = ensureNumeric($exclusive_lock[0]);
if ($lock_id > microtimeFloat() + $lock_timeout) {
// wipe the exclusive lock if it has expired
writeFile($table->getExcPath());
} else {
$ret['exclusive'] = $lock_id;
}
}
}
private $_db, $_locked, $_id, $_history;
}
chdir($_SERVER['DOCUMENT_ROOT']);
$db = new Database("temp");
$tab = $db->create("t1");
$tab->add('c1', 10);
$tab->add('c2', 20);
$tab->insert(array('c1'=>'a', 'c2'=>'b'));
$tab->insert(array('c1'=>'c', 'c2'=>'d'));
$tab->add('c3', 10);
$tab->insert(array('c3'=>'aaa'));
$tab->remove('c3');
$tab->insert(array('c2'=>'bbb'));
$tab->insert(array('c2'=>'ccc'));
$tab->delete(0);
$tab->update(1, array('c1'=>'new!'));
$tab->alter('c2', 'cnew');
$db->rename('t1', 't2');
var_dump($tab->read(0));
$db->drop('t2');
?>