Strips fields out of SQL functions before quoting.
Results of this method are stored in a memory cache. This improves performance, but
because the method uses a hashing algorithm it can have collisions.
Setting DboSource::$cacheMethods to false will disable the memory cache.
/** * SQL file header * * @param $datasource * @return string */ function _createSqlDumpHeader($datasource) { $sql = array(); $sql[] = $this->hr(0); $sql[] = '-- ' . $this->message; $sql[] = '-- generated on: ' . date('Y-m-d H:i:s') . ' : ' . time(); $sql[] = $this->hr(0); $sql[] = ''; if (preg_match('/^mysql/i', $this->DataSource->config['driver'])) { $sql[] = 'use ' . $this->DataSource->name($this->DataSource->config['database']) . ';'; } if (!empty($this->DataSource->config['encoding'])) { $sql[] = 'SET NAMES ' . $this->DataSource->value($this->DataSource->config['encoding']) . ';'; } return $this->out($sql); }
/** * testFieldParsing method * * @return void */ public function testFieldParsing() { $this->Model = new TestModel(); $result = $this->Dbo->fields($this->Model, 'Vendor', "Vendor.id, COUNT(Model.vendor_id) AS `Vendor`.`count`"); $expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, 'Vendor', "`Vendor`.`id`, COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`"); $expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, 'Post', "CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name, Node.created"); $expected = array("CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", "`Node`.`created`"); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, 'round( (3.55441 * fooField), 3 ) AS test'); $this->assertEquals(array('round( (3.55441 * fooField), 3 ) AS test'), $result); $result = $this->Dbo->fields($this->Model, null, 'ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating'); $this->assertEquals(array('ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating'), $result); $result = $this->Dbo->fields($this->Model, null, 'ROUND(Rating.rate_total / Rating.rate_count,2) AS rating'); $this->assertEquals(array('ROUND(Rating.rate_total / Rating.rate_count,2) AS rating'), $result); $result = $this->Dbo->fields($this->Model, 'Post', "Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name"); $expected = array("`Node`.`created`", "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name"); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, 'Post', "2.2,COUNT(*), SUM(Something.else) as sum, Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name,Post.title,Post.1,1.1"); $expected = array('2.2', 'COUNT(*)', 'SUM(`Something`.`else`) as sum', '`Node`.`created`', "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", '`Post`.`title`', '`Post`.`1`', '1.1'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, "(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`"); $expected = array("(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`"); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, 'Post'); $expected = array('`Post`.`id`', '`Post`.`client_id`', '`Post`.`name`', '`Post`.`login`', '`Post`.`passwd`', '`Post`.`addr_1`', '`Post`.`addr_2`', '`Post`.`zip_code`', '`Post`.`city`', '`Post`.`country`', '`Post`.`phone`', '`Post`.`fax`', '`Post`.`url`', '`Post`.`email`', '`Post`.`comments`', '`Post`.`last_login`', '`Post`.`created`', '`Post`.`updated`'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, 'Other'); $expected = array('`Other`.`id`', '`Other`.`client_id`', '`Other`.`name`', '`Other`.`login`', '`Other`.`passwd`', '`Other`.`addr_1`', '`Other`.`addr_2`', '`Other`.`zip_code`', '`Other`.`city`', '`Other`.`country`', '`Other`.`phone`', '`Other`.`fax`', '`Other`.`url`', '`Other`.`email`', '`Other`.`comments`', '`Other`.`last_login`', '`Other`.`created`', '`Other`.`updated`'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, array(), false); $expected = array('id', 'client_id', 'name', 'login', 'passwd', 'addr_1', 'addr_2', 'zip_code', 'city', 'country', 'phone', 'fax', 'url', 'email', 'comments', 'last_login', 'created', 'updated'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, 'COUNT(*)'); $expected = array('COUNT(*)'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, 'SUM(Thread.unread_buyer) AS ' . $this->Dbo->name('sum_unread_buyer')); $expected = array('SUM(`Thread`.`unread_buyer`) AS `sum_unread_buyer`'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, 'name, count(*)'); $expected = array('`TestModel`.`name`', 'count(*)'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, 'count(*), name'); $expected = array('count(*)', '`TestModel`.`name`'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, 'field1, field2, field3, count(*), name'); $expected = array('`TestModel`.`field1`', '`TestModel`.`field2`', '`TestModel`.`field3`', 'count(*)', '`TestModel`.`name`'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, array('dayofyear(now())')); $expected = array('dayofyear(now())'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, array('MAX(Model.field) As Max')); $expected = array('MAX(`Model`.`field`) As Max'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, array('Model.field AS AnotherName')); $expected = array('`Model`.`field` AS `AnotherName`'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, array('field AS AnotherName')); $expected = array('`field` AS `AnotherName`'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, null, array('TestModel.field AS AnotherName')); $expected = array('`TestModel`.`field` AS `AnotherName`'); $this->assertEquals($expected, $result); $result = $this->Dbo->fields($this->Model, 'Foo', array('id', 'title', '(user_count + discussion_count + post_count) AS score')); $expected = array('`Foo`.`id`', '`Foo`.`title`', '(user_count + discussion_count + post_count) AS score'); $this->assertEquals($expected, $result); }
/** * Prepares field names to be quoted by parent * * @param string $data * @return string SQL field */ function name($data) { if (is_string($data)) { $data = str_replace('"__"', '__', $data); } return parent::name($data); }
/** * Prepares field names to be quoted by parent * * @param string $data * @return string SQL field */ function name($data) { return parent::name(str_replace('"__"', '__', $data)); }