{"id":20762540,"url":"https://github.com/welllog/db","last_synced_at":"2025-04-30T07:08:55.814Z","repository":{"id":57033057,"uuid":"135605587","full_name":"welllog/DB","owner":"welllog","description":"a lightweight database operation class based on php pdo","archived":false,"fork":false,"pushed_at":"2020-07-13T14:00:54.000Z","size":122,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-30T07:08:48.274Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/welllog.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-31T15:55:33.000Z","updated_at":"2020-07-13T13:16:26.000Z","dependencies_parsed_at":"2022-08-23T20:50:23.757Z","dependency_job_id":null,"html_url":"https://github.com/welllog/DB","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/welllog%2FDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/welllog%2FDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/welllog%2FDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/welllog%2FDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/welllog","download_url":"https://codeload.github.com/welllog/DB/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251658212,"owners_count":21622822,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-17T10:36:12.216Z","updated_at":"2025-04-30T07:08:55.783Z","avatar_url":"https://github.com/welllog.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Welcome to Odb\nThe DB class is a simple query builder class in PHP,it based on PDO,and require php version \u003e 7.1.\n\n\n## Usage\nFirst load your database config\n```php\n\\Odb\\DB::loadConfig(\"DBConf.php\");\n```\ndatabase config like this:\n```php\nreturn [\n    'default' =\u003e [     // default must exists\n        'driver' =\u003e 'mysql',   // pgsql(postgresql)\n        'host' =\u003e '127.0.0.1',\n        'port' =\u003e '3306',\n        'username' =\u003e '',\n        'password' =\u003e '',\n        'dbname' =\u003e '',\n        'charset' =\u003e 'utf8',\n        'pconnect' =\u003e false,\n        'time_out' =\u003e 3,\n        'prefix' =\u003e '',\n        'throw_exception' =\u003e true\n    ]\n];\n```\nWe use the default \"default\" connection, so the default configuration is required.\nThen you have to instantiate the class like this\n```php\n\\Odb\\DB::connect()-\u003egetDBVersion();\n```\nSwitching other connection configurations\n```php\n\\Odb\\DB::connect('default')-\u003equery(...)-\u003eget();\n```\n### Insert values to a table\nInsert a record\n```php\n\\Odb\\DB::table('user')-\u003einsert(['name' =\u003e 'jack', 'age' =\u003e 24]);\n```\nInsert multiple records\n```php\n\\Odb\\DB::table('user')-\u003einsert([['name' =\u003e 'linda', 'age' =\u003e 21], ['name' =\u003e 'bob', 'age' =\u003e 24]]);\n```\nGet insert record id\n```php\n$id = \\Odb\\DB::table('user')-\u003einsertGetId(['name' =\u003e 'ailan', 'age' =\u003e 21]);\n```\nRestrict inserted columns\n```php\n\\Odb\\DB::table('user')-\u003eallow('name', 'age')-\u003einsert(['name' =\u003e 'jack', 'age' =\u003e 24, 'job' =\u003e 'programmer']);\n```\nTo see the SQL query that have executed, use the `getSQL()` Method like this:\n```php\necho \\Odb\\DB::table('user')-\u003ebuildInsert(['name' =\u003e 'jack', 'age' =\u003e 24])-\u003egetSQL();\n```\nOutput :\n```sql\n'insert into `user` (`name`,`age`) values (?,?)'\n```\n```php\necho \\Odb\\DB::table('user')-\u003ebuildInsert(['name' =\u003e 'jack', 'age' =\u003e 24])-\u003egetRSql();\n```\nOutput :\n```sql\n'insert into `user` (`name`,`age`) values ('jack', 24)'\n```\n### Update table values\n```php\n\\Odb\\DB::table('user')-\u003ewhere('id', 1)-\u003eupdate(['name' =\u003e 'remi']);\n```\nSQL Query :\n```sql\n'update `user` set `name`=? where `id`=?'\n```\nIncreased\n```php\n\\Odb\\DB::table('scores')-\u003ewhere('id', 10)-\u003eincrement('score');\n\\Odb\\DB::table('scores')-\u003ewhere('id', '=', 10)-\u003eincrement('score', 5);\n\\Odb\\DB::table('scores')-\u003ewhere('id', '=', 10)-\u003eincrement([['score', 1], ['level', 9]]);\n```\nDecrement\n```php\n\\Odb\\DB::table('scores')-\u003ewhere('id', '=', 10)-\u003edecrement('score');\n\\Odb\\DB::table('scores')-\u003ewhere('id', '=', 10)-\u003edecrement('score', 2);\n\\Odb\\DB::table('scores')-\u003ewhere('id', '=', 10)-\u003edecrement([['score', 2], ['level', 1]]);\n```\n#### notice\n**If there is no where，changes will not happened**\n\n### Delete values from table\n```php\n\\Odb\\DB::table('logs')-\u003ewhere([['id', '\u003e', 9], ['level', '\u003c', 2]])-\u003edelete();\n```\n#### notice\n**If there is no where，nothing be deleted**\n\n### Selection \n```php\n$rows = \\Odb\\DB::table('user')-\u003eget();\n```\nIt returns a Two-dimensional array\nOutput : \n```plain\n[\n    ['name' =\u003e 'jack', 'age' =\u003e 21],\n    ['name' =\u003e 'bob', 'age' =\u003e 23]\n]\n```\nGet a row record\n ```php\n$user = \\Odb\\DB::table('user')-\u003efirst();\n```\nIt returns a one-dimensional array\n```plain\n['name' =\u003e 'jack', 'age' =\u003e 21]\n```\nGet a list of records\n```php\n$userNames = \\Odb\\DB::table(\"users\")-\u003epluck('username');\n$userNames = \\Odb\\DB::table(\"users\")-\u003epluck('username', 'id');\n```\nIt returns a one-dimensional array\n```plain\n['job', 'jack']\n[1 =\u003e 'job', 2 =\u003e 'jack']\n```\nget a field value\n```php\necho \\Odb\\DB::table('users')-\u003ewhere('id', '=', 2)-\u003evalue('username');  // 'jack'\n```\nAggregate function\n```php\n\\Odb\\DB::table('user')-\u003emax('age');\n\\Odb\\DB::table('user')-\u003emin('age');\n\\Odb\\DB::table('user')-\u003esum('age');\n\\Odb\\DB::table('user')-\u003ecount();\n\\Odb\\DB::table('user')-\u003eavg('age');\n```\n#### use where \n```php\n\\Odb\\DB::table('user')-\u003ewhere('id', 10)-\u003ewhere('level', '=', 5)-\u003eget();\n\\Odb\\DB::table('user')-\u003ewhere([['id', '\u003e', 10], ['level', '=', 5]])-\u003eorWhere('status', '=', 0)-\u003eget();\n\\Odb\\DB::table('user')-\u003ewhere([['id', '\u003e', 10], ['level', '=', 5]])-\u003eorWhere('status', '=', 0)\n    -\u003eorWhere(function ($query) {\n        $query-\u003ewhereNull('username');\n    })-\u003eget();\n```\nwhere function\n```php\nwhereNull('username');\nwhereNotNull('username');\nwhereIn('id', [1, 2, 3]);\nwhereNotIn('id', [1, 2, 3]);\nwhereBetween('id', [1, 9]);\nwhereNotBetween('id', [1, 9]);\nwhereColumn('id', '\u003e', 'parent_id');\nwhere('`username`=? and `age`\u003e?', ['job', 23]);\nwhere('username', '=', 'job')-\u003ewhere('age', '\u003e', 23);\nwhere([['username', '=', 'job'], ['age', '\u003e', 23]]);\nwhereRaw('`id`\u003e? and `status`=?', [10, 1]);\n```\n#### use select\nfilter columns\n```php\n\\Odb\\DB::table('users')-\u003eselect('id', 'username', 'age')-\u003eget();\n\\Odb\\DB::table('users')-\u003eselect(['id', 'username', 'age'])-\u003eget();\n\\Odb\\DB::table('users')-\u003eselect(['id', 'username', 'age', 'sum(score) as total'])-\u003eget();\n```\n#### use join\njoin function\n```php\n\\Odb\\DB::table('user as u')-\u003ejoin('article as a', 'u.id', '=', 'a.uid')-\u003eselect('u.*', 'a.commend')-\u003eget();\n```\nSQL Query :\n```sql\n'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'\n```\nyou can olso use 'leftJoin' and 'rightJoin' and native sql\n```php\n\\Odb\\DB::table('user')-\u003eleftJoin('role', 'user.role_id', '=', 'role.id')-\u003erightJoin('posts', 'uid', '=', 'user.id');\n\\Odb\\DB::table('user')-\u003ejoin('role', 'test_user.role_id=test_role.id and test_role.status\u003e?', [1]);\n```\n#### order by,group by...\n```php\n\\Odb\\DB::table('user')-\u003eorderBy('id', 'desc')-\u003eget();\n\\Odb\\DB::table('user')-\u003eorderBy('id', 'asc')-\u003eget();\n\\Odb\\DB::table('user')-\u003eselect('count(id) as num')-\u003egroupBy('team_id')-\u003ehaving('num', '\u003e', 2)-\u003eget();\n\\Odb\\DB::table('user')-\u003eselect('count(id) as num')-\u003egroupBy('team_id')-\u003ehaving('`num`\u003e?', [2])-\u003eget();\n\\Odb\\DB::table('user')-\u003elimit(2)-\u003eget();\n\\Odb\\DB::table('user')-\u003elimit(5, 2)-\u003eget();\n```\n### transaction\noringfy/DB provide transaction common API\n```php\n\\Odb\\DB::beginTrans();\n\\Odb\\DB::inTrans();\n\\Odb\\DB::rollBack();\n\\Odb\\DB::commit();\n```\n### native sql\norinfy/DB also support native sql\n```php\n\\Odb\\DB::exec('delete from test_user');\n\\Odb\\DB::query('select * from test_user')-\u003eget();\n\\Odb\\DB::prepare('select * from test_user where id\u003e?')-\u003eexecute([10])-\u003eget();\n\\Odb\\DB::prepare('update test_user set username=?')-\u003eexecute(['aiwa'])-\u003erowCount();\n\\Odb\\DB::prepare('insert into test_user (username) values(?)')-\u003eexecute(['aiwa'])-\u003elastInsertId();\n```\n\n###### tips\nIf you have any questions please contact orinfy@foxmail.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwelllog%2Fdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwelllog%2Fdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwelllog%2Fdb/lists"}