-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Dbhelper Library
Category:Libraries | Category:Libraries::Community | Category:Libraries::Database This class is intend to help you crafting an SQL statement with funky way.
[h3]FEATURES[/h3]
- Support SELECT, INSERT, UPDATE, DELETE Statement
- Support chaining and nested WHERE.
- Some shorthand for SELECT, for now : CONCAT and DISTINCT.
- Using native SQL statement.
- Caching query.
- Chaining method (SELECT, FROM, WHERE, ORDER BY).
- Choose output : execute or just return the array of query.
- Log and error flag.
[h3]DOWNLOAD[/h3] File:DbhelperLibrary_v1.0.2.zip [LATEST] File:DbhelperLibrary_v1.0.1.zip
[h3]VERSION HISTORY AND DESCRIPTION[/h3] [i][from version 1.0.1][/i] USAGE : [code] /* SELECT statement */ $this->load->library('dbhelper'); // Save SQL generated statement to any variable, just to see it. $query = $this->dbhelper->select_from_sometable()->query(); var_dump($query); // Execute and save it into some result variable $res = $this->dbhelper->select_from_sometable()->execute(); var_dump($res); // Select specific field only, with Limit parameter $res = $this->dbhelper->select_somefield_from_sometable(10)->execute(); var_dump($res); // Select with specific Where parameter $res = $this->dbhelper->select_somefield_from_sometable_where_id(1)->execute(); var_dump($res); // Select with Where OR and Where AND parameter $res = $this->dbhelper->select_somefield_from_sometable_where_or(array('id' => 1, 'name' => 'John Doe'))->execute(); var_dump($res); $res = $this->dbhelper->select_from_sometable_where_and(array('group_id' => 1, 'active' => 1))->execute(); var_dump($res);
/* INSERT statement */ $this->load->library('dbhelper'); // Save SQL generated statement to any variable, just to see it. $query = $this->dbhelper->insert_into_sometable_values(array('foo' => 'bar'))->query(); var_dump($query); // Execute and save it into some result variable $res = $this->dbhelper->insert_into_sometable_values(array('foo' => 'bar'))->execute(); var_dump($res); // Notice that you can specifiy any tables, including table name with '_' character in it. $res = $this->dbhelper->insert_into_my_other_table_which_had_so_long_names_values(array('foo' => 'bar'))->execute(); var_dump($res);
/* UPDATE statement */ $this->load->library('dbhelper'); // Save SQL generated statement to any variable, just to see it. $query = $this->dbhelper->update_sometable_set(array('active' => 1))->query(); var_dump($query); // Execute and save it into some result variable $res = $this->dbhelper->update_sometable_set(array('active' => 1))->execute(); var_dump($res);
/* DELETE statement. */ $this->load->library('dbhelper'); // Save SQL generated statement to any variable, just to see it. $query = $this->dbhelper->delete_from_sometable_where_id(1)->query(); var_dump($query); // Delete with specific Where parameter $res = $this->dbhelper->delete_from_sometable_where_id(1)->execute(); var_dump($res); // Delete with Where OR and Where AND parameter $res = $this->dbhelper->delete_from_sometable_where_or(array('id' => 1, 'name' => 'John Doe'))->execute(); var_dump($res); $res = $this->dbhelper->delete_from_sometable_where_and(array('group_id' => 1, 'active' => 1))->execute(); var_dump($res); [/code]
[i][from version 1.0.2][/i] [b]MORE FEATURES(All old feature still there):[/b]
- More available chaining method (SELECT, FROM, WHERE, ORDER BY).
- Some shorthand for SELECT, for now : CONCAT and DISTINCT.
- Support chaining and nested WHERE.
[code] $this->load->library('dbhelper'); // Simple chain method $res = $this->dbhelper->select_all() ->_from('sometable') ->_where_open() ->_where(array('foo','=','bar')) ->_where_close() ->query(); var_dump($res); // Will generates "SELECT * FROM sometable WHERE ( foo = 'bar' )" // Another chain method, but with nested WHERE and using some concat shorthand $res = $this->dbhelper->select_concat('name,(,email,)') ->_fields('username, active') ->_from('sometable') ->_where_open() ->_where(array('foo','=','bar')) ->_and() ->_where_or(array('active' => 1, 'confirmed' => 1)) ->_where_close() ->_order_by('last_login') ->query(); var_dump($res); // Will generates "SELECT CONCAT(name,'(',email,')'), username, active FROM sometable WHERE ( foo = 'bar' AND (active = '1' OR confirmed = '1' ) )" [/code]
[i]Hope you find this object useful, Cheers![/i] Taufan Aditya A.K.A [b]Toopay[/b]