Created
March 2, 2018 19:32
-
-
Save qti3e/fe1056b5a760fd9c5d2e106027063ce6 to your computer and use it in GitHub Desktop.
Simple NoSQL entirely in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$time1 = microtime(true); | |
include "lib.php"; | |
try{ | |
$db = new array_db("db1"); | |
}catch(Exception $e){ | |
echo $e->getMessage(); | |
} | |
$db->install([ | |
"users" => [ | |
["id","username","password"], | |
[0,"AliReza",md5("")] | |
] | |
]); | |
$search = $db->select("users",["==" => ["username" => $_GET['u'],"password" => md5($_GET['p'])]]); | |
if($search->rowCount === 1){ | |
echo "Hi ".$_GET['u']; | |
}else{ | |
echo "Hi guest"; | |
} | |
$time2 = microtime(true); | |
echo '<br><b>Total Execution Time:</b> '.($time2-$time1).' Memory usage:'.memory_get_usage(); | |
echo "<br>Users count:".$db->selectAll("users")->rowCount; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class array_db | |
*/ | |
class array_db { | |
/** | |
* @var array|null | |
*/ | |
protected $db = null; | |
/** | |
* @var string|null | |
*/ | |
protected $file_name = null; | |
/** | |
* @var bool | |
*/ | |
protected $first_run = false; | |
/** | |
* @param $db | |
* @throws Exception | |
*/ | |
public function __construct($db){ | |
if(is_string($db)){ | |
$this->file_name = $db; | |
if(!file_exists($db.".php")){ | |
$this->save(); | |
$this->first_run = true; | |
}else{ | |
$this->load(); | |
} | |
}else{ | |
throw new Exception("Error:database isn't string"); | |
} | |
} | |
public function install($database){ | |
if($this->first_run === true){ | |
$this->db = $database; | |
$this->save(); | |
} | |
} | |
/** | |
* @param string $table | |
* @param int|array $where | |
* @param int $limit : show all rows if its 0 | |
* @return stdClass | |
* @throws Exception | |
*/ | |
public function select($table,$where = 1,$limit = 0){ | |
/** | |
* @param $where = [ | |
* "==,!=,LIKE" => ['column' => value,...] | |
* ] | |
*/ | |
if(isset($this->db[$table])){ | |
$p = $this->db[$table]; | |
}else{ | |
throw new Exception("Error:can't found `".$table."` in database"); | |
} | |
$re = []; | |
if($where === 1){ | |
if($limit === 0){ | |
$count = count($p)-1; | |
for($i = 1;$i <= $count;$i++){ | |
$_count = count($p[$i])-1; | |
for($b = 0;$b <= $_count;$b++){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
} | |
} | |
}else{ | |
$count = count($p)-1; | |
if($limit > $count){ | |
$limit = $count; | |
} | |
for($i = 1;$i <= $limit;$i++){ | |
$_count = count($p[$i])-1; | |
for($b = 0;$b <= $_count;$b++){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
} | |
} | |
} | |
}else{ | |
if($limit === 0){ | |
$count = count($p)-1; | |
$bl = []; | |
for($i = 1;$i <= $count;$i++){ | |
$_count = count($p[$i])-1; | |
for($b = 0;$b <= $_count;$b++){ | |
if(isset($where["=="])){ | |
if(isset($where["=="][$p[0][$b]])){ | |
if($where["=="][$p[0][$b]] == $p[$i][$b]){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
}else{ | |
$bl[] = $i; | |
unset($re[$i]); | |
} | |
} | |
} | |
if(isset($where["!="])){ | |
if(isset($where["!="][$p[0][$b]])){ | |
if($where["!="][$p[0][$b]] != $p[$i][$b]){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
}else{ | |
$bl[] = $i; | |
unset($re[$i]); | |
} | |
} | |
} | |
if(isset($where[">"])){ | |
if(isset($where[">"][$p[0][$b]])){ | |
if($where[">"][$p[0][$b]] > $p[$i][$b]){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
}else{ | |
$bl[] = $i; | |
unset($re[$i]); | |
} | |
} | |
}if(isset($where[">="])){ | |
if(isset($where[">="][$p[0][$b]])){ | |
if($where[">="][$p[0][$b]] >= $p[$i][$b]){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
}else{ | |
$bl[] = $i; | |
unset($re[$i]); | |
} | |
} | |
} | |
if(isset($where["<"])){ | |
if(isset($where["<"][$p[0][$b]])){ | |
if($where["<"][$p[0][$b]] < $p[$i][$b]){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
}else{ | |
$bl[] = $i; | |
unset($re[$i]); | |
} | |
} | |
}if(isset($where["<="])){ | |
if(isset($where["<="][$p[0][$b]])){ | |
if($where["<="][$p[0][$b]] <= $p[$i][$b]){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
}else{ | |
$bl[] = $i; | |
unset($re[$i]); | |
} | |
} | |
} | |
if(!in_array($i,$bl)){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
} | |
} | |
} | |
}else{ | |
$count = count($p)-1; | |
if($limit > $count){ | |
$limit = $count; | |
} | |
$bl = []; | |
for($i = 1;$i <= $limit;$i++){ | |
$_count = count($p[$i])-1; | |
for($b = 0;$b <= $_count;$b++){ | |
if(isset($where["=="])){ | |
if(isset($where["=="][$p[0][$b]])){ | |
if($where["=="][$p[0][$b]] == $p[$i][$b]){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
}else{ | |
$bl[] = $i; | |
unset($re[$i]); | |
} | |
} | |
} | |
if(isset($where["!="])){ | |
if(isset($where["!="][$p[0][$b]])){ | |
if($where["!="][$p[0][$b]] != $p[$i][$b]){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
}else{ | |
$bl[] = $i; | |
unset($re[$i]); | |
} | |
} | |
} | |
if(!in_array($i,$bl)){ | |
$re[$i][$p[0][$b]] = $p[$i][$b]; | |
} | |
} | |
} | |
} | |
} | |
$return = new stdClass(); | |
$return->fetchArray = $re; | |
$return->rowCount = count($re); | |
return $return; | |
} | |
/** | |
* @param string $table | |
* @return array | |
* @throws Exception | |
*/ | |
public function selectColumn($table){ | |
if(isset($this->db[$table])){ | |
$re = $this->db[$table][0]; | |
$return = new stdClass(); | |
$return->fetchArray = $re; | |
$return->rowCount = count($re); | |
return $return; | |
}else{ | |
throw new Exception("Error:can't found `".$table."` in database"); | |
} | |
} | |
/** | |
* @param string|int $table | |
* @return stdClass | |
* @throws Exception | |
*/ | |
public function selectAll($table = 0){ | |
if($table === 0){ | |
$re = $this->db; | |
$return = new stdClass(); | |
$return->fetchArray = $re; | |
$return->rowCount = count($re); | |
return $return; | |
} | |
if(isset($this->db[$table])){ | |
$return = $this->select($table); | |
return $return; | |
}else{ | |
throw new Exception("Error:can't found `".$table."` in database"); | |
} | |
} | |
/** | |
* @return stdClass | |
*/ | |
public function selectTable(){ | |
$re = array_keys($this->db); | |
$return = new stdClass(); | |
$return->fetchArray = $re; | |
$return->rowCount = count($re); | |
return $return; | |
} | |
/** | |
* @param string $table | |
* @param array $value | |
* @throws Exception | |
*/ | |
public function insert($table,$value){ | |
if(isset($this->db[$table])){ | |
if(is_array($value)){ | |
$this->db[$table][] = $value; | |
}else{ | |
throw new Exception("Error:value isn't array"); | |
} | |
}else{ | |
throw new Exception("Error:can't found `".$table."` in database"); | |
} | |
} | |
/** | |
* @param string $table | |
* @param string $name | |
* @throws Exception | |
*/ | |
public function newColumn($table,$name){ | |
if(isset($this->db[$table])){ | |
$this->db[$table][0] += [count($this->db[$table][0]) => $name]; | |
}else{ | |
throw new Exception("Error:can't found `".$table."` in database"); | |
} | |
} | |
/** | |
* @param string $table | |
* @param array $columns | |
* @throws Exception | |
*/ | |
public function newTable($table,$columns){ | |
if(!isset($this->db[$table])){ | |
$this->db[$table] = $columns; | |
}else{ | |
throw new Exception("Error:`".$table."` using now in database"); | |
} | |
} | |
/** | |
* @param string $table | |
* @param int|array $where | |
* @throws Exception | |
*/ | |
public function drop($table,$where = 1){ | |
if(isset($this->db[$table])){ | |
if($where == 1){ | |
$this->db[$table] = [$this->db[$table][0]]; | |
}else{ | |
$where_o= $where; | |
$where_n=[]; | |
if(isset($where_o['=='])){ | |
$where_n['!='] = $where_o['==']; | |
} | |
if(isset($where_o['!='])){ | |
$where_n['=='] = $where_o['!=']; | |
} | |
if(isset($where_o['>'])){ | |
$where_n['<'] = $where_o['>']; | |
} | |
if(isset($where_o['<'])){ | |
$where_n['>'] = $where_o['<']; | |
} | |
if(isset($where_o['>='])){ | |
$where_n['<='] = $where_o['>=']; | |
} | |
if(isset($where_o['<='])){ | |
$where_n['>='] = $where_o['<=']; | |
} | |
$search = $this->select($table,$where_n)->fetchArray; | |
$this->db[$table] = [$this->db[$table][0]]; | |
foreach($search as $row){ | |
$this->db[$table][] = array_values($row); | |
} | |
} | |
}else{ | |
throw new Exception("Error:can't found `".$table."` in database"); | |
} | |
} | |
/** | |
* @param string $table | |
* @throws Exception | |
*/ | |
public function dropTable($table){ | |
if(isset($this->db[$table])){ | |
unset($this->db[$table]); | |
}else{ | |
throw new Exception("Error:can't found `".$table."` in database"); | |
} | |
} | |
/** | |
* @param int|string $table | |
* @return string | |
* @throws Exception | |
*/ | |
public function toJSON($table = 0){ | |
if($table === 0){ | |
return json_encode($this->db); | |
}else{ | |
if(isset($this->db[$table])){ | |
return json_encode($this->db[$table]); | |
}else{ | |
throw new Exception("Error:can't found `".$table."` in database"); | |
} | |
} | |
} | |
/** | |
* @throws Exception | |
*/ | |
public function save(){ | |
$file_name = 0; | |
$table = 0; | |
if($file_name === 0){ | |
$file_name = $this->file_name; | |
} | |
$json = $this->toJSON($table); | |
$re = "<?php return json_decode('".str_replace("'",'\\\'',$json)."',true); ?>"; | |
file_put_contents($file_name.".php",$re); | |
} | |
/** | |
* @throws Exception | |
*/ | |
public function load(){ | |
$file_name = 0; | |
$table = 0; | |
if($file_name === 0){ | |
$file_name = $this->file_name; | |
} | |
if($table === 0){ | |
if(file_exists($file_name.".php")){ | |
$this->db = include($file_name.".php"); | |
}else{ | |
throw new Exception("Error:file `".$file_name.".php` doesn't exists"); | |
} | |
}else{ | |
$this->db[$table] = include($file_name.".php"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment