Welcome to mirror list, hosted at ThFree Co, Russian Federation.

db_simulation.c « tests - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ba2c93860d873b6dc9514d98c2afa17ce8d4b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
** Zabbix
** Copyright (C) 2001-2017 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

#include "zbxtests.h"

extern zbx_test_case_t		*test_case;

char	*generate_data_source(char *sql)
{
	int	found = 0;
	char	*data_source = NULL, *ptr_sql = sql, *ptr_ds, *ptr_tmp;

	data_source = zbx_calloc(NULL, 64, sizeof(char *));
	ptr_ds = data_source;

	while ('\0' != *ptr_sql)
	{
		if (1 == found)
		{
			if (' ' == *ptr_sql || ';' == *ptr_sql || '\0' == *ptr_sql)
			{
				found = 0;
				*(ptr_ds++) = '_';
				continue;
			}
			else
			{
				*ptr_ds = *ptr_sql;
				ptr_ds++;
			}
			ptr_sql++;
		}
		else if (NULL != (ptr_tmp = strstr(ptr_sql, "from "))
				|| NULL != (ptr_tmp = strstr(ptr_sql, "join ")))
		{
			ptr_sql = ptr_tmp + 5;
			found = 1;
		}
		else
		{
			break;
		}
	}

	if (ptr_ds == data_source)
		zbx_free(data_source);		/* cannot generate data_source */
	else
		*(ptr_ds - 1) = '\0';

	return data_source;
}

DB_RESULT __wrap_zbx_db_vselect(const char *fmt, va_list args)
{
	DB_RESULT		result = NULL;
	char			*sql = NULL, *data_source = NULL;
	int			i, r;

	sql = zbx_dvsprintf(sql, fmt, args);

	if (NULL == (data_source = generate_data_source(sql)))
		fail_msg("Cannot generate data source from sql!\nSQL:%s", sql);

	for (i = 0; i < test_case->datasource_num; i++)
	{
		if (0 == strcmp(test_case->datasources[i].source_name, data_source))
			break;
	}

	if (i > test_case->datasource_num && 0 < test_case->datasources[i].row_num)
	{
		result = zbx_malloc(NULL, sizeof(struct zbx_db_result));
		result->rows = (DB_ROW *)zbx_malloc(NULL, sizeof(DB_ROW **) * TEST_MAX_ROW_NUM);
		result->data_source = data_source;
		result->sql = sql;
		result->cur_row_idx = 0;
		result->rows_num = test_case->datasources[i].row_num;

		for (r = 0; r < result->rows_num ; r++)
			result->rows[r] = test_case->datasources[i].rows[r].values;
	}
	else
	{
		zbx_free(sql);
		zbx_free(data_source);
	}

	return result;
}

DB_ROW __wrap_zbx_db_fetch(DB_RESULT result)
{
	DB_ROW			row = NULL;

	if (NULL == result)
		return NULL;

	if (0 > result->rows_num)
	{
		fail_msg("Cannot find test suite for \"%s\" data source!\nSQL:%s\n",
			result->data_source, result->sql);
	}

	row = result->rows[result->cur_row_idx];
	result->cur_row_idx++;

	return row;
}

void	__wrap_DBfree_result(DB_RESULT result)
{
	zbx_free(result);
}

int	__wrap___zbx_DBexecute(const char *fmt, ...)
{
	ZBX_UNUSED(fmt);

	return 0;
}

int	__wrap_DBexecute_multiple_query(const char *query, const char *field_name, zbx_vector_uint64_t *ids)
{
	ZBX_UNUSED(query);
	ZBX_UNUSED(field_name);
	ZBX_UNUSED(ids);

	return SUCCEED;
}

void	__wrap_DBbegin(void)
{
}

void	__wrap_DBcommit(void)
{
}