From 4cdecfabd50e58266cf39e1c281d8e3891404a14 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Mon, 10 Sep 2018 15:44:42 +0300 Subject: [PATCH] Wrapper for using the same API with Wordpress $wpdb --- DatabaseMysqlWP.php | 144 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 DatabaseMysqlWP.php diff --git a/DatabaseMysqlWP.php b/DatabaseMysqlWP.php new file mode 100644 index 0000000..823d4cf --- /dev/null +++ b/DatabaseMysqlWP.php @@ -0,0 +1,144 @@ +_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($tables, $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); + } +}