Class: ActiveSQL::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_sql.rb,
ext/activesql.c

Class Method Summary collapse

Class Method Details

.add_methods(*args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'ext/activesql.c', line 83

static VALUE activeSQL_add_methods(int argc, VALUE *argv, VALUE self) {
  VALUE needed;
  rb_scan_args(argc, argv, "01", &needed);
  if (TYPE(needed) == T_TRUE) {
    mysObj.adds = 0;
    return Qtrue;
  } 
  else if (TYPE(needed) == T_FALSE) {
    mysObj.adds = 1;
    return Qtrue;
  }
  else
    return Qfalse;
}

.add_methods?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
# File 'ext/activesql.c', line 76

static VALUE activeSQL_added_methods(VALUE self) {
  if (mysObj.adds == 0)
    return Qtrue;
  else
    return Qfalse;
}

.connect!Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'ext/activesql.c', line 237

static VALUE activeSQL_connect(VALUE self) {
  if (mysObj.reconnect == 1) {
  mysObj.conn = mysql_init(NULL);
  if (!mysql_real_connect(mysObj.conn, myConfig.ser, myConfig.user, myConfig.pass, myConfig.db, myConfig.pp, myConfig.sock, myConfig.flg)) {
    fprintf(stderr, "%s\n", mysql_error(mysObj.conn));
    return Qfalse;
  }
  else {
    mysObj.reconnect = 0;
    return Qtrue;
  }
  }
  else
    return Qfalse;
}

.connected?Boolean

Returns:

  • (Boolean)


215
216
217
218
219
220
# File 'ext/activesql.c', line 215

static VALUE activeSQL_connected(VALUE self) {
  if (mysObj.reconnect == 0)
    return Qtrue;
  else
    return Qfalse;
}

.disconnect!Object



222
223
224
225
226
227
228
229
230
# File 'ext/activesql.c', line 222

static VALUE activeSQL_disconnect(VALUE self) {
  if (mysObj.reconnect == 0) {
    mysObj.reconnect = 1;
    mysql_close(mysObj.conn);
    return Qtrue;
  }
  else
    return Qfalse;
}

.establish_connection(config) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_sql.rb', line 36

def establish_connection(config) 
   host     = (config[:host] ||= 'localhost')
   username = config[:username] ? config[:username].to_s : 'root'
   password = config[:password] ? config[:password].to_s : ''
   database = config[:database] 
   port     = config[:port] 
   socket   = config[:socket] 
   default_flags = 0 
   # No multi-results as of now.
   options = [host, username, password, database, port, socket, default_flags]
	 load_config(*options)

   # make connection across the parameters.
   connect!
end

.exec(arg, ret_vl) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'ext/activesql.c', line 137

static VALUE activeSQL_exec(VALUE self, VALUE arg, VALUE ret_vl) {
  unsigned int * castAs;
  unsigned int i, n, fld_cnt, j;
  unsigned long * lengths;
  char **getters;
  char *tbl_name;
  char *dyn_method;
  MYSQL_RES *res;
  MYSQL_ROW row;
  MYSQL_FIELD *fields;
  VALUE ary, f, cmd;
  VALUE res_set; //Colllect result to send it back.
  if (mysObj.reconnect == 1)
    f = rb_funcall(rb_cBase, rb_intern("connect!"), 0);
  else
    f = Qtrue;
  if (TYPE(f) == T_TRUE) {
    if (TYPE(arg) != T_STRING)
      rb_raise(rb_eTypeError, "invalid type for input");
    if (mysql_real_query(mysObj.conn, RSTRING_PTR(arg), (unsigned int) strlen(RSTRING_PTR(arg)))) {
      rb_raise(rb_eSqlError, mysql_error(mysObj.conn));
    }
    if (TYPE(ret_vl) == T_TRUE) {
      res = mysql_use_result(mysObj.conn);
      res_set = rb_ary_new();
      fld_cnt = mysql_num_fields(res);
      fields = mysql_fetch_fields(res);
      ary = rb_ary_new2(fld_cnt);

      /* ALLOCATE ROOM : DYN METHODS FOR GETTERS */
      getters = (char **) malloc(fld_cnt * sizeof (char *));
      castAs = (unsigned int*) malloc(fld_cnt * sizeof (int));

      for (i = 0; i < fld_cnt; i++) {
        getters[i] = fields[i].name;
        tbl_name = fields[i].org_name ? fields[i].org_name : fields[i].name;
        strcat(tbl_name, "@");
        strcat(tbl_name, fields[i].org_table ? fields[i].org_table : fields[i].table);
        rb_ary_store(ary, i, rb_tainted_str_new2(tbl_name));
        tbl_name = "";
        castAs[i] = castPreWrk(fields[i].type);
      }
      rb_ary_push(res_set, ary);
      while ((row = mysql_fetch_row(res)) != NULL) {
        n = mysql_num_fields(res);
        lengths = mysql_fetch_lengths(res);
        ary = rb_ary_new2(n);
        for (i = 0; i < n; i++) {
          rb_ary_store(ary, i, row[i] ? castFrFetch(castAs[i], rb_tainted_str_new(row[i], lengths[i]), i) : Qnil);
          //rb_define_singleton_method(ary, "{dynamic getters}", activeSQL_dyamic_meth, 0); WE CN ADD IF HV NEW WAY. 
          if (mysObj.adds == 0)
            if (getters[i] != "") {
              dyn_method = (char *) malloc(((unsigned int) strlen(getters[i]) + 21)); // **
              sprintf(dyn_method, "def %s; self[%u]; end", getters[i], i);
              cmd = rb_str_new2(dyn_method);
              rb_obj_instance_eval(1, &cmd, ary);
              free(dyn_method);
            }
          }
        rb_ary_push(res_set, ary);
      }

      // FREE all.
      free(getters);
      free(castAs);
      mysql_free_result(res);
      return res_set;
      }
      else {
        res = mysql_use_result(mysObj.conn); // To avoid out of sync error from api.
        mysql_free_result(res);
        return Qtrue; /* Add affacted rows instead of status if needed */
     }
  }
  else
    rb_raise(rb_eSqlError, "Connection not established.");
}

.exec_sql(arg) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'ext/activesql.c', line 103

static VALUE activeSQL_exec_sql(VALUE self, VALUE arg) {
  VALUE f;
  if (mysObj.reconnect == 1)
    f = rb_funcall(rb_cBase, rb_intern("connect!"), 0);
  else
    f = Qtrue;
  if (TYPE(f) == T_TRUE) {
    if (TYPE(arg) != T_STRING)
      rb_raise(rb_eTypeError, "invalid type for input");
    if (mysql_real_query(mysObj.conn, RSTRING_PTR(arg), (unsigned int) strlen(RSTRING_PTR(arg))))
      rb_raise(rb_eSqlError, mysql_error(mysObj.conn));
    else
      return Qtrue;
  }
  else
    rb_raise(rb_eSqlError, "Connection not established.");
}

.execute_sql(qry) ⇒ Object Also known as: insert_sql

Result set not returned for execute_sql



27
28
29
30
31
32
33
# File 'lib/active_sql.rb', line 27

def execute_sql(qry)
  unless qry =~ my_select?
    exec_sql(qry)
  else
    raise ActiveSQLError, "no return of result set"
  end     
end

.load_config(*args) ⇒ Object

methods visible.



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'ext/activesql.c', line 257

static VALUE activeSQL_load_config(int argc, VALUE *argv, VALUE self) {
  VALUE host, user, passwd, db, port, sock, flag;
  rb_scan_args(argc, argv, "07", &host, &user, &passwd, &db, &port, &sock, &flag);
  myConfig.db = IfNILorSTRING(db);
  myConfig.flg = IfNILorINT(flag);
  myConfig.ser = IfNILorSTRING(host);
  myConfig.user = IfNILorSTRING(user);
  myConfig.pass = IfNILorSTRING(passwd);
  myConfig.pp = IfNILorINT(port);
  myConfig.sock = IfNILorSTRING(sock);
  mysObj.reconnect = 1;
  return Qnil;
}

.my_char_set(*args) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/activesql.c', line 121

static VALUE activeSQL_set_chars_set(int argc, VALUE *argv, VALUE self) {
  VALUE cset;
  if (!mysObj.reconnect) {
    rb_scan_args(argc, argv, "01", &cset);
    if (TYPE(cset) == T_STRING)
      if (!mysql_set_character_set(mysObj.conn, RSTRING_PTR(cset)))
        return rb_str_new2(mysql_character_set_name(mysObj.conn));
      else
        return Qfalse;
      else
        return rb_str_new2(mysql_character_set_name(mysObj.conn));
  } 
  else
    rb_raise(rb_eSqlError, "Connection not established.");
}

.my_database(*args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/activesql.c', line 60

static VALUE activeSQL_my_database(int argc, VALUE *argv, VALUE self) {
  VALUE cdb;
  if (!mysObj.reconnect) {
    rb_scan_args(argc, argv, "01", &cdb);
    if (TYPE(cdb) == T_STRING)
      if (mysql_select_db(mysObj.conn, RSTRING_PTR(cdb)) == 0)
        return Qtrue;
      else
        return Qfalse;
    else
      return Qfalse;
  } 
  else
    rb_raise(rb_eSqlError, "Connection not established.");
}

.my_select?Boolean

return Qtrue; Do it from query.

Returns:

  • (Boolean)


56
57
58
# File 'ext/activesql.c', line 56

static VALUE activeSQL_my_select(VALUE self) {
  return rb_reg_new("\\A\\s*(SELECT|SHOW|DESCRIBE|EXPLAIN|CHECK TABLE|DESC|CALL)", 57, 1);
}

.reconnect!Object



232
233
234
235
# File 'ext/activesql.c', line 232

static VALUE activeSQL_reconnect(VALUE self) {
  rb_funcall(rb_cBase, rb_intern("disconnect!"), 0);
  return rb_funcall(rb_cBase, rb_intern("connect!"), 0);
}

.select_sql(qry) ⇒ Object

Execute the sql queries.



15
16
17
18
19
20
21
22
23
24
# File 'lib/active_sql.rb', line 15

def select_sql(qry)
  if qry =~ my_select?    
    result = exec(qry, true)	
    cols = result.shift 
    result.instance_eval "def columns; #{cols.inspect}; end"
    result
  else
    exec_sql(qry) # status.
  end         
end

.versionObject



253
254
255
# File 'ext/activesql.c', line 253

static VALUE activeSQL_version(VALUE self) {
  return rb_str_new("2.0.1", 5);
}