Annotation of parser3/src/classes/void.C, revision 1.48

1.1       parser      1: /** @file
                      2:        Parser: @b VOID parser class.
                      3: 
1.42      misha       4:        Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.16      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.20      paf         6: */
1.1       parser      7: 
1.48    ! moko        8: static const char * const IDENT_VOID_C="$Date: 2010-10-21 15:06:29 $";
1.1       parser      9: 
                     10: #include "classes.h"
1.29      paf        11: #include "pa_vmethod_frame.h"
                     12: 
1.1       parser     13: #include "pa_request.h"
                     14: #include "pa_vvoid.h"
                     15: #include "pa_sql_connection.h"
                     16: 
1.33      paf        17: // externs
                     18: 
                     19: extern String sql_bind_name;
                     20: 
1.1       parser     21: // class
                     22: 
1.29      paf        23: class MVoid: public Methoded {
1.1       parser     24: public:
1.29      paf        25:        MVoid();
1.1       parser     26: };
                     27: 
1.48    ! moko       28: // void is inherited from string, thus global variable declared in string.C
1.29      paf        29: 
1.1       parser     30: // methods
                     31: 
1.5       parser     32: #ifndef DOXYGEN
1.26      paf        33: class Void_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.29      paf        34:        const String& statement_string;
1.5       parser     35: public:
1.29      paf        36:        Void_sql_event_handlers(const String& astatement_string): statement_string(astatement_string) {}
                     37:        bool add_column(SQL_Error& /*error*/, const char* /*str*/, size_t /*length*/) { /* ignore */ return false; }
1.27      paf        38:        bool before_rows(SQL_Error& error) {
1.5       parser     39:                // there are some result rows, which is wrong
1.38      misha      40:                error=SQL_Error(PARSER_RUNTIME,
1.29      paf        41:                        /*statement_string,*/
1.5       parser     42:                        "must return nothing");
1.27      paf        43:                return true;
1.5       parser     44:        }
1.27      paf        45:        bool add_row(SQL_Error& /*error*/) { /* never */ return false; }
1.29      paf        46:        bool add_row_cell(SQL_Error& /*error*/, const char* /*str*/, size_t /*length*/) { /* never */ return false; }
1.5       parser     47: 
                     48: };
                     49: #endif
1.34      paf        50: 
                     51: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
                     52: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
                     53: 
1.29      paf        54: static void _sql(Request& r, MethodParams& params) {
                     55:        Value& statement=params.as_junction(0, "statement must be code");
1.1       parser     56: 
1.33      paf        57:        HashStringValue* bind=0;
                     58:        if(params.count()>1) {
                     59:                Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.36      paf        60:                if(voptions.is_defined() && !voptions.is_string())
1.33      paf        61:                        if(HashStringValue* options=voptions.get_hash()) {
                     62:                                int valid_options=0;
                     63:                                if(Value* vbind=options->get(sql_bind_name)) {
                     64:                                        valid_options++;
                     65:                                        bind=vbind->get_hash();
                     66:                                }
                     67:                                if(valid_options!=options->count())
1.46      misha      68:                                        throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.33      paf        69:                        } else
1.46      misha      70:                                throw Exception(PARSER_RUNTIME, 0, OPTIONS_MUST_BE_HASH);
1.33      paf        71:        }
                     72: 
                     73:        SQL_Driver::Placeholder* placeholders=0;
                     74:        uint placeholders_count=0;
                     75:        if(bind)
                     76:                placeholders_count=marshal_binds(*bind, placeholders);
                     77: 
1.29      paf        78:        Temp_lang temp_lang(r, String::L_SQL);
1.18      paf        79:        const String& statement_string=r.process_to_string(statement);
1.45      misha      80:        const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection());
1.43      misha      81: 
1.29      paf        82:        Void_sql_event_handlers handlers(statement_string);
                     83:        r.connection()->query(
1.33      paf        84:                statement_cstr, 
                     85:                placeholders_count, placeholders,
1.39      misha      86:                0, SQL_NO_LIMIT,
1.22      paf        87:                handlers,
                     88:                statement_string);
1.34      paf        89: 
                     90:        if(bind)
                     91:                unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.1       parser     92: }
                     93: 
                     94: // constructor
                     95: 
1.48    ! moko       96: MVoid::MVoid(): Methoded("void", string_class) {
1.41      misha      97:        // ^void:sql{query}
1.33      paf        98:        add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.32      paf        99: 
1.48    ! moko      100:        // all other methods are inherinted from empty string
1.1       parser    101: }

E-mail: