Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #889 from xwp/db-driver
Preparing Stream for Alternate Database Drivers
- Loading branch information
Showing
9 changed files
with
513 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
} |
Oops, something went wrong.