Class: TokyoTyrant::Query

Inherits:
Object
  • Object
show all
Defined in:
ext/tokyo_tyrant.c

Constant Summary collapse

CSTREQ =
INT2NUM(RDBQCSTREQ)
CSTRINC =
INT2NUM(RDBQCSTRINC)
CSTRBW =
INT2NUM(RDBQCSTRBW)
CSTREW =
INT2NUM(RDBQCSTREW)
CSTRAND =
INT2NUM(RDBQCSTRAND)
CSTROR =
INT2NUM(RDBQCSTROR)
CSTROREQ =
INT2NUM(RDBQCSTROREQ)
CSTRRX =
INT2NUM(RDBQCSTRRX)
CNUMEQ =
INT2NUM(RDBQCNUMEQ)
CNUMGT =
INT2NUM(RDBQCNUMGT)
CNUMGE =
INT2NUM(RDBQCNUMGE)
CNUMLT =
INT2NUM(RDBQCNUMLT)
CNUMLE =
INT2NUM(RDBQCNUMLE)
CNUMBT =
INT2NUM(RDBQCNUMBT)
CNUMOREQ =
INT2NUM(RDBQCNUMOREQ)
CNEGATE =
INT2NUM(RDBQCNEGATE)
CNOIDX =
INT2NUM(RDBQCNOIDX)
OSTRASC =
INT2NUM(RDBQOSTRASC)
OSTRDESC =
INT2NUM(RDBQOSTRDESC)
ONUMASC =
INT2NUM(RDBQONUMASC)
ONUMDESC =
INT2NUM(RDBQONUMDESC)

Instance Method Summary collapse

Instance Method Details

#addcond(vname, vop, vexpr) ⇒ Object Also known as: add_condition, condition, add



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'ext/tokyo_tyrant_query.c', line 17

static VALUE cQuery_addcond(VALUE vself, VALUE vname, VALUE vop, VALUE vexpr){
  VALUE vqry;
  RDBQRY *qry;
  vname = StringValueEx(vname);
  vexpr = StringValueEx(vexpr);

  if (TYPE(vop) == T_SYMBOL) vop = rb_str_new2(rb_id2name(SYM2ID(vop)));

  if (TYPE(vop) == T_STRING){
    vop = StringValueEx(vop);
    vop = tctdbqrystrtocondop(RSTRING_PTR(vop));
    vop = INT2NUM(vop);
  }

  vqry = rb_iv_get(vself, RDBQRYVNDATA);
  Data_Get_Struct(vqry, RDBQRY, qry);
  tcrdbqryaddcond(qry, RSTRING_PTR(vname), NUM2INT(vop), RSTRING_PTR(vexpr));
  return vself;
}

#getObject

Rufus Compat



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
# File 'ext/tokyo_tyrant_query.c', line 109

static VALUE cQuery_get(VALUE vself){
  int i, num, ksiz;
  const char *name, *col;
  VALUE vqry, vary, vcols, vpkey, vname;
  RDBQRY *qry;
  TCLIST *res;
  TCMAP *cols;
  vqry = rb_iv_get(vself, RDBQRYVNDATA);
  Data_Get_Struct(vqry, RDBQRY, qry);

  res = tcrdbqrysearchget(qry);
  num = tclistnum(res);
  vary = rb_ary_new2(num);
  vpkey = rb_iv_get(vself, "@pkey");
  if(vpkey == Qnil) vpkey = rb_str_new2("__id");
  for(i = 0; i < num; i++){
    vcols = rb_hash_new();
    cols = tcrdbqryrescols(res, i);
    if(cols){
      tcmapiterinit(cols);
      while((name = tcmapiternext(cols, &ksiz)) != NULL){
        col = tcmapget2(cols, name);
        vname = ksiz == 0 ? vpkey : rb_str_new2(name);
        rb_hash_aset(vcols, vname, rb_str_new2(col));
      }
    }
    tcmapdel(cols);
    rb_ary_push(vary, vcols);
  }
  tclistdel(res);
  return vary;
}

#hintObject



146
147
148
149
150
151
152
# File 'ext/tokyo_tyrant_query.c', line 146

static VALUE cQuery_hint(VALUE vself){
  VALUE vqry;
  RDBQRY *qry;
  vqry = rb_iv_get(vself, RDBQRYVNDATA);
  Data_Get_Struct(vqry, RDBQRY, qry);
  return rb_str_new2(tcrdbqryhint(qry));
}

#searchObject Also known as: run

Rufus Compat



81
82
83
84
85
86
87
88
89
90
91
# File 'ext/tokyo_tyrant_query.c', line 81

static VALUE cQuery_search(VALUE vself){
  VALUE vqry, vary;
  RDBQRY *qry;
  TCLIST *res;
  vqry = rb_iv_get(vself, RDBQRYVNDATA);
  Data_Get_Struct(vqry, RDBQRY, qry);
  res = tcrdbqrysearch(qry);
  vary = listtovary(res);
  tclistdel(res);
  return vary;
}

#searchcountObject Also known as: count

Rufus Compat



101
102
103
104
105
106
107
# File 'ext/tokyo_tyrant_query.c', line 101

static VALUE cQuery_searchcount(VALUE vself){
  VALUE vqry;
  RDBQRY *qry;
  vqry = rb_iv_get(vself, RDBQRYVNDATA);
  Data_Get_Struct(vqry, RDBQRY, qry);
  return LL2NUM(tcrdbqrysearchcount(qry));
}

#searchoutObject Also known as: delete



93
94
95
96
97
98
99
# File 'ext/tokyo_tyrant_query.c', line 93

static VALUE cQuery_searchout(VALUE vself){
  VALUE vqry;
  RDBQRY *qry;
  vqry = rb_iv_get(vself, RDBQRYVNDATA);
  Data_Get_Struct(vqry, RDBQRY, qry);
  return tcrdbqrysearchout(qry) ? Qtrue : Qfalse;
}

#set_pkey(vpkey) ⇒ Object Also known as: pkey=



142
143
144
# File 'ext/tokyo_tyrant_query.c', line 142

static VALUE cQuery_set_pkey(VALUE vself, VALUE vpkey) {
  return rb_iv_set(vself, "@pkey", vpkey);
}

#setlimit(*args) ⇒ Object Also known as: setmax, limit

Rufus Compat



69
70
71
72
73
74
75
76
77
78
79
# File 'ext/tokyo_tyrant_query.c', line 69

static VALUE cQuery_setlimit(int argc, VALUE *argv, VALUE vself){
  VALUE vqry, vmax, vskip;
  RDBQRY *qry;
  vqry = rb_iv_get(vself, RDBQRYVNDATA);
  Data_Get_Struct(vqry, RDBQRY, qry);
  rb_scan_args(argc, argv, "11", &vmax, &vskip);
  if(NIL_P(vskip)) vskip = INT2FIX(0);

  tcrdbqrysetlimit(qry, NUM2INT(vmax), NUM2INT(vskip));
  return vself;
}

#setorder(*args) ⇒ Object Also known as: order_by

Rufus Compat



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
# File 'ext/tokyo_tyrant_query.c', line 37

static VALUE cQuery_setorder(int argc, VALUE *argv, VALUE vself){
  VALUE vname, vtype;
  int type;
  VALUE vqry;
  RDBQRY *qry;

  rb_scan_args(argc, argv, "11", &vname, &vtype);
  if(NIL_P(vtype)) vtype = INT2NUM(RDBQOSTRASC);

  vname = StringValueEx(vname);

  switch(TYPE(vtype)){
    case T_SYMBOL:
      vtype = rb_str_new2(rb_id2name(SYM2ID(vtype)));
    case T_STRING:
      vtype = StringValueEx(vtype);
      type = tctdbqrystrtoordertype(RSTRING_PTR(vtype));
      break;
    case T_FIXNUM:
      type = NUM2INT(vtype);
      break;
    default:
      rb_raise(rb_eArgError, "type must be symbol, string or integer");
      break;
  }

  vqry = rb_iv_get(vself, RDBQRYVNDATA);
  Data_Get_Struct(vqry, RDBQRY, qry);
  tcrdbqrysetorder(qry, RSTRING_PTR(vname), type);
  return vself;
}