library/src/databases/bpdb/BP-DB.cc
// Add multiple columns to a table at once.  Columns are copied using BP_DBColumnAdd iteratively.
Add multiple columns to a table at once.  Columns are copied using BP_DBColumnAdd iteratively.
Add multiple columns to a table at once . Columns are copied using BP_DBColumnAdd iteratively .
BP_ERROR_T BP_DBColumnsAdd
(
		P_BPDB                           bpdb,
		char *                           table_name,
		P_BPDB_SQL_TABLE_COLUMN_METADATA columns,
		size_t                           column_n
)
{
	if(!bpdb)
		return ERR_FAILURE;
	if(!bpdb->system_hreg)
		return ERR_FAILURE;
	if(!table_name)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBColumnsAdd was called with a NULL table_name parameter.");
		return ERR_FAILURE;
	}
	if(!columns)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBColumnsAdd was called with a NULL columns parameter.");
		return ERR_FAILURE;
	}
	if(!column_n)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBColumnsAdd was called with a zero column count.");
		return ERR_FAILURE;
	}
	BP_HASH_TABLE_ENTER_CRITICAL_SECTION(bpdb->system_hreg);
	size_t n = 0;
	for(; n < column_n; n++)
	{
		if(!BP_StringIsReasonableForColumnNameDB(columns[n].bpdb_column_name, bpstrlen(columns[n].bpdb_column_name)))
		{
			char tmp_str[400];
			BP_ZERO_STACK(tmp_str);
			sprintf(tmp_str, "BP_DBColumnsAdd attempted a BP_StringIsReasonableForColumnNameDB filter check on columns[%zu].bpdb_column_name but filter check failed.", n);
			BP_DBAddErrorMsg(bpdb, (char *) &tmp_str);
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(bpdb->system_hreg, ERR_FAILURE);
		}
	}
	P_BPDB_SQL_TABLE_METADATA table_metadata = BP_DBLookupTableMetadataFromTableName(bpdb, table_name);
	if(!table_metadata)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBColumnsAdd failed table lookup via BP_DBLookupTableMetadataFromTableName ");
		BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(bpdb->system_hreg, ERR_FAILURE);
	}
	for(n = 0; n < column_n; n++)
	{
		BP_ERROR_T ret_val = BP_DBColumnAdd
		(
			bpdb,
			table_name,
			columns[n].bpdb_column_name,
			columns[n].type,
			columns[n].primary_key,
			columns[n].indexed,
			columns[n].unique,
			columns[n].auto_inc
		);
		if(!ret_val)
		{
			char tmp_str[400];
			BP_ZERO_STACK(tmp_str);
			sprintf(tmp_str, "BP_DBColumnsAdd attempted to call BP_DBColumnAdd for column %zu but failed.", n);
			BP_DBAddErrorMsg(bpdb, (char *) &tmp_str);
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(bpdb->system_hreg, ERR_FAILURE);
		}
		%%%%%%
		%%%%%%
		table_metadata->column_count++;
		table_metadata->total_primary_key_cols += columns[n].primary_key;
		table_metadata->total_indexed_cols     += columns[n].indexed;
		table_metadata->total_unique_cols      += columns[n].unique;
		table_metadata->total_auto_inc_cols    += columns[n].auto_inc;
	}
	BP_HASH_TABLE_LEAVE_CRITICAL_SECTION(bpdb->system_hreg);
	return ERR_SUCCESS;
}
BP_ERROR_T BP_DBColumnsAdd ( P_BPDB bpdb , char * table_name , P_BPDB_SQL_TABLE_COLUMN_METADATA columns , size_t column_n ) { if ( ! bpdb ) return ERR_FAILURE ; if ( ! bpdb -> system_hreg ) return ERR_FAILURE ; if ( ! table_name ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return ERR_FAILURE ; } if ( ! columns ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return ERR_FAILURE ; } if ( ! column_n ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return ERR_FAILURE ; } BP_HASH_TABLE_ENTER_CRITICAL_SECTION ( bpdb -> system_hreg ) ; size_t n = 0 ; for ( ; n < column_n ; n ++ ) { if ( ! BP_StringIsReasonableForColumnNameDB ( columns [ n ] . bpdb_column_name , bpstrlen ( columns [ n ] . bpdb_column_name ) ) ) { char tmp_str [ 400 ] ; BP_ZERO_STACK ( tmp_str ) ; sprintf ( tmp_str , " " , n ) ; BP_DBAddErrorMsg ( bpdb , ( char * ) & tmp_str ) ; BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( bpdb -> system_hreg , ERR_FAILURE ) ; } } P_BPDB_SQL_TABLE_METADATA table_metadata = BP_DBLookupTableMetadataFromTableName ( bpdb , table_name ) ; if ( ! table_metadata ) { BP_DBAddErrorMsg ( bpdb , " " ) ; BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( bpdb -> system_hreg , ERR_FAILURE ) ; } for ( n = 0 ; n < column_n ; n ++ ) { BP_ERROR_T ret_val = BP_DBColumnAdd ( bpdb , table_name , columns [ n ] . bpdb_column_name , columns [ n ] . type , columns [ n ] . primary_key , columns [ n ] . indexed , columns [ n ] . unique , columns [ n ] . auto_inc ) ; if ( ! ret_val ) { char tmp_str [ 400 ] ; BP_ZERO_STACK ( tmp_str ) ; sprintf ( tmp_str , " " , n ) ; BP_DBAddErrorMsg ( bpdb , ( char * ) & tmp_str ) ; BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( bpdb -> system_hreg , ERR_FAILURE ) ; } table_metadata -> column_count ++ ; table_metadata -> total_primary_key_cols += columns [ n ] . primary_key ; table_metadata -> total_indexed_cols += columns [ n ] . indexed ; table_metadata -> total_unique_cols += columns [ n ] . unique ; table_metadata -> total_auto_inc_cols += columns [ n ] . auto_inc ; } BP_HASH_TABLE_LEAVE_CRITICAL_SECTION ( bpdb -> system_hreg ) ; return ERR_SUCCESS ; }
Add multiple columns to a table at once.
Add multiple columns to a table at once .
// basic null checks // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Enter Critical Section and Add Cols %%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // enter critical section // iterate through columns and check sanity // if any strings fail check, exit immediately // create space for a temporary string // copy in string // add error message // exit routine // lookup table metadata // at this point column names have been sanitized, so we can go ahead and add them // printf("\n Attempting to add column?!: %s - %u - %u", columns[n].column_name, n, column_n); // Add column.  Should never fail. // exit immediately if column isn't added // create space for a temporary string // copy in string // add error message // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Adjust Table Metadata %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // increment column count // increment designators where applicable (+1 on BP_TRUE, +0 on BP_FALSE) // leave critical section // return indicating success