public static final function factoryDataCount($where, $table, $dbconnection, array $options = null) { // Select * from $table where $where $build = QueryBuilder::count($table)->where($where); if (is_array($options)) { // Limit if (key_exists('limit', $options) && key_exists('offset', $options)) { $build->limit($options['limit'], $options['offset']); } elseif (key_exists('limit', $options)) { $build->limit($options['limit']); } } $query = new Query($dbconnection); list($data) = $query->sql($build)->execute(); return $data; }
public function testNotInBlankClause() { $qb = QueryBuilder::select(['database', 'schema', 'test' => 't'], ['id']); $qb->where(['!t.data' => []]); list($sql, $data) = $qb->resolve(); $this->assertEquals('SELECT `id` FROM `database`.`schema`.`test` as `t` WHERE true', $sql); $this->assertEquals(0, count($data)); }
public function getChildren() { // Find all direct child/parent relationships $query = new Query(static::getConnection()); $query->sql(QueryBuilder::select($this->closureTable, ['child_id'])->where(['parent_id' => $this->id, 'depth' => 1])); list($results) = $query->execute(); $children = []; foreach ($results as $row) { $children[] = $row['child_id']; } return static::findAll(['id' => $children]); }
protected function buildQuery(&$query) { // [FIXME] [NikB] Why did I split this back out to update/insert rather than replace? // Log says "Fixed major overwriting problem in commit()" but what was getting overwritten? // Insert/Update the data, and store the insert id into a variable if ($this->__delete) { $q = QueryBuilder::delete($this->__table, ['id' => $this->__data['id']]); $query->sql($q); } elseif ($this->__new) { $q = QueryBuilder::insert($this->__table, $this->__data); $query->sql($q)->sql("SELECT last_insert_id() into @id"); } else { $q = QueryBuilder::update($this->__table, $this->__data)->where(['id' => $this->__data['id']]); $query->sql($q)->sql("SELECT " . $this->__data['id'] . " into @id"); } if (!$this->__delete) { $origin_id = new SqlString('@id'); // Foreign tables foreach ($this->__external as $property_name => $value) { // Skip property if this isn't an M-M table (M-1 and 1-M tables are dealt with in other ways) if (!($pivot = $this->__model['many-to-many'][$property_name])) { continue; } // We can only do updates support simple connection access for 2 key pivots. if (count($pivot['connections']) != 1) { continue; } // Get the table name of the pivot table for this property $table = Schema::underscoreCase($pivot['pivot']); // Clear out any existing data for this object - this is safe because we are in an atomic transaction. $query->sql("Delete from {$table} where {$pivot['id']} = @id"); // Loops through the list of objects to link to this table foreach ($value as $object) { $newdata = [$pivot['id'] => $origin_id, $pivot['connections'][0]['column'] => $object->id]; $query->sql(QueryBuilder::insert($table, $newdata)); } } } }