|
|
| version 1.28.2.5.2.2, 2003/03/21 13:42:28 | version 1.48, 2010/10/21 15:25:07 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b VOID parser class. | Parser: @b VOID parser class. |
| Copyright (c) 2001-2003 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: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| static const char* IDENT_VOID_C="$Date$"; | static const char * const IDENT_VOID_C="$Date$"; |
| #include "classes.h" | #include "classes.h" |
| #include "pa_vmethod_frame.h" | #include "pa_vmethod_frame.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_vint.h" | |
| #include "pa_vdouble.h" | |
| #include "pa_vvoid.h" | #include "pa_vvoid.h" |
| #include "pa_sql_connection.h" | #include "pa_sql_connection.h" |
| // externs | |
| extern String sql_bind_name; | |
| // class | // class |
| class MVoid: public Methoded { | class MVoid: public Methoded { |
| public: | public: |
| MVoid(); | MVoid(); |
| public: // Methoded | |
| bool used_directly() { return true; } | |
| }; | }; |
| // global variable | // void is inherited from string, thus global variable declared in string.C |
| DECLARE_CLASS_VAR(void, new MVoid, 0); | |
| // methods | // methods |
| static void _length(Request& r, const String& /*method_name*/, MethodParams* /*params*/) { | |
| // always zero | |
| r.write_no_lang(Value*(new VInt(0))); | |
| } | |
| static void _pos(Request& r, const String& method_name, MethodParams* params) { | |
| // just checking for consistency | |
| params->as_no_junction(0, "substr must not be code"); | |
| // never found | |
| r.write_no_lang(Value*(new VInt(-1))); | |
| } | |
| static void _int(Request& r, const String& /*method_name*/, MethodParams* params) { | |
| VVoid& vvoid=*static_cast<VVoid *>(r.get_self().get()); | |
| r.write_no_lang(Value*(new VInt( | |
| params->count()==0?vvoid.as_int():params->as_int(0, "default must be int", r)))); | |
| } | |
| static void _double(Request& r, const String& /*method_name*/, MethodParams* params) { | |
| VVoid& vvoid=*static_cast<VVoid *>(r.get_self().get()); | |
| r.write_no_lang(Value*(new VDouble( | |
| params->count()==0?vvoid.as_double():params->as_double(0, "default must be double", 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; | const String& statement_string; |
| public: | public: |
| Void_sql_event_handlers(const String& astatement_string): statement_string(astatement_string) {} | Void_sql_event_handlers(const String& astatement_string): statement_string(astatement_string) {} |
| bool add_column(SQL_Error& /*error*/, void *ptr, size_t size) { /* ignore */ return false; } | bool add_column(SQL_Error& /*error*/, const char* /*str*/, size_t /*length*/) { /* ignore */ return false; } |
| bool before_rows(SQL_Error& error) { | bool before_rows(SQL_Error& error) { |
| // there are some result rows, which is wrong | // there are some result rows, which is wrong |
| error=SQL_Error("parser.runtime", | error=SQL_Error(PARSER_RUNTIME, |
| /*statement_string,*/ | /*statement_string,*/ |
| "must return nothing"); | "must return nothing"); |
| return true; | return true; |
| } | } |
| bool add_row(SQL_Error& /*error*/) { /* never */ return false; } | bool add_row(SQL_Error& /*error*/) { /* never */ return false; } |
| bool add_row_cell(SQL_Error& /*error*/, void *ptr, size_t size) { /* never */ return false; } | bool add_row_cell(SQL_Error& /*error*/, const char* /*str*/, size_t /*length*/) { /* never */ return false; } |
| }; | }; |
| #endif | #endif |
| static void _sql(Request& r, const String& method_name, MethodParams* params) { | |
| Value* statement=params->as_junction(0, "statement must be code"); | extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders); |
| extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders); | |
| static void _sql(Request& r, MethodParams& params) { | |
| Value& statement=params.as_junction(0, "statement must be code"); | |
| HashStringValue* bind=0; | |
| 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); | Temp_lang temp_lang(r, String::L_SQL); |
| const String& statement_string=r.process_to_string(statement); | const String& statement_string=r.process_to_string(statement); |
| const char* statement_cstr= | const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection()); |
| statement_string->cstr(String::L_UNSPECIFIED, r.connection(method_name)); | |
| Void_sql_event_handlers handlers(statement_string); | Void_sql_event_handlers handlers(statement_string); |
| r.connection(method_name)->query( | r.connection()->query( |
| statement_cstr, 0, 0, | statement_cstr, |
| placeholders_count, placeholders, | |
| 0, SQL_NO_LIMIT, | |
| handlers, | handlers, |
| statement_string); | statement_string); |
| if(bind) | |
| unmarshal_bind_updates(*bind, placeholders_count, placeholders); | |
| } | } |
| // constructor | // constructor |
| MVoid::MVoid(): Methoded("void") { | MVoid::MVoid(): Methoded("void", string_class) { |
| // ^void.length[] | // ^void:sql{query} |
| add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0); | add_native_method("sql", Method::CT_STATIC, _sql, 1, 2); |
| // ^void.pos[substr] | |
| add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1); | |
| // ^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); | |
| // ^sql[query] | // all other methods are inherinted from empty string |
| add_native_method("sql", Method::CT_STATIC, _sql, 1, 1); | |
| } | } |