<?php
class ControllerToolMemcacheTest extends Controller
{
    private $link;

    public function index()
    {
        $begin = microtime();
        ini_set('memory_limit',-1);



        $postgr = new \DB\Postgre('127.0.0.1', 'deer2piy_user', 'gHm9ds4!', 'deer2piy_db', '5432');

        $query = $postgr->query("SELECT * FROM ps_product ")->rows;
        //var_dump($query);

        $postgr->query("INSERT INTO ps_product (model,model2)
        VALUES( 
         'dsddsds',
         'dsfdfdf')
         ");

        die();



        $mongo = new MongoDB();
        /*$mongo->update(
            array('customer_id' => 61),
            array(
                'api_id' => (isset($this->session->data['api_id']) ? (int) $this->session->data['api_id'] : 0),
                'customer_id' => 61,
                'cart_id' => 222256565,
                'session_id' => $this->db->escape($this->session->getId()),
                'product_id' => (int) 2555,
                'recurring_id' => (int) 255,
                'customcase_image' => 2,
                'quantity' => (int) 6562,
                'date_added' => @date('Y-m-d')
            ),
            array(),
            'deercase.cart'
        );*/

        $mongo->update(
            array(
                'cart_id' => 25045,
                'api_id' => (isset($this->session->data['api_id']) ? (int) $this->session->data['api_id'] : 0),
                'customer_id' => (int) $this->customer->getId(),
                'session_id' => $this->db->escape($this->session->getId()),
            ),
            array(
                'cart_id' => 25045,
                'api_id' => (isset($this->session->data['api_id']) ? (int) $this->session->data['api_id'] : 0),
                'customer_id' => (int) $this->customer->getId(),
                'session_id' => $this->db->escape($this->session->getId()),
                'quantity' => 5,
            ),
            array(),
            'deercase.cart'
        );

        //$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');
            //new MongoDB\Driver\Manager('mongodb://rs1.example.com,rs2.example.com/', ['replicaSet' => 'myReplicaSet']),


        //$this->session->data['customers'] = $this->db->query("SELECT * FROM ps_customer")->rows;
/*
        $query = $this->db->query("SELECT * FROM ps_customer")->rows;
        foreach ($query as $que){
            echo $que['firstname'] . '<br>';
        } */

        /*$filter = ['firstname' => 'Burak'];
        $options = [
            'projection' => ['_id' => 0],
            'sort' => ['x' => -1],
        ];

        $query = new MongoDB\Driver\Query(array(), array());
        $cursor = $manager->executeQuery('deercase.customer', $query);

        foreach ($cursor as $document) {
            echo $document->firstname . '<br>';
        }*/


        /*foreach ($this->session->data['customers'] as $key => $customer){ //var_dump($customer); die();
            $bulk = new MongoDB\Driver\BulkWrite;
            $bulk->insert([
                'customer_id' => $customer['customer_id'],
                'old_id' => $customer['old_id'],
                'customer_group_id' => $customer['customer_group_id'],
                'store_id' => $customer['store_id'],
                'language_id' => $customer['language_id'],
                'firstname' => $customer['firstname'],
                'lastname' => $customer['lastname'],
                'email' => $customer['email'],
                'telephone' => $customer['telephone'],
                'profile_picture' => $customer['profile_picture'],
                'fax' => $customer['fax'],
                'password' => $customer['password'],
                'tc' => $customer['tc'],
                'salt' => $customer['salt'],
                'cart' => $customer['cart'],
                'wishlist' => $customer['wishlist'],
                'newsletter' => $customer['newsletter'],
                'address_id' => $customer['address_id'],
                'custom_field' => $customer['custom_field'],
                'ip' => $customer['ip'],
                'status' => $customer['status'],
                'approved' => $customer['approved'],
                'safe' => $customer['safe'],
                'token' => $customer['token'],
                'code' => $customer['code'],
                'date_added' => $customer['date_added'],
                'iyzico_SubMerchantKey' => $customer['iyzico_SubMerchantKey'],
                'paratika_api' => $customer['paratika_api'],
            ]);
            $manager->executeBulkWrite('deercase.customer', $bulk);
            unset($this->session->data['customers'][$key]);

        }
        */

        //var_dump(count($this->session->data['customers'])); die();

        //$bulk = new MongoDB\Driver\BulkWrite;
        //$bulk->insert(['customer_id' => 1, 'old_id' => 2, 'customer_group_id' => 3, 'store_id' => 3]);
        //$manager->executeBulkWrite('deercase.customer', $bulk);

       /* $filter = ['x' => ['$gt' => 1]];
        $options = [
            'projection' => ['_id' => 0],
            'sort' => ['x' => -1],
        ];

        $query = new MongoDB\Driver\Query($filter, $options);
        $cursor = $manager->executeQuery('deercase.customer', $query);

        foreach ($cursor as $document) {
            var_dump($document);
        }*/




        $totalTime = round($this->microtimeMinus($begin, microtime()) / 1000000);

        echo $totalTime;



    }

    private function microtimeMinus($start, $end) {
        list($s_usec, $s_sec) = explode(" ", $start);
        list($e_usec, $e_sec) = explode(" ", $end);
        $diff = ((int) $e_sec - (int) $s_sec) * 1000000 + ((float) $e_usec - (float) $s_usec) * 1000000;
        return $diff;
    }

}