<commit_before><?php

/*
 * This file is part of the core framework.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Core\Mapping;

import('core.mapping');

/**
 * Provides some basic mapping features that don't necessarily have to be used.
 */
abstract class PDOMapper extends \Core\Mapper {
    protected $pdo;
    protected
        $_select,
        $_delete,
        $_insert,
        $_update,
        $_joins,
        $_fields;

    protected
        $_default_select = "SELECT * FROM %s",
        $_default_delete = "DELETE * FROM %s",
        $_default_update = "UPDATE %s SET",
        $_default_insert = "INSERT INTO %s";

    public function __construct($parameters=False){
        //$this->pdo = \Core\Provision\PDO::Provide($parameters['pdo']);
    }

    public function attach_pdo(\PDO $pdo) {
        $this->pdo = $pdo;
        return $this;
    }
    public function fetch_all() {
        $objects = array();
        $sth = $this->pdo->prepare(sprintf(
            "%s\n%s\n%s",
            $this->_head('select'), 
            $this->_joins,
            $this->_order
        ));
        $sth->execute();
        $results = $sth->fetchAll(PDO::FETCH_ASSOC);

        foreach($results as $result) {
            $objects[] = $this->create_object($result);
        }
        return $objects;
    }

    public function fetch($id) {
        $sth = $this->pdo->prepare(sprintf(
            "%s\n%s\nWHERE id = :id",
            $this->_head('select'), 
            $this->_joins
        ));
        $sth->execute(array(
            ':id' => $id
        ));
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        return $this->create_object($result);
    }

    public function save(Mapped $object) {
        if($object->id > 0) {
            $this->_update($object);
        } else {
            $this->_insert($object);   
        }
    }

    protected function _insert(Mapped $object) {
        $data = $this->_filter($object->_array());
        $sth = $this->pdo->prepare(sprintf(
            "%s\n(%s)\nVALUES (%s)",
            $this->_head('insert'),
            $this->_insert_fields($data),
            $this->_insert_fields($data,':')
        ));
        $sth->execute($this->_binds($data));
    }

    protected function _insert_fields($data, $prefix=False) {
        if(!prefix){
            $sqls = array('id');   
        } else {
            $sqls = array('NULL');
        }
        $sqls = array();
        foreach($data as $key => $value) {
            $sqls[] = $prefix . $key;
        }
        return implode(",", $sqls);
    }

    protected function _update(Mapped $object) {
        $data = $this->_filter($object->_array());<commit_msg>Returning ID on insert. Fuck MySQL.<commit_after><?php

/*
 * This file is part of the core framework.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Core\Mapping;

import('core.mapping');

/**
 * Provides some basic mapping features that don't necessarily have to be used.
 */
abstract class PDOMapper extends \Core\Mapper {
    protected $pdo;
    protected
        $_select,
        $_delete,
        $_insert,
        $_update,
        $_joins,
        $_fields;

    protected
        $_default_select = "SELECT * FROM %s",
        $_default_delete = "DELETE * FROM %s",
        $_default_update = "UPDATE %s SET",
        $_default_insert = "INSERT INTO %s";

    public function __construct($parameters=False){
        //$this->pdo = \Core\Provision\PDO::Provide($parameters['pdo']);
    }

    public function attach_pdo(\PDO $pdo) {
        $this->pdo = $pdo;
        return $this;
    }
    public function fetch_all() {
        $objects = array();
        $sth = $this->pdo->prepare(sprintf(
            "%s\n%s\n%s",
            $this->_head('select'), 
            $this->_joins,
            $this->_order
        ));
        $sth->execute();
        $results = $sth->fetchAll(PDO::FETCH_ASSOC);

        foreach($results as $result) {
            $objects[] = $this->create_object($result);
        }
        return $objects;
    }

    public function fetch($id) {
        $sth = $this->pdo->prepare(sprintf(
            "%s\n%s\nWHERE id = :id",
            $this->_head('select'), 
            $this->_joins
        ));
        $sth->execute(array(
            ':id' => $id
        ));
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        return $this->create_object($result);
    }

    public function save(Mapped $object) {
        if($object->id > 0) {
            $this->_update($object);
        } else {
            $this->_insert($object);   
        }
    }

    protected function _insert(Mapped $object) {
        $data = $this->_filter($object->_array());
        $sth = $this->pdo->prepare(sprintf(
            "%s\n(%s)\nVALUES (%s)
	    RETURNING id",
            $this->_head('insert'),
            $this->_insert_fields($data),
            $this->_insert_fields($data,':')
        ));
        $sth->execute($this->_binds($data));
	$inserted = $sth->fetch();
	return $inserted['id'];
    }

    protected function _insert_fields($data, $prefix=False) {
        if(!prefix){
            $sqls = array('id');   
        } else {
            $sqls = array('NULL');
        }
        $sqls = array();
        foreach($data as $key => $value) {
            $sqls[] = $prefix . $key;
        }
        return implode(",", $sqls);
    }

    protected function _update(Mapped $object) {
        $data = $this->_filter($object->_array());
{'fraction_non_alphanumeric': 0.16322204557498676, 'fraction_numerical': 0.0003532944709415298, 'mean_word_length': 1.9291257113295395, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 78, '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': '2443985', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1726, 'n_words': 370}