library/src/blockclass/subclasses/command_line_tools/BP_CommandLineToolBase.cc
// insert command line tool into the db
insert command line tool into the db
insert command line tool into the db
BP_ERROR_T BP_CommandLineToolBase::insertCommandLineTool(char * sensor_id, char *name, char *version, char *description)
{
	if(!sensor_id && !name && !version && !description)
	{
		return ERR_FAILURE;
	}
	if(strnlen(sensor_id, BP_KILOBYTE * 8) > (BP_KILOBYTE * 4))
		return ERR_FAILURE;
	if(strnlen(name, BP_KILOBYTE * 8) > (BP_KILOBYTE * 4))
		return ERR_FAILURE;
	if(strnlen(version, BP_KILOBYTE * 8) > (BP_KILOBYTE * 4))
		return ERR_FAILURE;
	if(strnlen(description, BP_KILOBYTE * 8) > (BP_KILOBYTE * 4))
		return ERR_FAILURE;
	BP_StringUtils * builder = new BP_StringUtils(NULL);
	char * sensor_id_s   = NULL;
	char * name_s        = NULL;
	char * version_s     = NULL;
	char * description_s = NULL;
	if(!sensor_id)
		sensor_id_s = (char *) bpstrdup("NULL");
	else
		sensor_id_s = (char *) BP_BufferToMySQLHexString(sensor_id, bpstrlen(sensor_id));
	if(!name)
		name_s = (char *) bpstrdup("NULL");
	else
		name_s = (char *) BP_BufferToMySQLHexString(name, bpstrlen(name));
	if(!version)
		version_s = (char *) bpstrdup("NULL");
	else
		version_s = (char *) BP_BufferToMySQLHexString(version, bpstrlen(version));
	if(!description)
		description_s = (char *) bpstrdup("NULL");
	else
		description_s = (char *) BP_BufferToMySQLHexString(description, bpstrlen(description));
	builder->BuildString((char *) "INSERT INTO `command_line_tools` ( `tool_sha1`, `sensor_identifier_sha1`, `run_attempts`, `name`, `version`, `description` ) VALUES ");
	char * tool_sha1 = (char *) NULL;
	size_t alloc_size =  bpstrlen(sensor_id)+bpstrlen(name)+64;
	char * unique_buff = (char *) tq_calloc(this->application_m, alloc_size, 1, BPLN);
	snprintf(unique_buff, alloc_size-1, "%s%s", sensor_id, name);
	size_t c = bpstrlen(unique_buff);
	this->GenSHA1(unique_buff, c);
	if(!this->sha1_ascii)
		this->sha1_ascii = (char *) tq_strdup(this->application_m, "BADSHA1FROMBLOCK", BPLN);
	builder->BuildString(" ( '");
	builder->BuildString(this->sha1_ascii);
	builder->BuildString("' , ");
	builder->BuildString(sensor_id_s);
	builder->BuildString(" , ");
	builder->BuildString(" 1 ");
	builder->BuildString(" , ");
	builder->BuildString(name_s);
	builder->BuildString(" , ");
	builder->BuildString(version_s);
	builder->BuildString(" , ");
	builder->BuildString(description_s);
	builder->BuildString(" ) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id), run_attempts = run_attempts + 1");
	if(unique_buff)
		tq_free(this->application_m, unique_buff);
	if(this->mysqlPrepareQuery(builder->build_str) != ERR_SUCCESS)
	{
		delete builder;
		if(sensor_id_s)
			bpfree(sensor_id_s);
		if(name_s)
			bpfree(name_s);
		if(version_s)
			bpfree(version_s);
		if(description_s)
			bpfree(description_s);
		return ERR_FAILURE;
	}
	if(this->mysqlExecuteStatement() != ERR_SUCCESS)
	{
		delete builder;
		if(sensor_id_s)
			bpfree(sensor_id_s);
		if(name_s)
			bpfree(name_s);
		if(version_s)
			bpfree(version_s);
		if(description_s)
			bpfree(description_s);
		return ERR_FAILURE;
	}
	if(this->mysqlSession->stmt)
	{
#if USE_MARIADB == 1
	this->cltdb_id = mysql_insert_id(this->mysqlSession->conn);
#else
	this->cltdb_id =this->mysqlSession->stmt->insert_id;
#endif
	}
	if(this->mysqlDestroyStatement() != ERR_SUCCESS)
	{
		delete builder;
		if(sensor_id_s)
			bpfree(sensor_id_s);
		if(name_s)
			bpfree(name_s);
		if(version_s)
			bpfree(version_s);
		if(description_s)
			bpfree(description_s);
		return ERR_FAILURE;
	}
	if(sensor_id_s)
		bpfree(sensor_id_s);
	if(name_s)
		bpfree(name_s);
	if(version_s)
		bpfree(version_s);
	if(description_s)
		bpfree(description_s);
	delete builder;
	return ERR_SUCCESS;
}
BP_ERROR_T BP_CommandLineToolBase :: insertCommandLineTool ( char * sensor_id , char * name , char * version , char * description ) { if ( ! sensor_id && ! name && ! version && ! description ) { return ERR_FAILURE ; } if ( strnlen ( sensor_id , BP_KILOBYTE * 8 ) > ( BP_KILOBYTE * 4 ) ) return ERR_FAILURE ; if ( strnlen ( name , BP_KILOBYTE * 8 ) > ( BP_KILOBYTE * 4 ) ) return ERR_FAILURE ; if ( strnlen ( version , BP_KILOBYTE * 8 ) > ( BP_KILOBYTE * 4 ) ) return ERR_FAILURE ; if ( strnlen ( description , BP_KILOBYTE * 8 ) > ( BP_KILOBYTE * 4 ) ) return ERR_FAILURE ; BP_StringUtils * builder = new BP_StringUtils ( NULL ) ; char * sensor_id_s = NULL ; char * name_s = NULL ; char * version_s = NULL ; char * description_s = NULL ; if ( ! sensor_id ) sensor_id_s = ( char * ) bpstrdup ( " " ) ; else sensor_id_s = ( char * ) BP_BufferToMySQLHexString ( sensor_id , bpstrlen ( sensor_id ) ) ; if ( ! name ) name_s = ( char * ) bpstrdup ( " " ) ; else name_s = ( char * ) BP_BufferToMySQLHexString ( name , bpstrlen ( name ) ) ; if ( ! version ) version_s = ( char * ) bpstrdup ( " " ) ; else version_s = ( char * ) BP_BufferToMySQLHexString ( version , bpstrlen ( version ) ) ; if ( ! description ) description_s = ( char * ) bpstrdup ( " " ) ; else description_s = ( char * ) BP_BufferToMySQLHexString ( description , bpstrlen ( description ) ) ; builder -> BuildString ( ( char * ) " " ) ; char * tool_sha1 = ( char * ) NULL ; size_t alloc_size = bpstrlen ( sensor_id ) + bpstrlen ( name ) + 64 ; char * unique_buff = ( char * ) tq_calloc ( this -> application_m , alloc_size , 1 , BPLN ) ; snprintf ( unique_buff , alloc_size - 1 , " " , sensor_id , name ) ; size_t c = bpstrlen ( unique_buff ) ; this -> GenSHA1 ( unique_buff , c ) ; if ( ! this -> sha1_ascii ) this -> sha1_ascii = ( char * ) tq_strdup ( this -> application_m , " " , BPLN ) ; builder -> BuildString ( " " ) ; builder -> BuildString ( this -> sha1_ascii ) ; builder -> BuildString ( " " ) ; builder -> BuildString ( sensor_id_s ) ; builder -> BuildString ( " " ) ; builder -> BuildString ( " " ) ; builder -> BuildString ( " " ) ; builder -> BuildString ( name_s ) ; builder -> BuildString ( " " ) ; builder -> BuildString ( version_s ) ; builder -> BuildString ( " " ) ; builder -> BuildString ( description_s ) ; builder -> BuildString ( " " ) ; if ( unique_buff ) tq_free ( this -> application_m , unique_buff ) ; if ( this -> mysqlPrepareQuery ( builder -> build_str ) != ERR_SUCCESS ) { delete builder ; if ( sensor_id_s ) bpfree ( sensor_id_s ) ; if ( name_s ) bpfree ( name_s ) ; if ( version_s ) bpfree ( version_s ) ; if ( description_s ) bpfree ( description_s ) ; return ERR_FAILURE ; } if ( this -> mysqlExecuteStatement ( ) != ERR_SUCCESS ) { delete builder ; if ( sensor_id_s ) bpfree ( sensor_id_s ) ; if ( name_s ) bpfree ( name_s ) ; if ( version_s ) bpfree ( version_s ) ; if ( description_s ) bpfree ( description_s ) ; return ERR_FAILURE ; } if ( this -> mysqlSession -> stmt ) { #if USE_MARIADB == 1 this -> cltdb_id = mysql_insert_id ( this -> mysqlSession -> conn ) ; #else this -> cltdb_id = this -> mysqlSession -> stmt -> insert_id ; #endif } if ( this -> mysqlDestroyStatement ( ) != ERR_SUCCESS ) { delete builder ; if ( sensor_id_s ) bpfree ( sensor_id_s ) ; if ( name_s ) bpfree ( name_s ) ; if ( version_s ) bpfree ( version_s ) ; if ( description_s ) bpfree ( description_s ) ; return ERR_FAILURE ; } if ( sensor_id_s ) bpfree ( sensor_id_s ) ; if ( name_s ) bpfree ( name_s ) ; if ( version_s ) bpfree ( version_s ) ; if ( description_s ) bpfree ( description_s ) ; delete builder ; return ERR_SUCCESS ; }
insert command line tool into the db
insert command line tool into the db
// ensure we have at least some parameters // do length checks up front // create a builder // set information here // set sensor id value (always alloc so we can simplify freeing below) // set name // set version // set description // start building the query // create pointer to hold our sha1 // set allocation size // allocate space for the buffer // this->GenSHA1(); // set length // generate sha1 // create bad value if necessary // build the rest of the query // destroy the sha1 // tq_free(this->application_m, this->sha1_ascii); // prepare and run insert query // destroy data // destroy data // set the insert id // save insert id for next set of inserts // save insert id for next set of inserts // destroy the statement // destroy data // destroy data // destroy the builder string // return indicating success