rem: query_cols[n_query_cols] = "event"; query_vals[n_query_cols].type = DB_STR; query_vals[n_query_cols].nul = 0; query_vals[n_query_cols].val.str_val.s = "subscribe"; query_vals[n_query_cols].val.str_val.len = strlen("subscribe"); n_query_cols++;
add: query_cols[n_query_cols] = "status"; query_vals[n_query_cols].type = DB_STR; query_vals[n_query_cols].nul = 0; if (new_watcher_pending) { query_vals[n_query_cols].val.str_val.s = "pending"; query_vals[n_query_cols].val.str_val.len = strlen("pending"); } else { query_vals[n_query_cols].val.str_val.s = "active"; query_vals[n_query_cols].val.str_val.len = strlen("active"); } n_query_cols++;
context: int new_watcher(presentity_t *_p, str* _uri, time_t _e, int event_type, doctype_t _a, dlg_t* _dlg, 		str *_dn, watcher_t** _w){	watcher_t* watcher;	/* Check parameters */	if (!_uri && !_dlg && !_w) {		LOG(L_ERR, "new_watcher(): Invalid parameter value\n");		return -1;	}	/* Allocate memory buffer for watcher_t structure and uri string */	watcher = (watcher_t*)shm_malloc(sizeof(watcher_t) + _uri->len + _dn->len + S_ID_LEN);	if (!watcher) {		paerrno = PA_NO_MEMORY;	        LOG(L_ERR, "new_watcher(): No memory left\n");		return -1;	}	memset(watcher, 0, sizeof(watcher_t));	/* Copy uri string */	watcher->uri.s = (char*)watcher + S_ID_LEN + sizeof(watcher_t);	watcher->uri.len = _uri->len;	memcpy(watcher->uri.s, _uri->s, _uri->len);		/* Copy display_name string */	watcher->display_name.s = (char*)watcher + S_ID_LEN + sizeof(watcher_t) + _uri->len;	watcher->display_name.len = _dn->len;	memcpy(watcher->display_name.s, _dn->s, _dn->len);	watcher->s_id.s = (char*)watcher + sizeof(watcher_t);	watcher->s_id.len = 0;	watcher->event_type = event_type;	watcher->expires = _e; /* Expires value */	watcher->accept = _a;  /* Accepted document type */	watcher->dialog = _dlg; /* Dialog handle */	*_w = watcher;	if (use_db) {		db_key_t query_cols[10];		db_op_t query_ops[10];		db_val_t query_vals[10];		db_key_t result_cols[4];		db_res_t *res;		int n_query_cols = 0;		int n_result_cols = 0;		char *package = event_package_name[watcher->event_type];		LOG(L_ERR, "db_new_watcher starting\n");		query_cols[n_query_cols] = "r_uri";		query_ops[n_query_cols] = OP_EQ;		query_vals[n_query_cols].type = DB_STR;		query_vals[n_query_cols].nul = n_query_cols;		query_vals[n_query_cols].val.str_val.s = _p->uri.s;		query_vals[n_query_cols].val.str_val.len = _p->uri.len;		n_query_cols++;		LOG(L_ERR, "db_new_watcher:  _p->uri=%.*s\n", _p->uri.len, _p->uri.s);		query_cols[n_query_cols] = "w_uri";		query_ops[n_query_cols] = OP_EQ;		query_vals[n_query_cols].type = DB_STR;		query_vals[n_query_cols].nul = 0;		query_vals[n_query_cols].val.str_val.s = watcher->uri.s;		query_vals[n_query_cols].val.str_val.len = watcher->uri.len;		n_query_cols++;		LOG(L_ERR, "db_new_watcher:  watcher->uri=%.*s\n", watcher->uri.len, watcher->uri.s);		query_cols[n_query_cols] = "package";		query_ops[n_query_cols] = OP_EQ;		query_vals[n_query_cols].type = DB_STR;		query_vals[n_query_cols].nul = 0;		query_vals[n_query_cols].val.str_val.s = package;		query_vals[n_query_cols].val.str_val.len = strlen(package);		n_query_cols++;		LOG(L_ERR, "db_new_watcher:  watcher->package=%s\n", package);		result_cols[0] = "s_id";		result_cols[1] = "status";		result_cols[2] = "display_name";		n_result_cols = 3;				db_use_table(pa_db, watcherinfo_table);		if (db_query (pa_db, query_cols, query_ops, query_vals,			      result_cols, n_query_cols, n_result_cols, 0, &res) < 0) {			LOG(L_ERR, "db_new_tuple(): Error while querying tuple\n");			return -1;		}		LOG(L_INFO, "new_tuple: getting values: res=%p res->n=%d\n",		    res, (res ? res->n : 0));		if (res && res->n > 0) {			/* fill in tuple structure from database query result */			db_row_t *row = &res->rows[0];			db_val_t *row_vals = ROW_VALUES(row);			str s_id = row_vals[0].val.str_val;			str status = row_vals[1].val.str_val;			// str display_name = row_vals[2].val.str_val;			if (strcmp(status.s, "pending") == 0) {				watcher->status = WS_PENDING;			} else {				watcher->status = WS_ACTIVE;			}			if (s_id.s) {				strncpy(watcher->s_id.s, s_id.s, S_ID_LEN);				watcher->s_id.len = strlen(s_id.s);			}		} else {			watcher_assign_statement_id(_p, watcher);			query_cols[n_query_cols] = "s_id";			query_vals[n_query_cols].type = DB_STR;			query_vals[n_query_cols].nul = 0;			query_vals[n_query_cols].val.str_val.s = watcher->s_id.s;			query_vals[n_query_cols].val.str_val.len = watcher->s_id.len;			n_query_cols++;			query_cols[n_query_cols] = "status";			query_vals[n_query_cols].type = DB_STR;			query_vals[n_query_cols].nul = 0;			if (new_watcher_pending) {			     query_vals[n_query_cols].val.str_val.s = "pending";			     query_vals[n_query_cols].val.str_val.len = strlen("pending");			} else {			     query_vals[n_query_cols].val.str_val.s = "active";			     query_vals[n_query_cols].val.str_val.len = strlen("active");			}			n_query_cols++;			query_cols[n_query_cols] = "event";			query_vals[n_query_cols].type = DB_STR;			query_vals[n_query_cols].nul = 0;			query_vals[n_query_cols].val.str_val.s = "subscribe";			query_vals[n_query_cols].val.str_val.len = strlen("subscribe");			n_query_cols++;			query_cols[n_query_cols] = "display_name";			query_vals[n_query_cols].type = DB_STR;			query_vals[n_query_cols].nul = 0;			query_vals[n_query_cols].val.str_val.s = watcher->display_name.s;			query_vals[n_query_cols].val.str_val.len = watcher->display_name.len;			n_query_cols++;			/* insert new record into database */			LOG(L_INFO, "new_tuple: inserting %d cols into table\n", n_query_cols);			if (db_insert(pa_db, query_cols, query_vals, n_query_cols) < 0) {				LOG(L_ERR, "db_new_tuple(): Error while inserting tuple\n");				return -1;			}		}		db_free_query(pa_db, res);	}	return 0;}