|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Basic example program about handling CrateDB OBJECTs with Doctrine DBAL. |
|
5
|
|
|
* https://github.com/crate/crate-dbal |
|
6
|
|
|
*/ |
|
7
|
|
|
require __DIR__ . '/../vendor/autoload.php'; |
|
8
|
|
|
|
|
9
|
|
|
use Crate\DBAL\Driver\PDOCrate\Driver as CrateDBDriver; |
|
10
|
|
|
use Crate\DBAL\Types\MapType; |
|
11
|
|
|
use Doctrine\DBAL\DriverManager; |
|
12
|
|
|
use Doctrine\DBAL\Exception\TableNotFoundException; |
|
13
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
14
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\DBAL\Tools\DsnParser; |
|
17
|
|
|
use Doctrine\DBAL\Types\Type; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
// Register driver and select platform. |
|
21
|
|
|
// Because DBAL can't connect to the database to determine its version dynamically, |
|
22
|
|
|
// it is the obligation of the user to provide the CrateDB version number. |
|
23
|
|
|
// Remark: Please let us know if you find a way how to set up more ergonomically. |
|
24
|
|
|
// https://www.doctrine-project.org/projects/doctrine-dbal/en/3.10/reference/platforms.html |
|
25
|
|
|
$driver = new CrateDBDriver(); |
|
26
|
|
|
$dsnParser = new DsnParser(["crate" => $driver::class]); |
|
27
|
|
|
$driver->createDatabasePlatformForVersion('6.0.0'); |
|
28
|
|
|
|
|
29
|
|
|
// Create connection options from data source URL. |
|
30
|
|
|
$options = $dsnParser->parse('crate://crate:crate@localhost:4200/'); |
|
31
|
|
|
|
|
32
|
|
|
// Connect to database. |
|
33
|
|
|
$connection = DriverManager::getConnection($options); |
|
34
|
|
|
|
|
35
|
|
|
// Define table schema. |
|
36
|
|
|
$table = new Table('example'); |
|
37
|
|
|
$objDefinition = array( |
|
38
|
|
|
'type' => MapType::STRICT, |
|
39
|
|
|
'fields' => array( |
|
40
|
|
|
new Column('id', Type::getType('integer'), array()), |
|
41
|
|
|
new Column('name', Type::getType('string'), array()), |
|
42
|
|
|
), |
|
43
|
|
|
); |
|
44
|
|
|
$table->addColumn( |
|
45
|
|
|
'data', |
|
46
|
|
|
MapType::NAME, |
|
47
|
|
|
array('platformOptions' => $objDefinition), |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
// Provision database table. |
|
51
|
|
|
$schemaManager = $connection->createSchemaManager(); |
|
52
|
|
|
try { |
|
53
|
|
|
$schemaManager->dropTable($table->getName()); |
|
54
|
|
|
} catch (TableNotFoundException) { |
|
55
|
|
|
} |
|
56
|
|
|
$schemaManager->createTable($table); |
|
57
|
|
|
|
|
58
|
|
|
// Insert data. |
|
59
|
|
|
$connection->insert('example', array('data' => array('id' => 42, 'name' => 'foo')), array('data' => 'map')); |
|
60
|
|
|
$connection->insert('example', array('data' => array('id' => 43, 'name' => 'bar')), array('data' => 'map')); |
|
61
|
|
|
$connection->executeStatement('REFRESH TABLE example'); |
|
62
|
|
|
|
|
63
|
|
|
// Query data. |
|
64
|
|
|
$result = $connection->executeQuery('SELECT * FROM example'); |
|
65
|
|
|
print_r($result->fetchAllAssociative()); |
|
66
|
|
|
|
|
67
|
|
|
?> |
|
|
|
|
|
|
68
|
|
|
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.