|
|
| version 1.33, 2004/06/18 15:55:47 | version 1.38, 2007/04/23 10:30:10 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b VOID parser class. | Parser: @b VOID parser class. |
| Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2005 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) |
| */ | */ |
| Line 14 static const char * const IDENT_VOID_C=" | Line 14 static const char * const IDENT_VOID_C=" |
| #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" |
| // externs | // externs |
| Line 59 static void _double(Request& r, MethodPa | Line 60 static void _double(Request& r, MethodPa |
| params.count()==0?vvoid.as_double():params.as_double(0, "default must be double", r))); | params.count()==0?vvoid.as_double():params.as_double(0, "default must be double", r))); |
| } | } |
| #ifndef DOXYGEN | static void _bool(Request& r, MethodParams& params) { |
| static void marshal_bind( | VVoid& vvoid=GET_SELF(r, VVoid); |
| HashStringValue::key_type aname, | r.write_no_lang(*new VBool( |
| HashStringValue::value_type avalue, | params.count()==0?vvoid.as_bool():params.as_bool(0, "default must be bool", r))); |
| SQL_Driver::Placeholder** pptr) | |
| { | |
| SQL_Driver::Placeholder& ph=**pptr; | |
| ph.name=aname.cstr(); | |
| ph.value=avalue->as_string().cstr(); | |
| ph.is_null=avalue->get_class()==void_class; | |
| ph.were_updated=false; | |
| *pptr++; | |
| } | |
| // not static, used elsewhere | |
| int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders) { | |
| int hash_count=hash.count(); | |
| placeholders=new(UseGC) SQL_Driver::Placeholder[hash_count]; | |
| SQL_Driver::Placeholder* ptr=placeholders; | |
| hash.for_each(marshal_bind, &ptr); | |
| return hash_count; | |
| } | } |
| #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: |
| Line 90 public: | Line 74 public: |
| bool add_column(SQL_Error& /*error*/, const char* /*str*/, size_t /*length*/) { /* 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; |
| Line 100 public: | Line 84 public: |
| }; | }; |
| #endif | #endif |
| 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) { | static void _sql(Request& r, MethodParams& params) { |
| Value& statement=params.as_junction(0, "statement must be code"); | Value& statement=params.as_junction(0, "statement must be code"); |
| HashStringValue* bind=0; | HashStringValue* bind=0; |
| if(params.count()>1) { | if(params.count()>1) { |
| Value& voptions=params.as_no_junction(1, "options must be hash, not code"); | Value& voptions=params.as_no_junction(1, "options must be hash, not code"); |
| if(!voptions.is_string()) | if(voptions.is_defined() && !voptions.is_string()) |
| if(HashStringValue* options=voptions.get_hash()) { | if(HashStringValue* options=voptions.get_hash()) { |
| int valid_options=0; | int valid_options=0; |
| if(Value* vbind=options->get(sql_bind_name)) { | if(Value* vbind=options->get(sql_bind_name)) { |
| Line 114 static void _sql(Request& r, MethodParam | Line 102 static void _sql(Request& r, MethodParam |
| bind=vbind->get_hash(); | bind=vbind->get_hash(); |
| } | } |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| 0, | 0, |
| "called with invalid option"); | "called with invalid option"); |
| } else | } else |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| 0, | 0, |
| "options must be hash"); | "options must be hash"); |
| } | } |
| Line 139 static void _sql(Request& r, MethodParam | Line 127 static void _sql(Request& r, MethodParam |
| 0, 0, | 0, 0, |
| handlers, | handlers, |
| statement_string); | statement_string); |
| if(bind) | |
| unmarshal_bind_updates(*bind, placeholders_count, placeholders); | |
| } | } |
| static void _left_right(Request& r, MethodParams& params) { | static void _left_right(Request& r, MethodParams& params) { |
| ssize_t sn=params.as_int(0, "n must be int", r); | ssize_t sn=params.as_int(0, "n must be int", r); |
| if(sn<0) | if(sn<0) |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| 0, | 0, |
| "n(%d) must be >=0", sn); | "n(%d) must be >=0", sn); |
| Line 154 static void _left_right(Request& r, Meth | Line 145 static void _left_right(Request& r, Meth |
| static void _mid(Request& r, MethodParams& params) { | static void _mid(Request& r, MethodParams& params) { |
| ssize_t sbegin=params.as_int(0, "p must be int", r); | ssize_t sbegin=params.as_int(0, "p must be int", r); |
| if(sbegin<0) | if(sbegin<0) |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| 0, | 0, |
| "p(%d) must be >=0", sbegin); | "p(%d) must be >=0", sbegin); |
| if(params.count()>1) { | if(params.count()>1) { |
| ssize_t sn=params.as_int(1, "n must be int", r); | ssize_t sn=params.as_int(1, "n must be int", r); |
| if(sn<0) | if(sn<0) |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| 0, | 0, |
| "n(%d) must be >=0", sn); | "n(%d) must be >=0", sn); |
| } | } |
| Line 186 MVoid::MVoid(): Methoded("void") { | Line 177 MVoid::MVoid(): Methoded("void") { |
| // ^void.double(default) | // ^void.double(default) |
| add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1); | 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); | |
| // ^sql[query] | // ^sql[query] |
| add_native_method("sql", Method::CT_STATIC, _sql, 1, 2); | add_native_method("sql", Method::CT_STATIC, _sql, 1, 2); |