Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/JSONDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,34 @@ public function update( array $columns ) {
*
* @param string $file json filename without extension
* @param array $values Array of columns as keys and values
* @param bool $_id Generate a unique ID
*
* @return array $last_indexes Array of last index inserted
*/
public function insert( $file, array $values ) : array {
public function insert( $file, array $values, $_id = true ) : array {
$this->from( $file );

if( $_id ) {
$bin = sprintf(
"%s%s%s%s",
pack('N', time()),
substr(md5(php_uname('n')), 0, 3),
pack('n', getmypid()),
substr(pack('N', mt_rand(0, mt_getrandmax())), 1, 3)
);

$_id = '';
for ($i = 0; $i < 12; $i++) {
$_id .= sprintf("%02x", ord($bin[$i]));
}

/**
* Generate ID like MongoDB
* @author Julius Beckmann <github.com/h4cc>
*/
$values['_id'] = $_id;
}

if( !empty( $this->content[ 0 ] ) ) {
$nulls = array_diff_key( ( array ) $this->content[ 0 ], $values );
if( $nulls ) {
Expand Down