<commit_before><?php

/*
	This function is used to read the structure of a database table or 
	an entire database and create yml files for Dinkly

	Example Usages: 
		php tools/import_db.php -s=my_database
		php tools/import_db.php -s=my_database -m=my_table

	Note: You must first add your database to config/db.yml
*/

require_once('config/bootstrap.php');

//get the options to use
$options = getopt("s:m::");
$model_name = null;
if(isset($options['m'])) $model_name = $options['m'];

//call the main function
if($options['s']) getDbStructure($options['s'], $model_name);
else echo "\nYou must specify a database\n";

//this function theoretically would be moved into Dinkly core when completed
function getDbStructure($schema, $model_name = null, $verbose_output = true, $override_database_name = null)
{
	//set active connection to schema and stop if there is no matching schema
	if(!DinklyDataConfig::setActiveConnection($schema))
	{
		echo '\nNo such schema in config/db.yml\n';

		return false;
	} 

	//Connect to database
	$db = DinklyDataConnector::fetchDB();

	$creds = DinklyDataConnector::getDBCreds();
	$db_name = $creds['name'];
	$db_name = DinklyBuilder::sanitize($db, $db_name);
	$model_name = DinklyBuilder::sanitize($db, $model_name);

	//get columns from specified table or all tables
	if($model_name)
	{
		$stmt = $db->prepare("SHOW COLUMNS FROM " . $model_name . "");
		$stmt->execute();	
		$table_schema = $stmt->fetchAll();
		echo '\nRead table ' . $model_name . ' columns...\n';

		sqlToYml($table_schema, $model_name, $db_name);
	}
	else<commit_msg>another adjustment<commit_after><?php

/*
	This function is used to read the structure of a database table or 
	an entire database and create yml files for Dinkly

	Example Usages: 
		php tools/import_db.php -s=my_database
		php tools/import_db.php -s=my_database -m=my_table

	Note: You must first add your database to config/db.yml
*/

require_once('config/bootstrap.php');

//get the options to use
$options = getopt("s:m::");
$model_name = null;
if(isset($options['m'])) $model_name = $options['m'];

//call the main function
if($options['s']) getDbStructure($options['s'], $model_name);
else echo "\nYou must specify a database\n";

//this function theoretically would be moved into Dinkly core when completed
function getDbStructure($schema, $model_name = null, $verbose_output = true, $override_database_name = null)
{
	//set active connection to schema and stop if there is no matching schema
	if(!DinklyDataConfig::setActiveConnection($schema))
	{
		echo '\nNo such schema in config/db.yml\n';

		return false;
	} 

	//Connect to database
	$db = DinklyDataConnector::fetchDB();

	$creds = DinklyDataConfig::getDBCreds();
	$db_name = $creds['name'];
	$db_name = DinklyBuilder::sanitize($db, $db_name);
	$model_name = DinklyBuilder::sanitize($db, $model_name);

	//get columns from specified table or all tables
	if($model_name)
	{
		$stmt = $db->prepare("SHOW COLUMNS FROM " . $model_name . "");
		$stmt->execute();	
		$table_schema = $stmt->fetchAll();
		echo '\nRead table ' . $model_name . ' columns...\n';

		sqlToYml($table_schema, $model_name, $db_name);
	}
	else
{'fraction_non_alphanumeric': 0.14086294416243655, 'fraction_numerical': 0.0, 'mean_word_length': 4.7536496350364965, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '71551', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1050, 'n_words': 300}