<commit_before><?lassoscript

//------------------------------------------------------------------------
//
//	Slapcode handles
//
//------------------------------------------------------------------------

define slapcode => var(_slapcode) || $_slapcode := slapcode_type
define slapcode_editor => slapcode->current_editor
define slapcode_result => slapcode->current_result

define slap => debug => givenblock

define slap(p::any) => {
	debug(#p)
	return #p
}

//------------------------------------------------------------------------
//
//	Slapcode settings
//
//------------------------------------------------------------------------

define slapcode_host= (val::string) => { var(__slapcode_host) = #val } 
define slapcode_host => var(__slapcode_host) || ''

define slapcode_database= (val::string) => { var(__slapcode_database) = #val }
define slapcode_database => var(__slapcode_database) || 'slapcode'

define slapcode_username= (val::string) => { var(__slapcode_username) = #val }
define slapcode_username => var(__slapcode_username) || ''

define slapcode_password= (val::string) => { var(__slapcode_password) = #val }
define slapcode_password => var(__slapcode_password) || ''

//------------------------------------------------------------------------
//
//	Slapcode types
//
//------------------------------------------------------------------------

define slapcode_type => type {
	data 
		public database = 'slapcode',

		public client_id::integer=0,
		public project_id::integer=0,
		public user_id::integer=0,

		public compile::boolean=true,
		public debug::boolean = true,
		
		private compiled,
		private output=map,
		private ds
		

	public oncreate => {
		//	Start session
		.project_id = web_request->postparam('project_id')->asinteger
		.client_id  = web_request->postparam('client_id')->asinteger
		.user_id    = web_request->postparam('user_id')->asinteger
		
	}
	
	public ds => .'ds' || .'ds' := ds(
		-datasource	= 'mysqlds',
		-table 		= 'code',
		-database	= slapcode_database, 
		-host 		= slapcode_host, 
		-username 	= slapcode_username,
		-username 	= slapcode_password
	)
	
	public installed => .installation == 'installed'

	public installation => {
		var(__slapcode_install)->isnota(::null) ? return $__slapcode_install
		
		debug('Slapcode Installation') => {
		
			local(installed) = false
			
			protect => {
				handle => {
					
					debug(error_code ? 'Error' | 'OK')
					
					local(error) = error_msg
						
					match(true) => {
						case(#error >> 'Unknown database')
							return $__slapcode_install := 'No database'	
						case(#error >> 'doesn\'t exist')
							return $__slapcode_install := 'No tables'	
						
					}
				}
	
				// Create if not there
				.ds->sql('CREATE DATABASE IF NOT EXISTS `'+slapcode_database + '`')
	
				// Check for database
				.ds->sql('USE '+slapcode_database)
				
				// Create tables
				.createtables
				
				#installed = .ds->info->columns->size > 0
			}
		
		}
	
		return $__slapcode_install := (#installed ? true | false)
	}

	
	public asstring => {

		local(
			out = .output,

			id = integer(web_request->postparam('id')) || null,
			project_id = .project_id,
			client_id = .client_id,
			user_id = .user_id,

			name = web_request->postparam('name')->asstring,
			code = web_request->postparam('code')->asstring,
			task = web_request->param('task')->asstring,
			
			project_name = web_request->postparam('project_name')->asstring,
			project

		)
		
		debug('task'=#task)
					
		match(#task) => {
		
			case('load')
				if(#project_id)=>{
					#out->insert('project' = .load('projects',#project_id))
				else
					#out->insert('project' = .default_project)
				}
				#out->insert('projects' = .projects(#client_id,#user_id))
				#out->insert('files' = .files(#project_id))
				#out->insert('tabs' = .tabs(#project_id))

				return .current_result

			case('compile')
				.compile(#code)
				return .current_result

			case('open')
				.update('code',map('isopen'=0),#id)
				#out->insert(
					'tab' = .load('code',#id)
				)
				return .current_result

			case('close')
				.update('code',map('isopen'=0),#id)
				return .current_result
					
			case('saveproject')
				#project 	= .save('projects',#project_id,'name' = #project_name)
				#project_id = #project->keyvalue
				#out->insert(
					'project' = #project->asmap
				)
				return .current_result

			case('save')			
				.savecode(#id,#name,#code)
				return .current_result
			case('projects')
				#out->insert('projects'=.projects)
				return .current_result
			case('files')
				#out->insert('files'=.files)
				return .current_result
			case('tabs')
				#out->insert('tabs'=.tabs)
				return .current_result
			case('run')
				debug->activate	
				.run(#code)
				
				#out->insert(
					'id' = .savecode(#id,#name,#code)->keyvalue 
				)
				
				return .current_result
			case
				debug->activate	
		}
		
		return include('_templates/host.htm')
	}
	
	public current_editor => {
		return include('_templates/editor.htm')
	}
	
	public current_result => {

		local(out = .output || map)
		
		#out->insert(
			'debug' = debug->ashtml
		)
		
//		content_body += #out
		
		if(client_headers >> 'json') => {
			content_type('application/json; charset=UTF-8')
			return json_serialize(#out || null)
		else
			return #out->values->join('')
		}

	}
	
	public createtables => {
	
		local(sql) = ''	
				
		#sql->append(
			'CREATE TABLE IF NOT EXISTS `clients` (
				`ID` bigint(20) NOT NULL AUTO_INCREMENT,
				`name` VARCHAR(128) NOT NULL DEFAULT "",
				PRIMARY KEY (`ID`)
			) AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n'
		)		
		#sql->append(
			'CREATE TABLE IF NOT EXISTS `users` (
				`ID` bigint(20) NOT NULL AUTO_INCREMENT,
				`hash` VARCHAR(128) NOT NULL DEFAULT "",
				`client_id` bigint(20) NOT NULL,
				PRIMARY KEY (`ID`)
			) AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n'
		)		
		
		#sql->append(
			'CREATE TABLE IF NOT EXISTS `projects` (
				`ID` bigint(20) NOT NULL AUTO_INCREMENT,
				`created` datetime DEFAULT NULL,
				`modified` datetime DEFAULT NULL,
				`user_id` bigint(20) DEFAULT 0,
				`client_id` bigint(20) DEFAULT 0,
				`name` varchar(128) DEFAULT '',
				`code` text,
				PRIMARY KEY (`ID`)
			) AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n'
		)		
		
		#sql->append(
			'CREATE TABLE IF NOT EXISTS `code` (
				`ID` bigint(20) NOT NULL AUTO_INCREMENT,
				`created` datetime DEFAULT NULL,
				`modified` datetime DEFAULT NULL,
				`project_id` bigint(20) DEFAULT 0,
				`user_id` bigint(20) NOT NULL DEFAULT 0,
				`name` VARCHAR(64) NOT NULL,
				`isopen` tinyint(2) NOT NULL DEFAULT 1,
				`code` text,
				PRIMARY KEY (`ID`),
				UNIQUE KEY `project_key` (`project_id`,`name`)
			) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n'
		)
		<commit_msg>Fixed table definitions<commit_after><?lassoscript

//------------------------------------------------------------------------
//
//	Slapcode handles
//
//------------------------------------------------------------------------

define slapcode => var(_slapcode) || $_slapcode := slapcode_type
define slapcode_editor => slapcode->current_editor
define slapcode_result => slapcode->current_result

define slap => debug => givenblock

define slap(p::any) => {
	debug(#p)
	return #p
}

//------------------------------------------------------------------------
//
//	Slapcode settings
//
//------------------------------------------------------------------------

define slapcode_host= (val::string) => { var(__slapcode_host) = #val } 
define slapcode_host => var(__slapcode_host) || ''

define slapcode_database= (val::string) => { var(__slapcode_database) = #val }
define slapcode_database => var(__slapcode_database) || 'slapcode'

define slapcode_username= (val::string) => { var(__slapcode_username) = #val }
define slapcode_username => var(__slapcode_username) || ''

define slapcode_password= (val::string) => { var(__slapcode_password) = #val }
define slapcode_password => var(__slapcode_password) || ''

//------------------------------------------------------------------------
//
//	Slapcode types
//
//------------------------------------------------------------------------

define slapcode_type => type {
	data 
		public database = 'slapcode',

		public client_id::integer=0,
		public project_id::integer=0,
		public user_id::integer=0,

		public compile::boolean=true,
		public debug::boolean = true,
		
		private compiled,
		private output=map,
		private ds
		

	public oncreate => {
		//	Start session
		.project_id = web_request->postparam('project_id')->asinteger
		.client_id  = web_request->postparam('client_id')->asinteger
		.user_id    = web_request->postparam('user_id')->asinteger
		
	}
	
	public ds => .'ds' || .'ds' := ds(
		-datasource	= 'mysqlds',
		-table 		= 'code',
		-database	= slapcode_database, 
		-host 		= slapcode_host, 
		-username 	= slapcode_username,
		-username 	= slapcode_password
	)
	
	public installed => .installation == 'installed'

	public installation => {
		var(__slapcode_install)->isnota(::null) ? return $__slapcode_install
		
		debug('Slapcode Installation') => {
		
			local(installed) = false
			
			protect => {
				handle => {
					
					debug(error_code ? 'Error' | 'OK')
					
					local(error) = error_msg
						
					match(true) => {
						case(#error >> 'Unknown database')
							return $__slapcode_install := 'No database'	
						case(#error >> 'doesn\'t exist')
							return $__slapcode_install := 'No tables'	
						
					}
				}
	
				// Create if not there
				.ds->sql('CREATE DATABASE IF NOT EXISTS `'+slapcode_database + '`')
	
				// Check for database
				.ds->sql('USE '+slapcode_database)
				
				// Create tables
				.createtables
				
				#installed = .ds->info->columns->size > 0
			}
		
		}
	
		return $__slapcode_install := (#installed ? true | false)
	}

	
	public asstring => {

		local(
			out = .output,

			id = integer(web_request->postparam('id')) || null,
			project_id = .project_id,
			client_id = .client_id,
			user_id = .user_id,

			name = web_request->postparam('name')->asstring,
			code = web_request->postparam('code')->asstring,
			task = web_request->param('task')->asstring,
			
			project_name = web_request->postparam('project_name')->asstring,
			project

		)
		
		debug('task'=#task)
					
		match(#task) => {
		
			case('load')
				if(#project_id)=>{
					#out->insert('project' = .load('projects',#project_id))
				else
					#out->insert('project' = .default_project)
				}
				#out->insert('projects' = .projects(#client_id,#user_id))
				#out->insert('files' = .files(#project_id))
				#out->insert('tabs' = .tabs(#project_id))

				return .current_result

			case('compile')
				.compile(#code)
				return .current_result

			case('open')
				.update('code',map('isopen'=0),#id)
				#out->insert(
					'tab' = .load('code',#id)
				)
				return .current_result

			case('close')
				.update('code',map('isopen'=0),#id)
				return .current_result
					
			case('saveproject')
				#project 	= .save('projects',#project_id,'name' = #project_name)
				#project_id = #project->keyvalue
				#out->insert(
					'project' = #project->asmap
				)
				return .current_result

			case('save')			
				.savecode(#id,#name,#code)
				return .current_result
			case('projects')
				#out->insert('projects'=.projects)
				return .current_result
			case('files')
				#out->insert('files'=.files)
				return .current_result
			case('tabs')
				#out->insert('tabs'=.tabs)
				return .current_result
			case('run')
				debug->activate	
				.run(#code)
				
				#out->insert(
					'id' = .savecode(#id,#name,#code)->keyvalue 
				)
				
				return .current_result
			case
				debug->activate	
		}
		
		return include('_templates/host.htm')
	}
	
	public current_editor => {
		return include('_templates/editor.htm')
	}
	
	public current_result => {

		local(out = .output || map)
		
		#out->insert(
			'debug' = debug->ashtml
		)
		
//		content_body += #out
		
		if(client_headers >> 'json') => {
			content_type('application/json; charset=UTF-8')
			return json_serialize(#out || null)
		else
			return #out->values->join('')
		}

	}
	
	public createtables => {
	
		local(sql) = ''	
				
		#sql->append(
			'CREATE TABLE IF NOT EXISTS `clients` (
				`ID` bigint(20) NOT NULL AUTO_INCREMENT,
				`name` VARCHAR(128) NOT NULL DEFAULT "",
				PRIMARY KEY (`ID`)
			) AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n'
		)		
		#sql->append(
			'CREATE TABLE IF NOT EXISTS `users` (
				`ID` bigint(20) NOT NULL AUTO_INCREMENT,
				`hash` VARCHAR(128) NOT NULL DEFAULT "",
				`client_id` bigint(20) NOT NULL,
				PRIMARY KEY (`ID`)
			) AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n'
		)		
		
		#sql->append(
			'CREATE TABLE IF NOT EXISTS `projects` (
				`ID` bigint(20) NOT NULL AUTO_INCREMENT,
				`created` datetime DEFAULT NULL,
				`modified` datetime DEFAULT NULL,
				`user_id` bigint(20) DEFAULT 0,
				`client_id` bigint(20) DEFAULT 0,
				`name` varchar(128) DEFAULT "",
				`code` text,
				PRIMARY KEY (`ID`)
			) AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n'
		)		
		
		#sql->append(
			'CREATE TABLE IF NOT EXISTS `code` (
				`ID` bigint(20) NOT NULL AUTO_INCREMENT,
				`created` datetime DEFAULT NULL,
				`modified` datetime DEFAULT NULL,
				`project_id` bigint(20) DEFAULT 0,
				`user_id` bigint(20) NOT NULL DEFAULT 0,
				`name` VARCHAR(64) NOT NULL,
				`isopen` tinyint(2) NOT NULL default 1,
			
				`code` text,
				PRIMARY KEY (`ID`),
				UNIQUE KEY `project_key` (`project_id`,`name`)
			) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n'
		)
		
{'fraction_non_alphanumeric': 0.2095435684647303, 'fraction_numerical': 0.007409602845287492, 'mean_word_length': 2.472343709801904, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 171, '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': '6938697', 'n_tokens_mistral': 5587, 'n_tokens_neox': 4344, 'n_words': 872}