Skip to content
This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
/ xpdo Public archive

Abstração de banco de dados e classe PHP auxiliar, bem simples.

Notifications You must be signed in to change notification settings

humolot/xpdo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

xPdo Documentação

Instalar

Incluir a classe xPdo.php em seu projeto.

require_once("xPdo.php");

Uso Rápido

require 'xPdo.php';

$config = [ 'host' => 'localhost', 'driver' => 'mysql', 'database' => 'test', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => '' ];

$db = new xPdo($config);

Parabéns! Agora você pode usar o xPdo.

Se você tiver um problema, você pode entrar em contato comigo.

Uso Detalhado e Métodos

Conteúdo

Methods

encode/decode THOR

# Enable openssl 
# Usage : string parameter, secret key
$db->thor('encode', 'String', 'Secret Key');
$db->thor('decode', 'String', 'Secret Key');

Output: hash sha256

generate radom hash

# Usage : string parameter size to generate
$db->get_hash('48');

Output: hash

get lat and lng google maps

# Usage : string parameter address
$db->getLatLong('São Paulo, SP, Brazil');

Output: array latitude and longitude

limit text

# Usage : string parameter and number limit characters
$db->text_limit('String','60');

Output: text limited

date and datetime

# Usage : date and format / en = yyyy-mm-dd / br = dd/mm/yyyy
$db->date('01/01/2018','en');
# Output: 2018-01-01

$db->datetime('01/01/2018 12:00:00','en');

Output: 2018-01-01 12:00:00

$db->date('2018-01-01','br');

Output: 01/01/2018

$db->datetime('01/01/2018 12:00:00','br');

Output: 01/01/2018 12:00:00

select

# Usage 1: string parameter
$db->select('title, content');
$db->select('title AS t, content AS c');

Usage2: array parameter

$db->select(['title', 'content']); $db->select(['title AS t', 'content AS c']);

select functions (min, max, sum, avg, count)

# Usage 1:
$db->table('test')->max('price');

Output: "SELECT MAX(price) FROM test"

Usage 2:

$db->table('test')->count('id', 'total_row');

Output: "SELECT COUNT(id) AS total_row FROM test"

table

# Usage 1: string parameter
$db->table('table');
$db->table('table1, table2');
$db->table('table1 AS t1, table2 AS t2');

Usage2: array parameter

$db->table(['table1', 'table2']); $db->table(['table1 AS t1', 'table2 AS t2']);

get AND getAll

# get(): return 1 record.
# getAll(): return multiple records.

$db->table('test')->getAll(); // " SELECT * FROM test " $db->select('username')->table('users')->where('status', 1)->getAll(); // " SELECT username FROM users WHERE status = '1' "

$db->select('title')->table('pages')->where('id', 17)->get(); // " SELECT title FROM pages WHERE id = '17' LIMIT 1 "

Results

$records = $db->table('test')->getAll(); foreach ((array) $records as $record) { echo $record->title; }

join

# Usage 1:
$db->table('foo')->join('bar', 'foo.field', 'bar.field')->getAll();
$db->table('foo')->leftJoin('bar', 'foo.field', 'bar.field')->getAll();
$db->table('foo')->rightJoin('bar', 'foo.field', 'bar.field')->get();
$db->table('foo')->innerJoin('bar', 'foo.field', 'bar.field')->get();

Usage 2:

$db->table('foo')->join('bar', 'foo.field', '=', 'bar.field')->getAll(); $db->table('foo')->leftJoin('bar', 'foo.field', '=', 'bar.field')->getAll(); $db->table('foo')->rightJoin('bar', 'foo.field', '=', 'bar.field')->get(); $db->table('foo')->innerJoin('bar', 'foo.field', '=', 'bar.field')->get();

where - orWhere

# Usage 1: array parameter
$where = [
    'name' => 'Burak',
    'age' => 23,
    'status' => 1
];
$db->where($where);

Usage 2:

$db->where('status', 2); $db->where('status', 1)->where('name', 'burak'); $db->where('status', 1)->orWhere('status', 2);

Usage 3:

$db->where('age', '>', 20); $db->where('age', '>', 20)->orWhere('age', '<', 30);

Usage 4:

$db->where('status = ? AND age = ?', [1, 20]); $db->where('status = ? AND title = ?', [0, 'example title']);

grouped

$db->table('users')
    ->grouped(function($q) {
        $q->where('country', 'TURKEY')->orWhere('country', 'ENGLAND');
    })
    ->where('status', 1)
    ->getAll();

$key = 10; $db->table('users') ->grouped(function($q) use ($key) { $q->where('key_field', $key)->orWhere('status', 0); }) ->where('status', 1) ->getAll();

in - notIn - orIn - orNotIn

$db->in('page', ['about', 'contact', 'products']);
$db->orIn('id', [1, 2, 3]);
$db->notIn('age', [20, 21, 22, 23]);
$db->orNotIn('age', [30, 31, 32, 32]);

between - orBetween - notBetween - orNotBetween

$db->between('age', 10, 20);
$db->orBetween('age', 20, 30);
$db->notBetween('year', 2010, 2015);
$db->orNotBetween('year', 2005, 2009);

like - orLike

$db->like('title', '%burak%');      // " title LIKE '%burak%' "
$db->like('title', 'humolot%');   // " title LIKE 'humolot%' "
$db->like('title', '%humolot');   // " title LIKE '%humolot' "

$db->like('tag', '%php%')->orLike('tag', '%web%'); $db->like('tag', '%php%')->orLike('tag', 'web%'); $db->like('tag', '%php%')->orLike('tag', '%web');

groupBy

# Usage 1: string parameter
$db->groupBy('country');
$db->groupBy('country, city');

Usage 2: array parameter

$db->groupBy(['country', 'city']);

having

$db->having('AVG(price)', 2000);    // " AVG(price) > 2000 "
$db->having('AVG(price)', '>=', 3000);  // " AVG(price) >= 3000 "
$db->having('SUM(age) <= ?', [50]); // " SUM(age) <= 50 "

orderBy

$db->orderBy('id'); // " ORDER BY id ASC
$db->orderBy('id DESC');
$db->orderBy('id', 'desc');
$db->orderBy('rand()'); // " ORDER BY rand() "

limit

$db->limit(10);     // " LIMIT 10 "
$db->limit(10, 20); // " LIMIT 10, 20 "

insert

$data = array(
    'title' => 'test',
    'content' => 'Lorem ipsum dolor sit amet...',
    'time' => time(),
    'status' => 1
);

OR

$data = array(); $data['title'] = 'test'; $data['content'] = 'Lorem ipsum dolor sit amet...'; $data['time'] = time(); $data['status'] = 1;

OR

$data = [ 'title' => 'test', 'content' => 'Lorem ipsum dolor sit amet...', 'time' => time(), 'status' => 1 ];

$db->table('pages')->insert($data);

update

$data = array(
    'username' => 'humolot',
    'password' => md5('demo-password'),
    'activation' => 1,
    'status' => 1
);

OR

$data = array(); $data['username'] = 'humolot'; $data['password'] = md5('demo-password'); $data['activation'] = 1; $data['status'] = 1;

OR

$data = [ 'username' => 'humolot', 'password' => md5('demo-password'), 'activation' => 1, 'status' => 1 ];

$db->table('users')->where('id', 10)->update($data);

delete

$db->table('users')->where('id', 5)->delete();

analyze

$query = $db->table('users')->analyze();
var_dump($query);

Output:

"ANALYZE TABLE users"

check

$query = $db->table(['users', 'pages'])->check();
var_dump($query);

Output:

"CHECK TABLE users, pages"

checksum

$query = $db->table(['users', 'pages'])->checksum();
var_dump($query);

Output:

"CHECKSUM TABLE users, pages"

optimize

$query = $db->table(['users', 'pages'])->optimize();
var_dump($query);

Output:

"OPTIMIZE TABLE users, pages"

repair

$query = $db->table(['users', 'pages'])->repair();
var_dump($query);

Output:

"REPAIR TABLE users, pages"

query

$ds = $db->query("SELECT * FROM test WHERE id = '10' AND status = '1'");

OR

$db->query('SELECT * FROM test WHERE id = ? AND status = ?', [10, 1]);

insertId

$data = array(
    'title' => 'test',
    'content' => 'Lorem ipsum dolor sit amet...',
    'time' => time(),
    'status' => 1
);

OR

$data = array(); $data['title'] = 'test'; $data['content'] = 'Lorem ipsum dolor sit amet...'; $data['time'] = time(); $data['status'] = 1;

OR

$data = [ 'title' => 'test', 'content' => 'Lorem ipsum dolor sit amet...', 'time' => time(), 'status' => 1 ];

$db->table('pages')->insert($data);

var_dump($db->insertId());

numRows

$db->select('id, title')->table('test')->where('status', 1)->orWhere('status', 2)->getAll();

var_dump($db->numRows());

error

$db->error();

cache

# Usage: ...->cache($time)->...
$db->table('pages')->where('slug', 'example-page.html')->cache(60)->get(); // cache time: 60 seconds

queryCount

$db->queryCount(); // The number of all SQL queries on the page until the end of the beginning.

getQuery

$db->getQuery(); // Last SQL Query.

About

Abstração de banco de dados e classe PHP auxiliar, bem simples.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages