Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[MySQL] Add in MySqlDataTable and MySqlDataRow types. Make sure they …

…build. Untested
  • Loading branch information...
commit 4c6b9898da32de5af37be03e616fe2d9c79915b4 1 parent de4db04
@Whiteknight authored
View
12 mysql/include/ps_mysql.h
@@ -0,0 +1,12 @@
+#include <mysql/mysql.h>
+
+#define STRING_TO_CSTRING(i, s, c, code) do { \
+ char * (c) = Parrot_str_to_cstring((i), (s)); \
+ { \
+ code \
+ } \
+ Parrot_str_free_cstring((c)); \
+} while(0)
+
+extern INTVAL MySqlDataTable_type;
+extern INTVAL MySqlDataRow_type;
View
6 mysql/pmc/datatable.pmc
@@ -1,6 +0,0 @@
-pmclass DataTable dynpmc auto_attrs {
- ATTR PMC *colnames;
- ATTR void *data;
-
-
-}
View
67 mysql/pmc/mysqldatarow.pmc
@@ -0,0 +1,67 @@
+#include "ps_mysql.h"
+
+INTVAL MySqlDataRow_type;
+
+pmclass MySqlDataRow dynpmc auto_attrs provides array provides hash {
+ ATTR PMC *table;
+ ATTR PMC *data;
+
+ void class_init() {
+ MySqlDataRow_type = entry;
+ }
+
+ VTABLE void init_pmc(PMC *table)
+ {
+ PARROT_MYSQLDATAROW(SELF)->table = table;
+ PObj_custom_mark_SET(SELF);
+ }
+
+ VTABLE void mark()
+ {
+ Parrot_MySqlDataRow_attributes * const attr = PARROT_MYSQLDATAROW(SELF);
+ if (!PMC_IS_NULL(attr->table))
+ Parrot_gc_mark_PMC_alive(INTERP, attr->table);
+ if (!PMC_IS_NULL(attr->data))
+ Parrot_gc_mark_PMC_alive(INTERP, attr->data);
+ }
+
+ VTABLE void set_pmc(PMC *data)
+ {
+ PARROT_MYSQLDATAROW(SELF)->data = data;
+ }
+
+ VTABLE STRING * get_string_keyed_int(INTVAL idx)
+ {
+ return VTABLE_get_string_keyed_int(INTERP, PARROT_MYSQLDATAROW(SELF)->data, idx);
+ }
+
+ VTABLE STRING * get_string_keyed_str(STRING *colname)
+ {
+ const INTVAL idx = VTABLE_get_integer_keyed_str(INTERP, PARROT_MYSQLDATAROW(SELF)->table, colname);
+ return VTABLE_get_string_keyed_int(INTERP, SELF, idx);
+ }
+
+ VTABLE STRING * get_string_keyed(PMC *key)
+ {
+ STRING * const colname = VTABLE_get_string(INTERP, key);
+ return VTABLE_get_string_keyed_str(INTERP, SELF, colname);
+ }
+
+ VTABLE PMC *get_pmc_keyed(PMC *key)
+ {
+ STRING * const colname = VTABLE_get_string(INTERP, key);
+ return VTABLE_get_pmc_keyed_str(INTERP, SELF, colname);
+ }
+
+ VTABLE PMC * get_pmc_keyed_str(STRING *colname)
+ {
+ STRING * const value = VTABLE_get_string_keyed_str(INTERP, SELF, colname);
+ return Parrot_pmc_box_string(INTERP, value);
+ }
+
+ VTABLE PMC *get_pmc_keyed_int(INTVAL idx)
+ {
+ STRING * const value = VTABLE_get_string_keyed_int(INTERP, SELF, idx);
+ return Parrot_pmc_box_string(INTERP, value);
+ }
+}
View
95 mysql/pmc/mysqldatatable.pmc
@@ -0,0 +1,95 @@
+#include "ps_mysql.h"
+#define GET_RESULT(s) ((MYSQL_RES*)PARROT_MYSQLDATATABLE(s)->result)
+#define GET_COLNAMES(s) (PARROT_MYSQLDATATABLE(s)->colnames)
+
+INTVAL MySqlDataTable_type;
+
+pmclass MySqlDataTable dynpmc auto_attrs provides iterator {
+ ATTR Hash *colnames;
+ ATTR void *result;
+ ATTR void *current_row;
+ ATTR void *current_row_lengths;
+ ATTR INTVAL num_fields;
+
+ void class_init() {
+ MySqlDataTable_type = entry;
+ }
+
+ VTABLE void init()
+ {
+ PObj_custom_mark_destroy_SETALL(SELF);
+ }
+
+ VTABLE void set_pointer(void *rawptr)
+ {
+ MYSQL_RES * const result = (MYSQL_RES*)rawptr;
+ INTVAL num_fields = mysql_num_fields(result);
+ Hash * const colnames = Parrot_hash_create_sized(INTERP, enum_hash_int, Hash_key_type_STRING, num_fields);
+ MYSQL_FIELD *field;
+
+ INTVAL idx = 0;
+ while(field = mysql_fetch_field(result)) {
+ STRING * const colnamestr = Parrot_str_new(INTERP, field->name, 0);
+ Parrot_hash_put(INTERP, colnames, (void*)colnamestr, (void*)idx);
+ idx++;
+ }
+
+ PARROT_MYSQLDATATABLE(SELF)->colnames = colnames;
+ PARROT_MYSQLDATATABLE(SELF)->result = rawptr;
+ PARROT_MYSQLDATATABLE(SELF)->current_row = mysql_fetch_row(result);
+ PARROT_MYSQLDATATABLE(SELF)->current_row_lengths = mysql_fetch_lengths(result);
+ PARROT_MYSQLDATATABLE(SELF)->num_fields = num_fields;
+ }
+
+ VTABLE void mark()
+ {
+ Hash * const colnames = GET_COLNAMES(SELF);
+ if (colnames)
+ Parrot_hash_mark(INTERP, colnames);
+ }
+
+ VTABLE void* get_pointer()
+ {
+ MYSQL_RES * const result = GET_RESULT(SELF);
+ return result;
+ }
+
+ VTABLE void destroy()
+ {
+ MYSQL_RES * const result = GET_RESULT(SELF);
+ if (result)
+ mysql_free_result(result);
+ }
+
+ VTABLE INTVAL get_bool()
+ {
+ return PARROT_MYSQLDATATABLE(SELF)->current_row != NULL;
+ }
+
+ VTABLE PMC* shift_pmc()
+ {
+ Parrot_MySqlDataTable_attributes * const attrs = PARROT_MYSQLDATATABLE(SELF);
+ if (!attrs->current_row)
+ return PMCNULL;
+ else {
+ MYSQL_ROW row = attrs->current_row;
+ unsigned long * field_lengths = PARROT_MYSQLDATATABLE(SELF)->current_row_lengths;
+ INTVAL num_fields = PARROT_MYSQLDATATABLE(SELF)->num_fields;
+ PMC * const rowpmc = Parrot_pmc_new_init(INTERP, MySqlDataRow_type, SELF);
+ PMC * const data = Parrot_pmc_new_init_int(INTERP, enum_class_FixedStringArray, num_fields);
+ INTVAL i = 0;
+ for (; i < num_fields; i++) {
+ STRING * const field_val = Parrot_str_new_init(INTERP, row[i], (INTVAL)field_lengths[i], Parrot_binary_encoding_ptr, 0);
+ VTABLE_set_string_keyed_int(INTERP, data, i, field_val);
+ }
+ VTABLE_set_pmc(INTERP, rowpmc, data);
+ attrs->current_row = mysql_fetch_row((MYSQL_RES*)attrs->result);
+ return rowpmc;
+ }
+ }
+
+ VTABLE INTVAL get_integer_keyed_str(STRING *colname)
+ {
+ return (INTVAL)Parrot_hash_get(INTERP, PARROT_MYSQLDATATABLE(SELF)->colnames, (void*)colname);
+ }
+}
View
22 mysql/pmc/mysqldbcontext.pmc
@@ -1,20 +1,13 @@
-#include <mysql/mysql.h>
+#include "ps_mysql.h"
#define GET_CONN(s) ((MYSQL*)PARROT_MYSQLDBCONTEXT(s)->conn)
-#define STRING_TO_CSTRING(i, s, c, code) do { \
- char * (c) = Parrot_str_to_cstring((i), (s)); \
- { \
- code \
- } \
- Parrot_str_free_cstring((c)); \
-} while(0)
-
pmclass MySQLDbContext dynpmc auto_attrs {
ATTR void *conn;
VTABLE void init() {
MYSQL * const conn = mysql_init(NULL);
PARROT_MYSQLDBCONTEXT(SELF)->conn = (void*)conn;
+ PObj_custom_destroy_SET(SELF);
}
VTABLE void destroy() {
@@ -43,7 +36,7 @@ pmclass MySQLDbContext dynpmc auto_attrs {
METHOD query_no_result(STRING * query) {
MYSQL * const conn = GET_CONN(SELF);
- int len = Parrot_str_byte_length(INTERP, query);
+ INTVAL len = Parrot_str_byte_length(INTERP, query);
INTVAL stat;
STRING_TO_CSTRING(INTERP, query, cquery,
stat = mysql_real_query(conn, cquery, len);
@@ -55,12 +48,19 @@ pmclass MySQLDbContext dynpmc auto_attrs {
METHOD query(STRING * query) {
MYSQL * const conn = GET_CONN(SELF);
+ INTVAL len = Parrot_str_byte_length(INTERP, query);
INTVAL stat;
STRING_TO_CSTRING(INTERP, query, cquery,
- stat = mysql_query(conn, cquery);
+ stat = mysql_real_query(conn, cquery, len);
);
if (stat)
Parrot_ex_throw_from_c_args(INTERP, NULL, 0, "MySql Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
+ else {
+ MYSQL_RES * const result = mysql_use_result(conn);
+ PMC * const table = Parrot_pmc_new(INTERP, MySqlDataTable_type);
+ VTABLE_set_pointer(INTERP, table, result);
+ RETURN(PMC * table);
+ }
}
}
View
6 setup.winxed
@@ -15,10 +15,12 @@ function setup_memcached(var parrotstore, var argv)
function setup_mysql(var parrotstore, var argv)
{
- parrotstore["dynpmc_cflags"] = "-g";
+ parrotstore["dynpmc_cflags"] = "-g -I./mysql/include";
parrotstore["dynpmc_ldflags"] = "-lmysqlclient";
parrotstore["dynpmc"]["mysql_group"] = [
- "mysql/pmc/mysqldbcontext.pmc"
+ "mysql/pmc/mysqldbcontext.pmc",
+ "mysql/pmc/mysqldatatable.pmc",
+ "mysql/pmc/mysqldatarow.pmc"
];
}
Please sign in to comment.
Something went wrong with that request. Please try again.