<?php
require_once(PLUGIN_DIR . 'lib/php4log/Logger.php');

class DBManager
{
    public static function query($sql)
    {
        global $wpdb;
        $result = $wpdb->query($sql);
        DBManager::getLog()->info($wpdb->last_query);
        if ($result->last_error) {
            DBManager::getLog()->error($wpdb->last_error);
            return $result->last_error;
        }
        return $result;
    }

    public static function get_results($sql)
    {
        global $wpdb;
        $result = $wpdb->get_results($sql);
        DBManager::getLog()->info($wpdb->last_query);
        if ($result->last_error) {
            DBManager::getLog()->error($wpdb->last_error);
            return $result->last_error;
        }
        return $result;
    }

    public static function get_var($sql)
    {
        global $wpdb;
        $result = $wpdb->get_var($sql);
        DBManager::getLog()->info($wpdb->last_query);
        if ($result->last_error) {
            DBManager::getLog()->error($wpdb->last_error);
            return $result->last_error;
        }
        return $result;
    }

    public static function get_row($sql)
    {
        global $wpdb;
        $result = $wpdb->get_row($sql);
        DBManager::getLog()->info($wpdb->last_query);
        if ($result->last_error) {
            DBManager::getLog()->error($wpdb->last_error);
            return $result->last_error;
        }
        return $result;
    }

    public static function insert($table, $data)
    {
        global $wpdb;
        $result = $wpdb->insert($table, $data);
        DBManager::getLog()->info($wpdb->last_query);
        if ($result->last_error) {
            DBManager::getLog()->error($wpdb->last_error);
            return $result->last_error;
        }
        return $result;
    }

    public static function delete($table, $where)
    {
        global $wpdb;
        $result = $wpdb->delete($table, $where);
        DBManager::getLog()->info($wpdb->last_query);
        if ($result->last_error) {
            DBManager::getLog()->error($wpdb->last_error);
            return $result->last_error;
        }
        return $result;
    }

    public static function replace($table, $data, $format = null)
    {
        global $wpdb;
        $result = $wpdb->replace($table, $data, $format);
        DBManager::getLog()->info($wpdb->last_query);
        if ($result->last_error) {
            DBManager::getLog()->error($wpdb->last_error);
            return $result->last_error;
        }
        return $result;
    }

    public static function update($table, $data, $where)
    {
        global $wpdb;
        $result = $wpdb->update($table, $data, $where);
        DBManager::getLog()->info($wpdb->last_query);
        if ($result->last_error) {
            DBManager::getLog()->error($wpdb->last_error);
            return $result->last_error;
        }
        return $result;
    }

    public static function getLog()
    {
        $config = PLUGIN_DIR . 'appender_pdo.properties';
        $logger = Logger::configure($config);
        $log = Logger::getLogger('log');
        return $log;
    }

} 