--- parser3/src/classes/op.C 2001/12/07 15:24:46 1.63 +++ parser3/src/classes/op.C 2002/01/25 11:33:45 1.68 @@ -4,11 +4,10 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://paf.design.ru) - $Id: op.C,v 1.63 2001/12/07 15:24:46 paf Exp $ + $Id: op.C,v 1.68 2002/01/25 11:33:45 paf Exp $ */ #include "classes.h" -#include "pa_config_includes.h" #include "pa_common.h" #include "pa_request.h" #include "pa_vint.h" @@ -125,7 +124,10 @@ static void _process(Request& r, const S // process source code, append processed methods to 'self' class // maybe-define new @main - r.use_buf(source.cstr(String::UL_UNSPECIFIED, r.connection), place, &self_class); + r.use_buf( + source.cstr(String::UL_UNSPECIFIED, r.connection(0)), + place, + &self_class); // maybe-execute @main[] if(const Method *method=self_class.get_method(*main_method_name)) { @@ -246,7 +248,7 @@ struct timeval mt[2]; gettimeofday(&mt[0],NULL); #endif // connect - SQL_Connection& connection=SQL_driver_manager->get_connection( + SQL_Connection_ptr connection=SQL_driver_manager->get_connection( url.as_string(), method_name, protocol2driver_and_client); #ifdef RESOURCES_DEBUG @@ -259,35 +261,15 @@ for(int i=0;i<2;i++) r.sql_connect_time+=t[1]-t[0]; #endif - // remember/set current connection - SQL_Connection *saved_connection=r.connection; - r.connection=&connection; + Temp_connection temp_connection(r, connection.get()); // execute body try { - try { - r.write_assign_lang(r.process(body_code)); - - connection.commit(); - } catch(...) { // process/commit problem - connection.rollback(); - - /*re*/throw; - } - - } catch(...) { - // close connection [cache it] - connection.close(); - // recall current connection from remembered - r.connection=saved_connection; - - /*re*/throw; + r.write_assign_lang(r.process(body_code)); + + } catch(...) { // process/commit problem + connection->mark_to_rollback(); + /*re*/throw; } - - // and anyway - // close connection [cache it] - connection.close(); - // recall current connection from remembered - r.connection=saved_connection; } #ifndef DOXYGEN @@ -356,10 +338,10 @@ struct Data_string_serialized_prolog { }; #endif -void cache_delete(Pool& pool, const String& file_spec) { - file_delete(pool, file_spec, false/*fail_on_read_problem*/); +void cache_delete(const String& file_spec) { + file_delete(file_spec, false/*fail_on_read_problem*/); } -void cache_put(Pool& pool, const String& file_spec, const String& data_string) { +void cache_put(const String& file_spec, const String& data_string) { void *data; size_t data_size; data_string.serialize( sizeof(Data_string_serialized_prolog), @@ -369,7 +351,7 @@ void cache_put(Pool& pool, const String& prolog.version=DATA_STRING_SERIALIZED_VERSION; - file_write(pool, + file_write( file_spec, data, data_size, false/*as_text*/); @@ -397,20 +379,21 @@ String *cache_get(Pool& pool, const Stri prolog.version, DATA_STRING_SERIALIZED_VERSION); String& result=*new(pool) String(pool); - if(data_size) { - result.deserialize( - sizeof(Data_string_serialized_prolog), - data, data_size, file_spec.cstr()); - } - + result.deserialize( + sizeof(Data_string_serialized_prolog), + data, data_size, file_spec.cstr()); return &result; } static void _cache(Request& r, const String& method_name, MethodParams *params) { - // ^cache[file_spec](lifespan){code} time=0 no cache Pool& pool=r.pool(); // file_spec, expires, body code - const String &file_spec=params->as_string(0, "key must be string"); + const String &file_spec=r.absolute(params->as_string(0, "filespec must be string")); + if(params->size()==1) { // delete + cache_delete(file_spec); + return; + } + time_t lifespan=(time_t)params->as_double(1, "lifespan must be number", r); Value& body_code=params->as_junction(2, "body must be code"); @@ -420,7 +403,7 @@ static void _cache(Request& r, const Str // {file_spec} modification time if(!file_stat(file_spec, size, atime, mtime, ctime, false/*no exception on error*/) || (time(0)-mtime) > lifespan) // cached file expired - cache_delete(pool, file_spec); + cache_delete(file_spec); else if(String *cached_body=cache_get(pool, file_spec)) { // have cached copy? // write it out @@ -429,14 +412,14 @@ static void _cache(Request& r, const Str return; } } else // 'lifespan'=0, forget cached copy - cache_delete(pool, file_spec); + cache_delete(file_spec); // process Value& processed_body=r.process(body_code); // put it to cache if 'lifespan' specified if(lifespan) - cache_put(pool, file_spec, processed_body.as_string()); + cache_put(file_spec, processed_body.as_string()); // write it out r.write_assign_lang(processed_body); @@ -487,8 +470,9 @@ MOP::MOP(Pool& apool) : Methoded(apool), add_native_method("connect", Method::CT_ANY, _connect, 2, 2); + // ^cache[file_spec] delete cache // ^cache[file_spec](time){code} time=0 no cache - add_native_method("cache", Method::CT_ANY, _cache, 3, 3); + add_native_method("cache", Method::CT_ANY, _cache, 1, 3); // switch