/** * Add a comment on GitHub. * * @param TrackerProject $project The project. * @param integer $issueNumber The issue number. * @param string $comment The comment to add. * @param string $userName The username. * @param DatabaseDriver $database The database driver object. * * @return Comment The GitHub comment object * * @throws \DomainException * * @since 1.0 */ public function addComment(TrackerProject $project, $issueNumber, $comment, $userName, DatabaseDriver $database) { $data = new Comment(); if ($project->gh_user && $project->gh_project) { $gitHubResponse = $this->gitHub->issues->comments->create($project->gh_user, $project->gh_project, $issueNumber, $comment); if (!isset($gitHubResponse->id)) { throw new \DomainException('Invalid response from GitHub'); } $data->created_at = $gitHubResponse->created_at; $data->opened_by = $gitHubResponse->user->login; $data->comment_id = $gitHubResponse->id; $data->text_raw = $gitHubResponse->body; $data->text = $this->gitHub->markdown->render($comment, 'gfm', $project->gh_user . '/' . $project->gh_project); } else { $date = new Date(); $data->created_at = $date->format($database->getDateFormat()); $data->opened_by = $userName; $data->comment_id = '???'; $data->text_raw = $comment; $data->text = $this->gitHub->markdown->render($comment, 'markdown'); } (new ActivityModel($database))->addActivityEvent('comment', $data->created_at, $data->opened_by, $project->project_id, $issueNumber, $data->comment_id, $data->text, $data->text_raw); $data->activities_id = $database->insertid(); $date = new Date($data->created_at); $data->created_at = $date->format('j M Y'); return $data; }
/** * Get table columns. * * @param string $table Table name. * * @return array Table columns with type. */ public function getColumns($table) { if (empty(self::$columnCache[$table])) { self::$columnCache[$table] = $this->db->getTableColumns($table); } return self::$columnCache[$table]; }
/** * Method to get the list of database options. * * This method produces a drop down list of available databases supported * by JDatabaseDriver classes that are also supported by the application. * * @return array The field option objects. * * @since 1.0 * @see Joomla\Database\DatabaseDriver */ protected function getOptions() { // This gets the connectors available in the platform and supported by the server. $available = DatabaseDriver::getConnectors(); /** * This gets the list of database types supported by the application. * This should be entered in the form definition as a comma separated list. * If no supported databases are listed, it is assumed all available databases * are supported. */ $supported = $this->element['supported']; if (!empty($supported)) { $supported = explode(',', $supported); foreach ($supported as $support) { if (in_array($support, $available)) { $options[$support] = Text::_(ucfirst($support)); } } } else { foreach ($available as $support) { $options[$support] = Text::_(ucfirst($support)); } } // This will come into play if an application is installed that requires // a database that is not available on the server. if (empty($options)) { $options[''] = Text::_('JNONE'); } return $options; }
public function register(Container $container) { $container->alias("db", "Joomla\\Database\\DatabaseDriver")->share("Joomla\\Database\\DatabaseDriver", function () use($container) { $config = $container->get("config"); return DatabaseDriver::getInstance((array) $config["database"]); }, true); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.0 */ public function register(Container $container) { $container->alias('db', 'Joomla\\Database\\DatabaseDriver')->share('Joomla\\Database\\DatabaseDriver', function (Container $container) { $config = $container->get('config'); return DatabaseDriver::getInstance((array) $config['database']); }, true); }
/** * Process the main SQL file. * * @return $this * * @since 1.0 * @throws \RuntimeException * @throws \UnexpectedValueException */ private function processSql() { // Install. $dbType = $this->getApplication()->get('database.driver'); if ('mysqli' == $dbType) { $dbType = 'mysql'; } $fName = JPATH_ROOT . '/etc/' . $dbType . '.sql'; if (false == file_exists($fName)) { throw new \UnexpectedValueException(sprintf(g11n3t('Install SQL file for %s not found.'), $dbType)); } $sql = file_get_contents($fName); if (false == $sql) { throw new \UnexpectedValueException(g11n3t('SQL file corrupted.')); } $this->out(sprintf(g11n3t('Creating tables from file %s'), realpath($fName)), false); foreach ($this->db->splitSql($sql) as $query) { $q = trim($this->db->replacePrefix($query)); if ('' == trim($q)) { continue; } $this->db->setQuery($q)->execute(); $this->out('.', false); } $this->outOk(); return $this; }
/** * Parses the unit test results and records them into the database * * @param string $package Package name * * @return void * * @since 1.0 * @throws \UnexpectedValueException */ private function recordResults($package) { // Initialize variables. $report = array(); // Make sure the files exist. if (!file_exists(JPATH_ROOT . '/coverage/logs/clover.' . $package . '.xml')) { throw new \UnexpectedValueException('The clover test report for the ' . $this->helper->getPackageDisplayName($package) . ' package is missing.'); } if (!file_exists(JPATH_ROOT . '/coverage/logs/junit.' . $package . '.xml')) { throw new \UnexpectedValueException('The junit test report for the ' . $this->helper->getPackageDisplayName($package) . ' package is missing.'); } // Display update to console $this->app->out('Parsing test results for the ' . $this->helper->getPackageDisplayName($package) . ' package and recording to the database.'); // Load the Clover XML file. $xml = simplexml_load_file(JPATH_ROOT . '/coverage/logs/clover.' . $package . '.xml'); // Get the project metrics element. $metrics = $xml->project[0]->metrics[0]; // Add the data to the report $report['total_lines'] = (int) $metrics['statements']; $report['lines_covered'] = (int) $metrics['coveredstatements']; // Load the JUnit XML file $xml = simplexml_load_file(JPATH_ROOT . '/coverage/logs/junit.' . $package . '.xml'); // Get the project testsuite element. $metrics = $xml->testsuite[0]; // Add the data to the report $report['assertions'] = (int) $metrics['assertions']; $report['tests'] = (int) $metrics['tests']; $report['failures'] = (int) $metrics['failures']; $report['errors'] = (int) $metrics['errors']; // Insert the report data into the database $this->db->setQuery($this->db->getQuery(true)->insert($this->db->quoteName('#__test_results'))->columns(array($this->db->quoteName('package_id'), $this->db->quoteName('tests'), $this->db->quoteName('assertions'), $this->db->quoteName('errors'), $this->db->quoteName('failures'), $this->db->quoteName('total_lines'), $this->db->quoteName('lines_covered')))->values((int) $this->packageId . ', ' . $this->db->quote($report['tests']) . ', ' . $this->db->quote($report['assertions']) . ', ' . $this->db->quote($report['errors']) . ', ' . $this->db->quote($report['failures']) . ', ' . $this->db->quote($report['total_lines']) . ', ' . $this->db->quote($report['lines_covered'])))->execute(); }
/** * Process labels for adding into the issues table * * @param integer $issueId Issue ID to process * * @return array * * @since 1.0 */ protected function processLabels($issueId) { try { $githubLabels = $this->github->issues->get($this->project->gh_user, $this->project->gh_project, $issueId)->labels; } catch (\DomainException $exception) { $this->logger->error(sprintf('Error parsing the labels for GitHub issue %s/%s #%d - %s', $this->project->gh_user, $this->project->gh_project, $issueId, $exception->getMessage())); return ''; } $appLabelIds = array(); // Make sure the label is present in the database by pulling the ID, add it if it isn't $query = $this->db->getQuery(true); foreach ($githubLabels as $label) { $query->clear()->select($this->db->quoteName('label_id'))->from($this->db->quoteName('#__tracker_labels'))->where($this->db->quoteName('project_id') . ' = ' . (int) $this->project->project_id)->where($this->db->quoteName('name') . ' = ' . $this->db->quote($label->name)); $this->db->setQuery($query); $id = $this->db->loadResult(); // If null, add the label if ($id === null) { $table = new LabelsTable($this->db); $data = array(); $data['project_id'] = $this->project->project_id; $data['name'] = $label->name; $data['color'] = $label->color; try { $table->save($data); $id = $table->label_id; } catch (\RuntimeException $exception) { $this->logger->error(sprintf('Error adding label %s for project %s/%s to the database: %s', $label->name, $this->project->gh_user, $this->project->gh_project, $exception->getMessage())); } } // Add the ID to the array $appLabelIds[] = $id; } return $appLabelIds; }
/** * gc * * @param string $past * * @return bool */ public function gc($past) { $query = $this->db->getQuery(true); $query->delete($this->db->quoteName($this->options['table']))->where($this->db->quoteName($this->options['time_col']) . ' < ' . $this->db->quote((int) $past)); // Remove expired sessions from the database. $this->db->setQuery($query); return (bool) $this->db->execute(); }
/** * Retrieve the hashed password for the specified user. * * @param string $username Username to lookup. * * @return string|boolean Hashed password on success or boolean false on failure. * * @since 1.1.0 */ protected function getHashedPassword($username) { $password = $this->db->setQuery($this->db->getQuery(true)->select($this->dbOptions['password_column'])->from($this->dbOptions['database_table'])->where($this->dbOptions['username_column'] . ' = ' . $this->db->quote($username)))->loadResult(); if (!$password) { return false; } return $password; }
/** * Tests the \Joomla\Database\DatabaseQuery::__clone method properly clones an object. * * @return void * * @since 1.0 */ public function test__clone_object() { $baseElement = $this->dbo->getQuery(true); $baseElement->testObject = new \stdClass(); $cloneElement = clone $baseElement; $this->assertThat(TestHelper::getValue($baseElement, 'db'), $this->identicalTo(TestHelper::getValue($cloneElement, 'db')), 'The cloned $db variable should be identical after cloning.'); $this->assertFalse($baseElement === $cloneElement); $this->assertFalse($baseElement->testObject === $cloneElement->testObject); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.0 */ public function register(Container $container) { $container->alias('db', DatabaseDriver::class)->share(DatabaseDriver::class, function (Container $container) { $config = $container->get('config'); $db = DatabaseDriver::getInstance((array) $config->get('database')); $db->setDebug($config->get('database.debug')); $db->setLogger($container->get('monolog.logger.database')); return $db; }, true); }
/** * Database object constructor * * @param array $options List of options used to configure the connection * * @since 1.0 */ public function __construct($options) { $options['host'] = isset($options['host']) ? $options['host'] : 'localhost'; $options['user'] = isset($options['user']) ? $options['user'] : ''; $options['password'] = isset($options['password']) ? $options['password'] : ''; $options['database'] = isset($options['database']) ? $options['database'] : ''; $options['port'] = isset($options['port']) ? $options['port'] : null; // Finalize initialization parent::__construct($options); }
/** * This method is called before the first test of this test class is run. * * An example DSN would be: dbname=//localhost:1521/joomla_ut;charset=AL32UTF8;user=utuser;pass=ut1234 * * @return void * * @since 1.0 */ public static function setUpBeforeClass() { // First let's look to see if we have a DSN defined or in the environment variables. if (defined('JTEST_DATABASE_ORACLE_DSN') || getenv('JTEST_DATABASE_ORACLE_DSN')) { $dsn = defined('JTEST_DATABASE_ORACLE_DSN') ? JTEST_DATABASE_ORACLE_DSN : getenv('JTEST_DATABASE_ORACLE_DSN'); } else { return; } // First let's trim the oci: part off the front of the DSN if it exists. if (strpos($dsn, 'oci:') === 0) { $dsn = substr($dsn, 4); } // Split the DSN into its parts over semicolons. $parts = explode(';', $dsn); // Parse each part and populate the options array. foreach ($parts as $part) { list($k, $v) = explode('=', $part, 2); switch ($k) { case 'charset': self::$options['charset'] = $v; break; case 'dbname': $components = parse_url($v); self::$options['host'] = $components['host']; self::$options['port'] = $components['port']; self::$options['database'] = ltrim($components['path'], '/'); break; case 'user': self::$options['user'] = $v; break; case 'pass': self::$options['password'] = $v; break; case 'dbschema': self::$options['schema'] = $v; break; case 'prefix': self::$options['prefix'] = $v; break; } } // Ensure some defaults. self::$options['charset'] = isset(self::$options['charset']) ? self::$options['charset'] : 'AL32UTF8'; self::$options['port'] = isset(self::$options['port']) ? self::$options['port'] : 1521; try { // Attempt to instantiate the driver. self::$driver = DatabaseDriver::getInstance(self::$options); } catch (\RuntimeException $e) { self::$driver = null; } // If for some reason an exception object was returned set our database object to null. if (self::$driver instanceof \Exception) { self::$driver = null; } }
/** * Constructor. * * @param array $options List of options used to configure the connection * * @since 1.0 */ public function __construct($options) { // Get some basic values from the options. $options['host'] = isset($options['host']) ? $options['host'] : 'localhost'; $options['user'] = isset($options['user']) ? $options['user'] : ''; $options['password'] = isset($options['password']) ? $options['password'] : ''; $options['database'] = isset($options['database']) ? $options['database'] : ''; $options['select'] = isset($options['select']) ? (bool) $options['select'] : true; // Finalize initialisation parent::__construct($options); }
/** * {@inheritdoc} */ public function register(Container $container) { $container->set('Joomla\\Database\\DatabaseDriver', function () use($container) { $config = $container->get('config'); $options = array('driver' => $config->get('database.driver'), 'host' => $config->get('database.host'), 'user' => $config->get('database.user'), 'password' => $config->get('database.password'), 'database' => $config->get('database.name'), 'prefix' => $config->get('database.prefix')); $db = DatabaseDriver::getInstance($options); $db->setDebug($config->get('debug.database', false)); return $db; }, true, true); // Alias the database $container->alias('db', 'Joomla\\Database\\DatabaseDriver'); }
/** * Get the columns from database table. * * @return mixed An array of the field names, or false if an error occurs. * * @since 1.0 * @throws \UnexpectedValueException */ public function getFields() { static $cache = null; if ($cache === null) { // Lookup the fields for this table only once. $fields = $this->db->getTableColumns($this->tableName, false); if (empty($fields)) { throw new \UnexpectedValueException(sprintf('No columns found for %s table', $this->tableName)); } $cache = $fields; } return $cache; }
/** * Garbage collect stale sessions from the SessionHandler backend. * * @param integer $lifetime The maximum age of a session. * * @return boolean True on success, false otherwise. * * @since 1.0 */ public function gc($lifetime = 1440) { // Determine the timestamp threshold with which to purge old sessions. $past = time() - $lifetime; try { $query = $this->db->getQuery(true); $query->delete($this->db->quoteName('#__session'))->where($this->db->quoteName('time') . ' < ' . $this->db->quote((int) $past)); // Remove expired sessions from the database. $this->db->setQuery($query); return (bool) $this->db->execute(); } catch (\Exception $e) { return false; } }
/** * Do delete action, this method should be override by sub class. * * @param mixed $conditions Where conditions, you can use array or Compare object. * * @throws \Exception * @return boolean Will be always true. */ protected function doDelete(array $conditions) { $query = $this->db->getQuery(true); // Conditions. QueryHelper::buildWheres($query, $conditions); $query->delete($this->table); $this->db->transactionStart(true); try { $result = (bool) $this->db->setQuery($query)->execute(); } catch (\Exception $e) { $this->db->transactionRollback(true); throw $e; } $this->db->transactionCommit(true); return $result; }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. * * @since 1.0 */ public function register(Container $container) { $container->set('Joomla\\Database\\DatabaseDriver', function () use($container) { $app = $container->get('app'); $options = array('driver' => $app->get('database.driver'), 'host' => $app->get('database.host'), 'user' => $app->get('database.user'), 'password' => $app->get('database.password'), 'database' => $app->get('database.name'), 'prefix' => $app->get('database.prefix')); $db = DatabaseDriver::getInstance($options); $db->setDebug($app->get('debug.database', false)); $logger = new Logger('JTracker-Database'); $logger->pushHandler(new StreamHandler($app->get('debug.log-path', JPATH_ROOT) . '/database.log', Logger::ERROR)); $logger->pushProcessor(new WebProcessor()); $db->setLogger($logger); return $db; }, true, true); // Alias the database $container->alias('db', 'Joomla\\Database\\DatabaseDriver'); }
/** * This method is called before the first test of this test class is run. * * An example DSN would be: host=localhost;port=5432;dbname=joomla_ut;user=utuser;pass=ut1234 * * @return void * * @since 1.0 */ public static function setUpBeforeClass() { // First let's look to see if we have a DSN defined or in the environment variables. if (defined('JTEST_DATABASE_PGSQL_DSN') || getenv('JTEST_DATABASE_PGSQL_DSN')) { $dsn = defined('JTEST_DATABASE_PGSQL_DSN') ? JTEST_DATABASE_PGSQL_DSN : getenv('JTEST_DATABASE_PGSQL_DSN'); } else { return; } // First let's trim the pgsql: part off the front of the DSN if it exists. if (strpos($dsn, 'pgsql:') === 0) { $dsn = substr($dsn, 6); } // Split the DSN into its parts over semicolons. $parts = explode(';', $dsn); // Parse each part and populate the options array. foreach ($parts as $part) { list($k, $v) = explode('=', $part, 2); switch ($k) { case 'host': self::$options['host'] = $v; break; case 'port': self::$options['port'] = $v; break; case 'dbname': self::$options['database'] = $v; break; case 'user': self::$options['user'] = $v; break; case 'pass': self::$options['password'] = $v; break; } } try { // Attempt to instantiate the driver. self::$driver = DatabaseDriver::getInstance(self::$options); } catch (\RuntimeException $e) { self::$driver = null; } // If for some reason an exception object was returned set our database object to null. if (self::$driver instanceof \Exception) { self::$driver = null; } }
/** * Merges the incoming structure definition with the existing structure. * * @return void * * @note Currently only supports XML format. * @since 1.0 * @throws \RuntimeException on error. */ public function mergeStructure() { $tables = $this->db->getTableList(); if ($this->from instanceof \SimpleXMLElement) { $xml = $this->from; } else { $xml = new \SimpleXMLElement($this->from); } // Get all the table definitions. $xmlTables = $xml->xpath('database/table_structure'); foreach ($xmlTables as $table) { // Convert the magic prefix into the real table name. $tableName = $this->getRealTableName((string) $table['name']); if (in_array($tableName, $tables)) { // The table already exists. Now check if there is any difference. if ($queries = $this->getAlterTableql($xml->database->table_structure)) { // Run the queries to upgrade the data structure. foreach ($queries as $query) { $this->db->setQuery((string) $query); try { $this->db->execute(); } catch (\RuntimeException $e) { $this->db->log(LogLevel::DEBUG, 'Fail: ' . $this->db->getQuery()); throw $e; } $this->db->log(LogLevel::DEBUG, 'Pass: '******'Fail: ' . $this->db->getQuery()); throw $e; } $this->db->log(LogLevel::DEBUG, 'Pass: ' . $this->db->getQuery()); } } }
/** * This method is called before the first test of this test class is run. * * @return void * * @since 1.0 */ public static function setUpBeforeClass() { // We always want the default database test case to use an SQLite memory database. $options = array('driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'jos_'); try { // Attempt to instantiate the driver. self::$driver = DatabaseDriver::getInstance($options); // Create a new PDO instance for an SQLite memory database and load the test schema into it. $pdo = new \PDO('sqlite::memory:'); $pdo->exec(file_get_contents(__DIR__ . '/Schema/ddl.sql')); // Set the PDO instance to the driver using reflection whizbangery. TestHelper::setValue(self::$driver, 'connection', $pdo); } catch (\RuntimeException $e) { self::$driver = null; } // If for some reason an exception object was returned set our database object to null. if (self::$driver instanceof \Exception) { self::$driver = null; } }
/** * getFilterFields * * @param int $prefixFirst * * @return array */ public function getSelectFields($prefixFirst = self::COLS_WITH_FIRST) { $fields = array(); $i = 0; foreach ($this->tables as $alias => $table) { $columns = DatabaseFactory::getCommand()->getColumns($table['name']); foreach ($columns as $column => $var) { if ($i === 0) { if ($prefixFirst & self::COLS_WITH_FIRST) { $fields[] = $this->db->quoteName("{$alias}.{$column}", $column); } if ($prefixFirst & self::COLS_PREFIX_WITH_FIRST) { $fields[] = $this->db->quoteName("{$alias}.{$column}", "{$alias}_{$column}"); } } else { $fields[] = $this->db->quoteName("{$alias}.{$column}", "{$alias}_{$column}"); } } $i++; } return $fields; }
/** * Process the main SQL file. * * @return $this * * @since 1.0 * @throws \RuntimeException * @throws \UnexpectedValueException */ private function processSql() { $fName = JPATH_ROOT . '/etc/schema.sql'; if (!file_exists($fName)) { throw new \UnexpectedValueException('Install SQL file not found.'); } $sql = file_get_contents($fName); if (!$sql) { throw new \UnexpectedValueException('Unable to read SQL file.'); } $this->app->out(sprintf('Creating tables from file %s', realpath($fName)), false); foreach ($this->db->splitSql($sql) as $query) { $q = trim($this->db->replacePrefix($query)); if (trim($q) == '') { continue; } $this->db->setQuery($q)->execute(); $this->app->out('.', false); } $this->app->out("\nFinished!"); return $this; }
/** * Get the database prefix. * * @return string * * @since 1.0 */ public function getPrefix() { return $this->database->getPrefix(); }
/** * Sets the SQL statement string for later execution. * * @param DatabaseQuery|string $query The SQL statement to set either as a DatabaseQuery object or a string. * @param integer $offset The affected row offset to set. * @param integer $limit The maximum affected rows to set. * * @return PostgresqlDriver This object to support method chaining. * * @since __DEPLOY_VERSION__ */ public function setQuery($query, $offset = null, $limit = null) { $this->connect(); $this->freeResult(); if (is_string($query)) { // Allows taking advantage of bound variables in a direct query: $query = $this->getQuery(true)->setQuery($query); } if ($query instanceof LimitableInterface && !is_null($offset) && !is_null($limit)) { $query->setLimit($limit, $offset); } $sql = $this->replacePrefix((string) $query); $this->prepared = pg_prepare($this->connection, $this->queryName . $this->getCount(), $sql); // Store reference to the DatabaseQuery instance return parent::setQuery($query, $offset, $limit); }
/** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. * * @return void */ protected function setUp() { $this->instance = \Joomla\Database\DatabaseDriver::getInstance(array('driver' => 'nosql', 'database' => 'europa', 'prefix' => '&')); }
/** * Return Database Object * * @param $driver * @param $host * @param $user * @param $password * @param $database * @param string $prefix * @param bool $select * @return DatabaseDriver */ public function getDbo($driver, $host, $user, $password, $database, $prefix = '', $select = true) { static $db; if (!$db) { // Build the connection options array. $options = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix, 'select' => $select); // Get a database object. $db = DatabaseDriver::getInstance($options); } return $db; }
/** * loadToData * * @param mixed $query * @param string $dataClass * * @return mixed */ protected function loadToData($query, $dataClass = 'Windwalker\\Data\\Data') { $data = $this->db->setQuery($query)->loadObject($dataClass); return $data; }