<commit_before><?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Sections_model extends CI_Model {

    public function get_section($slug) {
        return $this->db->get_where('wd_sections', array('slug' => $slug))->row_array();
    }

    public function search_sections($page, $keyword = null, $total = null, $offset = null) {
        $this->db->like('name', $keyword);
        $this->db->limit($total, $offset);
        $this->db->order_by('id DESC');
        $this->db->where('fk_page', $page);
        return $this->db->get('wd_sections')->result_array();
    }

    public function search_sections_total_rows($page, $keyword = null) {
        $this->db->select('count(id) total');
        $this->db->like('name', $keyword);
        $this->db->where('fk_page', $page);
        return $this->db->get('wd_sections')->row()->total;
    }

    public function list_sections_select($section = null) {
        $this->db->order_by('id DESC');
        //$this->db->where('fk_page', $page);
        if ($section) {
            $this->db->where('id!=', $section);
        }
        return $this->db->get('wd_sections')->result_array();
    }

    public function create($data) {
        $set = [
            'fk_page' => $data['page'],
            'name' => $data['name'],
            'directory' => $data['directory'],
            'table' => $data['table'],
            'status' => $data['status'],
            'slug' => $data['slug']
        ];
        $this->db->insert('wd_sections', $set);
    }

    public function list_sections($page) {
        return $this->db->get_where('wd_sections', ['fk_page' => $page, 'status' => '1'])->result_array();
    }

    public function remove($table, $section) {
        $remove = $this->db->delete('wd_sections', array('id' => $section));
        if ($remove) {
            return $this->remove_table($table);
        }
    }

    public function remove_table($table) {
        $this->load->dbforge();
        $stmt = $this->dbforge->drop_table($table);
        return $stmt;
    }

    public function create_columns($table, $fields) {
        $this->load->dbforge();
        foreach ($fields as $field) {
            $column = $field['column'];
            $type = $field['type'];
            $limit = $field['limit'];
            $default = $field['default'];
            $comment = $field['comment'];
            $set = array();
            $set[$column] = array(
                'type' => $type,
                'constraint' => $limit,
                'comment' => $comment
            );
            if (!empty($default)) {
                $set[$column]['default'] = $default;
            }
            $this->dbforge->add_column($table, $set);
        }
    }

    public function create_table($table) {
        $this->load->dbforge();
        /* Columns default */
        $fields['id'] = array(
            'type' => 'INT',
            'auto_increment' => TRUE
        );
        $this->dbforge->add_field($fields);
        $this->dbforge->add_key('id', TRUE);
        $stmt = $this->dbforge->create_table($table, TRUE, ['engine' => 'InnoDB']);
        return $stmt;
    }

    public function check_table_exists($table) {
        $check = $this->db->query('SELECT * FROM information_schema.tables WHERE table_name = ?', array($table))->row();
        return $check;
    }

    public function remove_column($data) {
        $table = $data['table'];
        $column = $data['old_column'];
        $this->load->dbforge();
        $remove = $this->dbforge->drop_column($table, $column);
        return $remove;
    }

    public function modify_column($data) {
        $table = $data['table'];
        $column = $data['column'];
        $old_column = $data['old_column'];
        $type = $data['type'];
        $limit = $data['limit'];
        $default = $data['default'];
        $comment = $data['comment'];<commit_msg>Correção de erro ao verificar se tabela existe<commit_after><?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Sections_model extends CI_Model {

    public function get_section($slug) {
        return $this->db->get_where('wd_sections', array('slug' => $slug))->row_array();
    }

    public function search_sections($page, $keyword = null, $total = null, $offset = null) {
        $this->db->like('name', $keyword);
        $this->db->limit($total, $offset);
        $this->db->order_by('id DESC');
        $this->db->where('fk_page', $page);
        return $this->db->get('wd_sections')->result_array();
    }

    public function search_sections_total_rows($page, $keyword = null) {
        $this->db->select('count(id) total');
        $this->db->like('name', $keyword);
        $this->db->where('fk_page', $page);
        return $this->db->get('wd_sections')->row()->total;
    }

    public function list_sections_select($section = null) {
        $this->db->order_by('id DESC');
        //$this->db->where('fk_page', $page);
        if ($section) {
            $this->db->where('id!=', $section);
        }
        return $this->db->get('wd_sections')->result_array();
    }

    public function create($data) {
        $set = [
            'fk_page' => $data['page'],
            'name' => $data['name'],
            'directory' => $data['directory'],
            'table' => $data['table'],
            'status' => $data['status'],
            'slug' => $data['slug']
        ];
        $this->db->insert('wd_sections', $set);
    }

    public function list_sections($page) {
        return $this->db->get_where('wd_sections', ['fk_page' => $page, 'status' => '1'])->result_array();
    }

    public function remove($table, $section) {
        $remove = $this->db->delete('wd_sections', array('id' => $section));
        if ($remove) {
            return $this->remove_table($table);
        }
    }

    public function remove_table($table) {
        $this->load->dbforge();
        $stmt = $this->dbforge->drop_table($table);
        return $stmt;
    }

    public function create_columns($table, $fields) {
        $this->load->dbforge();
        foreach ($fields as $field) {
            $column = $field['column'];
            $type = $field['type'];
            $limit = $field['limit'];
            $default = $field['default'];
            $comment = $field['comment'];
            $set = array();
            $set[$column] = array(
                'type' => $type,
                'constraint' => $limit,
                'comment' => $comment
            );
            if (!empty($default)) {
                $set[$column]['default'] = $default;
            }
            $this->dbforge->add_column($table, $set);
        }
    }

    public function create_table($table) {
        $this->load->dbforge();
        /* Columns default */
        $fields['id'] = array(
            'type' => 'INT',
            'auto_increment' => TRUE
        );
        $this->dbforge->add_field($fields);
        $this->dbforge->add_key('id', TRUE);
        $stmt = $this->dbforge->create_table($table, TRUE, ['engine' => 'InnoDB']);
        return $stmt;
    }

    public function check_table_exists($table) {
        $database = $this->db->database;
        $check = $this->db->query('SELECT * FROM information_schema.tables WHERE table_schema=? AND table_name = ?', array($database, $table))->row();
        return $check;
    }

    public function remove_column($data) {
        $table = $data['table'];
        $column = $data['old_column'];
        $this->load->dbforge();
        $remove = $this->dbforge->drop_column($table, $column);
        return $remove;
    }

    public function modify_column($data) {
        $table = $data['table'];
        $column = $data['column'];
        $old_column = $data['old_column'];
        $type = $data['type'];
        $limit = $data['limit'];
        $default = $data['default'];
        $comment = $data['comment'];
{'fraction_non_alphanumeric': 0.19323061379136144, 'fraction_numerical': 0.00025258903763576663, 'mean_word_length': 2.2561677631578947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 165, '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': '2767288', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2363, 'n_words': 433}