<commit_before><?php
namespace app\model\db;
use \mysqli;
use \Exception;

/**
 * :: DB Model ::
 * Maintains connection to database
 */

class Db extends \app\model\Model {
    private $dbSettings;
    private $dbOject;
    private $connected = false;
    private $resultObject;
    private $insertId = 0;
    
    public function __construct() {
        parent::__construct();
        $conf = $this->getConfig();
        if(isset($conf['db'])) {
            $this->dbSettings = $conf['db'];
            $this->connect();
        } else {
            throw new Exception('No DB settings found in config file!');
        }
    }
    
    /**
     * Connects to datbase using MySQLi
     * @throws Exception
     */
    public function connect() {
        if($this->connected == true) return true;
        $this->dbOject = new mysqli($this->dbSettings['server'], $this->dbSettings['username'], $this->dbSettings['password'], $this->dbSettings['database']);
        if($this->dbOject->connect_errno) {
            throw new Exception('Could not connect to database! Server returned: '. $this->dbOject->connect_errno);
        } else {
            $this->dbOject->set_charset('utf8');
            $this->connected = true;
        }
    }

    public function close() {
        $this->connected = false;
        $this->dbOject->close();
    }
    
    /**
     * Runs a query against the DB
     * 
     * @param type $q
     * @return mysqli_result
     */
    public function query($q = null) {
        if(!is_string($q)) return false;
        if(!$this->connected) return false;
        $this->resultObject = $this->dbOject->query($q);
        if($this->resultObject == false) {
            throw new Exception('Query Error: '. $this->dbOject->error. 'Query: '. $q);
            return false;
        }
        $this->insertId = $this->dbOject->insert_id;
        $this->close();
        return $this->resultObject;
    }
    
    /**
     * Inset some data iinto db
     * 
     * @param string $table
     * @param array $data
     * @return mysqli_result
     * @throws Exception
     */
    public function insert($table = null, $data = null) {
        if(!is_string($table))  throw new Exception('Table given is not a sting');
        if(!is_array($data))    throw new Exception('Data given is not an array!');

        $table = $this->escape($table, false);
        $data = $this->escape($data);
        $fields = "(";
        $values = "(";
        $lenth = count($data);
        $c = 1;
        
        foreach($data as $field => $value) {
            $fields .= "`". $field. "`";
            $values .= $value;
            
            if($c != $lenth) {
                $fields .= ", ";
                $values .= ", ";
                $c++;
            }
        }
        
        $fields .= ")";
        $values .= ")";
        
        $q = "INSERT INTO ". $table. " ". $fields. " VALUES ". $values;
        return ($this->query($q)) ? $this->insertId : false;
    }

    /**
     * Returns list where the query fits
     * 
     * @param string $table
     * @param array $query
     * @return mysqli_result
     * @throws Exception
     */
    public function get_where($table = null, $query = null, $fields = null, $sorting = array(), $limit = 0) {
        if(!is_string($table))  throw new Exception('Table given is not a sting');
        if(!is_array($query))    throw new Exception('Query given is not an array!');
        
        $table = $this->escape($table, false);
        $query = $this->whereValueList($query);

        if(is_array($fields) && count($fields) > 0) {
            $fields = $this->escape($fields, false);
            $fields = $this->getFieldList($fields);
        } else {
            $fields = "*";
        }

        $q = "SELECT $fields FROM ". $table. " WHERE ". $query. " ". $this->getSortingString($sorting). " ". $this->getLimit($limit);
        return $this->query($q);
    }
    
    /**
     * Runs a select command against the given table
     * 
     * @param string $table
     * @return mysqli_result
     * @throws Exception
     */
    public function get($table = null, $sorting = array(), $limit = 0) {
        if(!is_string($table))  throw new Exception('Table given is not a sting');
        $table = $this->escape($table, false);
        $q = "SELECT * FROM ". $table. " ". $this->getSortingString($sorting). " ". $this->getLimit($limit);
        return $this->query($q);
    }

    /**
     * Returns only specific fields from table query
     * @param string $table
     * @param string/array $field
     * @return mysqli_result
     * @throws Exception
     */
    public function get_only($table = null, $field = null, $sort = array(), $limit = 0) {
        if(!is_string($table)) throw new Exception('Table given is not a string');
        if(!is_string($field) && !is_array($field)) throw new Exception('Field given is not string or array');
        $table = $this->escape($table, false);
        $field = $this->escape($field, false);
        if(!is_array($field)) $field = array($field);
        $fildStr = $this->getFieldList($field);

        $q = "SELECT $fildStr FROM $table ". $this->getSortingString($sort). " ". $this->getLimit($limit);
        return $this->query($q);
    }
    
    /**
     * Delets a item in the table, where query is fullfilled
     * 
     * @param string $table
     * @param array $query
     * @return mysqli_result;
     * @throws Exception
     */
    public function delete($table = null, $query = null) {
        if(!is_string($table))  throw new Exception('Table given is not a sting');
        
        $table = $this->escape($table, false);
        $q = "DELETE FROM ". $table;
        
        if(isset($query) && is_array($query)) {
            $query = $this->setValueList($query);
            $q .= " WHERE ". $query;
        }

        return $this->query($q);
    }
    
    /**
     * Runs an update against DB
     * 
     * @param string $table
     * @param array $set
     * @param array $query
     * @return mysqli_result
     * @throws Exception
     */
    public function update($table = null, $set = null, $query = null) {
        if(!is_string($table))  throw new Exception('Table given is not a sting');
        if(!is_array($set))    throw new Exception('Set parameter given is not an array!');
        
        $table = $this->escape($table, false);
        $set = $this->setValueList($set);
        $q = "UPDATE ". $table. " SET ". $set;
        
        if(is_array($query)) {
            $queryString = $this->setValueList($query);
            $q .= " WHERE ". $queryString;
        }<commit_msg>Delete now uses WhereValueList method for generating query<commit_after><?php
namespace app\model\db;
use \mysqli;
use \Exception;

/**
 * :: DB Model ::
 * Maintains connection to database
 */

class Db extends \app\model\Model {
    private $dbSettings;
    private $dbOject;
    private $connected = false;
    private $resultObject;
    private $insertId = 0;
    
    public function __construct() {
        parent::__construct();
        $conf = $this->getConfig();
        if(isset($conf['db'])) {
            $this->dbSettings = $conf['db'];
            $this->connect();
        } else {
            throw new Exception('No DB settings found in config file!');
        }
    }
    
    /**
     * Connects to datbase using MySQLi
     * @throws Exception
     */
    public function connect() {
        if($this->connected == true) return true;
        $this->dbOject = new mysqli($this->dbSettings['server'], $this->dbSettings['username'], $this->dbSettings['password'], $this->dbSettings['database']);
        if($this->dbOject->connect_errno) {
            throw new Exception('Could not connect to database! Server returned: '. $this->dbOject->connect_errno);
        } else {
            $this->dbOject->set_charset('utf8');
            $this->connected = true;
        }
    }

    public function close() {
        $this->connected = false;
        $this->dbOject->close();
    }
    
    /**
     * Runs a query against the DB
     * 
     * @param type $q
     * @return mysqli_result
     */
    public function query($q = null) {
        if(!is_string($q)) return false;
        if(!$this->connected) return false;
        $this->resultObject = $this->dbOject->query($q);
        if($this->resultObject == false) {
            throw new Exception('Query Error: '. $this->dbOject->error. 'Query: '. $q);
            return false;
        }
        $this->insertId = $this->dbOject->insert_id;
        $this->close();
        return $this->resultObject;
    }
    
    /**
     * Inset some data iinto db
     * 
     * @param string $table
     * @param array $data
     * @return mysqli_result
     * @throws Exception
     */
    public function insert($table = null, $data = null) {
        if(!is_string($table))  throw new Exception('Table given is not a sting');
        if(!is_array($data))    throw new Exception('Data given is not an array!');
        $table = $this->escape($table, false);
        $data = $this->escape($data);
        $fields = "(";
        $values = "(";
        $lenth = count($data);
        $c = 1;
        
        foreach($data as $field => $value) {
            $fields .= "`". $field. "`";
            $values .= $value;
            
            if($c != $lenth) {
                $fields .= ", ";
                $values .= ", ";
                $c++;
            }
        }
        
        $fields .= ")";
        $values .= ")";
        
        $q = "INSERT INTO ". $table. " ". $fields. " VALUES ". $values;
        return ($this->query($q)) ? $this->insertId : false;
    }

    /**
     * Returns list where the query fits
     * 
     * @param string $table
     * @param array $query
     * @return mysqli_result
     * @throws Exception
     */
    public function get_where($table = null, $query = null, $fields = null, $sorting = array(), $limit = 0) {
        if(!is_string($table))  throw new Exception('Table given is not a sting');
        if(!is_array($query))    throw new Exception('Query given is not an array!');
        
        $table = $this->escape($table, false);
        $query = $this->whereValueList($query);

        if(is_array($fields) && count($fields) > 0) {
            $fields = $this->escape($fields, false);
            $fields = $this->getFieldList($fields);
        } else {
            $fields = "*";
        }

        $q = "SELECT $fields FROM ". $table. " WHERE ". $query. " ". $this->getSortingString($sorting). " ". $this->getLimit($limit);
        return $this->query($q);
    }
    
    /**
     * Runs a select command against the given table
     * 
     * @param string $table
     * @return mysqli_result
     * @throws Exception
     */
    public function get($table = null, $sorting = array(), $limit = 0) {
        if(!is_string($table))  throw new Exception('Table given is not a sting');
        $table = $this->escape($table, false);
        $q = "SELECT * FROM ". $table. " ". $this->getSortingString($sorting). " ". $this->getLimit($limit);
        return $this->query($q);
    }

    /**
     * Returns only specific fields from table query
     * @param string $table
     * @param string/array $field
     * @return mysqli_result
     * @throws Exception
     */
    public function get_only($table = null, $field = null, $sort = array(), $limit = 0) {
        if(!is_string($table)) throw new Exception('Table given is not a string');
        if(!is_string($field) && !is_array($field)) throw new Exception('Field given is not string or array');
        $table = $this->escape($table, false);
        $field = $this->escape($field, false);
        if(!is_array($field)) $field = array($field);
        $fildStr = $this->getFieldList($field);

        $q = "SELECT $fildStr FROM $table ". $this->getSortingString($sort). " ". $this->getLimit($limit);
        return $this->query($q);
    }
    
    /**
     * Delets a item in the table, where query is fullfilled
     * 
     * @param string $table
     * @param array $query
     * @return mysqli_result;
     * @throws Exception
     */
    public function delete($table = null, $query = null) {
        if(!is_string($table))  throw new Exception('Table given is not a sting');
        
        $table = $this->escape($table, false);
        $q = "DELETE FROM ". $table;
        
        if(isset($query) && is_array($query)) {
            $query = $this->whereValueList($query);
            $q .= " WHERE ". $query;
        }

        return $this->query($q);
    }
    
    /**
     * Runs an update against DB
     * 
     * @param string $table
     * @param array $set
     * @param array $query
     * @return mysqli_result
     * @throws Exception
     */
    public function update($table = null, $set = null, $query = null) {
        if(!is_string($table))  throw new Exception('Table given is not a sting');
        if(!is_array($set))    throw new Exception('Set parameter given is not an array!');
        
        $table = $this->escape($table, false);
        $set = $this->setValueList($set);
        $q = "UPDATE ". $table. " SET ". $set;
        
        if(is_array($query)) {
            $queryString = $this->setValueList($query);
            $q .= " WHERE ". $queryString;
        }
{'fraction_non_alphanumeric': 0.15972952667167542, 'fraction_numerical': 0.0010518407212622089, 'mean_word_length': 2.034191930704354, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 123, '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': '5634853', 'n_tokens_mistral': 3954, 'n_tokens_neox': 3874, 'n_words': 1005}