ok($table->close(), 'save'); } else { fail('set integer'); fail('set string'); fail('set empty string'); fail('set true'); fail('set false'); fail('save'); } if (($table = metatable::open('table')) !== FALSE) { is($table->get('*', '*'), array('x' => array('int' => 1, 'string' => 'abc', 'emptystring' => '', 'true' => TRUE, 'false' => FALSE)), 'get'); ok($table->close(), 'close'); } else { fail('get'); fail('close'); } if (($table = metatable::open('table', metatable::READWRITE)) !== FALSE) { is($table->get('*', '*'), array('x' => array('int' => 1, 'string' => 'abc', 'emptystring' => '', 'true' => TRUE, 'false' => FALSE)), 'get when opened only with READWRITE flag'); ok($table->close(), 'close when opened only with READWRITE flag'); } else { fail('get when opened only with READWRITE flag'); fail('close when opened only with READWRITE flag'); } if (($table = metatable::open('table', metatable::READONLY)) !== FALSE) { is($table->get('*', '*'), array('x' => array('int' => 1, 'string' => 'abc', 'emptystring' => '', 'true' => TRUE, 'false' => FALSE)), 'get readonly'); ok($table->close(), 'close readonly'); } else { fail('get readonly'); fail('close readonly'); } unlink('table');
<?php // require some basic definitions require_once dirname(__FILE__) . '/squares.php'; // open in readwrite mode (default) $table = metatable::open(TABLE_FILE); if (!$table) { die("cannot open table file " . TABLE_FILE . "! :-(\n"); } // store data for ($i = 1; $i <= N; $i++) { if (!$table->set(sprintf(ROW_PRINTF, $i), 'value', $i * $i)) { die("cannot store data into table!\n"); } } // save if (!$table->close()) { die("cannot save table file\n"); }
/** * Initialize * @var string */ public static function init($dir) { parent::init($dir); self::$stats = metatable::open($dir . DIRECTORY_SEPARATOR . '.stats', metatable::READWRITE | metatable::AUTOCLOSE); }
<?php // require some basic definitions require_once dirname(__FILE__) . '/squares.php'; // open readonly (default) $table = metatable::open(TABLE_FILE, metatable::READONLY); if (!$table) { die("cannot open table file " . TABLE_FILE . "! :-(\n"); } // retrieve data -- * can be used as wild card foreach ($table->get('*', '*') as $row => $data) { list($n) = sscanf($row, ROW_PRINTF); $value = $data['value']; printf("% 4d * %4d = % 8d\n", $n, $n, $value); } // no need to close() if do not want to save -- file is closed automatically
<?php // import all libs require_once 'lib/Nette/Tools.php'; foreach (Tools::glob(dirname(__FILE__) . '/lib/*.php') as $_) { require_once $_; } // init $table = dirname(__FILE__) . '/db/guestbook'; if (!is_dir(dirname($table)) && !mkdir(dirname($table), 0777, TRUE)) { die('Cannot create database :-('); } if (!file_exists($table) && !(($_ = metatable::open($table)) && $_->close())) { die('Cannot create table :-('); } if (!(${'@table'} = metatable::open($table, $_SERVER['REQUEST_METHOD'] === 'GET' ? metatable::READONLY : metatable::READWRITE | metatable::STRINGS_GC))) { die('Cannot open table :-('); } $_ = ${'@table'}->get('=', '*'); ${'@all'} = array(); if (!empty($_)) { ${'@all'} = array_keys(array_reverse(array_shift($_))); } ${'@limit'} = 10; ${'@page'} = 1; if (!empty($_SERVER['QUERY_STRING'])) { ${'@page'} = intval($_SERVER['QUERY_STRING']); } // create form ${'@form'} = new Form(); ${'@form'}->addText('name', 'Jméno:')->addRule(Form::FILLED, 'Anonymy tu nechceme.');