当前位置: 首页>>代码示例>>PHP>>正文


PHP Query::connection方法代码示例

本文整理汇总了PHP中Cake\Database\Query::connection方法的典型用法代码示例。如果您正苦于以下问题:PHP Query::connection方法的具体用法?PHP Query::connection怎么用?PHP Query::connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Cake\Database\Query的用法示例。


在下文中一共展示了Query::connection方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: _insertQueryTranslator

 /**
  * Transforms an insert query that is meant to insert multiple rows at a time,
  * otherwise it leaves the query untouched.
  *
  * The way Firebird works with multi insert is by having multiple select statements
  * joined with UNION.
  *
  * @param \Cake\Database\Query $query The query to translate
  * @return \Cake\Database\Query
  */
 protected function _insertQueryTranslator($query)
 {
     $v = $query->clause('values');
     if (count($v->values()) === 1 || $v->query()) {
         return $query;
     }
     $newQuery = $query->connection()->newQuery();
     $cols = $v->columns();
     $placeholder = 0;
     $replaceQuery = false;
     foreach ($v->values() as $k => $val) {
         $fillLength = count($cols) - count($val);
         if ($fillLength > 0) {
             $val = array_merge($val, array_fill(0, $fillLength, null));
         }
         foreach ($val as $col => $attr) {
             if (!$attr instanceof ExpressionInterface) {
                 $val[$col] = sprintf(':c%d', $placeholder);
                 $placeholder++;
             }
         }
         $select = array_combine($cols, $val);
         if ($k === 0) {
             $replaceQuery = true;
             $newQuery->select($select);
             continue;
         }
         $q = $newQuery->connection()->newQuery();
         $newQuery->unionAll($q->select($select));
     }
     if ($replaceQuery) {
         $v->query($newQuery);
     }
     return $query;
 }
开发者ID:mbamarante,项目名称:cakephp-firebird-driver,代码行数:45,代码来源:FirebirdDialectTrait.php

示例2: _buildInsertPart

 /**
  * Builds the SQL fragment for INSERT INTO.
  *
  * @param array $parts The insert parts.
  * @param \Cake\Database\Query $query The query that is being compiled
  * @param \Cake\Database\ValueBinder $generator the placeholder generator to be used in expressions
  * @return string SQL fragment.
  */
 protected function _buildInsertPart($parts, $query, $generator)
 {
     $driver = $query->connection()->driver();
     $table = $driver->quoteIfAutoQuote($parts[0]);
     $columns = $this->_stringifyExpressions($parts[1], $generator);
     return sprintf('INSERT INTO %s (%s)', $table, implode(', ', $columns));
 }
开发者ID:cakedc,项目名称:cakephp-oracle-driver,代码行数:15,代码来源:OracleCompiler.php

示例3: _transformTupleComparison

 /**
  * Receives a TupleExpression and changes it so that it conforms to this
  * SQL dialect.
  *
  * It transforms expressions looking like '(a, b) IN ((c, d), (e, f)' into an
  * equivalent expression of the form '((a = c) AND (b = d)) OR ((a = e) AND (b = f))'.
  *
  * It can also transform transform expressions where the right hand side is a query
  * selecting the same amount of columns as the elements in the left hand side of
  * the expression:
  *
  * (a, b) IN (SELECT c, d FROM a_table) is transformed into
  *
  * 1 = (SELECT 1 FROM a_table WHERE (a = c) AND (b = d))
  *
  * @param \Cake\Database\Expression\TupleComparison $expression The expression to transform
  * @param \Cake\Database\Query $query The query to update.
  * @return void
  */
 protected function _transformTupleComparison(TupleComparison $expression, $query)
 {
     $fields = $expression->getField();
     if (!is_array($fields)) {
         return;
     }
     $value = $expression->getValue();
     $op = $expression->getOperator();
     $true = new QueryExpression('1');
     if ($value instanceof Query) {
         $selected = array_values($value->clause('select'));
         foreach ($fields as $i => $field) {
             $value->andWhere([$field . " {$op}" => new IdentifierExpression($selected[$i])]);
         }
         $value->select($true, true);
         $expression->setField($true);
         $expression->setOperator('=');
         return;
     }
     $surrogate = $query->connection()->newQuery()->select($true);
     if (!is_array(current($value))) {
         $value = [$value];
     }
     foreach ($value as $tuple) {
         $surrogate->orWhere(function ($exp) use($fields, $tuple) {
             foreach (array_values($tuple) as $i => $value) {
                 $exp->add([$fields[$i] => $value]);
             }
             return $exp;
         });
     }
     $expression->setField($true);
     $expression->setValue($surrogate);
     $expression->setOperator('=');
 }
开发者ID:CakeDC,项目名称:cakephp,代码行数:54,代码来源:TupleComparisonTranslatorTrait.php

示例4: _buildSelectPart

 /**
  * Helper function used to build the string representation of a SELECT clause,
  * it constructs the field list taking care of aliasing and
  * converting expression objects to string. This function also constructs the
  * DISTINCT clause for the query. This has been modified to allow it to support Salesforce
  *
  * @param array $parts list of fields to be transformed to string
  * @param \Cake\Database\Query $query The query that is being compiled
  * @param \Cake\Database\ValueBinder $generator the placeholder generator to be used in expressions
  * @return string
  */
 protected function _buildSelectPart($parts, $query, $generator)
 {
     $driver = $query->connection()->driver();
     $select = 'SELECT %s%s%s';
     if ($this->_orderedUnion && $query->clause('union')) {
         $select = '(SELECT %s%s%s';
     }
     $distinct = $query->clause('distinct');
     $modifiers = $query->clause('modifier') ?: null;
     $normalized = [];
     $parts = $this->_stringifyExpressions($parts, $generator);
     foreach ($parts as $k => $p) {
         if (!is_numeric($k)) {
             $p = $p;
             //Leave it alone
         }
         $normalized[] = $p;
     }
     if ($distinct === true) {
         $distinct = 'DISTINCT ';
     }
     if (is_array($distinct)) {
         $distinct = $this->_stringifyExpressions($distinct, $generator);
         $distinct = sprintf('DISTINCT ON (%s) ', implode(', ', $distinct));
     }
     if ($modifiers !== null) {
         $modifiers = $this->_stringifyExpressions($modifiers, $generator);
         $modifiers = implode(' ', $modifiers) . ' ';
     }
     return sprintf($select, $distinct, $modifiers, implode(', ', $normalized));
 }
开发者ID:voycey,项目名称:cakephp-salesforce,代码行数:42,代码来源:SalesforceQueryCompiler.php

示例5: testBind

 /**
  * Tests parameter binding
  *
  * @return void
  */
 public function testBind()
 {
     $query = new Query($this->connection);
     $driver = $query->connection()->driver();
     $createdField = $driver->quoteIfAutoQuote('created');
     $results = $query->select(['id', 'comment'])->from('comments')->where(["{$createdField} BETWEEN :foo AND :bar"])->bind(':foo', new \DateTime('2007-03-18 10:50:00'), 'datetime')->bind(':bar', new \DateTime('2007-03-18 10:52:00'), 'datetime')->execute();
     $expected = [['id' => '4', 'comment' => 'Fourth Comment for First Article']];
     $this->assertEquals($expected, $results->fetchAll('assoc'));
     $query = new Query($this->connection);
     $results = $query->select(['id', 'comment'])->from('comments')->where(["{$createdField} BETWEEN :foo AND :bar"])->bind(':foo', '2007-03-18 10:50:00')->bind(':bar', '2007-03-18 10:52:00')->execute();
     $this->assertEquals($expected, $results->fetchAll('assoc'));
 }
开发者ID:cakedc,项目名称:cakephp-oracle-driver,代码行数:17,代码来源:QueryTest.php

示例6: _insertQueryTranslator

 /**
  * Transforms an insert query that is meant to insert multiple rows at a time,
  * otherwise it leaves the query untouched.
  *
  * The way SQLite works with multi insert is by having multiple select statements
  * joined with UNION.
  *
  * @param \Cake\Database\Query $query The query to translate
  * @return Query
  */
 protected function _insertQueryTranslator($query)
 {
     $v = $query->clause('values');
     if (count($v->values()) === 1 || $v->query()) {
         return $query;
     }
     $newQuery = $query->connection()->newQuery();
     $cols = $v->columns();
     foreach ($v->values() as $k => $val) {
         $fillLength = count($cols) - count($val);
         if ($fillLength > 0) {
             $val = array_merge($val, array_fill(0, $fillLength, null));
         }
         $val = array_map(function ($val) {
             return $val instanceof ExpressionInterface ? $val : '?';
         }, $val);
         $select = array_combine($cols, $val);
         if ($k === 0) {
             $newQuery->select($select);
             continue;
         }
         $q = $newQuery->connection()->newQuery();
         $newQuery->unionAll($q->select($select));
     }
     if ($newQuery->type()) {
         $v->query($newQuery);
     }
     return $query;
 }
开发者ID:ripzappa0924,项目名称:carte0.0.1,代码行数:39,代码来源:SqliteDialectTrait.php


注:本文中的Cake\Database\Query::connection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。