Skip to content

Instantly share code, notes, and snippets.

@itzikbenh
Last active October 5, 2016 22:23
Show Gist options
  • Save itzikbenh/23c1f2a0ee6f9c5731a44d98097a0f6f to your computer and use it in GitHub Desktop.
Save itzikbenh/23c1f2a0ee6f9c5731a44d98097a0f6f to your computer and use it in GitHub Desktop.
Simple WordPress query builder. In progress.
<?php
class Ath_DB
{
protected static $table = null;
function __construct()
{
global $wpdb;
$this->db = $wpdb;
$this->prefix = $this->db->prefix;
}
static function table( $name )
{
self::$table = $name;
return new self;
}
function get()
{
if( isset( $this->query ) )
{
$this->query = "SELECT * FROM ".$this->query;
return $this->db->get_results( $this->query );
}
$table_name = $this->prefix . self::$table;
return $this->db->get_results( "SELECT * FROM $table_name" );
}
function delete()
{
if( isset( $this->query ) )
{
$this->query = "DELETE FROM ".$this->query;
return $this->db->query( $this->query );
}
$table_name = $this->prefix . self::$table;
return $this->db->get_results( "DELETE FROM $table_name" );
}
function where( $column, $sign, $value )
{
$table_name = $this->prefix . self::$table;
$this->query = "$table_name WHERE $column $sign '$value'";
return $this;
}
function or_where( $column, $sign, $value )
{
$this->query .= "OR $column $sign '$value'";
return $this;
}
function order_by( $column, $value )
{
$this->query .= " ORDER BY $column $value";
return $this;
}
function insert( $data )
{
$table_name = $this->prefix . self::$table;
//If user entered a single array we will make it multi dimensional so the for loop will handle it correctly.
if ( ! isset( $data[0] ) )
{
$data = array( $data );
}
//The array keys are the columns. We grab the first one incase there is only one array.
$columns = "(". implode( ',', array_keys( $data[0] ) ) .")";
$this->query = "INSERT INTO $table_name $columns VALUES";
$query_values = array();
for ( $i = 0; $i < count( $data ); $i++ )
{
$values = array_values( $data[$i] );
$placeholders = $this->create_placeholders( $values );
//Will prevent SQL injection.
$query_values[] = $this->db->prepare( $placeholders, $values );
}
$this->query .= implode( ',', $query_values );
return $this->db->query( $this->query );
}
function create_placeholders( $values )
{
$placeholders = array();
foreach ( $values as $value )
{
if ( is_float( $value ) )
{
$placeholders[] = "%f";
}
elseif ( is_numeric( $value ) )
{
$placeholders[] = "%d";
}
else
{
$placeholders[] = "%s";
}
}
return "(".implode( ',', $placeholders ).")";
}
}

Ath_DB class

NOTE

When you add a table name do it without the prefix. It will be prefixed for you. This is only good for custom queries on tables you created or just custom queries that you can't find a WordPress function that will do it for you.

SELECT

will get all rows.

Ath_DB::table( 'events' )->get();

will get all rows that matches this ID

Ath_DB::table( 'events' )->where( 'post_id', '=', 420 )->get();

You can specify order as well

Ath_DB::table( 'events' )->where( 'post_id', '=', 420 )->order_by( 'id', 'DESC' )->get();

You can also nest where clauses

Ath_DB::table( 'events' )->where( 'id', '=', 81 )->or_where( 'id', '=', 82 )->get();

INSERT - will create placeholders and prepare your query for you.

Ath_DB::table( 'events' )->insert( [title => "pizza tasting", location => "NYC"] );

You can insert multiple arrays in this form:

Ath_DB::table( 'events' )->insert( [
	[title => "pizza tasting", location => "NYC"],
	[title => "burger tasting", location => "Chicago"]
] );

DELETE

Will delete where ID is 25

Ath_DB::table( 'events' )->where( 'id', '=', 25 )->delete();

Will delete all events!

Ath_DB::table( 'events' )->delete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment