Interface of the kmediad_SearchConstraints clas; a subclass of klib_Object s.
More...
Go to the source code of this file.
Data Structures |
| struct | _kmediad_SearchConstraints |
| | Models search constraints in a way that can be converted to and from a URI in a web browser or other client. Each constraint is represented by a kmediad_SearchOp object in the list 'ops'. The search contraints can be applied distjuncively (OR) or conjunctively (AND). Obviously this syntax is not as powerful as SQL, but it's safer and more elegant than passing SQL queries in HTTP requests. More...
|
Defines |
| #define | KMEDIA_SC_DEFAULT_LIMIT 20 |
| #define | KMEDIA_SC_MAX_LIMIT 100 |
Typedefs |
typedef struct
_kmediad_SearchConstraints | kmediad_SearchConstraints |
| | Models search constraints in a way that can be converted to and from a URI in a web browser or other client. Each constraint is represented by a kmediad_SearchOp object in the list 'ops'. The search contraints can be applied distjuncively (OR) or conjunctively (AND). Obviously this syntax is not as powerful as SQL, but it's safer and more elegant than passing SQL queries in HTTP requests.
|
Functions |
| void | kmediad_searchconstraints_construct (kmediad_SearchConstraints *self, const char *class_name, int first, int limit, klib_List *ops, BOOL disjunctive) |
| void | kmediad_searchconstraints_dispose (kmediad_SearchConstraints *self) |
| kmediad_SearchConstraints * | kmediad_searchconstraints_new (int first, int limit, klib_List *ops, BOOL disjunctive) |
| kmediad_SearchConstraints * | kmediad_searchconstraints_new_from_args (const klib_AssocArray *args) |
| | Make search constraints from the arguments passed in the HTTP URL.
|
| klib_String * | kmedia_search_contraints_make_argstr (const kmediad_SearchConstraints *self) |
| | Convert these search constraints to a form suitable to append to an HTTP URL.
|
| klib_String * | kmediad_searchconstraints_make_displayable (const kmediad_SearchConstraints *sc) |
| | Convert these constraints into something broadly human-readble, for including on a Web page.
|
Detailed Description
Interface of the kmediad_SearchConstraints clas; a subclass of klib_Object s.
Define Documentation
| #define KMEDIA_SC_DEFAULT_LIMIT 20 |
| #define KMEDIA_SC_MAX_LIMIT 100 |
Typedef Documentation
Models search constraints in a way that can be converted to and from a URI in a web browser or other client. Each constraint is represented by a kmediad_SearchOp object in the list 'ops'. The search contraints can be applied distjuncively (OR) or conjunctively (AND). Obviously this syntax is not as powerful as SQL, but it's safer and more elegant than passing SQL queries in HTTP requests.
Function Documentation
Convert these search constraints to a form suitable to append to an HTTP URL.
Convert these constraints into something broadly human-readble, for including on a Web page.
{
klib_String *s = klib_string_new_empty ();
int i, l = klib_list_length (self->ops);
const char *junct = " and ";
if (self->disjunctive)
junct = " or ";
const klib_List *ops = self->ops;
for (i = 0; i < l; i++)
{
const kmediad_SearchOp *op = (kmediad_SearchOp *) klib_list_get (ops, i);
klib_String *colname = klib_string_new (op->column->str);
klib_string_capfirst (colname);
klib_string_append (s, colname->str);
klib_object_unref ((klib_Object *)colname);
klib_String *escaped = klib_string_clone_safe (op->operand);
klib_string_escape_html (escaped);
switch (op->op)
{
case op_contains:
klib_string_printf (s, " contains '%s'", escaped->str);
break;
case op_less:
klib_string_printf (s, " less than '%s'", escaped->str);
break;
case op_begins:
klib_string_printf (s, " begins with '%s'", escaped->str);
break;
default:
klib_string_printf (s, " is '%s'", escaped->str);
}
klib_object_unref ((klib_Object *)escaped);
if (i != l - 1)
klib_string_append (s, junct);
}
return s;
}
Make search constraints from the arguments passed in the HTTP URL.
{
int first = 0;
int limit = -1;
BOOL disjunctive = FALSE;
klib_String *sdisjunctive = (klib_String *)klib_assocarray_get
(args, "disjunctive");
if (!klib_string_is_null_or_empty (sdisjunctive))
disjunctive = (BOOL) atoi (sdisjunctive->str);
klib_String *sfirst = (klib_String *)klib_assocarray_get (args, "first");
if (!klib_string_is_null_or_empty (sfirst))
first = atoi (sfirst->str);
klib_String *slimit = (klib_String *)klib_assocarray_get (args, "limit");
if (!klib_string_is_null_or_empty (slimit))
limit = atoi (slimit->str);
if (limit > KMEDIA_SC_MAX_LIMIT) limit = KMEDIA_SC_MAX_LIMIT;
if (limit < 0) limit = KMEDIA_SC_DEFAULT_LIMIT;
if (first < 0) first = 0;
klib_List *ops = klib_list_new ();
int i, l = klib_assocarray_length (args);
for (i = 0; i < l; i++)
{
const char *key = klib_assocarray_get_key_by_index (args, i);
const char *value = ((klib_String *)klib_assocarray_get_value_by_index
(args, i))->str;
char *r = strchr (key, '-');
if (r)
{
klib_String *col = klib_string_new_substring (key, 0, r - key);
klib_String *operator = klib_string_new_substring
(r + 1, 0, strlen(r + 1));
KMSearchOpType op_type = kmediad_searchop_parse_opstring
(operator->str);
if (op_type != op_unknown)
{
kmediad_SearchOp *op = kmediad_searchop_new
(col->str, op_type, value);
klib_list_append (ops, (klib_Object *)op);
klib_object_unref ((klib_Object *)op);
}
klib_object_unref ((klib_Object *)col);
klib_object_unref ((klib_Object *)operator);
}
}
kmediad_SearchConstraints *self = kmediad_searchconstraints_new
(first, limit, ops, disjunctive);
klib_object_unref ((klib_Object *)ops);
return self;
}