/** * @return void */ public static function delete($table, $id) { // Sets the "active" column name if (self::is_init() && text::check_name($table) && \bbn\str\text::is_integer($id)) { self::$db->query("\n DELETE FROM " . self::$db->escape(self::$htable) . "\n WHERE " . self::$db->escape('column') . " LIKE '" . self::$db->table_full_name($table) . ".%'\n AND " . self::$db->escape('line') . " = {$id}"); } }
/** * Inserts row(s) in a table. * * <code> * $this->db->insert( * "table_users", * [ * ["name" => "Ted"], * ["surname" => "McLow"] * ]); * </code> * * @param string $table The table name. * @param array $values The values to insert. * @param bool $ignore If true, controls if the row is already existing and ignores it. * * @return int Number affected rows. */ public function insert($table, array $values, $ignore = false) { $r = false; $trig = 1; $keys = array_keys($values); if (isset($keys[0]) && $keys[0] === 0) { $keys = array_keys($values[0]); } else { $values = [$values]; } $affected = 0; if ($sql = $this->_statement('insert', $table, $keys, $ignore)) { foreach ($values as $i => $vals) { if ($this->triggers_disabled) { if ($r = $this->query($sql['sql'], $sql['hash'], array_values($vals))) { $affected += $r; } } else { // in case the trigger is called, values might be changed by the callback. // In this case, they will be sent back in the result array as 'values' // Then the SQL statement needs to be rebuilt $trig = $this->_trigger($table, 'insert', 'before', $vals); if (!is_array($trig)) { $trig = ['trig' => $trig]; } if ($trig['trig']) { if (isset($trig['values'])) { $vals = $trig['values']; if (!($sql = $this->_statement('insert', $table, array_keys($vals), $ignore))) { die($this->log("Problem with the values returned by the callback function(s)", $table, $vals)); } } if ($r = $this->query($sql['sql'], $sql['hash'], array_values($vals))) { $affected += $r; $this->_trigger($table, 'insert', 'after', $vals); } if (isset($trig['value'])) { if (\bbn\str\text::is_integer($trig['value'])) { $affected += $trig['value']; } else { $affected = $trig['value']; } } } } } } return $affected; }
public function filter($filters) { $res = ''; if ($this->check() && isset($filters['filters'])) { if (isset($filters['filters']) && count($filters['filters']) > 0) { $logic = isset($filters['logic']) && $filters['logic'] === 'or' ? 'OR' : 'AND'; foreach ($filters['filters'] as $f) { $ok = false; if (empty($res)) { $pre = " ( "; } else { $pre = " {$logic} "; } if (isset($f['logic'])) { $res .= $pre . $this->filter($f); } else { if ($field = $this->get_field($f)) { if ($this->structure && isset($this->structure['fields'][$f['field']])) { if ($this->structure['fields'][$f['field']]['type'] === 'int' && $this->structure['fields'][$f['field']]['maxlength'] == 1 && !\bbn\str\text::is_integer($f['value'])) { $f['value'] = $f['value'] === 'true' ? 1 : 0; } } $res .= $pre . $field . " "; switch ($f['operator']) { case 'eq': $res .= \bbn\str\text::is_number($f['value']) ? "= " . $f['value'] : "LIKE '" . $this->db->escape_value($f['value']) . "'"; break; case 'neq': $res .= \bbn\str\text::is_number($f['value']) ? "!= " . $f['value'] : "NOT LIKE '" . $this->db->escape_value($f['value']) . "'"; break; case 'startswith': $res .= "LIKE '" . $this->db->escape_value($f['value']) . "%'"; break; case 'endswith': $res .= "LIKE '%" . $this->db->escape_value($f['value']) . "'"; break; case 'gte': $res .= ">= '" . $this->db->escape_value($f['value']) . "'"; break; case 'gt': $res .= "> '" . $this->db->escape_value($f['value']) . "'"; break; case 'lte': $res .= "<= '" . $this->db->escape_value($f['value']) . "'"; break; case 'lt': $res .= "< '" . $this->db->escape_value($f['value']) . "'"; break; case 'contains': default: $res .= "LIKE '%" . $this->db->escape_value($f['value']) . "%'"; break; case 'doesnotcontain': $res .= "NOT LIKE ? "; $f['value'] = '%' . $f['value'] . '%'; break; } } } } if (!empty($res)) { $res .= " ) "; } } } return $res; }