/** * {@inheritdoc} */ public function register(DiInterface $di) { $di->set(self::SERVICE_NAME, function () use($di) { $arrayConfig = (array) $di->get('config')->{self::SERVICE_NAME}; $db = new \Phalcon\Db\Adapter\Pdo\Sqlite($arrayConfig); $db->setEventsManager($di->getShared('eventsManager')); return $db; }, true); }
public function testValidatorsSqlite() { require 'unit-tests/config.db.php'; if (empty($configSqlite)) { $this->markTestSkipped("Skipped"); return; } $di = $this->_getDI(); $di->set('db', function () { require 'unit-tests/config.db.php'; $conn = new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite); $conn->getInternalHandler()->sqliteCreateFunction('now', 'sqlite_now', 0); return $conn; }, true); $this->_testValidatorsNormal($di); $this->_testValidatorsRenamed($di); }
public function estDbSqlite() { require 'unit-tests/config.db.php'; if (empty($configSqlite)) { $this->markTestSkipped("Skipped"); return; } $connection = new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite); //List tables $expectedTables = array(0 => 'customers', 1 => 'm2m_parts', 2 => 'm2m_robots', 3 => 'm2m_robots_parts', 4 => 'parts', 5 => 'personas', 6 => 'personnes', 7 => 'prueba', 8 => 'robots', 9 => 'robots_parts', 10 => 'sqlite_sequence', 11 => 'subscriptores', 12 => 'tipo_documento'); $tables = $connection->listTables(); $this->assertEquals($tables, $expectedTables); $tables = $connection->listTables('public'); $this->assertEquals($tables, $expectedTables); //Table exist $this->assertEquals($connection->tableExists('personas'), 1); $this->assertEquals($connection->tableExists('noexist'), 0); $this->assertEquals($connection->tableExists('personas', 'public'), 1); $this->assertEquals($connection->tableExists('personas', 'test'), 1); //Columns $expectedDescribe = $this->getExpectedColumnsSqlite(); $describe = $connection->describeColumns('personas'); $this->assertEquals($describe, $expectedDescribe); $describe = $connection->describeColumns('personas', 'main'); $this->assertEquals($describe, $expectedDescribe); //Indexes ps. sqlite's integer primary key autoincrement is not listed in indexes $expectedIndexes = array('robots_parts_parts_id' => Phalcon\Db\Index::__set_state(array('_indexName' => 'robots_parts_parts_id', '_columns' => array('parts_id'))), 'robots_parts_robots_id' => Phalcon\Db\Index::__set_state(array('_indexName' => 'robots_parts_robots_id', '_columns' => array('robots_id')))); $describeIndexes = $connection->describeIndexes('robots_parts'); $this->assertEquals($describeIndexes, $expectedIndexes); $describeIndexes = $connection->describeIndexes('robots_parts', 'main'); $this->assertEquals($describeIndexes, $expectedIndexes); //References $expectedReferences = array('foreign_key_0' => Phalcon\Db\Reference::__set_state(array('_referenceName' => 'foreign_key_0', '_referencedTable' => 'parts', '_columns' => array('parts_id'), '_referencedColumns' => array('id'), '_referencedSchema' => null)), 'foreign_key_1' => Phalcon\Db\Reference::__set_state(array('_referenceName' => 'foreign_key_1', '_referencedTable' => 'robots', '_columns' => array('robots_id'), '_referencedColumns' => array('id'), '_referencedSchema' => null))); $describeReferences = $connection->describeReferences('robots_parts'); $this->assertEquals($describeReferences, $expectedReferences); }
/** * Set Dependency Injector with configuration variables * * @throws Exception * @param string $file full path to configuration file */ public function setConfig($file) { if (!file_exists($file)) { throw new \Exception('Unable to load configuration file'); } $di = new \Phalcon\DI\FactoryDefault\CLI(); $di->set('config', new \Phalcon\Config(require $file)); $di->set('db', function () use($di) { $type = strtolower($di->get('config')->database->adapter); $creds = array('host' => $di->get('config')->database->host, 'username' => $di->get('config')->database->username, 'password' => $di->get('config')->database->password, 'dbname' => $di->get('config')->database->name); if ($type == 'mysql') { $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($creds); } else { if ($type == 'postgres') { $connection = new \Phalcon\Db\Adapter\Pdo\Postgesql($creds); } else { if ($type == 'sqlite') { $connection = new \Phalcon\Db\Adapter\Pdo\Sqlite($creds); } else { throw new Exception('Bad Database Adapter'); } } } $connection->setEventsManager(new \Events\Database\Profile()); return $connection; }); $this->setDI($di); }
<?php require __DIR__ . '/../../vendor/autoload.php'; $db = new \Phalcon\Db\Adapter\Pdo\Sqlite(['dbname' => __DIR__ . '/../db.sqlite']); // Generating 1000 record in user table $db->query("DELETE FROM user;"); $balances = [100, 200, 500]; for ($i = 0; $i < 100; $i++) { $faker = Faker\Factory::create(); $name = $faker->userName; $email = $faker->email; $balance = $balances[array_rand($balances)]; $db->query("INSERT INTO user VALUES({$i}, '{$name}', '{$email}', '{$balance}')"); echo 'Insert record [' . $i . '/100]' . PHP_EOL; } echo 'All done!' . PHP_EOL;
$fileName = date("d_m_y"); $logger = new FileLogger("/tmp/{$fileName}.log"); // $registry = new \Phalcon\Registry(); $registry = $di->get('registry'); $registry->dbCount = 0; // Listen all the database events $eventsManager->attach('db', function ($event, $connection) use($logger, $registry) { if ($event->getType() == 'beforeQuery') { $count = $registry->dbCount; $count++; $registry->dbCount = $count; // $logger->log($connection->getSQLStatement(), Logger::INFO); } }); // $connection = new Connection($config['database']); $connection = new Phalcon\Db\Adapter\Pdo\Sqlite($config); // Assign the eventsManager to the db adapter instance $connection->setEventsManager($eventsManager); return $connection; }); /** * If our request contains a body, it has to be valid JSON. * This parses the body into a standard Object and makes that available from the DI. * If this service is called from a function, and the request body is not valid JSON or is empty, * the program will throw an Exception. */ $di->setShared('requestBody', function () { $in = file_get_contents('php://input'); $in = json_decode($in, FALSE); // JSON body could not be parsed, throw exception if ($in === null) {