Giter VIP home page Giter VIP logo

howdy_qb's Introduction

WP Query Builder

Relational Database Query builder for WordPress. WP Query Builder uses PDO for database queries. It has zero dependencies with third-party query builders or any other composer library.


Documentation

Documentation website here.
This Query Builder is also used in Kathamo Framework. Kathamo is a modern framework for WordPress plugin development.


Examples

Create Table

DB::create('querybuilder')
	->column('ID')->bigInt()->unsigned()->autoIncrement()->primary()->required()
	->column('user_id')->bigInt()->unsigned()->required()
	->column('name')->string(255)->required()
	->column('email')->string(255)->nullable()
	->foreignKey('user_id', 'users.ID', 'cascade')
	->index(['ID'])
	->execute();

Insert Statement

DB::insert('querybuilder', [
    [
        'name' => 'Keramot UL Islam',
        'email' => '[email protected]',
    ]
]);

Update Statement

DB::update('querybuilders', [
    'name' => 'Keramot UL',
    'email' => '[email protected]'
])
->where('ID', '=', 10)
->andWhere('name', '=', 'Abm Sourav')
->execute();

Select Statement

$result =
DB::select('qb.ID', 'qb.name, qb.email')
    ->from('querybuilders')
    ->alias('qb')
    ->groupBy('name')
    ->get();


// *** where clouse
$result =
DB::select('posts.ID', 'posts.post_title')
    ->distinct()
    ->from('posts posts')
    ->where('posts.post_status', '=', 'publish')
    ->orderBy('post_title', 'DESC')
    ->limit(10)->offset(2)
    ->get();

// *** JOIN
DB::select('users.display_name name')
    ->count('posts.ID', 'posts')
    ->from('users users')
    ->join('posts posts')
    ->where('posts.post_status', '=', 'publish')
    ->andWhere('posts.post_type', '=', 'post')
    ->get();

// raw sql
DB::select('posts.post_title')
    ->from('posts posts')
    ->raw("WHERE posts.post_type = 'post'")
    ->andWhere('posts.post_status', '=', 'publish')
    ->raw("LIMIT 10")
    ->get();

Delete Statement

// delete one row
DB::delete('posts')
    ->where('ID', '=', 3)
    ->execute();

// delete all records
DB::delete('posts')->execute();

Drop Statement

DB::drop('posts');
DB::dropIfExists('terms');

Alter Statement

DB::alter('cv_users')
    ->modify('name', 'username')->string(455)->required()
    ->modify('settings')->json()
    ->execute();


Single instence

Expressions also can be exicuted with one instence of DB class. By doing this database connection will be stablished only once.

$db = new DB();

$result =
$db::select('posts.ID', 'posts.post_title')
    ...

$db::create('meta')
    ...


Database Connection

By default database connection will set out of the box, automaically. But you can also manually input database configurations. This way, you also can debug your database queries from terminal.

$db = DB::setConnection(
	[
		"dbhost"        => 'mysql_host',
		"dbname"        => 'database_name',
		"dbuser"        => 'database_user',
		"dbpassword"    => 'database_password',
		"prefix"        => 'database_table_prefix'
	]
);


Driver

The default driver is pdo. But if you want to use wpdb which uses Mysqli, you also can do that by changing the driver.

$db = new DB('wpdb');

$db::select('posts.post_title')
    ->from('posts posts')
    ->get();


Dev Envirenment Setup for Contributors

Want to contribute to this package? Please follow the steps below.

  • Create a local WordPress envirenment setup.
  • Create a basic plugin.
  • Run composer init into the plugin.
  • Clone [email protected]:CodesVault/howdy_qb.git into plugin folder.
  • Add repository for local package in plugin's composer.json.
    "repositories": [
    	{
    		"type": "path",
    		"url": "./howdy_qb",
    		"options": {
    			"symlink": true
    		}
    	}
    ],
            
  • Require this package. composer require "codesvault/howdy-qb @dev"

howdy_qb's People

Contributors

abmsourav avatar alaminfirdows avatar arif98741 avatar sayedulsayem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

howdy_qb's Issues

Unable to create new table

I was trying to create a table with below code.

DB::create('cache')
	->column('ID')->bigInt()->unsigned()->autoIncrement()->primary()->required()
	->column('key')->string(255)->required()
	->column('data')->string(255)->default('NULL')
	->index(['ID'])
	->execute();

It is throwing an error. See below:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

Reason: Some words are reserved for SQL so those are not possible to use as a table column name without proper quotation.

Not sure what is wrong with innerJoin

Hi, I am using this wonderful library for our client's project. However, I am unable to retrieve data from this innerJoin query.

I seem to be having some trouble here 😕. Perhaps I overlooked something. If anyone could offer a solution, I would greatly appreciate it. 🙏

$result = $db::select( 'bv_sku_key_map.sku_child' )
	->from( 'bv_sku_key_map' )
	->innerJoin( 'bv_sku_child','bv_sku_key_map.sku_key','bv_sku_child.main_sku')
	->Where('bv_sku_child.child_sku', '=','G 10078')
	->get();

Here is the original SQL query, and it is working.

SELECT DISTINCT
	wp_bv_sku_key_map.sku_child
	
FROM
	wp_bv_sku_key_map
	INNER JOIN wp_bv_sku_child ON wp_bv_sku_key_map.sku_key = wp_bv_sku_child.main_sku
WHERE
	wp_bv_sku_child.child_sku = 'G 10078';

Thank you

Create join APIs

Join APIs need to be compatible with different types of joins

Feedback for “Select Statement”

Hello
I'm facing a error of undefined $db when I'm trying to use the select query. Am I doing something wrong? I have installed the composer using composer require codesvault/howdy_qb statement. Do I have to include any namespace or any other thing?
I'm trying to get values upon the the below code.
$result =
$db::select('posts.ID', 'posts.post_title')
->distinct()
->from('posts posts')
->where('posts.post_status', '=', 'publish')
->orderBy('post_title', 'DESC')
->limit(10)->offset(2)
->get();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.