當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Query::newExpr方法代碼示例

本文整理匯總了PHP中Cake\Database\Query::newExpr方法的典型用法代碼示例。如果您正苦於以下問題:PHP Query::newExpr方法的具體用法?PHP Query::newExpr怎麽用?PHP Query::newExpr使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Cake\Database\Query的用法示例。


在下文中一共展示了Query::newExpr方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: _selectQueryTranslator

 /**
  * Modify the limit/offset to TSQL
  *
  * @param \Cake\Database\Query $query The query to translate
  * @return \Cake\Database\Query The modified query
  */
 protected function _selectQueryTranslator($query)
 {
     $limit = $query->clause('limit');
     $offset = $query->clause('offset');
     if ($limit && $offset === null) {
         $query->modifier(['_auto_top_' => sprintf('TOP %d', $limit)]);
     }
     if ($offset !== null && !$query->clause('order')) {
         $query->order($query->newExpr()->add('(SELECT NULL)'));
     }
     if ($this->_version() < 11 && $offset !== null) {
         return $this->_pagingSubquery($query, $limit, $offset);
     }
     return $this->_transformDistinct($query);
 }
開發者ID:CakeDC,項目名稱:cakephp,代碼行數:21,代碼來源:SqlserverDialectTrait.php

示例2: testSqlCaseStatement

 /**
  * Tests that case statements work correctly for various use-cases.
  *
  * @return void
  */
 public function testSqlCaseStatement()
 {
     $query = new Query($this->connection);
     $publishedCase = $query->newExpr()->addCase($query->newExpr()->add(['published' => 'Y']), 1, 'integer');
     $notPublishedCase = $query->newExpr()->addCase($query->newExpr()->add(['published' => 'N']), 1, 'integer');
     //Postgres requires the case statement to be cast to a integer
     if ($this->connection->driver() instanceof \Cake\Database\Driver\Postgres) {
         $publishedCase = $query->func()->cast([$publishedCase, 'integer' => 'literal'])->type(' AS ');
         $notPublishedCase = $query->func()->cast([$notPublishedCase, 'integer' => 'literal'])->type(' AS ');
     }
     $results = $query->select(['published' => $query->func()->sum($publishedCase), 'not_published' => $query->func()->sum($notPublishedCase)])->from(['comments'])->execute()->fetchAll('assoc');
     $this->assertEquals(5, $results[0]['published']);
     $this->assertEquals(1, $results[0]['not_published']);
     $query = new Query($this->connection);
     $query->insert(['article_id', 'user_id', 'comment', 'published'])->into('comments')->values(['article_id' => 2, 'user_id' => 1, 'comment' => 'In limbo', 'published' => 'L'])->execute()->closeCursor();
     $query = new Query($this->connection);
     $conditions = [$query->newExpr()->add(['published' => 'Y']), $query->newExpr()->add(['published' => 'N'])];
     $values = ['Published', 'Not published', 'None'];
     $results = $query->select(['id', 'comment', 'status' => $query->newExpr()->addCase($conditions, $values)])->from(['comments'])->execute()->fetchAll('assoc');
     $this->assertEquals('Published', $results[2]['status']);
     $this->assertEquals('Not published', $results[3]['status']);
     $this->assertEquals('None', $results[6]['status']);
 }
開發者ID:KarimaLadhani,項目名稱:cakephp,代碼行數:28,代碼來源:QueryTest.php

示例3: testInsertExpressionValues

 /**
  * Test that insert can use expression objects as values.
  *
  * @return void
  */
 public function testInsertExpressionValues()
 {
     $query = new Query($this->connection);
     $query->insert(['title', 'author_id'])->into('articles')->values(['title' => $query->newExpr("SELECT 'jose' FROM DUAL"), 'author_id' => 99]);
     $result = $query->execute();
     $result->closeCursor();
     //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
     if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
         $this->assertCount(1, $result);
     }
     $result = (new Query($this->connection))->select('*')->from('articles')->where(['author_id' => 99])->execute();
     $this->assertCount(1, $result);
     $expected = ['id' => 4, 'title' => 'jose', 'body' => null, 'author_id' => '99', 'published' => 'N'];
     $this->assertEquals($expected, $result->fetch('assoc'));
     $subquery = new Query($this->connection);
     $subquery->select(['name'])->from('authors')->where(['id' => 1]);
     $query = new Query($this->connection);
     $query->insert(['title', 'author_id'])->into('articles')->values(['title' => $subquery, 'author_id' => 100]);
     $result = $query->execute();
     $result->closeCursor();
     //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
     if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
         $this->assertCount(1, $result);
     }
     $result = (new Query($this->connection))->select('*')->from('articles')->where(['author_id' => 100])->execute();
     $this->assertCount(1, $result);
     $expected = ['id' => 5, 'title' => 'mariano', 'body' => null, 'author_id' => '100', 'published' => 'N'];
     $this->assertEquals($expected, $result->fetch('assoc'));
 }
開發者ID:cakedc,項目名稱:cakephp-oracle-driver,代碼行數:34,代碼來源:QueryTest.php

示例4: testQuotingInsert

 /**
  * Tests that insert query parts get quoted automatically
  *
  * @return void
  */
 public function testQuotingInsert()
 {
     $this->connection->driver()->autoQuoting(true);
     $query = new Query($this->connection);
     $sql = $query->insert(['bar', 'baz'])->into('foo')->where(['something' => 'value'])->sql();
     $this->assertQuotedQuery('INSERT INTO <foo> \\(<bar>, <baz>\\)', $sql);
     $query = new Query($this->connection);
     $sql = $query->insert([$query->newExpr()->add('bar')])->into('foo')->where(['something' => 'value'])->sql();
     $this->assertQuotedQuery('INSERT INTO <foo> \\(\\(bar\\)\\)', $sql);
 }
開發者ID:ripzappa0924,項目名稱:carte0.0.1,代碼行數:15,代碼來源:QueryTest.php


注:本文中的Cake\Database\Query::newExpr方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。