Fix test for bug #32625
[gnash.git] / extensions / mysql / mysql_table.cpp
blobf671c454cfee334fbcbf06a947c607f00105ecdb
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "as_value.h"
20 #include "fn_call.h"
21 #include "mysql_table.h"
23 namespace mysqldb
25 using namespace gnash;
27 as_value size_method(const fn_call& fn)
29 assert(fn.this_ptr); assert(fn.env);
30 table* tbl = (table*) (as_object*) fn.this_ptr;
31 return as_value(tbl->size());
34 table::table(MYSQL_RES* result)
36 as_object::set_member("size", &size_method);
38 // retrieve data
39 MYSQL_FIELD* fld = mysql_fetch_fields(result);
40 int num_fields = mysql_num_fields(result);
41 int num_rows = mysql_num_rows(result);
43 m_data.resize(num_rows);
44 for (int i = 0; i < num_rows; i++)
46 MYSQL_ROW row = mysql_fetch_row(result);
48 m_data[i] = new as_object();
49 m_data[i]->add_ref();
51 for (int j = 0; j < num_fields; j++)
53 as_value val;
54 if (row[j] == NULL)
56 val.set_null();
58 else
60 switch (fld[j].type)
62 case MYSQL_TYPE_TINY:
63 case MYSQL_TYPE_SHORT:
64 case MYSQL_TYPE_INT24:
65 val.set_int(stroul(row[j], NULL, 0));
66 break;
68 case MYSQL_TYPE_DECIMAL:
69 case MYSQL_TYPE_LONG:
70 case MYSQL_TYPE_FLOAT:
71 case MYSQL_TYPE_DOUBLE:
72 case MYSQL_TYPE_LONGLONG:
73 val.set_double(strtod(row[j], NULL));
74 break;
76 case MYSQL_TYPE_NULL:
77 case MYSQL_TYPE_TIMESTAMP:
78 case MYSQL_TYPE_DATE:
79 case MYSQL_TYPE_TIME:
80 case MYSQL_TYPE_DATETIME:
81 case MYSQL_TYPE_YEAR:
82 case MYSQL_TYPE_NEWDATE:
83 case MYSQL_TYPE_VARCHAR:
84 case MYSQL_TYPE_BIT:
85 case MYSQL_TYPE_NEWDECIMAL:
86 case MYSQL_TYPE_ENUM:
87 case MYSQL_TYPE_SET:
88 case MYSQL_TYPE_TINY_BLOB:
89 case MYSQL_TYPE_MEDIUM_BLOB:
90 case MYSQL_TYPE_LONG_BLOB:
91 case MYSQL_TYPE_BLOB:
92 case MYSQL_TYPE_VAR_STRING:
93 case MYSQL_TYPE_STRING:
94 case MYSQL_TYPE_GEOMETRY:
95 val.set_string(row[j]);
96 break;
99 m_data[i]->set_member(fld[j].name, val);
104 table::~table()
106 for (int i = 0; i < size(); i++)
108 m_data[i]->drop_ref();
112 bool table::get_member(const std::string& name, as_value* val)
114 // check table methods
115 if ( as_object::get_member(name, val) == false )
117 // hack
118 int idx = strtoul(name.c_str(), NULL, 0);
119 if (idx >=0 && idx < size())
121 *val = m_data[idx];
123 else
125 val->set_undefined();
128 return true;
131 int table::size()
133 return m_data.size();
136 } // end of namespace mysqldb