Skip to content

Commit

Permalink
Update ssql.php
Browse files Browse the repository at this point in the history
Added `->put()` method as a shorthand for `->insert()`, `->update()` and `->delete()`
  • Loading branch information
ratajs authored Nov 25, 2021
1 parent 9ecc7a4 commit d3937a0
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions ssql.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function truncate(string $table, int $flags = 0) {
return $this->query($query, $flags, "truncate");
}

public function insert(string $table, array $values, int $flags = 0) {
public function insert(string $table, array $values, int $flags = 0, string $name = "insert") {
$cols = [""];
$useCols = true;
foreach($values as $k => $v) {
Expand Down Expand Up @@ -430,19 +430,19 @@ public function insert(string $table, array $values, int $flags = 0) {
};
$r = $this->query("
INSERT INTO {$table}{$colString} VALUES ({$valueString})
", $flags, "insert");
", $flags, $name);
return (boolval($flags & self::INSERT_RETURN_ID) ? $this->connect->lastInsertId() : $r);
}

public function delete(string $table, $cond, int $flags = 128) {
public function delete(string $table, $cond, int $flags = 128, string $name = "delete") {
$all = !boolval($flags & self::COND_OR);
$condString = $this->getCondString($cond, $all);
return $this->query("
DELETE FROM `{$table}` WHERE {$condString}
", $flags, "delete");
", $flags, $name);
}

public function update(string $table, $cond, array $values, int $flags = 128) {
public function update(string $table, $cond, array $values, int $flags = 128, string $name = "update") {
$all = !boolval($flags & self::COND_OR);
$condString = $this->getCondString($cond, $all);
$string = "";
Expand All @@ -461,7 +461,7 @@ public function update(string $table, $cond, array $values, int $flags = 128) {
};
return $this->query("
UPDATE `{$table}` SET {$string} WHERE {$condString}
", $flags, "update");
", $flags, $name);
}

public function add(string $table, string $name, string $type, int $length, bool $null, string $where, string$key, string $data = "", int $flags = 0) {
Expand Down Expand Up @@ -639,6 +639,18 @@ public function get(string $table, $options = [], int $flags = 4096) {
$result = $this->selectJoinWhere($table, $join, $on, $cond, $order, $cols, $limit, $flags | $jointype | $condtype | $ordertype, "get");
return $this->fetch($flags | $fetch);
}

public function put(string $table, $data, $cond = NULL, int $flags = 0) {
if(is_int($cond) && $flags==0) {
$flags = $cond;
$cond = NULL;
};
if(empty($cond))
return $this->insert($table, $data, $flags, "put");
if($data===NULL)
return $this->delete($table, $cond, $flags, "put");
return $this->update($table, $cond, $data, $flags, "put");
}

public function cond($cond = "") {
return new SSQLCond($this, $cond);
Expand Down

0 comments on commit d3937a0

Please sign in to comment.