1 /*
2  * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2013-2014, Peter Olsson <peter@olssononline.se>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Andrey Volk <andywolk@gmail.com>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  * Peter Olsson <peter@olssononline.se>
26  * Andrey Volk <andywolk@gmail.com>
27  *
28  * fsdbh.hpp -- JavaScript DBH class header
29  *
30  */
31 
32 #ifndef FS_DBH_H
33 #define FS_DBH_H
34 
35 #include "mod_v8.h"
36 
37 /* Macros for easier V8 callback definitions */
38 #define JS_DBH_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSDBH)
39 #define JS_DBH_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSDBH)
40 #define JS_DBH_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSDBH)
41 #define JS_DBH_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSDBH)
42 #define JS_DBH_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSDBH)
43 #define JS_DBH_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSDBH)
44 
45 #define JS_DBH_GET_PROPERTY_IMPL_STATIC(method_name) JS_GET_PROPERTY_IMPL_STATIC(method_name, FSDBH)
46 #define JS_DBH_SET_PROPERTY_IMPL_STATIC(method_name) JS_SET_PROPERTY_IMPL_STATIC(method_name, FSDBH)
47 #define JS_DBH_FUNCTION_IMPL_STATIC(method_name) JS_FUNCTION_IMPL_STATIC(method_name, FSDBH)
48 
49 class FSDBH : public JSBase
50 {
51 private:
52 	v8::Persistent<v8::Function> _callback;
53 	std::string _dsn;
54 	switch_cache_db_handle_t *dbh;
55 	char *err;
56 
57 	void Init();
58 
59 	bool _release();
60 	void clear_error();
61 
62 	static int Callback(void *pArg, int argc, char **argv, char **columnNames);
63 public:
FSDBH(JSMain * owner)64 	FSDBH(JSMain *owner) : JSBase(owner) { Init(); }
FSDBH(const v8::FunctionCallbackInfo<v8::Value> & info)65 	FSDBH(const v8::FunctionCallbackInfo<v8::Value>& info) : JSBase(info) { Init(); }
66 
67 	virtual ~FSDBH(void);
68 	virtual std::string GetJSClassName();
69 
70 	static const v8_mod_interface_t *GetModuleInterface();
71 
72 	static FSDBH *New(char *dsn, char *username, char *password, const v8::FunctionCallbackInfo<v8::Value>& info);
73 
74 	/* Methods available from JavaScript */
75 	static void *Construct(const v8::FunctionCallbackInfo<v8::Value>& info);
76 	JS_DBH_FUNCTION_DEF(affected_rows);
77 	JS_DBH_FUNCTION_DEF(connected);
78 	JS_DBH_FUNCTION_DEF(last_error);
79 	JS_DBH_FUNCTION_DEF(query);
80 	JS_DBH_FUNCTION_DEF(release);
81 	JS_DBH_FUNCTION_DEF(test_reactive);
82 	JS_DBH_GET_PROPERTY_DEF(GetProperty);
83 };
84 
85 #endif /* FS_DB_H */
86 
87 /* For Emacs:
88  * Local Variables:
89  * mode:c
90  * indent-tabs-mode:t
91  * tab-width:4
92  * c-basic-offset:4
93  * End:
94  * For VIM:
95  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
96  */
97