PARAMETERS lInit


=init_dbJson(!EMPTY(lInit))


FUNCTION init_dbJson(lClear)

		IF !EMPTY(lClear)
			*oApp.sqlexec('drop table ghost_json')
			oApp.sqlexec('drop table ghost_dblog')
			oApp.sqlexec('drop table loader_d')
			oApp.sqlexec('drop table loader_h')
		ENDIF
		
		
		TEXT TO cCmd NOSHOW
			CREATE TABLE IF NOT EXISTS ghost_json (
			ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,  
			idcmd INTEGER DEFAULT 0,  
			cmd varchar(40) DEFAULT '',  
			param longtext,
			jparam longtext,
			iduser INTEGER DEFAULT 0,
			leido tinyint(4) DEFAULT 0,
			fechahora datetime default "20170101",
			llave varchar(20) default "",
			PRIMARY KEY (ID)
			) ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
		ENDTEXT
		
		oApp.sqlexec(cCmd)

		TEXT TO cCmd NOSHOW
			CREATE TABLE IF NOT EXISTS ghost_dblog (
			ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,  
			error longtext,
			PRIMARY KEY (ID)
			) ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
		ENDTEXT
		
		oApp.sqlexec(cCmd)
		

		
		TEXT TO cCmd NOSHOW
			CREATE TABLE IF NOT EXISTS ghost.commander (  
			ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,  
			iduser int(10) DEFAULT '0',  
			idc int(11) DEFAULT '0',  
			cmd varchar(40) DEFAULT NULL,  
			param longtext,  jparam longtext NOT NULL,  
			respuesta longtext,  tiempo datetime DEFAULT NULL,  
			tleido datetime DEFAULT NULL,  estado int(11) DEFAULT '0',  
			leido tinyint(4) NOT NULL DEFAULT '0',  
			PRIMARY KEY (id),  KEY ipk1 (idc),  
			KEY ipk2 (idc,iduser,cmd)
			) ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
		ENDTEXT
		
		oApp.sqlexec(cCmd)
		
		TEXT TO cCmd NOSHOW
			CREATE TABLE IF NOT EXISTS ghost.control (  
			id int(10) NOT NULL AUTO_INCREMENT,  
			ruc varchar(20) DEFAULT NULL,
			nombre varchar(80) DEFAULT NULL,
			users_vfac text NOT NULL,
			activa int(11) DEFAULT '0',
			activity int(11) DEFAULT '0',
			PRIMARY KEY (id),  KEY ipk1 (ruc)
			) ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
		ENDTEXT	
		
		oApp.sqlexec(cCmd)
		
		IF oApp.sqlexec('SELECT id from ghost.control where ruc="'+oApp.cbRuc+'"')=0
		
			TEXT TO cCmd NOSHOW TEXTMERGE
				INSERT INTO ghost.control (ruc, nombre, users_vfac, activa, activity) 
				VALUES("<<oApp.cbRUC>>", "<<oApp.cbComercio>>", '1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21', 1, 1) ;
			ENDTEXT	
			
			oApp.sqlexec(cCmd)
		ENDIF
		
		
ENDFUNC

