Skip to content

welllog/DB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Welcome to Odb

The DB class is a simple query builder class in PHP,it based on PDO,and require php version > 7.1.

Usage

First load your database config

\Odb\DB::loadConfig("DBConf.php");

database config like this:

return [
    'default' => [     // default must exists
        'driver' => 'mysql',   // pgsql(postgresql)
        'host' => '127.0.0.1',
        'port' => '3306',
        'username' => '',
        'password' => '',
        'dbname' => '',
        'charset' => 'utf8',
        'pconnect' => false,
        'time_out' => 3,
        'prefix' => '',
        'throw_exception' => true
    ]
];

We use the default "default" connection, so the default configuration is required. Then you have to instantiate the class like this

\Odb\DB::connect()->getDBVersion();

Switching other connection configurations

\Odb\DB::connect('default')->query(...)->get();

Insert values to a table

Insert a record

\Odb\DB::table('user')->insert(['name' => 'jack', 'age' => 24]);

Insert multiple records

\Odb\DB::table('user')->insert([['name' => 'linda', 'age' => 21], ['name' => 'bob', 'age' => 24]]);

Get insert record id

$id = \Odb\DB::table('user')->insertGetId(['name' => 'ailan', 'age' => 21]);

Restrict inserted columns

\Odb\DB::table('user')->allow('name', 'age')->insert(['name' => 'jack', 'age' => 24, 'job' => 'programmer']);

To see the SQL query that have executed, use the getSQL() Method like this:

echo \Odb\DB::table('user')->buildInsert(['name' => 'jack', 'age' => 24])->getSQL();

Output :

'insert into `user` (`name`,`age`) values (?,?)'
echo \Odb\DB::table('user')->buildInsert(['name' => 'jack', 'age' => 24])->getRSql();

Output :

'insert into `user` (`name`,`age`) values ('jack', 24)'

Update table values

\Odb\DB::table('user')->where('id', 1)->update(['name' => 'remi']);

SQL Query :

'update `user` set `name`=? where `id`=?'

Increased

\Odb\DB::table('scores')->where('id', 10)->increment('score');
\Odb\DB::table('scores')->where('id', '=', 10)->increment('score', 5);
\Odb\DB::table('scores')->where('id', '=', 10)->increment([['score', 1], ['level', 9]]);

Decrement

\Odb\DB::table('scores')->where('id', '=', 10)->decrement('score');
\Odb\DB::table('scores')->where('id', '=', 10)->decrement('score', 2);
\Odb\DB::table('scores')->where('id', '=', 10)->decrement([['score', 2], ['level', 1]]);

notice

If there is no where,changes will not happened

Delete values from table

\Odb\DB::table('logs')->where([['id', '>', 9], ['level', '<', 2]])->delete();

notice

If there is no where,nothing be deleted

Selection

$rows = \Odb\DB::table('user')->get();

It returns a Two-dimensional array Output :

[
    ['name' => 'jack', 'age' => 21],
    ['name' => 'bob', 'age' => 23]
]

Get a row record

$user = \Odb\DB::table('user')->first();

It returns a one-dimensional array

['name' => 'jack', 'age' => 21]

Get a list of records

$userNames = \Odb\DB::table("users")->pluck('username');
$userNames = \Odb\DB::table("users")->pluck('username', 'id');

It returns a one-dimensional array

['job', 'jack']
[1 => 'job', 2 => 'jack']

get a field value

echo \Odb\DB::table('users')->where('id', '=', 2)->value('username');  // 'jack'

Aggregate function

\Odb\DB::table('user')->max('age');
\Odb\DB::table('user')->min('age');
\Odb\DB::table('user')->sum('age');
\Odb\DB::table('user')->count();
\Odb\DB::table('user')->avg('age');

use where

\Odb\DB::table('user')->where('id', 10)->where('level', '=', 5)->get();
\Odb\DB::table('user')->where([['id', '>', 10], ['level', '=', 5]])->orWhere('status', '=', 0)->get();
\Odb\DB::table('user')->where([['id', '>', 10], ['level', '=', 5]])->orWhere('status', '=', 0)
    ->orWhere(function ($query) {
        $query->whereNull('username');
    })->get();

where function

whereNull('username');
whereNotNull('username');
whereIn('id', [1, 2, 3]);
whereNotIn('id', [1, 2, 3]);
whereBetween('id', [1, 9]);
whereNotBetween('id', [1, 9]);
whereColumn('id', '>', 'parent_id');
where('`username`=? and `age`>?', ['job', 23]);
where('username', '=', 'job')->where('age', '>', 23);
where([['username', '=', 'job'], ['age', '>', 23]]);
whereRaw('`id`>? and `status`=?', [10, 1]);

use select

filter columns

\Odb\DB::table('users')->select('id', 'username', 'age')->get();
\Odb\DB::table('users')->select(['id', 'username', 'age'])->get();
\Odb\DB::table('users')->select(['id', 'username', 'age', 'sum(score) as total'])->get();

use join

join function

\Odb\DB::table('user as u')->join('article as a', 'u.id', '=', 'a.uid')->select('u.*', 'a.commend')->get();

SQL Query :

'select  `test_u`.`*`,`test_a`.`commend` from `test_user` as `test_u`  inner join `test_article` as `test_a` on `test_u`.id=`test_a`.uid'

you can olso use 'leftJoin' and 'rightJoin' and native sql

\Odb\DB::table('user')->leftJoin('role', 'user.role_id', '=', 'role.id')->rightJoin('posts', 'uid', '=', 'user.id');
\Odb\DB::table('user')->join('role', 'test_user.role_id=test_role.id and test_role.status>?', [1]);

order by,group by...

\Odb\DB::table('user')->orderBy('id', 'desc')->get();
\Odb\DB::table('user')->orderBy('id', 'asc')->get();
\Odb\DB::table('user')->select('count(id) as num')->groupBy('team_id')->having('num', '>', 2)->get();
\Odb\DB::table('user')->select('count(id) as num')->groupBy('team_id')->having('`num`>?', [2])->get();
\Odb\DB::table('user')->limit(2)->get();
\Odb\DB::table('user')->limit(5, 2)->get();

transaction

oringfy/DB provide transaction common API

\Odb\DB::beginTrans();
\Odb\DB::inTrans();
\Odb\DB::rollBack();
\Odb\DB::commit();

native sql

orinfy/DB also support native sql

\Odb\DB::exec('delete from test_user');
\Odb\DB::query('select * from test_user')->get();
\Odb\DB::prepare('select * from test_user where id>?')->execute([10])->get();
\Odb\DB::prepare('update test_user set username=?')->execute(['aiwa'])->rowCount();
\Odb\DB::prepare('insert into test_user (username) values(?)')->execute(['aiwa'])->lastInsertId();
tips

If you have any questions please contact orinfy@foxmail.com

About

a lightweight database operation class based on php pdo

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages