<commit_before><?php

namespace Vinala\Kernel\Cache\Driver ;

use Vinala\Kernel\Database\Query;
use Vinala\Kernel\Database\Schema;

/**
* The cache driver of database
*
* @version 2.0
* @author <NAME>
* @package Vinala\Kernel\Cache\Driver
* @since v3.3.0
*/
class PDODriver extends Driver
{

    //--------------------------------------------------------
    // Properties
    //--------------------------------------------------------

    /**
    * The database table 
    *
    * @var string
    */
    protected $table ;

    //--------------------------------------------------------
    // Constructor
    //--------------------------------------------------------

    function __construct()
    {
        $this->table = config('cache.options.database.table');

        $this->establish();
    }

    //--------------------------------------------------------
    // Functions
    //--------------------------------------------------------

    /**
    * Find item in cache 
    *
    * @param string $key 
    * @return array
    */
    private function find($key)
    {
        if($this->exists($name))
        {
            $data = Query::from($this->table)
                        ->where('name' , '=' , $name)
                        ->first();

            if($data->lifetime >= time())
            {
                return ['name' => $key , 'value' => $this->unpacking($data->value) , 'lifetime' => $data->lifetime ];
            }
            else
            {   
                $this->remove($name);
            }
        }
    }

    /**
    * Check if Cache table is exists
    *
    * @return boom
    */
    private function checkTable()
    {
        return Schema::existe($this->table);
    }


    /**
    * Create database cache table
    *
    * @return null
    */
    private function createTable()
    {
        Schema::create($this->table,function($tab)
			{
				$tab->inc("id");
				$tab->string("name");
				$tab->string("value");
				$tab->long("lifetime");
				$tab->unique("cacheunique",["name"]);
			});
    }

    /**
    * Establish conenction to database
    *
    * @return null
    */
    private function establish()
    {
        if( ! $this->checkTable())
        {
            $this->createTable();
        }
    }

    /**
    * Packing the data to string 
    *
    * @param mixed
    * @return string 
    */
    protected function packing($value)
    {
        return serialize($value);
    }

    /**
    * Unpacking the data from string <commit_msg>use DatabaseSurfaceDisabledException<commit_after><?php

namespace Vinala\Kernel\Cache\Driver ;

use Vinala\Kernel\Database\Query;
use Vinala\Kernel\Database\Schema;
use Vinala\Kernel\Cache\Exception\DatabaseSurfaceDisabledException;

/**
* The cache driver of database
*
* @version 2.0
* @author <NAME>
* @package Vinala\Kernel\Cache\Driver
* @since v3.3.0
*/
class PDODriver extends Driver
{

    //--------------------------------------------------------
    // Properties
    //--------------------------------------------------------

    /**
    * The database table 
    *
    * @var string
    */
    protected $table ;

    //--------------------------------------------------------
    // Constructor
    //--------------------------------------------------------

    function __construct()
    {
        $this->table = config('cache.options.database.table');

        $this->establish();
    }

    //--------------------------------------------------------
    // Functions
    //--------------------------------------------------------

    /**
    * Find item in cache 
    *
    * @param string $key 
    * @return array
    */
    private function find($key)
    {
        if($this->exists($name))
        {
            $data = Query::from($this->table)
                        ->where('name' , '=' , $name)
                        ->first();

            if($data->lifetime >= time())
            {
                return ['name' => $key , 'value' => $this->unpacking($data->value) , 'lifetime' => $data->lifetime ];
            }
            else
            {   
                $this->remove($name);
            }
        }
    }

    /**
    * Check if Cache table is exists
    *
    * @return boom
    */
    private function checkTable()
    {
        return Schema::existe($this->table);
    }


    /**
    * Create database cache table
    *
    * @return null
    */
    private function createTable()
    {
        Schema::create($this->table,function($tab)
			{
				$tab->inc("id");
				$tab->string("name");
				$tab->string("value");
				$tab->long("lifetime");
				$tab->unique("cacheunique",["name"]);
			});
    }

    /**
    * Establish conenction to database
    *
    * @return null
    */
    private function establish()
    {
        exception_if( ! config('components.database') , DatabaseSurfaceDisabledException::class);

        if( ! $this->checkTable())
        {
            $this->createTable();
        }
    }

    /**
    * Packing the data to string 
    *
    * @param mixed
    * @return string 
    */
    protected function packing($value)
    {
        return serialize($value);
    }

    /**
    * Unpacking the data from string 
{'fraction_non_alphanumeric': 0.2636748844375963, 'fraction_numerical': 0.001926040061633282, 'mean_word_length': 1.168267223382046, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3853389', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1353, 'n_words': 260}