Skip to content

Commit

Permalink
Merge pull request #889 from xwp/db-driver
Browse files Browse the repository at this point in the history
Preparing Stream for Alternate Database Drivers
  • Loading branch information
Luke Carbis committed Feb 15, 2017
2 parents eee892b + a1d4fe6 commit c284f36
Show file tree
Hide file tree
Showing 9 changed files with 513 additions and 231 deletions.
3 changes: 1 addition & 2 deletions classes/class-admin.php
Expand Up @@ -162,8 +162,7 @@ public function __construct( $plugin ) {
add_action( 'wp_ajax_wp_stream_reset', array( $this, 'wp_ajax_reset' ) );

// Uninstall Streams and Deactivate plugin.
$uninstall = new Uninstall( $this->plugin );
add_action( 'wp_ajax_wp_stream_uninstall', array( $uninstall, 'uninstall' ) );
$uninstall = $this->plugin->db->driver->purge_storage( $this->plugin );

// Auto purge setup.
add_action( 'wp_loaded', array( $this, 'purge_schedule_setup' ) );
Expand Down
168 changes: 168 additions & 0 deletions classes/class-db-driver-wpdb.php
@@ -0,0 +1,168 @@
<?php
namespace WP_Stream;

class DB_Driver_WPDB implements DB_Driver {
/**
* Holds Query class
*
* @var Query
*/
protected $query;

/**
* Hold records table name
*
* @var string
*/
public $table;

/**
* Hold meta table name
*
* @var string
*/
public $table_meta;

/**
* Class constructor.
*/
public function __construct() {
$this->query = new Query( $this );

global $wpdb;
$prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->base_prefix );

$this->table = $prefix . 'stream';
$this->table_meta = $prefix . 'stream_meta';

$wpdb->stream = $this->table;
$wpdb->streammeta = $this->table_meta;

// Hack for get_metadata
$wpdb->recordmeta = $this->table_meta;
}

/**
* Insert a record.
*
* @param array $data Data to insert.
*
* @return int
*/
public function insert_record( $data ) {
global $wpdb;

if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
return false;
}

$meta = $data['meta'];
unset( $data['meta'] );

$result = $wpdb->insert( $this->table, $data );
if ( ! $result ) {
return false;
}

$record_id = $wpdb->insert_id;

// Insert record meta
foreach ( (array) $meta as $key => $vals ) {
foreach ( (array) $vals as $val ) {
$this->insert_meta( $record_id, $key, $val );
}
}

return $record_id;
}

/**
* Insert record meta
*
* @param int $record_id
* @param string $key
* @param string $val
*
* @return array
*/
public function insert_meta( $record_id, $key, $val ) {
global $wpdb;

$result = $wpdb->insert(
$this->table_meta,
array(
'record_id' => $record_id,
'meta_key' => $key,
'meta_value' => $val,
)
);

return $result;
}

/**
* Retrieve records
*
* @param array $args
*
* @return array
*/
public function get_records( $args ) {
return $this->query->query( $args );
}

/**
* Returns array of existing values for requested column.
* Used to fill search filters with only used items, instead of all items.
*
* GROUP BY allows query to find just the first occurrence of each value in the column,
* increasing the efficiency of the query.
*
* @param string $column
*
* @return array
*/
public function get_column_values( $column ) {
global $wpdb;
return (array) $wpdb->get_results(
"SELECT DISTINCT $column FROM $wpdb->stream", // @codingStandardsIgnoreLine can't prepare column name
'ARRAY_A'
);
}

/**
* Public getter to return table names
*
* @return array
*/
public function get_table_names() {
return array(
$this->table,
$this->table_meta,
);
}

/**
* Init storage.
*
* @param \WP_Stream\Plugin $plugin Instance of the plugin.
* @return \WP_Stream\Install
*/
public function setup_storage( $plugin ) {
return new Install( $plugin );
}

/**
* Purge storage.
*
* @param \WP_Stream\Plugin $plugin Instance of the plugin.
* @return \WP_Stream\Uninstall
*/
public function purge_storage( $plugin ) {
$uninstall = new Uninstall( $plugin );
add_action( 'wp_ajax_wp_stream_uninstall', array( $uninstall, 'uninstall' ) );

return $uninstall;
}

}
53 changes: 53 additions & 0 deletions classes/class-db-driver.php
@@ -0,0 +1,53 @@
<?php
namespace WP_Stream;

interface DB_Driver {
/**
* Insert a record
*
* @param array $data
*
* @return int
*/
public function insert_record( $data );

/**
* Retrieve records
*
* @param array $args
*
* @return array
*/
public function get_records( $args );

/**
* Returns array of existing values for requested column.
* Used to fill search filters with only used items, instead of all items.
*
* @param string $column
*
* @return array
*/
public function get_column_values( $column );

/**
* Public getter to return table names
*
* @return array
*/
public function get_table_names();

/**
* Init storage.
*
* @param \WP_Stream\Plugin $plugin Instance of the plugin.
*/
public function setup_storage( $plugin );

/**
* Purge storage.
*
* @param \WP_Stream\Plugin $plugin Instance of the plugin.
*/
public function purge_storage( $plugin );
}

0 comments on commit c284f36

Please sign in to comment.