Skip to content

Commit

Permalink
docs updates simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
tadhgboyle committed Nov 3, 2020
1 parent 572976d commit 310dba8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 8,7 @@
## Documentation

### Setup:
- `require 'src/Koss.php'` somewhere in your PHP script
- `require 'src/Koss.php'` somewhere in your PHP script (This will be done automatically if you're using Composer)
- Initiate a new Koss instance by passing your MySQL login and database information.
- Parameters:
- 1: Hostname
Expand Down Expand Up @@ -54,6 54,16 @@ Functions which are available in both Selection and Update/Insert queries.
- Example SQL code: `LIMIT 3`

### Update/Insert Functions:
- `update(string $table, array $values)`
- Updates any rows in the `$table` to new `$values`.
- Obviously, ***must*** be followed by at least one `where()` clause.
- `$values` must be an array in the format:
```php
// Column name => Value
$values = array(
'username' => 'Aber'
);
```
- `insert(string $table, array $row)`
- Inserts a new row into `$table`.
- `$row` must be an array in the format:
Expand Down Expand Up @@ -84,7 94,8 @@ Functions which are not in Selection or Update/Insert queries
- Common usage would be raw queries where Koss does not have functionality to help.
- *Note: Cannot be mixed with other functions*

### Examples:
## Examples

*All assuming you have autoloaded `Koss.php` and created a new instance of it with your database credentials.*

- Selecting information
Expand All @@ -100,10 111,14 @@ Functions which are not in Selection or Update/Insert queries

- Inserting information
```php
// TODO
// Insert a new row into the "users" table, if there is a unique row constraint, update only the username to "Aber"
$koss->insert('users', ['username' => 'Aberdeener', 'first_name' => 'tadhg', 'last_name' => 'boyle'])->onDuplicateKey(['username' => 'Aber'])->execute();
// MySQL Output: INSERT INTO `users` (`username`, `first_name`, `last_name`) VALUES ('Aberdeener', 'tadhg', 'boyle') ON DUPLICATE KEY UPDATE `username` = 'Aber'
```

- Updating information
```php
// TODO
// Update any existing rows in the "users" table which match the following criteria, update the username to "Aber" and the first_name to "Tadhg" where their "id" is 1 and their last_name is "Boyle"
$koss->update('users', ['username' => 'Aber', 'first_name' => 'Tadhg'])->where('id', 1)->where('last_name', '=', 'Boyle')->execute();
// MySQL Output: UPDATE `users` SET `username` = 'Aber', `first_name` = 'Tadhg' WHERE `id` = '1' AND `last_name` = 'Boyle'
```
4 changes: 4 additions & 0 deletions src/IKossQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 40,9 @@ public function reset(): void;
* Debugging only: Output the built string of all queries so far
*/
public function toString(): string;

/**
* Debugging only: Output the built string of all queries so far
*/
public function __toString(): string;
}
2 changes: 1 addition & 1 deletion src/Koss.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 27,7 @@ public function __construct(string $host, string $port, string $database, string
require_once('KossUpdateQuery.php');

try {
$this->_pdo = new PDO('mysql:host=' . $host . ';port=' . $port . ';dbname=' . $database, $username, $password);
$this->_pdo = new PDO("mysql:host=$host;port=$port;dbname=$database", $username, $password);
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die($e->getMessage());
Expand Down
2 changes: 1 addition & 1 deletion src/KossSelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 33,7 @@ public function __construct(PDO $pdo, string $query_select)

public static function get(PDO $pdo, string $table, array $columns): KossSelectQuery
{
$columns = implode(', ', $columns[0] != '*' ? array_map(fn ($string) => '`' . $string . '`', $columns) : $columns);
$columns = implode(', ', ($columns[0] != '*') ? array_map(fn ($string) => "`$string`", $columns) : $columns);
return new self($pdo, "SELECT $columns FROM `$table`");
}

Expand Down
6 changes: 3 additions & 3 deletions src/KossUpdateQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 31,8 @@ public function __construct(PDO $pdo, string $query)

public static function insert(PDO $pdo, string $table, array $row): KossUpdateQuery
{
$columns = implode(', ', array_map(fn($string) => '`' . $string . '`', array_keys($row)));
$values = implode(', ', array_map(fn($string) => '\'' . $string . '\'', array_values($row)));
$columns = implode(', ', array_map(fn($string) => "`$string`", array_keys($row)));
$values = implode(', ', array_map(fn($string) => "'$string'", array_values($row)));
return new self($pdo, "INSERT INTO `$table` ($columns) VALUES ($values)");
}

Expand All @@ -43,7 43,7 @@ public static function update(PDO $pdo, string $table, array $values): KossUpdat
$values_compiled .= "`$column` = '$value', ";
}
$values_compiled = substr($values_compiled, 0, -2);
return new self($pdo, "UPDATE $table SET $values_compiled");
return new self($pdo, "UPDATE `$table` SET $values_compiled");
}

public function where(string $column, string $operator, string $matches = null): KossUpdateQuery
Expand Down

0 comments on commit 310dba8

Please sign in to comment.