Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preparing Stream for Alternate Database Drivers #889

Merged
merged 29 commits into from Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
452d6e1
abstracted db class interface, so it is configurable
eugenekireev Dec 15, 2015
99012d2
Merge pull request #808 from evgenykireev/vip
Dec 18, 2015
0ba557a
Merge branch 'develop' into vip
Dec 18, 2015
2f84584
Remove unecessary files and rework initial changes to db classes
Dec 18, 2015
ed35fb1
Merge branch 'develop' into vip
Jun 8, 2016
c0d8c47
Loaded stream plugin class on plugin loaded to allow others to apply …
faishal Jun 14, 2016
a8d94c2
DB Driver Tests - Added the initial test for class-db-driver-wpdb.php
Jun 17, 2016
2c032c8
DB Driver Tests - Added the test for inserting data and meta
Jun 18, 2016
7e957c4
DB Driver Tests - Added the test for get_column_values
Jun 18, 2016
f07bcc5
DB Driver Tests - Removed the contsruction test from test-class-db.ph…
Jun 20, 2016
3dc8462
DB Driver Tests - Added get_table_names() and its test to the db-driv…
Jun 20, 2016
a8f94cd
DB Driver Tests - Removed the get_table_names() test from test-class-…
Jun 20, 2016
f7f9862
DB Driver Tests - Fixed a bug in the test for get_table_names()
Jun 20, 2016
3038fd5
DB Driver Tests - Gave the test-class-db-driver-wpdb.php tests a diff…
Jun 20, 2016
5ba3b94
DB Driver Tests - Removed files that aren't being tracked
Jun 20, 2016
efd131f
Merge branch 'master' into Faison-feature/db-driver-tests
faishal Jul 5, 2016
9aa59c1
Fixed database installation issue with plugin_loaded hook
faishal Aug 2, 2016
3f6cd65
Merge branch 'master' into feature/db-driver
faishal Aug 2, 2016
3ffc216
Fixed database installation issue with plugin_loaded hook
faishal Aug 2, 2016
3742ff6
Fixed phpcs issue, Change driver on plugin loaded
faishal Aug 2, 2016
895342a
Removed tmp no_table hook from code
faishal Aug 2, 2016
b4cce39
Fixed undefined notice for foreach : Return table names array
faishal Aug 2, 2016
4302d7c
Merge branch 'develop' into feature/db-driver
Sep 9, 2016
d186fd6
Minor formatting and documentation fixes.
Sep 12, 2016
e1994f8
Make the default driver class (WPDB) use the setup_storage and purge_…
Sep 14, 2016
53fa39f
Merge branch 'develop' into feature/db-driver
Sep 23, 2016
2d1a316
Merge pull request #881 from 10up/feature/db-driver
Oct 31, 2016
db60b3c
Instantiate Admin class without requiring second driver param
Oct 31, 2016
a1d4fe6
Merge branch 'develop' into db-driver
Jan 9, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 );
}