[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Gnash-commit] gnash/extensions/mysql README mysql_db.cpp mysq...
From: |
Vitaly Alexeev |
Subject: |
[Gnash-commit] gnash/extensions/mysql README mysql_db.cpp mysq... |
Date: |
Mon, 12 Jun 2006 17:35:36 +0000 |
CVSROOT: /sources/gnash
Module name: gnash
Changes by: Vitaly Alexeev <alexeev> 06/06/12 17:35:36
Added files:
extensions/mysql: README mysql_db.cpp mysql_db.h mysql_table.cpp
mysql_table.h
Log message:
MySQL support in Flash
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gnash/extensions/mysql/README?cvsroot=gnash&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/gnash/extensions/mysql/mysql_db.cpp?cvsroot=gnash&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/gnash/extensions/mysql/mysql_db.h?cvsroot=gnash&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/gnash/extensions/mysql/mysql_table.cpp?cvsroot=gnash&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/gnash/extensions/mysql/mysql_table.h?cvsroot=gnash&rev=1.1
Patches:
Index: README
===================================================================
RCS file: README
diff -N README
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ README 12 Jun 2006 17:35:36 -0000 1.1
@@ -0,0 +1,58 @@
+This extension is used to MYSQL database access. It's simple and convenient to
use with Flash.
+
+There is an example of usage of this extension.
+
+// creates new object with type mysql_db
+db = new mysql_db();
+
+// bool db.connect(host, dbname, user, passwd)
+// creates connection to host "host", database "dbname" by user "user" and
password "passwd"
+// The value of host may be either a hostname or an IP address.
+// If host is NULL or the string "localhost", a connection to the local host
is assumed.
+// The user parameter contains the user's MySQL login ID. If user is NULL or
the empty string "",
+// the current user is assumed. Under Unix, this is the current login name.
Under Windows ODBC,
+// the current username must be specified explicitly.
+// The passwd parameter contains the password for user.
+
+
+// if there were errors during MYSQL access, db.err property contains error
message
+if (db.connect("localhost", "gamedb", "vitaly", "abcdefgh") == false)
+{
+ trace("connection error: "+db.err);
+}
+
+
+// opens table
+tbl = db.open("select * from game");
+if (tbl == null)
+{
+ trace(db.err);
+}
+
+// tbl.size() returns number of table rows
+trace("size="+tbl.size());
+
+
+// tbl[i].gamename takes value of field 'gamename' of row 'i'
+for (i = 0; i < tbl.size(); i++)
+{
+ trace(tbl[i].gamename);
+}
+
+// closes table & free memory located by table
+delete tbl;
+
+
+// executes MYSQL statement & returns affected rows
+// affected rows = -1 means that the error was occured
+affected_rows = db.run("update game set gamename='newname' where id_game=1");
+if (affected_rows == -1)
+{
+ trace(db.err);
+}
+
+
+// closes connection
+delete db;
+
+
Index: mysql_db.cpp
===================================================================
RCS file: mysql_db.cpp
diff -N mysql_db.cpp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ mysql_db.cpp 12 Jun 2006 17:35:36 -0000 1.1
@@ -0,0 +1,220 @@
+//
+// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+// Linking Gnash statically or dynamically with other modules is making a
+// combined work based on Gnash. Thus, the terms and conditions of the GNU
+// General Public License cover the whole combination.
+//
+// As a special exception, the copyright holders of Gnash give you
+// permission to combine Gnash with free software programs or libraries
+// that are released under the GNU LGPL and with code included in any
+// release of Talkback distributed by the Mozilla Foundation. You may
+// copy and distribute such a system following the terms of the GNU GPL
+// for all but the LGPL-covered parts and Talkback, and following the
+// LGPL for the LGPL-covered parts.
+//
+// Note that people who make modified versions of Gnash are not obligated
+// to grant this special exception for their modified versions; it is their
+// choice whether to do so. The GNU General Public License gives permission
+// to release a modified version without this exception; this exception
+// also makes it possible to release a modified version which carries
+// forward this exception.
+//
+
+#include <stdarg.h>
+
+#include "as_value.h"
+#include "fn_call.h"
+#include "mysql_db.h"
+
+#ifdef _WIN32
+# define snprintf _snprintf
+#endif
+
+namespace mysqldb
+{
+
+ void connect_method(const fn_call& fn)
+ {
+ assert(fn.this_ptr); assert(fn.env);
+ db* mydb = (db*) (as_object*) fn.this_ptr;
+
+ if (fn.nargs < 4)
+ {
+ mydb->set_err("'connect' needs 4 arguments\n");
+ fn.result->set_bool(false);
+ return;
+ }
+
+ fn.result->set_bool(mydb->connect(fn.arg(0).to_string(),
fn.arg(1).to_string(), fn.arg(2).to_string(), fn.arg(3).to_string()));
+ }
+
+ void run_method(const fn_call& fn)
+ {
+ assert(fn.this_ptr); assert(fn.env);
+ db* mydb = (db*) (as_object*) fn.this_ptr;
+
+ if (fn.nargs < 1)
+ {
+ mydb->set_err("'run' needs one argument");
+ fn.result->set_int(-1);
+ return;
+ }
+
+ fn.result->set_int(mydb->run(fn.arg(0).to_tu_string()));
+ }
+
+ void open_method(const fn_call& fn)
+ {
+ assert(fn.this_ptr); assert(fn.env);
+ db* mydb = (db*) (as_object*) fn.this_ptr;
+
+ if (fn.nargs < 1)
+ {
+ mydb->set_err("'open' needs one argument");
+ fn.result->set_null();
+ return;
+ }
+
+ as_object* tbl = mydb->open(fn.arg(0).to_string());
+ if (tbl == NULL)
+ {
+ fn.result->set_null();
+ return;
+ }
+
+ fn.result->set_as_object(tbl);
+ }
+
+ void constructor(const fn_call& fn)
+ {
+ *fn.result = new db();
+ }
+
+ db::db(): m_db(NULL)
+ {
+ as_object::set_member("connect", &connect_method);
+ as_object::set_member("run", &run_method);
+ as_object::set_member("open", &open_method);
+ }
+
+ db::~db()
+ {
+ disconnect();
+ }
+
+ void db::disconnect()
+ {
+ if (m_db != NULL)
+ {
+ mysql_close(m_db);
+ m_db = NULL;
+ }
+ }
+
+ bool db::connect(const char* host, const char* dbname, const char*
user, const char* pwd)
+ {
+ // Closes a previously opened connection &
+ // also deallocates the connection handle
+ disconnect();
+
+ m_db = mysql_init(NULL);
+
+ if ( m_db == NULL )
+ {
+ set_err("no memory");
+ return false;
+ }
+
+ if (mysql_real_connect(m_db, host, user, pwd, dbname, 0,
NULL, CLIENT_MULTI_STATEMENTS) == NULL)
+ {
+ set_err("%s", mysql_error(m_db));
+ return false;
+ }
+
+ set_err("");
+ return true;
+ }
+
+ int db::run(const tu_string& sql)
+ {
+ if (m_db == NULL)
+ {
+ set_err("missing connection");
+ return -1;
+ }
+
+ if (mysql_query(m_db, sql))
+ {
+ set_err("%s", mysql_error(m_db));
+ return -1;
+ }
+
+ set_err("");
+ return mysql_affected_rows(m_db);
+ }
+
+ table* db::open(const tu_string& sql)
+ {
+ if (m_db == NULL)
+ {
+ set_err("missing connection");
+ return NULL;
+ }
+
+ if (mysql_query(m_db, sql.c_str()))
+ {
+ set_err("%s", mysql_error(m_db));
+ return NULL;
+ }
+
+ // query succeeded, process any data returned by it
+ MYSQL_RES* result = mysql_store_result(m_db);
+ if (result)
+ {
+ table* tbl = new table(result);
+ mysql_free_result(result);
+ set_err("");
+ return tbl;
+ }
+
+ set_err("query does not return data");
+ return NULL;
+ }
+
+// bool db::get_member(const tu_stringi& name, as_value* val)
+// {
+// return as_object::get_member(name, val);
+// }
+
+// void db::set_member(const tu_stringi& name, const as_value& val)
+// {
+// as_object::set_member(name,val);
+// }
+
+ void db::set_err(const char *fmt, ...)
+ {
+ char msg[BUFSIZE];
+ va_list ap;
+ va_start(ap, fmt);
+ vsnprintf(msg, BUFSIZE, fmt, ap);
+ va_end(ap);
+
+ as_object::set_member("err", msg);
+ }
+
+
+} // end of namespace mysqldb
Index: mysql_db.h
===================================================================
RCS file: mysql_db.h
diff -N mysql_db.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ mysql_db.h 12 Jun 2006 17:35:36 -0000 1.1
@@ -0,0 +1,81 @@
+//
+// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+// Linking Gnash statically or dynamically with other modules is making a
+// combined work based on Gnash. Thus, the terms and conditions of the GNU
+// General Public License cover the whole combination.
+//
+// As a special exception, the copyright holders of Gnash give you
+// permission to combine Gnash with free software programs or libraries
+// that are released under the GNU LGPL and with code included in any
+// release of Talkback distributed by the Mozilla Foundation. You may
+// copy and distribute such a system following the terms of the GNU GPL
+// for all but the LGPL-covered parts and Talkback, and following the
+// LGPL for the LGPL-covered parts.
+//
+// Note that people who make modified versions of Gnash are not obligated
+// to grant this special exception for their modified versions; it is their
+// choice whether to do so. The GNU General Public License gives permission
+// to release a modified version without this exception; this exception
+// also makes it possible to release a modified version which carries
+// forward this exception.
+//
+
+#ifndef DB_H
+#define DB_H
+
+#include <mysql.h>
+
+#include "as_value.h"
+#include "as_object.h"
+#include "mysql_table.h"
+
+
+namespace mysqldb
+{
+ using namespace gnash;
+
+# define BUFSIZE 512
+
+ void constructor(const fn_call& fn);
+
+ class db: public as_object
+ {
+ public:
+
+ db();
+ ~db();
+
+// virtual bool get_member(const tu_stringi& name, as_value*
val);
+// virtual void set_member(const tu_stringi& name, const
as_value& val);
+
+ void set_err(const char *fmt, ...);
+
+ bool connect(const char* host, const char* dbname,
const char* user, const char* pwd);
+ table* open(const tu_string& sql);
+ int run(const tu_string& sql);
+
+ private:
+
+ void disconnect();
+
+ MYSQL* m_db;
+ };
+
+
+}
+
+#endif // SWF_DBGRID_H
Index: mysql_table.cpp
===================================================================
RCS file: mysql_table.cpp
diff -N mysql_table.cpp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ mysql_table.cpp 12 Jun 2006 17:35:36 -0000 1.1
@@ -0,0 +1,160 @@
+//
+// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+// Linking Gnash statically or dynamically with other modules is making a
+// combined work based on Gnash. Thus, the terms and conditions of the GNU
+// General Public License cover the whole combination.
+//
+// As a special exception, the copyright holders of Gnash give you
+// permission to combine Gnash with free software programs or libraries
+// that are released under the GNU LGPL and with code included in any
+// release of Talkback distributed by the Mozilla Foundation. You may
+// copy and distribute such a system following the terms of the GNU GPL
+// for all but the LGPL-covered parts and Talkback, and following the
+// LGPL for the LGPL-covered parts.
+//
+// Note that people who make modified versions of Gnash are not obligated
+// to grant this special exception for their modified versions; it is their
+// choice whether to do so. The GNU General Public License gives permission
+// to release a modified version without this exception; this exception
+// also makes it possible to release a modified version which carries
+// forward this exception.
+//
+
+#include "as_value.h"
+#include "fn_call.h"
+#include "mysql_table.h"
+
+namespace mysqldb
+{
+ using namespace gnash;
+
+ void size_method(const fn_call& fn)
+ {
+ assert(fn.this_ptr); assert(fn.env);
+ table* tbl = (table*) (as_object*) fn.this_ptr;
+ *fn.result = tbl->size();
+ }
+
+ table::table(MYSQL_RES* result)
+ {
+ as_object::set_member("size", &size_method);
+
+ // retrieve data
+ MYSQL_FIELD* fld = mysql_fetch_fields(result);
+ int num_fields = mysql_num_fields(result);
+ int num_rows = mysql_num_rows(result);
+
+ m_data.resize(num_rows);
+ for (int i = 0; i < num_rows; i++)
+ {
+ MYSQL_ROW row = mysql_fetch_row(result);
+
+ m_data[i] = new as_object();
+ m_data[i]->add_ref();
+
+ for (int j = 0; j < num_fields; j++)
+ {
+ as_value val;
+ if (row[j] == NULL)
+ {
+ val.set_null();
+ }
+ else
+ {
+ switch (fld[j].type)
+ {
+ case MYSQL_TYPE_TINY:
+ case MYSQL_TYPE_SHORT:
+ case MYSQL_TYPE_INT24:
+
val.set_int(atoi(row[j]));
+ break;
+
+ case MYSQL_TYPE_DECIMAL:
+ case MYSQL_TYPE_LONG:
+ case MYSQL_TYPE_FLOAT:
+ case MYSQL_TYPE_DOUBLE:
+ case MYSQL_TYPE_LONGLONG:
+
val.set_double(atof(row[j]));
+ break;
+
+ case MYSQL_TYPE_NULL:
+ case MYSQL_TYPE_TIMESTAMP:
+ case MYSQL_TYPE_DATE:
+ case MYSQL_TYPE_TIME:
+ case MYSQL_TYPE_DATETIME:
+ case MYSQL_TYPE_YEAR:
+ case MYSQL_TYPE_NEWDATE:
+ case MYSQL_TYPE_VARCHAR:
+ case MYSQL_TYPE_BIT:
+ case MYSQL_TYPE_NEWDECIMAL:
+ case MYSQL_TYPE_ENUM:
+ case MYSQL_TYPE_SET:
+ case MYSQL_TYPE_TINY_BLOB:
+ case MYSQL_TYPE_MEDIUM_BLOB:
+ case MYSQL_TYPE_LONG_BLOB:
+ case MYSQL_TYPE_BLOB:
+ case MYSQL_TYPE_VAR_STRING:
+ case MYSQL_TYPE_STRING:
+ case MYSQL_TYPE_GEOMETRY:
+ val.set_string(row[j]);
+ break;
+ }
+ }
+ m_data[i]->set_member(fld[j].name, val);
+ }
+ }
+ }
+
+ table::~table()
+ {
+ for (int i = 0; i < size(); i++)
+ {
+ m_data[i]->drop_ref();
+ }
+ }
+
+ bool table::get_member(const tu_stringi& name, as_value* val)
+ {
+ // check table methods
+ if (as_object::get_member(name, val) == false)
+ {
+ // hack
+ int idx = atoi(name.c_str());
+ if (idx >=0 && idx < size())
+ {
+ *val = m_data[idx];
+ }
+ else
+ {
+ val->set_undefined();
+ }
+ }
+ return true;
+ }
+
+// void table::set_member(const tu_stringi& name, const as_value& val)
+// {
+// as_object::set_member(name,val);
+// }
+
+
+ int table::size()
+ {
+ return m_data.size();
+ }
+
+} // end of namespace mysqldb
Index: mysql_table.h
===================================================================
RCS file: mysql_table.h
diff -N mysql_table.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ mysql_table.h 12 Jun 2006 17:35:36 -0000 1.1
@@ -0,0 +1,68 @@
+//
+// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+// Linking Gnash statically or dynamically with other modules is making a
+// combined work based on Gnash. Thus, the terms and conditions of the GNU
+// General Public License cover the whole combination.
+//
+// As a special exception, the copyright holders of Gnash give you
+// permission to combine Gnash with free software programs or libraries
+// that are released under the GNU LGPL and with code included in any
+// release of Talkback distributed by the Mozilla Foundation. You may
+// copy and distribute such a system following the terms of the GNU GPL
+// for all but the LGPL-covered parts and Talkback, and following the
+// LGPL for the LGPL-covered parts.
+//
+// Note that people who make modified versions of Gnash are not obligated
+// to grant this special exception for their modified versions; it is their
+// choice whether to do so. The GNU General Public License gives permission
+// to release a modified version without this exception; this exception
+// also makes it possible to release a modified version which carries
+// forward this exception.
+//
+
+#ifndef TABLE_H
+#define TABLE_H
+
+#include <mysql.h>
+#include <deque>
+
+#include "as_value.h"
+#include "as_object.h"
+
+
+namespace mysqldb
+{
+ using namespace gnash;
+
+ struct table: public as_object
+ {
+ table(MYSQL_RES* result);
+ ~table();
+
+ virtual bool get_member(const tu_stringi& name, as_value*
val);
+// virtual void set_member(const tu_stringi& name, const
as_value& val);
+ int size();
+
+ std::vector< as_object* > m_data; // [columns][rows]
+
+ };
+
+
+
+}
+
+#endif // TABLE_H
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Gnash-commit] gnash/extensions/mysql README mysql_db.cpp mysq...,
Vitaly Alexeev <=