|
|
| version 1.5, 2001/07/23 11:19:25 | version 1.43, 2009/07/06 08:49:50 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b VOID parser class. | Parser: @b VOID parser class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | |
| */ | */ |
| static const char *RCSId="$Id$"; | |
| static const char * const IDENT_VOID_C="$Date$"; | |
| #include "classes.h" | #include "classes.h" |
| #include "pa_vmethod_frame.h" | |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_vint.h" | #include "pa_vint.h" |
| #include "pa_vdouble.h" | #include "pa_vdouble.h" |
| #include "pa_vvoid.h" | #include "pa_vvoid.h" |
| #include "pa_vbool.h" | |
| #include "pa_sql_connection.h" | #include "pa_sql_connection.h" |
| // defines | // externs |
| #define VOID_CLASS_NAME "void" | extern String sql_bind_name; |
| // class | // class |
| class MVoid : public Methoded { | class MVoid: public Methoded { |
| public: | public: |
| MVoid(Pool& pool); | MVoid(); |
| public: // Methoded | public: // Methoded |
| bool used_directly() { return true; } | bool used_directly() { return true; } |
| }; | }; |
| // global variable | |
| DECLARE_CLASS_VAR(void, new MVoid, 0); | |
| // methods | // methods |
| static void _int(Request& r, const String&, MethodParams *) { | static void _length(Request& r, MethodParams&) { |
| Pool& pool=r.pool(); | // always zero |
| VInt *vunknown=static_cast<VInt *>(r.self); | r.write_no_lang(*new VInt(0)); |
| Value& value=*new(pool) VInt(pool, vunknown->as_int()); | } |
| r.write_no_lang(value); | |
| static void _pos(Request& r, MethodParams& params) { | |
| // just checking for consistency | |
| params.as_no_junction(0, "substr must not be code"); | |
| if(params.count()>1){ | |
| ssize_t offset=params.as_int(1, "n must be int", r); | |
| if(offset<0) | |
| throw Exception(PARSER_RUNTIME, | |
| 0, | |
| "n(%d) must be >=0", offset); | |
| } | |
| // never found | |
| r.write_no_lang(*new VInt(-1)); | |
| } | |
| static void _int(Request& r, MethodParams& params) { | |
| VVoid& vvoid=GET_SELF(r, VVoid); | |
| r.write_no_lang(*new VInt( | |
| params.count()==0?vvoid.as_int():params.as_int(0, "default must be int", r))); | |
| } | } |
| static void _double(Request& r, const String&, MethodParams *) { | static void _double(Request& r, MethodParams& params) { |
| Pool& pool=r.pool(); | VVoid& vvoid=GET_SELF(r, VVoid); |
| VInt *vunknown=static_cast<VInt *>(r.self); | r.write_no_lang(*new VDouble( |
| Value& value=*new(pool) VDouble(pool, vunknown->as_double()); | params.count()==0?vvoid.as_double():params.as_double(0, "default must be double", r))); |
| r.write_no_lang(value); | } |
| static void _bool(Request& r, MethodParams& params) { | |
| VVoid& vvoid=GET_SELF(r, VVoid); | |
| r.write_no_lang(VBool::get(params.count()==0?vvoid.as_bool():params.as_bool(0, "default must be bool", r))); | |
| } | } |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| class Void_sql_event_handlers : public SQL_Driver_query_event_handlers { | class Void_sql_event_handlers: public SQL_Driver_query_event_handlers { |
| const String& statement_string; | |
| public: | public: |
| Void_sql_event_handlers(Pool& apool, const String& astatement_string) : | Void_sql_event_handlers(const String& astatement_string): statement_string(astatement_string) {} |
| pool(apool), statement_string(astatement_string) { | bool add_column(SQL_Error& /*error*/, const char* /*str*/, size_t /*length*/) { /* ignore */ return false; } |
| } | bool before_rows(SQL_Error& error) { |
| void add_column(void *ptr, size_t size) { /* ignore */ } | |
| void before_rows() { | |
| // there are some result rows, which is wrong | // there are some result rows, which is wrong |
| PTHROW(0, 0, | error=SQL_Error(PARSER_RUNTIME, |
| &statement_string, | /*statement_string,*/ |
| "must return nothing"); | "must return nothing"); |
| return true; | |
| } | } |
| void add_row() { /* never */ } | bool add_row(SQL_Error& /*error*/) { /* never */ return false; } |
| void add_row_cell(void *ptr, size_t size) { /* never */ } | bool add_row_cell(SQL_Error& /*error*/, const char* /*str*/, size_t /*length*/) { /* never */ return false; } |
| private: | |
| Pool& pool; | |
| const String& statement_string; | |
| }; | }; |
| #endif | #endif |
| static void _sql(Request& r, const String& method_name, MethodParams *params) { | |
| Pool& pool=r.pool(); | |
| if(!r.connection) | extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders); |
| PTHROW(0, 0, | extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders); |
| &method_name, | |
| "without connect"); | |
| Value& statement=params->as_junction(0, "statement must be code"); | |
| Temp_lang temp_lang(r, String::UL_SQL); | |
| const String& statement_string=r.process(statement).as_string(); | |
| const char *statement_cstr= | |
| statement_string.cstr(String::UL_UNSPECIFIED, r.connection); | |
| Void_sql_event_handlers handlers(pool, statement_string); | |
| bool need_rethrow=false; Exception rethrow_me; | |
| PTRY { | |
| r.connection->query( | |
| statement_cstr, 0, 0, | |
| handlers); | |
| } | |
| PCATCH(e) { | |
| rethrow_me=e; need_rethrow=true; | |
| } | |
| PEND_CATCH | |
| if(need_rethrow) | |
| PTHROW(rethrow_me.type(), rethrow_me.code(), | |
| &statement_string, // setting more specific source [were url] | |
| rethrow_me.comment()); | |
| } | |
| // constructor | static void _sql(Request& r, MethodParams& params) { |
| Value& statement=params.as_junction(0, "statement must be code"); | |
| MVoid::MVoid(Pool& apool) : Methoded(apool) { | HashStringValue* bind=0; |
| set_name(*NEW String(pool(), VOID_CLASS_NAME)); | if(params.count()>1) { |
| Value& voptions=params.as_no_junction(1, "options must be hash, not code"); | |
| if(voptions.is_defined() && !voptions.is_string()) | |
| if(HashStringValue* options=voptions.get_hash()) { | |
| int valid_options=0; | |
| if(Value* vbind=options->get(sql_bind_name)) { | |
| valid_options++; | |
| bind=vbind->get_hash(); | |
| } | |
| if(valid_options!=options->count()) | |
| throw Exception(PARSER_RUNTIME, | |
| 0, | |
| "called with invalid option"); | |
| } else | |
| throw Exception(PARSER_RUNTIME, | |
| 0, | |
| "options must be hash"); | |
| } | |
| SQL_Driver::Placeholder* placeholders=0; | |
| uint placeholders_count=0; | |
| if(bind) | |
| placeholders_count=marshal_binds(*bind, placeholders); | |
| Temp_lang temp_lang(r, String::L_SQL); | |
| const String& statement_string=r.process_to_string(statement); | |
| const char* statement_cstr=statement_string.cstr(String::L_UNSPECIFIED, r.connection()); | |
| Void_sql_event_handlers handlers(statement_string); | |
| r.connection()->query( | |
| statement_cstr, | |
| placeholders_count, placeholders, | |
| 0, SQL_NO_LIMIT, | |
| handlers, | |
| statement_string); | |
| // ^VOID.int[] | if(bind) |
| add_native_method("int", Method::CT_DYNAMIC, _int, 0, 0); | unmarshal_bind_updates(*bind, placeholders_count, placeholders); |
| } | |
| // ^VOID.double[] | static void _left_right(Request& r, MethodParams& params) { |
| add_native_method("double", Method::CT_DYNAMIC, _double, 0, 0); | ssize_t sn=params.as_int(0, "n must be int", r); |
| if(sn<0) | |
| throw Exception(PARSER_RUNTIME, | |
| 0, | |
| "n(%d) must be >=0", sn); | |
| // ^sql[query] | // return nothing |
| add_native_method("sql", Method::CT_STATIC, _sql, 1, 1); | |
| } | } |
| // global variable | static void _mid(Request& r, MethodParams& params) { |
| ssize_t sbegin=params.as_int(0, "p must be int", r); | |
| if(sbegin<0) | |
| throw Exception(PARSER_RUNTIME, | |
| 0, | |
| "p(%d) must be >=0", sbegin); | |
| if(params.count()>1) { | |
| ssize_t sn=params.as_int(1, "n must be int", r); | |
| if(sn<0) | |
| throw Exception(PARSER_RUNTIME, | |
| 0, | |
| "n(%d) must be >=0", sn); | |
| } | |
| Methoded *void_class; | // return nothing |
| } | |
| // constructor | |
| // creator | MVoid::MVoid(): Methoded("void") { |
| // ^void.length[] | |
| add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0); | |
| // ^void.pos[substr] | |
| // ^void.pos[substr](n) | |
| add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2); | |
| // ^void.int[] | |
| // ^void.int(default) | |
| add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1); | |
| // ^void.double[] | |
| // ^void.double(default) | |
| add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1); | |
| // ^void.bool[] | |
| // ^void.bool(default) | |
| add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1); | |
| // ^void:sql{query} | |
| add_native_method("sql", Method::CT_STATIC, _sql, 1, 2); | |
| // ^void.left() ^void.right() | |
| add_native_method("left", Method::CT_DYNAMIC, _left_right, 1, 1); | |
| add_native_method("right", Method::CT_DYNAMIC, _left_right, 1, 1); | |
| Methoded *MVoid_create(Pool& pool) { | // ^void.mid(p;n) |
| return void_class=new(pool) MVoid(pool); | add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2); |
| } | } |