php-db-drivers/DatabaseMysqlWP.php

145 lines
4.3 KiB
PHP

<?php
/**
* DatabaseMysql API shim for Wordpress
* Version: 2018-09-12
* (c) Vitaliy Filippov, 2012-2018
*/
require_once __DIR__.'/MysqlQueryBuilder.php';
class DatabaseMysqlWP extends MysqlQueryBuilder
{
const MS_HASH = 0;
const MS_LIST = 1;
const MS_ROW = 2;
const MS_COL = 4;
const MS_VALUE = 6;
const MS_RESULT = 8;
function quote($value)
{
if ($value === NULL)
return "NULL";
global $wpdb;
return "'" . $wpdb->_escape($value) . "'";
}
function query($sql)
{
global $wpdb;
return $wpdb->query($sql);
}
/**
* Run a SELECT query and return results.
*
* Usage: either
* $this->select($tables, $fields, $where, $options, $format)
* using $this->select_builder() or
* $this->select($sql_text, $format)
* using query text.
*
* @param int $format Return format, bitmask of MS_XX constants:
* MS_RESULT = return mysqli result object to manually fetch from
* MS_LIST = return rows as indexed arrays
* MS_HASH = return rows as associative arrays (default)
* MS_ROW = only return the first row
* MS_COL = only return the first column
* MS_VALUE = only return the first cell (just 1 value)
*/
function select($tables, $fields = '*', $where = 1, $options = NULL, $format = 0)
{
global $wpdb;
if (is_int($fields))
{
$sql = $tables;
$format = $fields;
}
else
$sql = $this->select_builder($tables, $fields, $where, $options);
if (($format & self::MS_VALUE) == self::MS_VALUE)
return $wpdb->get_var($sql, 0, 0);
elseif ($format & self::MS_COL)
return $wpdb->get_col($sql, 0);
elseif ($format & self::MS_ROW)
return $wpdb->get_row($sql, ($format & self::MS_LIST) ? ARRAY_N : ARRAY_A, 0);
else
return $wpdb->get_results($sql, ($format & self::MS_LIST) ? ARRAY_N : ARRAY_A);
}
function found_rows()
{
return $this->select('SELECT FOUND_ROWS()', self::MS_VALUE);
}
function delete($tables, $where, $options = NULL)
{
global $wpdb;
return $wpdb->query($this->delete_builder($tables, $where, $options));
}
/**
* Insert a single row into $table and return inserted ID.
* @param string $table Table name to insert row to.
* @param array $rows Row to be inserted.
* @return int $insert_id Autoincrement ID of inserted row (if appropriate).
*/
function insert_row($table, $row)
{
global $wpdb;
$sql = $this->insert_builder($table, array($row));
if ($wpdb->query($sql))
return $wpdb->insert_id;
return NULL;
}
function insert_id()
{
global $wpdb;
return $wpdb->insert_id;
}
/**
* Update row(s) in $table.
* $this->update($table, $set, $where, $options);
*
* @param string $table Table name to update.
* @param array $set Assoc array with values for update query.
* @param array $where Conditions for update query, see $this->where_builder().
* @param array $options Options for update query:
* 'LIMIT' => array($limit, $offset) or array($limit) or just $limit
* 'OFFSET' => $offset, for the case when 'LIMIT' is just $limit
*/
function update($table, $rows, $where = NULL, $options = NULL)
{
if (!$rows)
return false;
global $wpdb;
return $wpdb->query($this->update_builder($table, $rows, $where, $options));
}
function insert($table, $rows, $onConflict = NULL, $uniqueKey = NULL)
{
if (!$rows || !is_array($rows))
return false;
global $wpdb;
return $wpdb->query($this->insert_builder($table, $rows, $onConflict, $uniqueKey));
}
function insert_ignore($table, $rows, $uniqueKey = NULL)
{
return $this->insert($table, $rows, 'IGNORE', $uniqueKey);
}
function upsert($table, $rows, $uniqueKey = NULL, $updateCols = NULL)
{
return $this->insert($table, $rows, 'UPDATE', $uniqueKey, $updateCols);
}
function replace($table, $rows, $uniqueKey = NULL)
{
return $this->insert($table, $rows, 'REPLACE', $uniqueKey);
}
}