示例#1
0
 /**
  * Basic setup of the mock database to perform tests against.
  *
  */
 public static function setUpBeforeClass()
 {
     self::$di = new \Anax\DI\CDIFactoryDefault();
     self::$app = new \Anax\MVC\CApplicationBasic(self::$di);
     self::$di->setShared('db', function () {
         $db = new \Mos\Database\CDatabaseBasic();
         $db->setOptions(['dsn' => "sqlite:memory::", "verbose" => false]);
         $db->connect();
         $db->dropTableIfExists('content')->execute();
         $db->createTable('content', ['id' => ['integer', 'primary key', 'not null', 'auto_increment'], 'slug' => ['varchar(80)', 'unique'], 'url' => ['varchar(80)', 'unique'], 'type' => ['varchar(80)'], 'title' => ['varchar(80)'], 'data' => ['text'], 'filter' => ['char(80)'], 'created' => ['datetime'], 'published' => ['datetime'], 'updated' => ['datetime'], 'deleted' => ['datetime'], 'owner' => ['varchar(80)'], 'updated_by' => ['varchar(80)']])->execute();
         $now = gmdate('Y-m-h H:i:s');
         $db->insert('content', ['slug', 'type', 'title', 'data', 'created', 'published', 'owner']);
         $db->execute(['a-first-article', 'article', 'A first article', 'This is the text of the first article.', $now, $now, 'admin']);
         return $db;
     });
 }
示例#2
0
<?php

require "../src/Database/TSQLQueryBuilderBasic.php";
require "../src/Database/CDatabaseBasic.php";
$db = new \Mos\Database\CDatabaseBasic();
//
// Create a table
//
$db->setTablePrefix('mos_');
$db->createTable('test', ['id' => ['integer', 'primary key', 'not null'], 'age' => ['integer'], 'text' => ['varchar(20)'], 'text2' => ['varchar(20)']]);
echo "<pre>" . $db->getSQL() . "</pre>";
//
// Insert a single row into table using key => value
//
$db->insert('test', ['id' => 2, 'text' => "Mumintrollet", 'text2' => "Mumindalen"]);
echo "<pre>" . $db->getSQL() . "</pre>";
//
// Insert a single row into table using two arrays
//
$db->insert('test', ['id', 'text', 'text2'], [2, "Mumintrollet", "Mumindalen"]);
echo "<pre>" . $db->getSQL() . "</pre>";
//
// Insert a single row into table using one array (rest will be sent as parameters)
//
$db->insert('test', ['id', 'text', 'text2']);
echo "<pre>" . $db->getSQL() . "</pre>";
//
// Update a single row using key => value
//
$db->update('test', ['age' => 22, 'text' => "Mumintrollet", 'text2' => "Mumindalen"], "id = 2");
echo "<pre>" . $db->getSQL() . "</pre>";
示例#3
0
$options = (require "../config_mysql.php");
//
// Carry out som tests, db must exist
//
$db->setOptions($options);
$db->setTablePrefix($options['table_prefix']);
$db->connect();
//
// Drop a table if it exists
//
$tableName = 'test';
$db->dropTableIfExists($tableName)->execute();
//
// Create a table
//
$db->createTable($tableName, ['id' => ['integer', 'auto_increment', 'primary key', 'not null'], 'username' => ['varchar(20)'], 'password' => ['varchar(32)'], 'salt' => ['varchar(32)']])->execute();
//
// Add some users to test with
//
$sql = <<<EOD
INSERT INTO
    {$options['table_prefix']}{$tableName} (username, salt)
    VALUES (?, md5(NOW()));
EOD;
$db->execute($sql, ['doe']);
$db->execute($sql, ['admin']);
//
// Add some users, two steps since must use salt as created before.
//
$sql = <<<EOD
UPDATE