<commit_before><?php
namespace dynamic_model;
/*
   Author: <NAME> (c) Elexu Technologies www.elexutech.com
   Distributed under MIT and AGPL Licenses

   Add this controller inside yoru model and it will make sure than all the 
   fields defined in your model are also present in your SQL. If any fields
   are missing, the ALTER table will create them

   DANGER: Using this controller on production system is VERY discouraged,
   as it slows down database performance by doing constant "describe's"
    */
class Controller_AutoCreator extends \AbstractController {
	function init(){
		parent::init();

		$this->db=$this->owner->db;
		$this->table=$this->owner->table;

		// try describe table

		$q=$this->db->dsql()->describe($this->owner->table);

		$missing_fields=$this->owner->elements;

		foreach($q as $line){
			// TODO: match type of field, and perform alter if not matches

			unset($missing_fields[$line['name']]);

		}


		if($missing_fields[$this->owner->id_field]){
			// ouch, id field is missing too
			$this->createTable();
			unset($missing_fields[$this->owner->id_field]);
		}

		foreach($missing_fields as $field)if($field instanceof \Field){

			if($field instanceof \Field_Expression)continue;
			if($field->relation)continue;

			$this->alterField($field, true);

			if($field instanceof \Field_Reference){
				// TODO: create related model and add foreign key
			}
		}
	}
	// TODO: move into separate controller extended for each database type
	function createTable(){
		$q=$this->db->dsql()->expr('create table [cr_table] ([idfield] INTEGER not null PRIMARY KEY [auto_increment] )');
		$q->setCustom('cr_table',$this->table);
		$q->setCustom('auto_increment',$this->db->dsql()->expr($q instanceof DB_dsql_mysql?
			'auto_increment':''));
		$q->setCustom('idfield',$this->owner->id_field);<commit_msg>compatibility with mysql<commit_after><?php
namespace dynamic_model;
/*
   Author: <NAME> (c) Elexu Technologies www.elexutech.com
   Distributed under MIT and AGPL Licenses

   Add this controller inside yoru model and it will make sure than all the 
   fields defined in your model are also present in your SQL. If any fields
   are missing, the ALTER table will create them

   DANGER: Using this controller on production system is VERY discouraged,
   as it slows down database performance by doing constant "describe's"
    */
class Controller_AutoCreator extends \AbstractController {
	function init(){
		parent::init();

		$this->db=$this->owner->db;
		$this->table=$this->owner->table;

		// try describe table

		$q=$this->db->dsql()->describe($this->owner->table);

		$missing_fields=$this->owner->elements;

		try{
			foreach($q as $line){
				// TODO: match type of field, and perform alter if not matches

				unset($missing_fields[$line['name'] ?: $line['Field']]);

			}
		}catch(\Exception $e){
			// no table;
		}


		if($missing_fields[$this->owner->id_field]){
			// ouch, id field is missing too
			$this->createTable();
			unset($missing_fields[$this->owner->id_field]);
		}

		foreach($missing_fields as $field)if($field instanceof \Field){

			if($field instanceof \Field_Expression)continue;
			if($field->relation)continue;

			$this->alterField($field, true);

			if($field instanceof \Field_Reference){
				// TODO: create related model and add foreign key
			}
		}
	}
	// TODO: move into separate controller extended for each database type
	function createTable(){
		$q=$this->db->dsql()->expr('create table [cr_table] ([idfield] INTEGER not null PRIMARY KEY [auto_increment] )');
		$q->setCustom('cr_table',$this->table);
		$q->debug();
		$q->setCustom('auto_increment',$this->db->dsql()->expr($q instanceof \DB_dsql_mysql?
			'auto_increment':''));
		$q->setCustom('idfield',$this->owner->id_field);
{'fraction_non_alphanumeric': 0.14399787064146927, 'fraction_numerical': 0.0, 'mean_word_length': 4.315417256011315, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '123571', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1122, 'n_words': 326}