<commit_before><?php
/**
 * core/models/base.php
 *
 * Base Neechy model class.
 *
 * Provides basic interface for models. Each model should be associated with
 * a table and a schema property holding the sql for the table. It also
 * establishes a single PDO connection for all model instances.
 *
 *
 * USAGE
 *  require_once('core/models/base.php');
 *
 *  class Page extends NeechyModel {
 *  }
 *
 */
require_once('../core/neechy/constants.php');
require_once('../core/neechy/database.php');



class NeechyModel {

    /*
     * Static Properties
     * (These are just a sample and should be overridden in base class)
     */
    protected static $schema = <<<MYSQL
CREATE TABLE neeches (
	id int(10) unsigned NOT NULL auto_increment,
	neech varchar(75) NOT NULL default '',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME default NULL,
	PRIMARY KEY  (id),
	KEY idx_neech (neech)
) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE={{ engine }}
MYSQL;

    protected static $immutable_fields = array('id', 'created_at', 'updated_at');

    /*
     * Properties
     */
    public $fields = array();
    public $table = '';
    public $pdo = null;
    public $rows_affected = 0;

    /*
     * Constructor
     */
    public function __construct($fields=array()) {
        $this->table = self::extract_table_name();
        $this->pdo = NeechyDatabase::connect_to_db();
        $this->fields = $fields;
    }

    /*
     * Static Methods
     */
    static public function init($fields=array()) {
        $class = get_called_class();
        $instance = new $class($fields);
        return $instance;
    }

    static public function get_schema() {
        $schema = str_replace('{{ engine }}', MYSQL_ENGINE, static::$schema);
        return $schema;
    }

    static public function table_exists() {
        # http://stackoverflow.com/a/14355475/1093087
        $sql = sprintf('SELECT 1 FROM %s LIMIT 1', self::extract_table_name());
        $pdo = NeechyDatabase::connect_to_db();

        try {
            $found = $pdo->query($sql);
        } catch (PDOException $e) {
            return FALSE;
        }

        return $found !== FALSE;
    }

    static public function create_table_if_not_exists() {
        if ( ! self::table_exists() ) {
            $model_class = get_called_class();
            $model = new $model_class();
            $model->pdo->exec($model_class::get_schema());
            return $model->table;
        }
        else {
            return null;
        }
    }

    static public function all() {
        $sql = sprintf('SELECT * FROM %s', self::extract_table_name());
        $pdo = NeechyDatabase::connect_to_db();
        $statement = $pdo->query($sql);
        return $statement->fetchAll();<commit_msg>Adds count and table methods to base model class.<commit_after><?php
/**
 * core/models/base.php
 *
 * Base Neechy model class.
 *
 * Provides basic interface for models. Each model should be associated with
 * a table and a schema property holding the sql for the table. It also
 * establishes a single PDO connection for all model instances.
 *
 *
 * USAGE
 *  require_once('core/models/base.php');
 *
 *  class Page extends NeechyModel {
 *  }
 *
 */
require_once('../core/neechy/constants.php');
require_once('../core/neechy/database.php');



class NeechyModel {

    /*
     * Static Properties
     * (These are just a sample and should be overridden in base class)
     */
    protected static $schema = <<<MYSQL
CREATE TABLE neeches (
	id int(10) unsigned NOT NULL auto_increment,
	neech varchar(75) NOT NULL default '',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME default NULL,
	PRIMARY KEY  (id),
	KEY idx_neech (neech)
) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE={{ engine }}
MYSQL;

    protected static $immutable_fields = array('id', 'created_at', 'updated_at');

    /*
     * Properties
     */
    public $fields = array();
    public $table = '';
    public $pdo = null;
    public $rows_affected = 0;

    /*
     * Constructor
     */
    public function __construct($fields=array()) {
        $this->table = self::extract_table_name();
        $this->pdo = NeechyDatabase::connect_to_db();
        $this->fields = $fields;
    }

    /*
     * Static Methods
     */
    static public function init($fields=array()) {
        $class = get_called_class();
        $instance = new $class($fields);
        return $instance;
    }

    static public function get_schema() {
        $schema = str_replace('{{ engine }}', MYSQL_ENGINE, static::$schema);
        return $schema;
    }

    static public function table_name() {
        return self::extract_table_name();
    }

    static public function table_exists() {
        # http://stackoverflow.com/a/14355475/1093087
        $sql = sprintf('SELECT 1 FROM %s LIMIT 1', self::extract_table_name());
        $pdo = NeechyDatabase::connect_to_db();

        try {
            $found = $pdo->query($sql);
        } catch (PDOException $e) {
            return FALSE;
        }

        return $found !== FALSE;
    }

    static public function create_table_if_not_exists() {
        if ( ! self::table_exists() ) {
            $model_class = get_called_class();
            $model = new $model_class();
            $model->pdo->exec($model_class::get_schema());
            return $model->table;
        }
        else {
            return null;
        }
    }

    static public function drop_table_if_exists() {
        $model_class = get_called_class();
        $model = new $model_class();

        if ( self::table_exists() ) {
            $model->pdo->exec(sprintf('DROP TABLE IF EXISTS `%s`', $model->table));
        }

        return $model;
    }

    static public function all() {
        $sql = sprintf('SELECT * FROM %s', self::extract_table_name());
        $pdo = NeechyDatabase::connect_to_db();
        $statement = $pdo->query($sql);
        return $statement->fetchAll();
    }

    static public function count() {
        $sql = sprintf('SELECT COUNT(*) FROM %s', self::extract_table_name());
        $pdo = NeechyDatabase::connect_to_db();
        return $pdo->query($sql)->fetchColumn();
{'fraction_non_alphanumeric': 0.12990353697749196, 'fraction_numerical': 0.0077170418006430866, 'mean_word_length': 2.536668561682774, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 26, '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': '5859657', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1866, 'n_words': 478}