Ejemplo n.º 1
0
 /**
  * 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;
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }
Ejemplo n.º 3
0
 /**
  * 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();
 }
Ejemplo n.º 4
0
 /**
  * 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();
 }
Ejemplo n.º 5
0
 /**
  * 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;
 }
Ejemplo n.º 6
0
 /**
  * Execute the command
  *
  * @return  void
  *
  * @since   1.0
  */
 public function execute()
 {
     // Display status
     $this->app->out('Parsing the Composer data.');
     // Get the Composer data
     $helper = new Helper();
     $packages = $helper->parseComposer();
     // Insert the records into the database now
     foreach ($packages as $name => $package) {
         // Check to see if the package is already in the database
         $packageID = $this->db->setQuery($this->db->getQuery(true)->select($this->db->quoteName('id'))->from($this->db->quoteName('#__packages'))->where($this->db->quoteName('package') . ' = ' . $this->db->quote($name))->where($this->db->quoteName('version') . ' = ' . $this->db->quote($package['version'])))->loadResult();
         // If not present, insert it
         if (!$packageID) {
             $this->db->setQuery($this->db->getQuery(true)->insert($this->db->quoteName('#__packages'))->columns(array($this->db->quoteName('package'), $this->db->quoteName('version')))->values($this->db->quote($name) . ', ' . $this->db->quote($package['version'])))->execute();
         }
     }
     // Display status
     $this->app->out('Finished parsing Composer data.');
 }
Ejemplo n.º 7
0
 /**
  * Batch update some data.
  *
  * @param string $table      Table name.
  * @param string $data       Data you want to update.
  * @param mixed  $conditions Where conditions, you can use array or Compare object.
  *                           Example:
  *                           - `array('id' => 5)` => id = 5
  *                           - `new GteCompare('id', 20)` => 'id >= 20'
  *                           - `new Compare('id', '%Flower%', 'LIKE')` => 'id LIKE "%Flower%"'
  *
  * @return  boolean True if update success.
  */
 public function updateBatch($table, $data, $conditions = array())
 {
     $query = $this->db->getQuery(true);
     // Build conditions
     $query = QueryHelper::buildWheres($query, $conditions);
     // Build update values.
     $fields = array_keys($this->getColumns($table));
     $hasField = false;
     foreach ((array) $data as $field => $value) {
         if (!in_array($field, $fields)) {
             continue;
         }
         $query->set($query->format('%n = %q', $field, $value));
         $hasField = true;
     }
     if (!$hasField) {
         return false;
     }
     $query->update($table);
     return $this->db->setQuery($query)->execute();
 }
Ejemplo n.º 8
0
 /**
  * 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;
     }
 }
Ejemplo n.º 9
0
 /**
  * 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;
 }
Ejemplo n.º 10
0
 /**
  * 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());
         }
     }
 }
 /**
  * Validate that the primary key has been set.
  *
  * @return  boolean  True if the primary key(s) have been set.
  *
  * @since   1.0
  */
 public function hasPrimaryKey()
 {
     if ($this->autoIncrement) {
         $empty = true;
         foreach ($this->tableKeys as $key) {
             $empty = $empty && !$this->{$key};
         }
     } else {
         $query = $this->db->getQuery(true)->select('COUNT(*)')->from($this->tableName);
         $this->appendPrimaryKeys($query);
         $this->db->setQuery($query);
         $count = $this->db->loadResult();
         if ($count == 1) {
             $empty = false;
         } else {
             $empty = true;
         }
     }
     return !$empty;
 }
Ejemplo n.º 12
0
 /**
  * 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;
 }
Ejemplo n.º 13
0
 /**
  * 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);
 }
Ejemplo n.º 14
0
 /**
  * Tests the Joomla\Database\DatabaseDriver::setQuery method.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function testSetQuery()
 {
     $this->assertThat($this->instance->setQuery('SELECT * FROM #__dbtest'), $this->isInstanceOf('Joomla\\Database\\DatabaseDriver'), 'setQuery method should return an instance of Joomla\\Database\\DatabaseDriver.');
 }
Ejemplo n.º 15
0
 /**
  * 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;
 }
Ejemplo n.º 16
0
 /**
  * Select a user by conditions.
  *
  * @param array $where    an associative array of WHERE contitions
  * @param array $where    an associative array of SELECT contitions
  * @return object object of founded info about user
  */
 public function getEmails()
 {
     if ($this->emails) {
         return $this->emails;
     }
     $query = $this->database->getQuery(true);
     $query->select('email')->from('#__users_email_cf');
     $query->where('member_id=' . (int) $this->get('id'));
     $this->emails = $this->database->setQuery($query)->loadObjectList();
     return $this->emails;
 }