Beispiel #1
0
 protected function getDb()
 {
     $def = new DatabaseDefinitionBuilder();
     $tableDef = new DefinitionBuilder();
     $tableDef->a = DataType::string();
     $tableDef->b = DataType::string();
     $tableDef->c = DataType::string();
     $def->addDefinition('Foo', $tableDef);
     $typeAdapter = $this->getMockBuilder('Jivoo\\Data\\Database\\TypeAdapter')->getMock();
     $typeAdapter->method('encode')->willReturnCallback(function ($type, $value) {
         return '"' . $value . '"';
     });
     $db = $this->getMockBuilder('Jivoo\\Data\\Database\\Common\\SqlDatabase')->getMock();
     $db->method('getTypeAdapter')->willReturn($typeAdapter);
     $db->method('getDefinition')->willReturn($def);
     $db->method('sqlLimitOffset')->willReturnCallback(function ($limit, $offset) {
         if (isset($offset)) {
             return 'LIMIT ' . $limit . ' OFFSET ' . $offset;
         }
         return 'LIMIT ' . $limit;
     });
     $db->method('tableName')->willReturnCallback(function ($table) {
         return \Jivoo\Utilities::camelCaseToUnderscores($table);
     });
     $db->method('quoteModel')->willReturnCallback(function ($model) {
         return '{' . $model . '}';
     });
     $db->method('quoteField')->willReturnCallback(function ($field) {
         return $field;
     });
     $db->method('quoteLiteral')->willReturnCallback(function ($type, $value) {
         return '"' . $value . '"';
     });
     $db->method('quoteString')->willReturnCallback(function ($value) {
         return '"' . $value . '"';
     });
     return $db;
 }
Beispiel #2
0
use Jivoo\Data\Database\DatabaseSchema;
use Jivoo\Data\Database\Loader;
use Jivoo\Data\DefinitionBuilder;
use Jivoo\Log\CallbackHandler;
use Jivoo\Log\Logger;
// Include Jivoo by using composer:
require '../vendor/autoload.php';
// Initialize database loader
$loader = new Loader();
// Log database queries to output
$logger = new Logger();
$logger->addHandler(new CallbackHandler(function (array $record) {
    if (isset($record['context']['query'])) {
        echo 'query: ' . $record['context']['query'] . PHP_EOL;
    }
}));
$loader->setLogger($logger);
// Initialize definition for User-table
$definition = new DatabaseDefinitionBuilder();
$definition->addDefinition('User', DefinitionBuilder::auto(['username', 'created']));
// Connect to database
$schema = new DatabaseSchema($loader->connect(['driver' => 'PdoMysql', 'server' => 'localhost', 'username' => 'jivoo', 'database' => 'jivoo'], $definition));
echo '<pre>';
// Get data for root user:
print_r($schema->User->where('username = %s', 'root')->first()->getData());
// List names of users created after 2015-01-01
$users = $schema->User->where('created > %d', '2015-01-01')->orderBy('created');
foreach ($users as $user) {
    echo $user->username . PHP_EOL;
}
echo '</pre>';
Beispiel #3
0
 /**
  * Read definitions from a namespace.
  *
  * @param string $namespace
  *            Namespace of schema classes.
  * @param string $dir
  *            Location of schema classes.
  * @return DatabaseDefinitionBuilder Database schema.
  */
 public function readDefinition($namespace, $dir)
 {
     $definition = new DatabaseDefinitionBuilder();
     Assume::that(is_dir($dir));
     $files = scandir($dir);
     if ($files !== false) {
         foreach ($files as $file) {
             $split = explode('.', $file);
             if (isset($split[1]) and $split[1] == 'php') {
                 $class = rtrim($namespace, '\\') . '\\' . $split[0];
                 $definition->addDefinition($split[0], $class::getDefinition());
             }
         }
     }
     return $definition;
 }