/** * Tests sqlValue() */ public function testSqlValue() { $this->assertSame("'don\\'t'", DB::sqlValue("don't")); $this->assertSame('true', DB::sqlValue(true)); $this->assertSame('false', DB::sqlValue(false)); $this->assertSame('null', DB::sqlValue(null)); $this->assertSame('3', DB::sqlValue(3)); $this->assertSame('3.1415', DB::sqlValue(3.1415)); }
public function __toString() { // type switch ($this->type) { case self::insert: $q .= 'INSERT INTO'; break; case self::select: default: $selectedFields = empty($this->selectedFields) ? '*' : $this->selectedFields; $q .= 'SELECT ' . $selectedFields . ' FROM'; break; case self::update: $q .= 'UPDATE'; break; case self::delete: $q .= 'DELETE FROM'; break; } // table $q .= ' ' . DB::$prefix . $this->table . ' '; // fields $fields = $this->fields; if (!empty($fields)) { if ($this->type == self::insert) { // column names $columns = array_keys($fields); $columns = implode(', ', $columns); // field values foreach ($fields as &$field) { $field = DB::sqlValue($field); } $fields = implode(', ', $fields); // adding $q .= '(' . $columns . ') VALUES(' . $fields . ') '; } elseif ($this->type == self::update) { // converting (key => val) to 'key = val' foreach ($fields as $column => &$value) { $value = $column . ' = ' . DB::sqlValue($value); } // adding $q .= 'SET ' . implode(', ', $fields) . ' '; } } // WHERE if (!empty($this->where)) { $q .= 'WHERE' . $this->where . ' '; } // ORDER BY if (!empty($this->order)) { $q .= 'ORDER BY' . $this->order . ' '; } // limit and offset if ($this->limit !== null) { if ($this->offset !== null) { $q .= 'LIMIT ' . $this->offset . ', ' . $this->limit . ' '; } else { $q .= 'LIMIT ' . $this->limit . ' '; } } // returning return substr($q, 0, -1); }