--- parser3/src/classes/table.C 2001/05/08 10:23:50 1.78 +++ parser3/src/classes/table.C 2001/07/07 16:38:01 1.90 @@ -4,9 +4,8 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: table.C,v 1.78 2001/05/08 10:23:50 paf Exp $ */ +static const char *RCSId="$Id: table.C,v 1.90 2001/07/07 16:38:01 parser Exp $"; #include "pa_config_includes.h" @@ -43,7 +42,7 @@ public: // Methoded static void _set(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); // data is last parameter - Value& vdata=params->get_junction(params->size()-1, "body must be code"); + Value& vdata=params->as_junction(params->size()-1, "body must be code"); Temp_lang temp_lang(r, String::UL_PASS_APPENDED); const String& data=r.process(vdata).as_string(); @@ -85,7 +84,7 @@ static void _set(Request& r, const Strin static void _load(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); // filename is last parameter - Value& vfilename=params->get_no_junction(params->size()-1, + Value& vfilename=params->as_no_junction(params->size()-1, "file name must not be code"); // loading text @@ -133,7 +132,7 @@ static void _load(Request& r, const Stri static void _save(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - Value& vtable_name=params->get_no_junction(params->size()-1, + Value& vtable_name=params->as_no_junction(params->size()-1, "file name must not be code"); Table& table=static_cast(r.self)->table(); @@ -194,8 +193,8 @@ static void _offset(Request& r, const St Pool& pool=r.pool(); Table& table=static_cast(r.self)->table(); if(params->size()) { - Value& offset_expr=params->get_junction(0, "offset must be expression"); - table.shift((int)r.process(offset_expr).as_double()); + Value& offset_expr=params->as_junction(0, "offset must be expression"); + table.shift(r.process(offset_expr).as_int()); } else { Value& value=*new(pool) VInt(pool, table.current()); r.write_no_lang(value); @@ -203,7 +202,7 @@ static void _offset(Request& r, const St } static void _menu(Request& r, const String& method_name, MethodParams *params) { - Value& body_code=params->get_junction(0, "body must be code"); + Value& body_code=params->as_junction(0, "body must be code"); Value *delim_code=params->size()==2?¶ms->get(1):0; @@ -246,7 +245,7 @@ static void store_column_item_to_hash(Ar if(const String *column_item=ri.table->item(column_name)) value=new(*ri.pool) VString(*column_item); else - value=new(*ri.pool) VUnknown(*ri.pool); + value=new(*ri.pool) VVoid(*ri.pool); ri.hash->put(column_name, value); } static void _record(Request& r, const String& method_name, MethodParams *) { @@ -293,7 +292,7 @@ static void _hash(Request& r, const Stri if(columns->size()>1) { Pool& pool=r.pool(); - const String& key_field_name=params->get_no_junction(0, + const String& key_field_name=params->as_no_junction(0, "key field name must not be code").as_string(); int key_field=table.column_name2index(key_field_name, true); int value_fields_count=params->size()-1; @@ -303,7 +302,7 @@ static void _hash(Request& r, const Stri Array value_fields(pool, value_fields_count); if(value_fields_by_params) { for(int i=1; isize(); i++) { - const String& value_field_name=params->get_no_junction(i, + const String& value_field_name=params->as_no_junction(i, "value field name must not be code").as_string(); value_fields+=table.column_name2index(value_field_name, true); } @@ -347,10 +346,10 @@ static int sort_cmp_double(const void *a return 0; } static void _sort(Request& r, const String& method_name, MethodParams *params) { - Value& key_maker=params->get_junction(0, "key-maker must be code"); + Value& key_maker=params->as_junction(0, "key-maker must be code"); bool reverse=params->size()==2/*..[asc|desc]*/? - reverse=params->get_no_junction(1, "order must not be code").as_string()=="desc": + reverse=params->as_no_junction(1, "order must not be code").as_string()=="desc": false; Table& table=static_cast(r.self)->table(); @@ -425,7 +424,7 @@ static void _append(Request& r, const St Pool& pool=r.pool(); // data is last parameter const String& string= - r.process(params->get_junction(0, "body must be code")).as_string(); + r.process(params->as_junction(0, "body must be code")).as_string(); // parse cells Array& row=*new(pool) Array(pool); @@ -441,7 +440,7 @@ static void _append(Request& r, const St static void _join(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - Table *maybe_src=params->get_no_junction(0, "table ref must not be code").get_table(); + Table *maybe_src=params->as_no_junction(0, "table ref must not be code").get_table(); if(!maybe_src) PTHROW(0, 0, &method_name, @@ -478,17 +477,17 @@ static void _sql(Request& r, const Strin &method_name, "without connect"); - Value& statement=params->get_junction(0, "statement must be code"); + Value& statement=params->as_junction(0, "statement must be code"); ulong limit=0; if(params->size()>1) { - Value& limit_code=params->get_junction(1, "limit must be expression"); + Value& limit_code=params->as_junction(1, "limit must be expression"); limit=(uint)r.process(limit_code).as_double(); } ulong offset=0; if(params->size()>2) { - Value& offset_code=params->get_junction(2, "offset must be expression"); + Value& offset_code=params->as_junction(2, "offset must be expression"); offset=(ulong)r.process(offset_code).as_double(); } @@ -505,7 +504,7 @@ static void _sql(Request& r, const Strin &sql_column_count, &sql_columns, &sql_row_count, &sql_rows); } - PCATCH(e) { // connect/process problem + PCATCH(e) { // query problem rethrow_me=e; need_rethrow=true; } PEND_CATCH @@ -525,20 +524,19 @@ static void _sql(Request& r, const Strin Table& table=*new(pool) Table(pool, &method_name, &table_columns); - { - for(unsigned long r=0; rget_no_junction(0, "path must not be code"); + Value& relative_path=params->as_no_junction(0, "path must not be code"); const String *regexp; pcre *regexp_code; int ovecsize; int *ovector; if(params->size()>1) { - regexp=¶ms->get_no_junction(1, "regexp must not be code").as_string(); + regexp=¶ms->as_no_junction(1, "regexp must not be code").as_string(); const char *pattern=regexp->cstr(String::UL_AS_IS); const char *errptr; @@ -620,6 +618,28 @@ static void _dir(Request& r, const Strin static_cast(r.self)->set_table(table); } +static void _columns(Request& r, const String& method_name, MethodParams *) { + Pool& pool=r.pool(); + + Array& result_columns=*new(pool) Array(pool); + result_columns+=new(pool) String(pool, "column"); + Table& result_table=*new(pool) Table(pool, &method_name, &result_columns); + + Table& source_table=static_cast(r.self)->table(); + if(const Array *source_columns=source_table.columns()) { + int size=source_columns->quick_size(); + for(int i=0; iquick_get(i); + result_table+=&result_row; + } + } + + VTable& result=*new(pool) VTable(pool, &result_table); + result.set_name(method_name); + r.write_no_lang(result); +} + // constructor MTable::MTable(Pool& apool) : Methoded(apool) { @@ -657,10 +677,8 @@ MTable::MTable(Pool& apool) : Methoded(a // ^table.record[] add_native_method("record", Method::CT_DYNAMIC, _record, 0, 0); - /** @fn _hash - ^table:hash[key field name] - ^table:hash[key field name][value field name;...] - */ + // ^table:hash[key field name] + // ^table:hash[key field name][value field name;...] add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 1000); // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc] @@ -686,6 +704,9 @@ MTable::MTable(Pool& apool) : Methoded(a // ^table:dir[path] // ^table:dir[path][regexp] add_native_method("dir", Method::CT_DYNAMIC, _dir, 1, 2); + + // ^table:columns[] + add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 0); } // global variable