public function testGeneratePostActivation() { // Touch a new "insert.sql" file and copy a "create.sql" file /** @var \org\bovigo\vfs\vfsStreamFile $file */ $configDir = $this->stream->getChild("Config"); $file = vfsStream::newFile("create.sql")->at($configDir); $file->setContent(file_get_contents(__DIR__ . "/../" . static::TEST_MODULE_PATH . "Config" . DS . "thelia.sql")); vfsStream::newFile("insert.sql")->at($configDir); // Then run the generator $generator = new ModulePhpGenerator($this->getSmarty()); $generator->doGenerate($this->event); // Read the class include $this->getStreamPath("TheliaStudioTestModule.php"); $reflection = new \ReflectionClass("TheliaStudioTestModule\\TheliaStudioTestModule"); $this->assertTrue($reflection->hasConstant("MESSAGE_DOMAIN")); $this->assertEquals("theliastudiotestmodule", $reflection->getConstant("MESSAGE_DOMAIN")); $this->assertTrue($reflection->hasMethod("postActivation")); // get a method closure $method = $reflection->getMethod("postActivation"); $closure = $method->getClosure($reflection->newInstance()); // Test that the table doesn't exist /** @var \Propel\Runtime\DataFetcher\PDODataFetcher $stmt */ $stmt = $this->con->query("SHOW TABLES LIKE 'example_table'"); $this->assertEquals(0, $stmt->count()); // Execute the method $closure($this->con); // Now it exists /** @var \Propel\Runtime\DataFetcher\PDODataFetcher $stmt */ $stmt = $this->con->query("SHOW TABLES LIKE 'example_table'"); $this->assertEquals(1, $stmt->count()); }
public function testCreateSqlRuleGeneration() { $generator = new RuleGenerator(); $generator->doGenerate($this->event); // Test file generation $this->assertFileExists($createFile = $this->getStreamPath("Config/create.sql")); // Test that the table doesn't exist /** @var \Propel\Runtime\DataFetcher\PDODataFetcher $stmt */ $stmt = $this->con->query("SHOW TABLES LIKE 'example_table'"); $this->assertEquals(0, $stmt->count()); // Test that the file has the correct behavior $db = new Database($this->con); $db->insertSql(null, [$createFile]); // Now it exists /** @var \Propel\Runtime\DataFetcher\PDODataFetcher $stmt */ $stmt = $this->con->query("SHOW TABLES LIKE 'example_table'"); $this->assertEquals(1, $stmt->count()); // Insert a new value in the table $this->con->exec("INSERT INTO `example_table`(`visible`, `position`) VALUES(1, 42)"); // Check that the value exists $stmt = $this->con->query("SELECT * FROM `example_table`"); $this->assertEquals(1, $stmt->count()); // Then test that it doesn't crash if we apply the script again $db = new Database($this->con); $db->insertSql(null, [$createFile]); // Check that the previous entry still exists ( proves that there is no DROP TABLE ) $stmt = $this->con->query("SELECT * FROM `example_table`"); $fetched = $stmt->fetch(\PDO::FETCH_ASSOC); $this->assertTrue(is_array($fetched)); $this->assertArrayHasKey("position", $fetched); $this->assertArrayHasKey("visible", $fetched); $this->assertEquals("1", $fetched["visible"]); $this->assertEquals("42", $fetched["position"]); }
/** * Gets ID for specified sequence name. * * @param ConnectionInterface $con * @param string $name * * @return integer */ public function getId(ConnectionInterface $con, $name = null) { if (null === $name) { throw new InvalidArgumentException("Unable to fetch next sequence ID without sequence name."); } $dataFetcher = $con->query(sprintf('SELECT nextval(%s)', $con->quote($name))); return $dataFetcher->fetchColumn(); }
/** * Gets ID for specified sequence name. * * @param ConnectionInterface $con * @param string $name * * @return integer */ public function getId(ConnectionInterface $con, $name = null) { if ($name === null) { throw new InvalidArgumentException("Unable to fetch next sequence ID without sequence name."); } $stmt = $con->query("SELECT nextval(" . $con->quote($name) . ")"); $row = $stmt->fetch(PDO::FETCH_NUM); return $row[0]; }
public function initConnection(ConnectionInterface $con, array $settings) { $con->query('PRAGMA foreign_keys = ON'); parent::initConnection($con, $settings); //add regex support $con->sqliteCreateFunction('regexp', function ($pattern, $value) { mb_regex_encoding('UTF-8'); return false !== mb_ereg($pattern, $value) ? 1 : 0; }); }
/** * @param ConnectionInterface $con * @param string $name * * @throws PropelException * @return integer */ public function getId(ConnectionInterface $con, $name = null) { if ($name === null) { throw new PropelException("Unable to fetch next sequence ID without sequence name."); } $stmt = $con->query("SELECT " . $name . ".nextval FROM dual"); $row = $stmt->fetch(PDO::FETCH_NUM); return $row[0]; }
/** * Insert the row in the database. * * @param ConnectionInterface $con * * @throws PropelException * @see doSave() */ protected function doInsert(ConnectionInterface $con) { $modifiedColumns = array(); $index = 0; $this->modifiedColumns[UserTableMap::COL_ID] = true; if (null !== $this->id) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . UserTableMap::COL_ID . ')'); } if (null === $this->id) { try { $dataFetcher = $con->query("SELECT nextval('system_user_id_seq')"); $this->id = $dataFetcher->fetchColumn(); } catch (Exception $e) { throw new PropelException('Unable to get sequence id.', 0, $e); } } // check the columns in natural order for more readable SQL queries if ($this->isColumnModified(UserTableMap::COL_ID)) { $modifiedColumns[':p' . $index++] = 'id'; } if ($this->isColumnModified(UserTableMap::COL_NAME)) { $modifiedColumns[':p' . $index++] = 'name'; } if ($this->isColumnModified(UserTableMap::COL_EMAIL)) { $modifiedColumns[':p' . $index++] = 'email'; } if ($this->isColumnModified(UserTableMap::COL_PASSWORD)) { $modifiedColumns[':p' . $index++] = 'password'; } if ($this->isColumnModified(UserTableMap::COL_SCORE)) { $modifiedColumns[':p' . $index++] = 'score'; } $sql = sprintf('INSERT INTO system_user (%s) VALUES (%s)', implode(', ', $modifiedColumns), implode(', ', array_keys($modifiedColumns))); try { $stmt = $con->prepare($sql); foreach ($modifiedColumns as $identifier => $columnName) { switch ($columnName) { case 'id': $stmt->bindValue($identifier, $this->id, PDO::PARAM_INT); break; case 'name': $stmt->bindValue($identifier, $this->name, PDO::PARAM_STR); break; case 'email': $stmt->bindValue($identifier, $this->email, PDO::PARAM_STR); break; case 'password': $stmt->bindValue($identifier, $this->password, PDO::PARAM_STR); break; case 'score': $stmt->bindValue($identifier, $this->score, PDO::PARAM_INT); break; } } $stmt->execute(); } catch (Exception $e) { Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e); } $this->setNew(false); }
/** * Insert the row in the database. * * @param ConnectionInterface $con * * @throws PropelException * @see doSave() */ protected function doInsert(ConnectionInterface $con) { $modifiedColumns = array(); $index = 0; $this->modifiedColumns[OrderTableMap::COL_ID] = true; if (null !== $this->id) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . OrderTableMap::COL_ID . ')'); } if (null === $this->id) { try { $dataFetcher = $con->query("SELECT nextval('orders_id_seq')"); $this->id = $dataFetcher->fetchColumn(); } catch (Exception $e) { throw new PropelException('Unable to get sequence id.', 0, $e); } } // check the columns in natural order for more readable SQL queries if ($this->isColumnModified(OrderTableMap::COL_ID)) { $modifiedColumns[':p' . $index++] = 'id'; } if ($this->isColumnModified(OrderTableMap::COL_TRUCK_ID)) { $modifiedColumns[':p' . $index++] = 'truck_id'; } if ($this->isColumnModified(OrderTableMap::COL_ITEM_NAME)) { $modifiedColumns[':p' . $index++] = 'item_name'; } if ($this->isColumnModified(OrderTableMap::COL_PRICE)) { $modifiedColumns[':p' . $index++] = 'price'; } if ($this->isColumnModified(OrderTableMap::COL_CHARGE_ID)) { $modifiedColumns[':p' . $index++] = 'charge_id'; } if ($this->isColumnModified(OrderTableMap::COL_CUSTOMER_ID)) { $modifiedColumns[':p' . $index++] = 'customer_id'; } if ($this->isColumnModified(OrderTableMap::COL_CUSTOMER_NAME)) { $modifiedColumns[':p' . $index++] = 'customer_name'; } if ($this->isColumnModified(OrderTableMap::COL_CUSTOMER_EMAIL)) { $modifiedColumns[':p' . $index++] = 'customer_email'; } if ($this->isColumnModified(OrderTableMap::COL_CUSTOMER_PHONE_NUMBER)) { $modifiedColumns[':p' . $index++] = 'customer_phone_number'; } if ($this->isColumnModified(OrderTableMap::COL_OPEN)) { $modifiedColumns[':p' . $index++] = 'open'; } if ($this->isColumnModified(OrderTableMap::COL_CREATED_AT)) { $modifiedColumns[':p' . $index++] = 'created_at'; } if ($this->isColumnModified(OrderTableMap::COL_UPDATED_AT)) { $modifiedColumns[':p' . $index++] = 'updated_at'; } $sql = sprintf('INSERT INTO orders (%s) VALUES (%s)', implode(', ', $modifiedColumns), implode(', ', array_keys($modifiedColumns))); try { $stmt = $con->prepare($sql); foreach ($modifiedColumns as $identifier => $columnName) { switch ($columnName) { case 'id': $stmt->bindValue($identifier, $this->id, PDO::PARAM_INT); break; case 'truck_id': $stmt->bindValue($identifier, $this->truck_id, PDO::PARAM_INT); break; case 'item_name': $stmt->bindValue($identifier, $this->item_name, PDO::PARAM_STR); break; case 'price': $stmt->bindValue($identifier, $this->price, PDO::PARAM_STR); break; case 'charge_id': $stmt->bindValue($identifier, $this->charge_id, PDO::PARAM_STR); break; case 'customer_id': $stmt->bindValue($identifier, $this->customer_id, PDO::PARAM_STR); break; case 'customer_name': $stmt->bindValue($identifier, $this->customer_name, PDO::PARAM_STR); break; case 'customer_email': $stmt->bindValue($identifier, $this->customer_email, PDO::PARAM_STR); break; case 'customer_phone_number': $stmt->bindValue($identifier, $this->customer_phone_number, PDO::PARAM_STR); break; case 'open': $stmt->bindValue($identifier, $this->open, PDO::PARAM_BOOL); break; case 'created_at': $stmt->bindValue($identifier, $this->created_at ? $this->created_at->format("Y-m-d H:i:s") : null, PDO::PARAM_STR); break; case 'updated_at': $stmt->bindValue($identifier, $this->updated_at ? $this->updated_at->format("Y-m-d H:i:s") : null, PDO::PARAM_STR); break; } } $stmt->execute(); } catch (Exception $e) { Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e); } $this->setNew(false); }
/** * Insert the row in the database. * * @param ConnectionInterface $con * * @throws PropelException * @see doSave() */ protected function doInsert(ConnectionInterface $con) { $modifiedColumns = array(); $index = 0; $this->modifiedColumns[PayPalIpnLogTableMap::COL_ID] = true; if (null !== $this->id) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . PayPalIpnLogTableMap::COL_ID . ')'); } if (null === $this->id) { try { $dataFetcher = $con->query("SELECT nextval('paypal_ipn_log_id_seq')"); $this->id = $dataFetcher->fetchColumn(); } catch (Exception $e) { throw new PropelException('Unable to get sequence id.', 0, $e); } } // check the columns in natural order for more readable SQL queries if ($this->isColumnModified(PayPalIpnLogTableMap::COL_ID)) { $modifiedColumns[':p' . $index++] = 'ID'; } if ($this->isColumnModified(PayPalIpnLogTableMap::COL_LOG)) { $modifiedColumns[':p' . $index++] = 'LOG'; } if ($this->isColumnModified(PayPalIpnLogTableMap::COL_CREATED_AT)) { $modifiedColumns[':p' . $index++] = 'CREATED_AT'; } if ($this->isColumnModified(PayPalIpnLogTableMap::COL_UPDATED_AT)) { $modifiedColumns[':p' . $index++] = 'UPDATED_AT'; } $sql = sprintf('INSERT INTO paypal_ipn_log (%s) VALUES (%s)', implode(', ', $modifiedColumns), implode(', ', array_keys($modifiedColumns))); try { $stmt = $con->prepare($sql); foreach ($modifiedColumns as $identifier => $columnName) { switch ($columnName) { case 'ID': $stmt->bindValue($identifier, $this->id, PDO::PARAM_INT); break; case 'LOG': $stmt->bindValue($identifier, $this->log, PDO::PARAM_STR); break; case 'CREATED_AT': $stmt->bindValue($identifier, $this->created_at ? $this->created_at->format("Y-m-d H:i:s") : null, PDO::PARAM_STR); break; case 'UPDATED_AT': $stmt->bindValue($identifier, $this->updated_at ? $this->updated_at->format("Y-m-d H:i:s") : null, PDO::PARAM_STR); break; } } $stmt->execute(); } catch (Exception $e) { Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e); } $this->setNew(false); }
/** * Insert the row in the database. * * @param ConnectionInterface $con * * @throws PropelException * @see doSave() */ protected function doInsert(ConnectionInterface $con) { $modifiedColumns = array(); $index = 0; $this->modifiedColumns[StripeTransactionTableMap::COL_ID] = true; if (null !== $this->id) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . StripeTransactionTableMap::COL_ID . ')'); } if (null === $this->id) { try { $dataFetcher = $con->query("SELECT nextval('stripe_transactions_id_seq')"); $this->id = $dataFetcher->fetchColumn(); } catch (Exception $e) { throw new PropelException('Unable to get sequence id.', 0, $e); } } // check the columns in natural order for more readable SQL queries if ($this->isColumnModified(StripeTransactionTableMap::COL_ID)) { $modifiedColumns[':p' . $index++] = 'ID'; } if ($this->isColumnModified(StripeTransactionTableMap::COL_STRIPE_ID)) { $modifiedColumns[':p' . $index++] = 'STRIPE_ID'; } if ($this->isColumnModified(StripeTransactionTableMap::COL_AMOUNT)) { $modifiedColumns[':p' . $index++] = 'AMOUNT'; } if ($this->isColumnModified(StripeTransactionTableMap::COL_DONATION_AMOUNT)) { $modifiedColumns[':p' . $index++] = 'DONATION_AMOUNT'; } if ($this->isColumnModified(StripeTransactionTableMap::COL_TRANSACTION_FEE)) { $modifiedColumns[':p' . $index++] = 'TRANSACTION_FEE'; } if ($this->isColumnModified(StripeTransactionTableMap::COL_TOTAL_AMOUNT)) { $modifiedColumns[':p' . $index++] = 'TOTAL_AMOUNT'; } if ($this->isColumnModified(StripeTransactionTableMap::COL_STATUS)) { $modifiedColumns[':p' . $index++] = 'STATUS'; } if ($this->isColumnModified(StripeTransactionTableMap::COL_PAYMENT_ID)) { $modifiedColumns[':p' . $index++] = 'PAYMENT_ID'; } if ($this->isColumnModified(StripeTransactionTableMap::COL_CREATED_AT)) { $modifiedColumns[':p' . $index++] = 'CREATED_AT'; } if ($this->isColumnModified(StripeTransactionTableMap::COL_UPDATED_AT)) { $modifiedColumns[':p' . $index++] = 'UPDATED_AT'; } $sql = sprintf('INSERT INTO stripe_transactions (%s) VALUES (%s)', implode(', ', $modifiedColumns), implode(', ', array_keys($modifiedColumns))); try { $stmt = $con->prepare($sql); foreach ($modifiedColumns as $identifier => $columnName) { switch ($columnName) { case 'ID': $stmt->bindValue($identifier, $this->id, PDO::PARAM_INT); break; case 'STRIPE_ID': $stmt->bindValue($identifier, $this->stripe_id, PDO::PARAM_STR); break; case 'AMOUNT': $stmt->bindValue($identifier, $this->amount, PDO::PARAM_STR); break; case 'DONATION_AMOUNT': $stmt->bindValue($identifier, $this->donation_amount, PDO::PARAM_STR); break; case 'TRANSACTION_FEE': $stmt->bindValue($identifier, $this->transaction_fee, PDO::PARAM_STR); break; case 'TOTAL_AMOUNT': $stmt->bindValue($identifier, $this->total_amount, PDO::PARAM_STR); break; case 'STATUS': $stmt->bindValue($identifier, $this->status, PDO::PARAM_STR); break; case 'PAYMENT_ID': $stmt->bindValue($identifier, $this->payment_id, PDO::PARAM_INT); break; case 'CREATED_AT': $stmt->bindValue($identifier, $this->created_at ? $this->created_at->format("Y-m-d H:i:s") : null, PDO::PARAM_STR); break; case 'UPDATED_AT': $stmt->bindValue($identifier, $this->updated_at ? $this->updated_at->format("Y-m-d H:i:s") : null, PDO::PARAM_STR); break; } } $stmt->execute(); } catch (Exception $e) { Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e); } $this->setNew(false); }
public function initConnection(ConnectionInterface $con, array $settings) { $con->query('PRAGMA foreign_keys = ON'); parent::initConnection($con, $settings); }
/** * Insert the row in the database. * * @param ConnectionInterface $con * * @throws PropelException * @see doSave() */ protected function doInsert(ConnectionInterface $con) { $modifiedColumns = array(); $index = 0; $this->modifiedColumns[ParticipantTableMap::COL_ID] = true; if (null !== $this->id) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . ParticipantTableMap::COL_ID . ')'); } if (null === $this->id) { try { $dataFetcher = $con->query("SELECT nextval('participant_id_seq')"); $this->id = $dataFetcher->fetchColumn(); } catch (Exception $e) { throw new PropelException('Unable to get sequence id.', 0, $e); } } // check the columns in natural order for more readable SQL queries if ($this->isColumnModified(ParticipantTableMap::COL_ID)) { $modifiedColumns[':p' . $index++] = 'id'; } if ($this->isColumnModified(ParticipantTableMap::COL_NAME)) { $modifiedColumns[':p' . $index++] = 'name'; } if ($this->isColumnModified(ParticipantTableMap::COL_COUNTRY_CODE)) { $modifiedColumns[':p' . $index++] = 'country_code'; } if ($this->isColumnModified(ParticipantTableMap::COL_IS_WINNER)) { $modifiedColumns[':p' . $index++] = 'is_winner'; } if ($this->isColumnModified(ParticipantTableMap::COL_GAME_ID)) { $modifiedColumns[':p' . $index++] = 'game_id'; } $sql = sprintf('INSERT INTO participant (%s) VALUES (%s)', implode(', ', $modifiedColumns), implode(', ', array_keys($modifiedColumns))); try { $stmt = $con->prepare($sql); foreach ($modifiedColumns as $identifier => $columnName) { switch ($columnName) { case 'id': $stmt->bindValue($identifier, $this->id, PDO::PARAM_INT); break; case 'name': $stmt->bindValue($identifier, $this->name, PDO::PARAM_STR); break; case 'country_code': $stmt->bindValue($identifier, $this->country_code, PDO::PARAM_STR); break; case 'is_winner': $stmt->bindValue($identifier, $this->is_winner, PDO::PARAM_BOOL); break; case 'game_id': $stmt->bindValue($identifier, $this->game_id, PDO::PARAM_INT); break; } } $stmt->execute(); } catch (Exception $e) { Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e); } $this->setNew(false); }
/** * Insert the row in the database. * * @param ConnectionInterface $con * * @throws PropelException * @see doSave() */ protected function doInsert(ConnectionInterface $con) { $modifiedColumns = array(); $index = 0; $this->modifiedColumns[GameTableMap::COL_ID] = true; if (null !== $this->id) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . GameTableMap::COL_ID . ')'); } if (null === $this->id) { try { $dataFetcher = $con->query("SELECT nextval('game_id_seq')"); $this->id = $dataFetcher->fetchColumn(); } catch (Exception $e) { throw new PropelException('Unable to get sequence id.', 0, $e); } } // check the columns in natural order for more readable SQL queries if ($this->isColumnModified(GameTableMap::COL_ID)) { $modifiedColumns[':p' . $index++] = 'id'; } if ($this->isColumnModified(GameTableMap::COL_MODALITY)) { $modifiedColumns[':p' . $index++] = 'modality'; } if ($this->isColumnModified(GameTableMap::COL_RESULT)) { $modifiedColumns[':p' . $index++] = 'result'; } if ($this->isColumnModified(GameTableMap::COL_BET_COST)) { $modifiedColumns[':p' . $index++] = 'bet_cost'; } if ($this->isColumnModified(GameTableMap::COL_START)) { $modifiedColumns[':p' . $index++] = 'start'; } if ($this->isColumnModified(GameTableMap::COL_FINISH)) { $modifiedColumns[':p' . $index++] = 'finish'; } $sql = sprintf('INSERT INTO game (%s) VALUES (%s)', implode(', ', $modifiedColumns), implode(', ', array_keys($modifiedColumns))); try { $stmt = $con->prepare($sql); foreach ($modifiedColumns as $identifier => $columnName) { switch ($columnName) { case 'id': $stmt->bindValue($identifier, $this->id, PDO::PARAM_INT); break; case 'modality': $stmt->bindValue($identifier, $this->modality, PDO::PARAM_STR); break; case 'result': $stmt->bindValue($identifier, $this->result, PDO::PARAM_STR); break; case 'bet_cost': $stmt->bindValue($identifier, $this->bet_cost, PDO::PARAM_INT); break; case 'start': $stmt->bindValue($identifier, $this->start ? $this->start->format("Y-m-d H:i:s") : null, PDO::PARAM_STR); break; case 'finish': $stmt->bindValue($identifier, $this->finish ? $this->finish->format("Y-m-d H:i:s") : null, PDO::PARAM_STR); break; } } $stmt->execute(); } catch (Exception $e) { Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e); } $this->setNew(false); }