Skip to content

Commit

Permalink
Implemented new autoload method and changed connection method to a ne…
Browse files Browse the repository at this point in the history
…w and more efficient one
  • Loading branch information
emagombe committed Dec 6, 2019
1 parent 92613e5 commit 89fd15a
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 48 deletions.
10 changes: 10 additions & 0 deletions Examples/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

use queryBuilder\JsonQB as JQB;

JQB::connect([
'database' => '',
'host' => '',
'port' => '',
'username' => '',
'password' => '',
'charset' => '',
]);


$sql = JQB::Delete('user', [
'where' => array(
array(
Expand Down
9 changes: 9 additions & 0 deletions Examples/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
require '../autoload.php';


JQB::connect([
'database' => '',
'host' => '',
'port' => '',
'username' => '',
'password' => '',
'charset' => '',
]);


// $qb = JsonQB::Insert('user', [
// 'value' => [
Expand Down
9 changes: 9 additions & 0 deletions Examples/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

use queryBuilder\JsonQB as JQB;

JQB::connect([
'database' => '',
'host' => '',
'port' => '',
'username' => '',
'password' => '',
'charset' => '',
]);

$sql = JQB::Insert('user',
array(
"value" => array(
Expand Down
10 changes: 10 additions & 0 deletions Examples/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

use queryBuilder\JsonQB as JQB;

JQB::connect([
'database' => '',
'host' => '',
'port' => '',
'username' => '',
'password' => '',
'charset' => '',
]);


$sql = JQB::Select(array(
"columns" => array("user.*", "user_type.*"),
"from" => array("user", "user_type"),
Expand Down
10 changes: 10 additions & 0 deletions Examples/Truncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

use queryBuilder\JsonQB as JQB;

JQB::connect([
'database' => '',
'host' => '',
'port' => '',
'username' => '',
'password' => '',
'charset' => '',
]);


$sql = JQB::Truncate('user')->sql();

print_r($sql);
Expand Down
9 changes: 9 additions & 0 deletions Examples/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

use queryBuilder\JsonQB as JQB;

JQB::connect([
'database' => '',
'host' => '',
'port' => '',
'username' => '',
'password' => '',
'charset' => '',
]);

$sql = JQB::Update('user', [
'value' => array(
'username' => 'example'
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ require_once 'JsonQB/autoload.php';

## Database settings

Setput your database connetion settings on **JsonQB/app.conf** file
```script
DATABASE=your_database
HOST=your_host
PORT=3306
USERNAME=your_username
PASSWORD=your_password
CHARSET=utf8
```php
use queryBuilder\JsonQB as JQB;

JQB::connect([
'database' => '', # Database name
'host' => '', # Host name
'port' => '', # Connection port
'username' => '', # Username
'password' => '', # Password
'charset' => '', # Charset
]);
```

## Generating queries
Expand Down
6 changes: 0 additions & 6 deletions app.conf

This file was deleted.

17 changes: 6 additions & 11 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
<?php
<?php

require_once 'database/Settings.php';
require_once 'database/Database.php';
require_once 'security/Security.php';
require_once 'queryBuilder/query/Where.php';
require_once 'queryBuilder/query/Insert.php';
require_once 'queryBuilder/query/Update.php';
require_once 'queryBuilder/query/Delete.php';
require_once 'queryBuilder/query/Select.php';
require_once 'queryBuilder/query/Truncate.php';
require_once 'queryBuilder/JsonQB.php';
require_once __DIR__.'/vendor/autoload.php';

spl_autoload_register(function ($class_name) {
require_once __DIR__.'/'.$class_name .'.php';
});
29 changes: 6 additions & 23 deletions database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
use database\Settings;
use PDO;

class Database
{
class Database {

function __construct() {
return $this;
public static $connection_object = [];

public static function get_connection_object($connection_object) {
self::$connection_object = $connection_object;
}

public function conn() {
$config = $this->read_conf();
$config = (object) self::$connection_object;

$host = $config->host;
$database = $config->database;
Expand All @@ -29,22 +30,4 @@ public function conn() {
throw new Exception($ex->getMessage());
}
}

private function read_conf() {
$content = file_get_contents(dirname(__FILE__)."/../app.conf");
$content = explode("\n", $content);
$config = [];
foreach($content as $item) {
$items = explode("=", $item);
$config[strtolower($items[0])] = $items[1];
}
$settings = new Settings();
$settings->database = str_replace("\r", '', $config['database']);
$settings->username = str_replace("\r", '', $config['username']);
$settings->password = str_replace("\r", '', $config['password']);
$settings->charset = str_replace("\r", '', $config['charset']);
$settings->host = str_replace("\r", '', $config['host']);
$settings->port = str_replace("\r", '', $config['port']);
return $settings;
}
}
5 changes: 5 additions & 0 deletions queryBuilder/JsonQB.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace queryBuilder;

use database\Database;
use security\Security;
use queryBuilder\query\Insert;
use queryBuilder\query\Update;
Expand All @@ -11,6 +12,10 @@

class JsonQB {

public static function connect($connection_object) {
Database::get_connection_object($connection_object);
}

public static function Insert($table, $request) {
return new Insert($table, $request);
}
Expand Down

0 comments on commit 89fd15a

Please sign in to comment.