Skip to content

Commit

Permalink
Update README (and make it less ugly)
Browse files Browse the repository at this point in the history
  • Loading branch information
cybrox committed Apr 20, 2017
1 parent 005b1a3 commit 693632f
Showing 1 changed file with 57 additions and 38 deletions.
95 changes: 57 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ So, obviously, crunchDB needs to have some sort of functionality. It would be pr
Here's a list of all functions that are currently implemented.

`$cdb = new crunchDB('./db/', 'json', 'rw')` Initialize crunchdb with the following parameters:
**directory** - The path to the directory where all the json files will be stored in.
**extension** - The extension of the db files. (This parameter is optional, it is set to `json` as default)
**db mode** - The database access mode. Can be read(r) or (rw). (This parameter is optional, it is set to `rw` as default, **It is not yet implemented!**)
- **directory** - The path to the directory where all the json files will be stored in.
- **extension** - The extension of the db files. (This parameter is optional, it is set to `json` as default)
- **db mode** - The database access mode. Can be read(r) or (rw). (Not yet implemented!)

**PLEASE NOTE:** crunchDB does not currently support or handle file locking. Thus using it for anything more than simple internal tools (mainly anything with concurrent access, obviously) is highly discouraged!

| Method | Description |
| --- | --- |
Expand All @@ -26,55 +28,72 @@ Here's a list of all functions that are currently implemented.
| `cdbTable->raw()` | Get the raw json data of the table's content.|
| `cdbTable->count()` | Count all rows in this table. This is equal to `->select('*')->count()`.|
| `cdbTable->insert('data')` | Insert an array of data, crunchDB will not check it's structure! |
| `cdbTable->select(XXXX)` | Select rows from the table, see explanation of XXXX below! Returns resouce `cdbRes` |
| `cdbTable->select(*1)` | Select rows from the table, see explanation of **\*1** below! Returns resouce `cdbRes` |
| -- | -- |
| `cdbRes->sort(YYYY)` | Sort rows in the resource, see explanation of YYYY below! |
| `cdbRes->sort(*2)` | Sort rows in the resource, see explanation of **\*2** below! |
| `cdbRes->sortfn('func')` | Sort rows in the resource via usort with a custom function (PHP 5.3+) |
| `cdbRes->count()` | Count all the rows in this resource |
| `cdbRes->fetch()` | Fetch all the rows in this resource |
| `cdbRes->delete()` | Fetch all the rows in this resource from the table |
| `cdbRes->update(ZZZZ)` | Update all the rows in this resource from the table, see explanation of ZZZZ below |
| `cdbRes->update(*3)` | Update all the rows in this resource from the table, see explanation of **\*3** below |


#### Parameter explanation
**XXXX** is the multi array parameter for select. You can add multiple keys to filter for, for example `['type', '==', 'user']` would search for all rows where the column `type` equals 'user'. Hereby, the first element is the **key** to search for, the second one the **compare operator** (`<`, `<=`, `==`, `>=` or `>`), the third one the respective **value** to compare and the optional fourth parameter can be an `'and'`. You can also chain select parameters as following:
`...->select(['type', '==', 'user'],['type', '==', 'admin'])->...` Search for rows with the type `user` **OR** `admin`
`...->select(['type', '==', 'user'],['name', '==', 'cybrox', 'and'])->...` Search for rows with the type `user` **AND** the `name` 'cybrox'

**YYYY** is the multi array parameter for sorting. You can add multiple keys to sort by, for exmaple `['name']` or `['name', SORT_DESC]`. Each array can have up to 3 parameters, that being the column name to sort on (mandatory), either SORT_ASC or SORT_DESC (optional) and a projection function (optional).
You can also chain sort parameters as following:
`['type', SORT_DESC], ['name', SORT_DESC]` Sort by number descending and then by name descending.
###### \*1 SELECT Parameter
This is the multi-array parameter for select. You can add multiple keys to filter for, for example `['type', '==', 'user']` would search for all rows where the column `type` equals 'user'. Hereby, the first element is the **key** to search for, the second one the **compare operator** (`<`, `<=`, `==`, `>=` or `>`), the third one the respective **value** to compare and the optional fourth parameter can be an `'and'` or `'or'`. You can also chain select parameters as following:

**ZZZZ** is the multi array parameter for updating. You can add multiple key-vaule pairs to update here like `['name','cybrox']` or `['type','admin'],['name','cybrox']`
You can chain as many fields in there as you want to change.
```php
// Search for rows with the type `user` **OR** `admin
...->select(['type', '==', 'user'],['type', '==', 'admin'])->...

// Search for rows with the type `user` **AND** the `name` 'cybrox'
...->select(['type', '==', 'user'],['name', '==', 'cybrox', 'and'])->...
```

#### On chaining actions
Though chaining a lot of arguments might get a bit long, it's definitely a really simple way to build *queries* for something like this. You can find a lot of examples on how to chain them in the *example.php* file.
###### \*2 SORT Parameter
This is the multi-array parameter for sorting. You can add multiple keys to sort by, for exmaple `['name']` or `['name', SORT_DESC]`. Each array can have up to 3 parameters, that being the column name to sort on (mandatory), either SORT_ASC or SORT_DESC (optional) and a projection function (optional).
You can also chain sort parameters as following:

```php
// Sort by number descending and then by name descending.
...->sort(['type', SORT_DESC], ['name', SORT_DESC])->...
```

### Some code examples
###### \*3 UPDATE Parameter
This is the multi-array parameter for updating. You can add multiple key-vaule pairs to update here like `['name','cybrox']`. You can chain as many fields in there as you want to change.
```php
// Update type and name of a resource.
...->update(['type','admin'],['name','cybrox'])->...
```


### Code examples
```php
<?php
$cdb->version()
$cdb->table('cookies')->exists()
$cdb->table('cookies')->create()
$cdb->table('cakes')->create()
$cdb->table('cakes')->alter('cheese')
$cdb->tables()
$cdb->table('cookies')->count()
$cdb->table('cookies')->insert(array("type" => "chocolate", "is" => "nice"))
$cdb->table('cookies')->insert(array("type" => "banana", "is" => "nice"))
$cdb->table('cookies')->insert(array("type" => "strawberry", "is" => "ok"))
$cdb->table('cookies')->raw()
$cdb->table('cookies')->select('*')->fetch()
$cdb->table('cookies')->select('*')->count()
$cdb->table('cookies')->select(['type', '==', 'chocolate'])->fetch()
$cdb->table('cookies')->select(['type', '==', 'chocolate'],['type', '==', 'banana', 'or'])->fetch()
$cdb->table('cookies')->select(['type', '==', 'chocolate'],['type', '==', 'banana', 'and'])->count()
$cdb->table('cookies')->select('*')->sort(['type'])->fetch()
$cdb->table('cookies')->select(['type', '==', 'strawberry'])->delete()
$cdb->table('cookies')->select(['type', '==', 'banana'])->update(['type', 'chocolate'])
$cdb->table('cookies')->select('*')->fetch()

use cybrox\crunchdb\crunchDB as crunchDB;
$cdb = new crunchDB('./db/');

$cdb->version();
$cdb->table('cookies')->exists();
$cdb->table('cookies')->create();
$cdb->table('cakes')->create();
$cdb->table('cakes')->alter('cheese');
$cdb->tables();
$cdb->table('cookies')->count();
$cdb->table('cookies')->insert(array("type" => "chocolate", "is" => "nice"));
$cdb->table('cookies')->insert(array("type" => "banana", "is" => "nice"));
$cdb->table('cookies')->insert(array("type" => "strawberry", "is" => "ok"));
$cdb->table('cookies')->raw();
$cdb->table('cookies')->select('*')->fetch();
$cdb->table('cookies')->select('*')->count();
$cdb->table('cookies')->select(['type', '==', 'chocolate'])->fetch();
$cdb->table('cookies')->select(['type', '==', 'chocolate'],['type', '==', 'banana', 'or'])->fetch();
$cdb->table('cookies')->select(['type', '==', 'chocolate'],['type', '==', 'banana', 'and'])->count();
$cdb->table('cookies')->select('*')->sort(['type'])->fetch();
$cdb->table('cookies')->select(['type', '==', 'strawberry'])->delete();
$cdb->table('cookies')->select(['type', '==', 'banana'])->update(['type', 'chocolate']);
$cdb->table('cookies')->select('*')->fetch();

?>
```

0 comments on commit 693632f

Please sign in to comment.