You are here

class SQLFIReader in Feed Import 8

SQL Reader class, used te read data with SQL queries.

Hierarchy

Expanded class hierarchy of SQLFIReader

File

feed_import_base/src/SQLFIReader.php, line 7

Namespace

Drupal\feed_import_base
View source
class SQLFIReader extends FeedImportUniVectorReader {

  /**
   * {@inheritdoc}
   */
  public function init() {

    // Require dsn and query.
    if (empty($this->options['dsn']) || empty($this->options['query'])) {
      return FALSE;
    }

    // Set default options.
    $this->options += array(
      'user' => 'root',
      'pass' => NULL,
      'params' => array(),
    );

    // Parse params if needed.
    if (!is_array($this->options['params'])) {
      $params = static::cleanLines($this->options['params']);
      $this->options['params'] = array();
      foreach ($params as &$param) {
        $param = explode('=', $param, 2);
        if (count($param) == 2) {
          $this->options['params'][$param[0]] = $param[1];
        }
        else {
          $this->options['params'][] = $param[0];
        }
      }
      unset($param, $params);
    }

    // Connect to db.
    $db = new PDO($this->options['dsn'], $this->options['user'], $this->options['pass']);
    if (!$db) {
      return FALSE;
    }

    // Prepare query.
    $this->items = $db
      ->prepare($this->options['query'], array(
      PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY,
    ));

    // Execute query
    if (!$this->items
      ->execute($this->options['params'])) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    return $this->items
      ->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedImportConfigurable::$options protected property
FeedImportConfigurable::cleanLines public static function Helper function to get lines of a string
FeedImportConfigurable::setOptions public function Sets options for this instance 4
FeedImportReader::$items protected property
FeedImportReader::getStreamContext public function Returns a stream context
FeedImportReader::__construct final public function Constructor of reader. Constructor is final but you'll have to implement init() to init your reader.
FeedImportReader::__destruct public function Destructor. 2
FeedImportUniVectorReader::formatPath public function Override this to preprocess your paths before they are used in map(). Overrides FeedImportReader::formatPath 1
FeedImportUniVectorReader::map public function Returns a value mapped from obj by path. Overrides FeedImportReader::map
SQLFIReader::get public function This method returns the next available item or NULL if there are no items left. Overrides FeedImportReader::get
SQLFIReader::init public function Here you'll init your reader. Overrides FeedImportReader::init