Skip to content

Commit 306969c

Browse files
author
modbot
committed
Update 2.2.0
1 parent 4bf4a7f commit 306969c

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ See Below for usage.
2727

2828
## Connections
2929

30-
Our class lets you connect to PDO whichever way you choose, This maximizes flexibility by letting you simply give us your already existing object, or by using our class as your primary Database package and asking for the PDO object if you need to pass it to other libraries.
30+
This class lets you connect to PDO whichever way you choose, This maximizes flexibility by letting you simply give us your already existing object, or by using our class as your primary Database package and asking for the PDO object if you need to pass it to other libraries.
3131

3232

3333
### Connection Syntax
3434

35-
We let you connect using multiple syntax to make it easy to use, available both in the `__construct()` as well as `connect()`
35+
You can connect using multiple syntax to make it easy to use, available both in the `__construct()` as well as `connect()`
3636

3737
You can either use an associative array or the default which uses reverse order to let you define the most important values first, and lets you default irrelevant values such as host or type to it's defaults ('mysql' and 'localhost')
3838

src/Database.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,39 @@ public function insert_id()
108108
$id = $this->pdo->lastInsertId();
109109
return $id;
110110
}
111+
112+
public function insert($table, $data)
113+
{
114+
$keys = ""; $values = "";
115+
foreach($data as $key => $value) {
116+
$keys .= "$key,";
117+
} $keys = substr($keys, 0, -1);
118+
foreach($data as $key => $value) {
119+
$values .= $this->quote($value).",";
120+
} $values = substr($values, 0, -1);
121+
$statement = "INSERT INTO $table ($keys) VALUES ($values)";
122+
$this->query($statement);
123+
}
124+
125+
public function update($table, $data)
126+
{
127+
$values = "";
128+
foreach($data as $key => $value) {
129+
$values .= "$key = ".$this->quote($value).", ";
130+
} $values = substr($values, 0, -2);
131+
$statement = "UPDATE $table SET $values";
132+
$this->query($statement);
133+
}
134+
135+
public function store($table, $match='id', $data)
136+
{
137+
$query = $this->query("SELECT $match FROM $table WHERE $match = ".$this->quote($data['url']));
138+
if($this->num_rows($query) == 0) {
139+
$this->insert($table, $data);
140+
} else {
141+
$this->update($table, $data);
142+
}
143+
}
111144

112145
public function walk_recursive($obj, $closure)
113146
{

src/Facade.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ public static function num_rows($query)
5454
{
5555
return self::$db->num_rows($query);
5656
}
57+
public static function insert($table, $data)
58+
{
59+
return self::$db->insert($table, $data);
60+
}
61+
public static function update($table, $data)
62+
{
63+
return self::$db->update($table, $data);
64+
}
65+
public static function store($table, $match='id', $data)
66+
{
67+
return self::$db->store($table, $match, $data);
68+
}
5769
public static function insert_id()
5870
{
5971
return self::$db->insert_id($query);

0 commit comments

Comments
 (0)