src/libs/zbxdbhigh/proxy.c
/******************************************************************************
 *                                                                            *
 * Function: compare_nth_field                                                *
 *                                                                            *
 * Purpose: This function compares a value from JSON record with the value    *
 *          of the n-th field of DB record. For description how DB record is  *
 *          stored in memory see comments in function remember_record().      *
 *                                                                            *
 * Comparing deals with 4 cases:                                              *
 *          - JSON value is not NULL, DB value is not NULL                    *
 *          - JSON value is not NULL, DB value is NULL                        *
 *          - JSON value is NULL, DB value is NULL                            *
 *          - JSON value is NULL, DB value is not NULL                        *
 *                                                                            *
 ******************************************************************************/


This function compares a value from JSON record with the value
of the n-th field of DB record. For description how DB record is
stored in memory see comments in function remember_record().


This function compares a value from JSON record with the value of the n - th field of DB record . For description how DB record is stored in memory see comments in function remember_record () .
static int	compare_nth_field(const ZBX_FIELD **fields, const char *rec_data, int n, const char *str, int is_null,
		int *last_n, size_t *last_pos)
{
	int		i = *last_n, null_in_db = 0;
	const char	*p = rec_data + *last_pos, *field_start = NULL;
	do	
	{
		field_start = p;
		while ('\0' != *p++)
			;
		null_in_db = 0;
		if (0 == (fields[i++]->flags & ZBX_NOTNULL))	
		{
			if ('\2' == *p && (rec_data == p - 1 || '\0' == *(p - 2) || '\1' == *(p - 2) ||
					'\2' == *(p - 2)))	
			{
				null_in_db = 1;
				p++;
			}
			else if ('\1' == *p)
			{
				p++;
			}
			else
			{
				THIS_SHOULD_NEVER_HAPPEN;
				*last_n = 0;
				*last_pos = 0;
				return 1;
			}
		}
	}
	while (n >= i);
	*last_n = i;				
	*last_pos = (size_t)(p - rec_data);	
	if (0 == is_null)	
	{
		if (0 == null_in_db)
			return strcmp(field_start, str);
		else
			return 1;
	}
	else
	{
		if ('\0' == *str)
		{
			if (1 == null_in_db)
				return 0;	
			else
				return 1;
		}
		else
		{
			THIS_SHOULD_NEVER_HAPPEN;
			*last_n = 0;
			*last_pos = 0;
			return 1;
		}
	}
}
static int compare_nth_field ( const ZBX_FIELD * * fields , const char * rec_data , int n , const char * str , int is_null , int * last_n , size_t * last_pos ) { int i = * last_n , null_in_db = 0 ; const char * p = rec_data + * last_pos , * field_start = NULL ; do { field_start = p ; while ( ' \0 ' != * p ++ ) ; null_in_db = 0 ; if ( 0 == ( fields [ i ++ ] -> flags & ZBX_NOTNULL ) ) { if ( ' \2 ' == * p && ( rec_data == p - 1 || ' \0 ' == * ( p - 2 ) || ' \1 ' == * ( p - 2 ) || ' \2 ' == * ( p - 2 ) ) ) { null_in_db = 1 ; p ++ ; } else if ( ' \1 ' == * p ) { p ++ ; } else { THIS_SHOULD_NEVER_HAPPEN ; * last_n = 0 ; * last_pos = 0 ; return 1 ; } } } while ( n >= i ) ; * last_n = i ; * last_pos = ( size_t ) ( p - rec_data ) ; if ( 0 == is_null ) { if ( 0 == null_in_db ) return strcmp ( field_start , str ) ; else return 1 ; } else { if ( ' \0 ' == * str ) { if ( 1 == null_in_db ) return 0 ; else return 1 ; } else { THIS_SHOULD_NEVER_HAPPEN ; * last_n = 0 ; * last_pos = 0 ; return 1 ; } } }
Function: compare_nth_field
Function : compare_nth_field
/* find starting position of the n-th field */ /* field could be NULL */ /* field value is NULL */ /* Preserve number of field and its start position */ /* across calls to avoid searching from start. */ /* value in JSON is not NULL */ /* fields are "equal" - both contain NULL */