<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Helper\Migration;

use \Illuminate\Database\Capsule\Manager;
use ModulesGarden\HostingQuotaNotifications\App\Libs\Usage\Manage;

/**
 * Class DBManager
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class DBManager
{

    public static function tableExist($tableName)
    {
        return Manager::schema()->hasTable($tableName);
    }

    public static  function fetchData($tableName)
    {
        return Manager::table($tableName)->get();
    }

    public static function fetchFromTableWhereRaw($tableName, $select, $condition, $param, $value = false)
    {
        $result = Manager::table($tableName)->select($select)->whereRaw($condition, $param);
        return $value ? $result->value($value) : $result->get();
    }

    public static function deleteTable($tableName)
    {
        if(!self::tableExist($tableName))
        {
            return;
        }
        $manager = new Manager();
        $manager->schema()->drop($tableName);
    }

    public static function dropView($tableName)
    {
        if(!self::tableExist($tableName))
        {
            return;
        }
        Manager::statement("DROP VIEW {$tableName}");
    }


}