getConnection() public method

public getConnection ( ) : Connection
return Connection
Example #1
0
 /** Funkcia v poli vrati zakladne info. o pripojeni.
  * @return array
  */
 public function getDBInfo()
 {
     $pom = explode(":", $this->connection->getConnection()->getDsn());
     $out = array();
     foreach (explode(";", $pom[1]) as $v) {
         $t = explode("=", $v);
         $out[$t[0]] = $t[1];
     }
     return $out;
 }
Example #2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $connection = $this->database->getConnection();
     Nette\Database\Helpers::loadFromFile($connection, $this->getFile(static::FILE_USERS_TABLE));
     Nette\Database\Helpers::loadFromFile($connection, $this->getFile(static::FILE_ROLES_TABLE));
     Nette\Database\Helpers::loadFromFile($connection, $this->getFile(static::FILE_RESOURCES_TABLE));
     Nette\Database\Helpers::loadFromFile($connection, $this->getFile(static::FILE_USER_ROLE_TABLE));
     Nette\Database\Helpers::loadFromFile($connection, $this->getFile(static::FILE_USER_REQUEST_TABLE));
     Nette\Database\Helpers::loadFromFile($connection, $this->getFile(static::FILE_ROLES_DATA_TABLE));
     Nette\Database\Helpers::loadFromFile($connection, $this->getFile(static::FILE_IDENTITY_HASH_TABLE));
 }
 /**
  * Import taken from Adminer, slightly modified
  *
  * @param    string path to imported file
  * @param    DibiConnection
  * @returns  int number of executed queries
  *
  * @author   Jakub Vrána
  * @author   Jan Tvrdík
  * @author   Michael Moravec
  * @author   Jan Skrasek
  * @license  Apache License
  */
 protected function loadFile($file)
 {
     $query = @file_get_contents($file);
     if (!$query) {
         throw new IOException("Cannot open file '{$file}'.");
     }
     $delimiter = ';';
     $offset = $queries = 0;
     $space = "(?:\\s|/\\*.*\\*/|(?:#|-- )[^\\n]*\\n|--\\n)";
     $driver = $this->context->getConnection()->getSupplementalDriver();
     if ($driver instanceof MsSqlDriver) {
         $parse = '[\'"[]|/\\*|-- |$';
     } elseif ($driver instanceof SqliteDriver) {
         $parse = '[\'"`[]|/\\*|-- |$';
     } elseif ($driver instanceof PgSqlDriver) {
         $parse = '[\'"]|/\\*|-- |$|\\$[^$]*\\$';
     } else {
         $parse = '[\'"`#]|/\\*|-- |$';
     }
     while ($query != '') {
         if (!$offset && preg_match("~^{$space}*DELIMITER\\s+(\\S+)~i", $query, $match)) {
             $delimiter = $match[1];
             $query = substr($query, strlen($match[0]));
         } else {
             preg_match('(' . preg_quote($delimiter) . "\\s*|{$parse})", $query, $match, PREG_OFFSET_CAPTURE, $offset);
             // should always match
             $found = $match[0][0];
             $offset = $match[0][1] + strlen($found);
             if (!$found && rtrim($query) === '') {
                 break;
             }
             if (!$found || rtrim($found) == $delimiter) {
                 // end of a query
                 $q = substr($query, 0, $match[0][1]);
                 $queries++;
                 $this->context->query($q);
                 $query = substr($query, $offset);
                 $offset = 0;
             } else {
                 // find matching quote or comment end
                 while (preg_match('(' . ($found == '/*' ? '\\*/' : ($found == '[' ? ']' : (preg_match('~^-- |^#~', $found) ? "\n" : preg_quote($found) . "|\\\\."))) . '|$)s', $query, $match, PREG_OFFSET_CAPTURE, $offset)) {
                     //! respect sql_mode NO_BACKSLASH_ESCAPES
                     $s = $match[0][0];
                     $offset = $match[0][1] + strlen($s);
                     if ($s[0] !== '\\') {
                         break;
                     }
                 }
             }
         }
     }
     return $queries;
 }
Example #4
0
 private function processViews()
 {
     $tables = $this->db->getConnection()->getSupplementalDriver()->getTables();
     foreach ($tables as $table) {
         if ($table['view'] == TRUE) {
             $viewName = $table['name'];
             // Fetch SHOW CREATE VIEW string
             $row = $this->db->query("SHOW CREATE VIEW `{$viewName}`")->fetch();
             $createView = $row["Create View"];
             // Remove Auto increment value
             $createView = Strings::replace($createView, '#SQL SECURITY DEFINER\\s?#i');
             $createView = Strings::replace($createView, '#ALGORITHM=UNDEFINED\\s?#i');
             $createView = Strings::replace($createView, '/DEFINER=`.+?`\\@`.+?`\\s?/');
             // Add ;
             $createView .= ";";
             $dropTable = "DROP VIEW IF EXISTS `{$viewName}`;\n";
             $data = Data::header() . $dropTable . $createView . "\n" . Data::footer();
             $dump = new Dump($viewName, Dump::TYPE_VIEW, $this->dumpDir);
             $dump->saveIfChanged($data, function ($fromFile) use($createView) {
                 $createViewFromFile = Strings::match($fromFile, "/CREATE.*;/s")[0];
                 return Strings::normalize($createViewFromFile) == Strings::normalize($createView);
             });
         }
     }
 }
Example #5
0
 public function __construct($tableName, Context $context)
 {
     $this->tableName = $tableName;
     $this->driver = $context->getConnection()->getSupplementalDriver();
     $this->conventions = $context->getConventions();
     $this->structure = $context->getStructure();
     $this->delimitedTable = implode('.', array_map([$this->driver, 'delimite'], explode('.', $tableName)));
 }
 /**
  * Helper for findBy, countBy
  *
  * @param array   $values
  * @param array   $order Order => column=>ASC/DESC
  * @param integer $limit Limit count
  * @param integer $offset Limit offset
  *
  * @return Nette\Database\Table\Selection
  */
 protected function selectionBy(array $values, $order = null, $limit = null, $offset = null)
 {
     $selection = $this->getTable();
     // compose Where
     foreach ($values as $key => $value) {
         // translate property name to SQL column name
         $keyTranslate = $this->translatePropertyToColumnSQL($key);
         if (is_int($key) and is_array($value)) {
             // multiple parameters in array must be first condition like (col = ? OR col = ? OR col2 = ?) and next is parameters
             $value[0] = $this->translatePropertyToColumnSQL($value[0]);
             call_user_func_array([$selection, "where"], $value);
         } else {
             $selection->where($keyTranslate, $value);
         }
     }
     // compose permanently filter
     foreach ($this->getPermanentlyFilter() as $key => $value) {
         // translate property name to SQL column name
         $keyTranslate = $this->translatePropertyToColumnSQL($key);
         if (is_int($key) and is_array($value)) {
             // multiple parameters in array must be first condition like (col = ? OR col = ? OR col2 = ?) and next is parameters
             $value[0] = $this->translatePropertyToColumnSQL($value[0]);
             call_user_func_array([$selection, "where"], $value);
         } else {
             $selection->where($keyTranslate, $value);
         }
     }
     // compose Order
     if ($order !== null) {
         $orderString = "";
         foreach ($order as $column => $ascdesc) {
             // translate properties to SQL column name
             $columnTranslate = $this->translatePropertyToColumnSQL($column);
             $orderString .= ($orderString ? "," : "") . $columnTranslate . (strtolower($ascdesc) == "desc" ? " DESC" : "");
         }
         if ($orderString) {
             $selection->order($orderString);
         } else {
         }
     }
     // compose Limit
     if ($limit !== null) {
         if ($offset !== null) {
             $selection->limit((int) $limit, (int) $offset);
         } else {
             $selection->limit((int) $limit);
         }
     }
     if ($limit !== null and !$order) {
         // is MS SQL? need order for OFFSET
         if ($this->database->getConnection()->getSupplementalDriver() instanceof Nette\Database\Drivers\SqlsrvDriver) {
             $selection->order($selection->getPrimary());
         }
     }
     return $selection;
 }
Example #7
0
 public function __construct(Nette\Database\Context $context)
 {
     trigger_error(sprintf('Class %s is deprecated, use class SqlHandler instead.', __CLASS__), E_USER_DEPRECATED);
     $connection = $context->getConnection();
     $driver = $connection->getSupplementalDriver();
     $dbal = new NetteAdapter($connection);
     if ($driver instanceof Nette\Database\Drivers\PgSqlDriver) {
         parent::__construct(new PgSqlDriver($dbal, 'migrations'));
     } elseif ($driver instanceof Nette\Database\Drivers\MySqlDriver) {
         parent::__construct(new MySqlDriver($dbal, 'migrations'));
     } else {
         throw new \LogicException();
     }
 }
Example #8
0
 /**
  * Inserts row in a table.
  * @param  array|\Traversable|Selection array($column => $value)|\Traversable|Selection for INSERT ... SELECT
  * @return IRow|int|bool Returns IRow or number of affected rows for Selection or table without primary key
  */
 public function insert($data)
 {
     if ($data instanceof self) {
         $return = $this->context->queryArgs($this->sqlBuilder->buildInsertQuery() . ' ' . $data->getSql(), $data->getSqlBuilder()->getParameters());
     } else {
         if ($data instanceof \Traversable) {
             $data = iterator_to_array($data);
         }
         $return = $this->context->query($this->sqlBuilder->buildInsertQuery() . ' ?values', $data);
     }
     $this->loadRefCache();
     if ($data instanceof self || $this->primary === NULL) {
         unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
         return $return->getRowCount();
     }
     $primarySequenceName = $this->getPrimarySequence();
     $primaryKey = $this->context->getInsertId(!empty($primarySequenceName) ? $this->context->getConnection()->getSupplementalDriver()->delimite($primarySequenceName) : $primarySequenceName);
     if ($primaryKey === FALSE) {
         unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
         return $return->getRowCount();
     }
     if (is_array($this->getPrimary())) {
         $primaryKey = array();
         foreach ((array) $this->getPrimary() as $key) {
             if (!isset($data[$key])) {
                 return $data;
             }
             $primaryKey[$key] = $data[$key];
         }
         if (count($primaryKey) === 1) {
             $primaryKey = reset($primaryKey);
         }
     }
     $row = $this->createSelectionInstance()->select('*')->wherePrimary($primaryKey)->fetch();
     if ($this->rows !== NULL) {
         if ($signature = $row->getSignature(FALSE)) {
             $this->rows[$signature] = $row;
             $this->data[$signature] = $row;
         } else {
             $this->rows[] = $row;
             $this->data[] = $row;
         }
     }
     return $row;
 }
Example #9
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $connection = $this->database->getConnection();
     $driverName = $connection->getPdo()->getAttribute(\PDO::ATTR_DRIVER_NAME);
     Nette\Database\Helpers::loadFromFile($connection, $this->getFileName($driverName . '.' . static::FILE_LABELS_TABLE));
 }
Example #10
0
 /** @param  NdbContext $database */
 public function __construct(NdbContext $database)
 {
     $this->database = $database;
     $this->transaction = new Transaction($database->getConnection());
 }
 public function getPdo()
 {
     return $this->context->getConnection()->getPdo();
 }
 public function __construct(Nette\Database\Context $context, $tableName)
 {
     trigger_error(sprintf('Class %s is deprecated, use class MySqlDriver instead.', __CLASS__), E_USER_DEPRECATED);
     parent::__construct(new NetteAdapter($context->getConnection()), $tableName);
 }
 public function __construct(Context $context, $tableName)
 {
     $this->context = $context;
     $this->delimitedTableName = $context->getConnection()->getSupplementalDriver()->delimite($tableName);
     $this->delimitedLockTableName = $context->getConnection()->getSupplementalDriver()->delimite($tableName . '_lock');
 }
 public function renderFilter($categoryId = null, $manufacturerId = null)
 {
     $this->template->goodsRecommended = array();
     $this->template->goodsOther = array();
     $this->template->selectedCategoryId = $categoryId;
     $this->template->selectedSubcategoryId = $categoryId;
     $this->template->selectedManufacturerId = $manufacturerId;
     if ($categoryId && $manufacturerId) {
         $category = $this->category->createSelectionInstance()->get($categoryId);
         $manufacturer = $this->manufacturer->get($manufacturerId);
         if (!$category || !$manufacturer) {
             $this->flashMessage('Kategorie nebo výrobce nebyl nalezen.', 'warning');
         } else {
             $parentCategory = $category->parent ? $this->category->createSelectionInstance()->get($category->parent) : null;
             $this->template->filterName = ($parentCategory ? $parentCategory->name . ' - ' : '') . $category->name . " od " . $manufacturer->name;
             $this->template->og = ['title' => $this->template->filterName . ' - alena.cz', 'description' => strip_tags($category->description)];
             $in = $category . ',';
             $subcategories = $category->related('category.parent');
             foreach ($subcategories as $subcategory) {
                 $in .= $subcategory . ',';
             }
             $in = rtrim($in, ',');
             $sql = "SELECT g.id\n\t\t\t\t\t\tFROM category_goods cg\n\t\t\t\t\t\tJOIN goods g ON cg.`goods_id` = g.id\n\t\t\t\t\t\tJOIN category c ON cg.`category_id` = c.id\n\t\t\t\t\t\tWHERE cg.`category_id` IN ({$in}) AND g.`manufacturer_id` = {$manufacturerId} AND g.recommended = ";
             $this->template->goodsRecommended = $this->good->createSelectionInstance()->where('id', $this->database->getConnection()->query($sql . "1 GROUP BY id")->fetchPairs());
             $this->template->goodsOther = $this->good->createSelectionInstance()->where('id', $this->database->getConnection()->query($sql . "0 GROUP BY id")->fetchPairs());
             if ($category->parent) {
                 $this->template->selectedCategoryId = $category->parent;
                 $this->template->selectedSubCategoryId = $categoryId;
             } else {
                 $this->template->selectedCategoryId = $categoryId;
             }
         }
     } elseif ($categoryId) {
         $category = $this->category->createSelectionInstance()->get($categoryId);
         if (!$category) {
             $this->flashMessage('Kategorie nebyla nalezena.', 'warning');
         } else {
             $parentCategory = $category->parent ? $this->category->createSelectionInstance()->get($category->parent) : null;
             $this->template->filterName = ($parentCategory ? $parentCategory->name . ' - ' : '') . $category->name;
             $this->template->og = ['title' => $this->template->filterName . ' - alena.cz', 'description' => strip_tags($category->description)];
             $in = $category . ',';
             $subcategories = $category->related('category.parent');
             foreach ($subcategories as $subcategory) {
                 $in .= $subcategory . ',';
             }
             $in = rtrim($in, ',');
             $sql = "SELECT g.id\n\t\t\t\t\t\tFROM category_goods cg\n\t\t\t\t\t\tJOIN goods g ON cg.`goods_id` = g.id\n\t\t\t\t\t\tJOIN category c ON cg.`category_id` = c.id\n\t\t\t\t\t\tWHERE cg.`category_id` IN ({$in}) AND g.recommended = ";
             $this->template->goodsRecommended = $this->good->createSelectionInstance()->where('id', $this->database->getConnection()->query($sql . "1 GROUP BY id")->fetchPairs());
             $this->template->goodsOther = $this->good->createSelectionInstance()->where('id', $this->database->getConnection()->query($sql . "0 GROUP BY id")->fetchPairs());
             if ($category->parent) {
                 $this->template->selectedCategoryId = $category->parent;
                 $this->template->selectedSubCategoryId = $categoryId;
             } else {
                 $this->template->selectedCategoryId = $categoryId;
             }
         }
     } elseif ($manufacturerId) {
         $manufacturer = $this->manufacturer->get($manufacturerId);
         if (!$manufacturer) {
             $this->flashMessage('Výrobce nebyl nalezen.', 'warning');
         } else {
             $this->template->filterName = $manufacturer->name;
             $this->template->goodsRecommended = $this->good->where('manufacturer_id = ? AND recommended != 0', $manufacturerId)->order('id DESC');
             $this->template->goodsOther = $this->good->createSelectionInstance()->where('manufacturer_id = ? AND recommended = 0', $manufacturerId)->order('id DESC');
             $this->template->selectedManufacturer = $manufacturerId;
             $this->template->og = ['title' => $this->template->filterName . ' - alena.cz', 'description' => strip_tags($manufacturer->description)];
         }
     } else {
         $this->redirect("default");
     }
 }
Example #15
0
 public function __construct($table, Nette\Database\Context $context)
 {
     parent::__construct($context->getConnection(), $table, $context->getDatabaseReflection());
 }