Skip to content

Instantly share code, notes, and snippets.

@brianium
Created March 28, 2017 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brianium/3f0857e16c4a0fbb1080c34099bf8d10 to your computer and use it in GitHub Desktop.
Save brianium/3f0857e16c4a0fbb1080c34099bf8d10 to your computer and use it in GitHub Desktop.
Barebones Phinx adapter powered by wpdb
<?php
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Db\Table;
use Phinx\Db\Table\Column;
use Phinx\Db\Table\ForeignKey;
use Phinx\Db\Table\Index;
use Phinx\Migration\MigrationInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class WpdbAdapter - for when you like to party, but not too hard
*/
class WpdbAdapter extends MysqlAdapter
{
/**
* @var PdoLikeObject
*/
protected $connection;
/**
* @param array $options Options
* @param OutputInterface $output Output Interface
*/
public function __construct(array $options, OutputInterface $output = null)
{
parent::__construct($options, $output);
$this->connection = new PdoLikeObject();
}
/**
* Creates the schema table.
*
* @return void
*/
public function createSchemaTable()
{
try {
$options = array(
'id' => false,
'primary_key' => 'version'
);
$table = new Table($this->getSchemaTableName(), $options, $this);
$table->addColumn('version', 'biginteger')
->addColumn('start_time', 'timestamp')
->addColumn('end_time', 'timestamp')
->save();
} catch (\Exception $exception) {
throw new \InvalidArgumentException('There was a problem creating the schema table: ' . $exception->getMessage());
}
}
/**
* {@inheritdoc}
*/
public function setOptions(array $options)
{
$adapter = parent::setOptions($options);
$this->tryCreateSchemaTable();
return $adapter;
}
/**
* Initializes the database connection.
*
* @throws \RuntimeException When the requested database driver is not installed.
* @return void
*/
public function connect()
{
$this->tryCreateSchemaTable();
}
/**
* Closes the database connection.
*
* @return void
*/
public function disconnect()
{
// not my problem!
}
/**
* Does the adapter support transactions?
*
* @return boolean
*/
public function hasTransactions()
{
return false;
}
/**
* Begin a transaction.
*
* @return void
*/
public function beginTransaction()
{
// no way!
}
/**
* Commit a transaction.
*
* @return void
*/
public function commitTransaction()
{
// yeah right!
}
/**
* Rollback a transaction.
*
* @return void
*/
public function rollbackTransaction()
{
// wpdb is flawless!
}
/**
* Executes a SQL statement and returns the number of affected rows.
*
* @param string $sql SQL
* @return int
*/
public function execute($sql)
{
$result = $this->query($sql);
return intval($result);
}
/**
* Executes a SQL statement and returns the result as an array.
*
* @param string $sql SQL
* @return array
*/
public function query($sql)
{
global $wpdb;
return $wpdb->query($sql);
}
/**
* Executes a query and returns only one row as an array.
*
* @param string $sql SQL
* @return array
*/
public function fetchRow($sql)
{
global $wpdb;
return $wpdb->get_row($sql);
}
/**
* Executes a query and returns an array of rows.
*
* @param string $sql SQL
* @return array
*/
public function fetchAll($sql)
{
global $wpdb;
return $wpdb->get_results($sql, ARRAY_A);
}
/**
* Inserts data into the table
*
* @param Table $table where to insert data
* @param array $columns column names
* @param $data
*/
public function insert(Table $table, $columns, $data)
{
global $wpdb;
$this->startCommandTimer();
$values = [];
$types = [];
$count = count($columns);
for ($i = 0; $i < $count; $i++) {
$name = $columns[$i];
$value = $data[$i];
$values[$name] = $value;
if (is_string($value)) {
$types[] = '%s';
} else {
$types[] = '%d';
}
}
$wpdb->insert(
$this->quoteTableName($table->getName()),
$values,
$types
);
$this->endCommandTimer();
}
/**
* Checks to see if a foreign key exists.
*
* @param string $tableName
* @param string[] $columns Column(s)
* @param string $constraint Constraint name
* @return boolean
*/
public function hasForeignKey($tableName, $columns, $constraint = null)
{
return false;
}
/**
* Adds the specified foreign key to a database table.
*
* @param Table $table
* @param ForeignKey $foreignKey
* @return void
*/
public function addForeignKey(Table $table, ForeignKey $foreignKey)
{
// pffffffft
}
/**
* Drops the specified foreign key from a database table.
*
* @param string $tableName
* @param string[] $columns Column(s)
* @param string $constraint Constraint name
* @return void
*/
public function dropForeignKey($tableName, $columns, $constraint = null)
{
// as if
}
/**
* This method is only used for quoting things
*
* @return PdoLikeObject
*/
public function getConnection()
{
return $this->connection;
}
/**
* Create the schema table if it does not exist
*/
protected function tryCreateSchemaTable()
{
if (!$this->hasSchemaTable()) {
$this->createSchemaTable();
}
}
}
@brianium
Copy link
Author

We tailor to the lowest common denominator here, so we avoided implementing transactions. It shouldn't be too difficult to support transactions if that is the desired behavior. Just a note, we have used this in production on a variety of hosts (shared and otherwise) without issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment