Fist step to #4

This commit is contained in:
2019-04-22 21:48:39 -03:00
parent 1d9b076382
commit e3b1ba9516
3 changed files with 44 additions and 42 deletions
Executable
+2
View File
@@ -0,0 +1,2 @@
/netbeans
/.idea
Regular → Executable
+6 -6
View File
@@ -6,37 +6,37 @@ class DBInstance {
public static function execute($sql, $data = null, $instance = 'default') { public static function execute($sql, $data = null, $instance = 'default') {
if ($data) { if ($data) {
self::query_prepare($sql, $data, $instance); self::queryPrepare($sql, $data, $instance);
} else { } else {
$con = Connections::get_connection($instance); $con = Connections::get_connection($instance);
$con->exec($sql); $con->exec($sql);
} }
} }
public static function last_insert($instance = 'default') { public static function lastInsert($instance = 'default') {
$con = Connections::get_connection($instance); $con = Connections::get_connection($instance);
return $con->lastInsertId(); return $con->lastInsertId();
} }
public static function query($sql, $data = null, $instance = 'default') { public static function query($sql, $data = null, $instance = 'default') {
if ($data) { if ($data) {
return self::query_prepare($sql, $data, $instance); return self::queryPrepare($sql, $data, $instance);
} else { } else {
$con = Connections::get_connection($instance); $con = Connections::get_connection($instance);
return $con->query($sql, \PDO::FETCH_OBJ); return $con->query($sql, \PDO::FETCH_OBJ);
} }
} }
public static function query_one($sql, $data = null, $instance = 'default') { public static function queryOne($sql, $data = null, $instance = 'default') {
if ($data) { if ($data) {
return self::query_prepare($sql, $data, $instance); return self::queryPrepare($sql, $data, $instance);
} else { } else {
$con = Connections::get_connection($instance); $con = Connections::get_connection($instance);
return $con->query($sql, \PDO::FETCH_OBJ)->fetch(); return $con->query($sql, \PDO::FETCH_OBJ)->fetch();
} }
} }
public static function query_prepare($sql, $data = Array(), $instance = 'default') { public static function queryPrepare($sql, $data = Array(), $instance = 'default') {
$con = Connections::get_connection($instance); $con = Connections::get_connection($instance);
$statement = $con->prepare($sql); $statement = $con->prepare($sql);
$statement->execute($data); $statement->execute($data);
Regular → Executable
+36 -36
View File
@@ -80,8 +80,8 @@ abstract class Entity {
$update_string = rtrim($update_string, ', '); $update_string = rtrim($update_string, ', ');
$sql = "UPDATE " . static::table_name . " SET $update_string WHERE id = :_update_id"; $sql = "UPDATE " . static::tableName . " SET $update_string WHERE id = :_update_id";
DBInstance::execute($sql, $update_data, static::connection_name); DBInstance::execute($sql, $update_data, static::connectionName);
} }
private function create() { private function create() {
@@ -102,9 +102,9 @@ abstract class Entity {
$first_argument = rtrim($first_argument, ', '); $first_argument = rtrim($first_argument, ', ');
$second_argument = rtrim($second_argument, ', '); $second_argument = rtrim($second_argument, ', ');
$sql = "INSERT INTO " . static::table_name . " ($first_argument) VALUES ($second_argument)"; $sql = "INSERT INTO " . static::tableName . " ($first_argument) VALUES ($second_argument)";
DBInstance::execute($sql, $insert_data, static::connection_name); DBInstance::execute($sql, $insert_data, static::connectionName);
$this->id = DBInstance::last_insert(); $this->id = DBInstance::lastInsert();
} }
public function delete() { public function delete() {
@@ -121,14 +121,14 @@ abstract class Entity {
return; return;
} }
$sql = "SELECT * FROM " . static::table_name . " WHERE id = ?"; $sql = "SELECT * FROM " . static::tableName . " WHERE id = ?";
$this->fill(DBInstance::query_prepare($sql, Array($id), static::connection_name)[0]); $this->fill(DBInstance::queryPrepare($sql, Array($id), static::connectionName)[0]);
} }
public function purge() { public function purge() {
$sql = "DELETE FROM " . static::table_name . " WHERE id = :id"; $sql = "DELETE FROM " . static::tableName . " WHERE id = :id";
DBInstance::execute($sql, Array('id' => $this->id), static::connection_name); DBInstance::execute($sql, Array('id' => $this->id), static::connectionName);
} }
private function fill($data) { private function fill($data) {
@@ -149,7 +149,7 @@ abstract class Entity {
* @param Array $limits Array( 'offset'=> 10, 'limit' => 10 ) * @param Array $limits Array( 'offset'=> 10, 'limit' => 10 )
* @param boolean $trashed Bring trashed elements? * @param boolean $trashed Bring trashed elements?
*/ */
public static function find_all($select = Array('*'), $limits = Array(), $trashed = false) { public static function findAll($select = Array('*'), $limits = Array(), $trashed = false) {
$criteria = ''; $criteria = '';
$limits_sql = ''; $limits_sql = '';
@@ -164,12 +164,12 @@ abstract class Entity {
} }
if (static::softdelete && !$trashed) { if (static::softdelete && !$trashed) {
$sql = "SELECT $criteria FROM " . static::table_name . " WHERE deleted_at is null $limits_sql"; $sql = "SELECT $criteria FROM " . static::tableName . " WHERE deleted_at is null $limits_sql";
} else { } else {
$sql = "SELECT $criteria FROM " . static::table_name . " $limits_sql"; $sql = "SELECT $criteria FROM " . static::tableName . " $limits_sql";
} }
$results = DBInstance::query($sql, Array(), static::connection_name); $results = DBInstance::query($sql, Array(), static::connectionName);
$objects = Array(); $objects = Array();
foreach ($results as $value) { foreach ($results as $value) {
@@ -216,12 +216,12 @@ abstract class Entity {
} }
} }
if (static::softdelete && !$trashed) { if (static::softdelete && !$trashed) {
$sql = "SELECT COUNT(*) FROM " . static::table_name . " $criteria_sql AND deleted_at is null"; $sql = "SELECT COUNT(*) FROM " . static::tableName . " $criteria_sql AND deleted_at is null";
} else { } else {
$sql = "SELECT COUNT(*) FROM " . static::table_name . " $criteria_sql"; $sql = "SELECT COUNT(*) FROM " . static::tableName . " $criteria_sql";
} }
$results = DBInstance::query_one($sql, $criteria_data, static::connection_name); $results = DBInstance::queryOne($sql, $criteria_data, static::connectionName);
return $results->count; return $results->count;
} }
@@ -233,7 +233,7 @@ abstract class Entity {
* @param Array $select Array(id, fullname) * @param Array $select Array(id, fullname)
* @param Boolan $trashed true, false * @param Boolan $trashed true, false
*/ */
public static function find_one($criterias = Array(), $select = Array('*'), $trashed = false) { public static function findOne($criterias = Array(), $select = Array('*'), $trashed = false) {
$select_sql = ""; $select_sql = "";
$criteria_sql = ""; $criteria_sql = "";
$limits_sql = ""; $limits_sql = "";
@@ -275,12 +275,12 @@ abstract class Entity {
} }
if (static::softdelete && !$trashed) { if (static::softdelete && !$trashed) {
$sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql AND deleted_at is null $limits_sql"; $sql = "SELECT $select_sql FROM " . static::tableName . " $criteria_sql AND deleted_at is null $limits_sql";
} else { } else {
$sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql $limits_sql"; $sql = "SELECT $select_sql FROM " . static::tableName . " $criteria_sql $limits_sql";
} }
$results = DBInstance::query($sql, $criteria_data, static::connection_name); $results = DBInstance::query($sql, $criteria_data, static::connectionName);
$objects = Array(); $objects = Array();
foreach ($results as $value) { foreach ($results as $value) {
@@ -304,7 +304,7 @@ abstract class Entity {
* @param boolean $trashed Description * @param boolean $trashed Description
* *
*/ */
public static function find_many($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) { public static function findMany($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) {
$select_sql = ""; $select_sql = "";
$criteria_sql = ""; $criteria_sql = "";
$limits_sql = ""; $limits_sql = "";
@@ -334,12 +334,12 @@ abstract class Entity {
$select_sql = rtrim($select_sql, ', '); $select_sql = rtrim($select_sql, ', ');
if (static::softdelete && !$trashed) { if (static::softdelete && !$trashed) {
$sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql AND deleted_at is null $limits_sql"; $sql = "SELECT $select_sql FROM " . static::tableName . " $criteria_sql AND deleted_at is null $limits_sql";
} else { } else {
$sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql $limits_sql"; $sql = "SELECT $select_sql FROM " . static::tableName . " $criteria_sql $limits_sql";
} }
$results = DBInstance::query($sql, Array(), static::connection_name); $results = DBInstance::query($sql, Array(), static::connectionName);
$objects = Array(); $objects = Array();
foreach ($results as $value) { foreach ($results as $value) {
@@ -372,9 +372,9 @@ abstract class Entity {
* @return Object Instance of the remote class * @return Object Instance of the remote class
* *
*/ */
protected function has_one($foreign_object, $field_in_remote) { protected function hasOne($foreign_object, $field_in_remote) {
$obj = new $foreign_object; $obj = new $foreign_object;
return $obj->find_one(Array($field_in_remote => Array('=', $this->id))); return $obj->findOne(Array($field_in_remote => Array('=', $this->id)));
} }
/** /**
@@ -392,9 +392,9 @@ abstract class Entity {
* @param (int, string) $field_in_foreign The field to match the local id * @param (int, string) $field_in_foreign The field to match the local id
* *
*/ */
protected function has_many($foreign_object, $field_in_foreign) { protected function hasMany($foreign_object, $field_in_foreign) {
$obj = new $foreign_object; $obj = new $foreign_object;
return $obj->find_many(Array($field_in_foreign => Array('=', $this->id))); return $obj->findMany(Array($field_in_foreign => Array('=', $this->id)));
} }
/** /**
@@ -413,9 +413,9 @@ abstract class Entity {
* @param (int, String) $local_field Remote field relate to local Object * @param (int, String) $local_field Remote field relate to local Object
* @param string $remote_field * @param string $remote_field
*/ */
protected function belongs_to($foreign_object, $local_field, $remote_field = 'id') { protected function belongsTo($foreign_object, $local_field, $remote_field = 'id') {
$obj = new $foreign_object; $obj = new $foreign_object;
return $obj->find_one(Array($remote_field => Array('=', $this->$local_field))); return $obj->findOne(Array($remote_field => Array('=', $this->$local_field)));
} }
/** /**
@@ -437,7 +437,7 @@ abstract class Entity {
* @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) ) * @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
* @param Array $remote_limit Array( 'offset'=> 10, 'limit' => 10 ) * @param Array $remote_limit Array( 'offset'=> 10, 'limit' => 10 )
*/ */
protected function belongs_to_many($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) { protected function belongsToMany($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) {
$obj = new $foreign_object; $obj = new $foreign_object;
$limits_sql = ''; $limits_sql = '';
@@ -446,7 +446,7 @@ abstract class Entity {
} }
$sql = "SELECT $remote_in_pivot FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql"; $sql = "SELECT $remote_in_pivot FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
$relations = DBInstance::query($sql, Array(), static::connection_name); $relations = DBInstance::query($sql, Array(), static::connectionName);
$ids = Array(); $ids = Array();
foreach ($relations as $relation) { foreach ($relations as $relation) {
$ids[] = $relation->$remote_in_pivot; $ids[] = $relation->$remote_in_pivot;
@@ -454,7 +454,7 @@ abstract class Entity {
if (empty($ids)) { if (empty($ids)) {
return Array(); return Array();
} }
return $obj->find_many(array_merge(Array('id' => Array('IN', $ids)), $remote_filter), Array('*'), $remote_limit); return $obj->findMany(array_merge(Array('id' => Array('IN', $ids)), $remote_filter), Array('*'), $remote_limit);
} }
/** /**
@@ -477,7 +477,7 @@ abstract class Entity {
* @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) ) * @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
* @param Array $pivot_limits Array( 'offset'=> 10, 'limit' => 10 ) * @param Array $pivot_limits Array( 'offset'=> 10, 'limit' => 10 )
*/ */
protected function belongs_to_many_extended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limits = Array()) { protected function belongsToManyExtended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limits = Array()) {
$obj = new $foreign_object; $obj = new $foreign_object;
$limits_sql = ''; $limits_sql = '';
foreach ($pivot_limits as $key => $value) { foreach ($pivot_limits as $key => $value) {
@@ -485,14 +485,14 @@ abstract class Entity {
} }
$sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql"; $sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
$relations = DBInstance::query($sql, Array(), static::connection_name); $relations = DBInstance::query($sql, Array(), static::connectionName);
$objects = Array(); $objects = Array();
if (empty($relations)) { if (empty($relations)) {
return Array(); return Array();
} }
foreach ($relations as $relation) { foreach ($relations as $relation) {
$relation->child_element = $obj->find_one(array_merge(Array("id" => Array("=", $relation->$remote_in_pivot)), $remote_filter)); $relation->child_element = $obj->findOne(array_merge(Array("id" => Array("=", $relation->$remote_in_pivot)), $remote_filter));
$objects[] = $relation; $objects[] = $relation;
} }
if (empty($objects)) { if (empty($objects)) {