--- parser3/src/classes/table.C 2001/05/08 10:23:50 1.78 +++ parser3/src/classes/table.C 2001/08/02 09:58:33 1.98 @@ -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.98 2001/08/02 09:58:33 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(); @@ -66,10 +65,10 @@ static void _set(Request& r, const Strin // parse cells Array rows(pool); data.split(rows, &pos_after, "\n", 1, String::UL_CLEAN); - int size=rows.quick_size(); - for(int i=0; iget_no_junction(params->size()-1, + Value& vfilename=params->as_no_junction(params->size()-1, "file name must not be code"); // loading text @@ -131,9 +130,123 @@ static void _load(Request& r, const Stri static_cast(r.self)->set_table(table); } +static Table *fill_month_days(Request& r, + const String& method_name, MethodParams *params, bool rus){ + Pool& pool=r.pool(); + Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/); + + int year=params->as_int(1, r); + int month=max(1, min(params->as_int(2, r), 12)) -1; + + tm tmIn={0, 0, 0, 1, month, year-1900}; + time_t t=mktime(&tmIn); + if(t<0) + PTHROW(0, 0, + &method_name, + "invalid date"); + tm *tmOut=localtime(&t); + + int weekDay1=tmOut->tm_wday; + if(rus) + weekDay1=weekDay1?weekDay1-1:6; //sunday last + int monthDays=getMonthDays(year, month); + + for(int _day=1-weekDay1; _day<=monthDays;) { + Array& row=*new(pool) Array(pool, 7); + for(int wday=0; wday<7; wday++, _day++) { + String *cell=new(pool) String(pool); + if(_day>=1 && _day<=monthDays) { + char *buf=(char *)malloc(2+1); + cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day), + method_name.origin().file, method_name.origin().line); + } + row+=cell; + } + *result+=&row; + } + + return result; +} + +static Table *fill_week_days(Request& r, + const String& method_name, MethodParams *params, bool rus){ + Pool& pool=r.pool(); + Array& columns=*new(pool) Array(pool, 4); + columns+=new(pool) String(pool, "year"); + columns+=new(pool) String(pool, "month"); + columns+=new(pool) String(pool, "day"); + columns+=new(pool) String(pool, "weekday"); + Table *result=new(pool) Table(pool, &method_name, &columns); + + int year=params->as_int(1, r); + int month=max(1, min(params->as_int(2, r), 12)) -1; + int day=params->as_int(3, r); + + tm tmIn={0, 0, 18, day, month, year-1900}; + time_t t=mktime(&tmIn); + if(t<0) + PTHROW(0, 0, + &method_name, + "invalid date"); + tm *tmOut=localtime(&t); + + int baseWeekDay=tmOut->tm_wday; + if(rus) + baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last + + t-=baseWeekDay*SECS_PER_DAY; + + for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) { + tm *tmOut=localtime(&t); + Array& row=*new(pool) Array(pool, 4); +#define WDFILL(size, value) { \ + char *buf=(char *)malloc(size+1); \ + String *cell=new(pool) String(pool); \ + cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \ + method_name.origin().file, \ + method_name.origin().line); \ + row+=cell; \ + } + WDFILL(4, 1900+tmOut->tm_year); + WDFILL(2, 1+tmOut->tm_mon); + WDFILL(2, tmOut->tm_mday); + WDFILL(2, tmOut->tm_wday); + *result+=&row; + } + + return result; +} + +static void _calendar(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); + + const String& what=params->as_string(0, "format must be strig"); + bool rus=false; + if(what=="rus") + rus=true; + else if(what=="eng") + rus=false; + else + PTHROW(0, 0, + &what, + "must be rus|eng"); + + Table *result=0; + if(params->size()==1+2) + result=fill_month_days(r, method_name, params, rus); + else // 1+3 + result=fill_week_days(r, method_name, params, rus); + + // replace any previous table value + static_cast(r.self)->set_table(*result); +} + + + + 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(); @@ -142,15 +255,15 @@ static void _save(Request& r, const Stri if(params->size()==1) { // not nameless=named output // write out names line if(table.columns()) { // named table - for(int column=0; columnsize(); column++) { - if(column) - sdata.APPEND_CONST("\t"); - sdata.append(*static_cast(table.columns()->quick_get(column)), + Array_iter i(*table.columns()); + while(i.has_next()) { + sdata.append(*i.next_string(), //*static_cast(table.columns()->quick_get(column)), String::UL_TABLE); + if(i.has_next()) + sdata.APPEND_CONST("\t"); } } else { // nameless table - int lsize=table.size()?static_cast(table.get(0))->size():0; - if(lsize) + if(int lsize=table.size()?static_cast(table.get(0))->size():0) for(int column=0; column(table.quick_get(index)); - for(int column=0; columnsize(); column++) { - if(column) - sdata.APPEND_CONST("\t"); - sdata.append(*static_cast(row->quick_get(column)), + Array_iter i(table); + while(i.has_next()) { + Array_iter c(*static_cast(i.next())); + while(c.has_next()) { + sdata.append(*c.next_string(), //*static_cast(row->quick_get(column)), String::UL_TABLE); + if(c.has_next()) + sdata.APPEND_CONST("\t"); } sdata.APPEND_CONST("\n"); } @@ -178,15 +292,17 @@ static void _save(Request& r, const Stri sdata.cstr(), sdata.size(), true); } -static void _count(Request& r, const String&, MethodParams *) { +static void _count(Request& r, const String& method_name, MethodParams *) { Pool& pool=r.pool(); Value& value=*new(pool) VInt(pool, static_cast(r.self)->table().size()); + value.set_name(method_name); r.write_no_lang(value); } static void _line(Request& r, const String& method_name, MethodParams *) { Pool& pool=r.pool(); Value& value=*new(pool) VInt(pool, 1+static_cast(r.self)->table().current()); + value.set_name(method_name); r.write_no_lang(value); } @@ -194,16 +310,17 @@ 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()); + value.set_name(method_name); r.write_no_lang(value); } } 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; @@ -233,34 +350,6 @@ static void _empty(Request& r, const Str r.write_no_lang(*new(pool) VBool(pool, table.size()==0)); } -/// used by table: _record / store_column_item_to_hash -struct Record_info { - Pool *pool; - Table *table; - Hash *hash; -}; -static void store_column_item_to_hash(Array::Item *item, void *info) { - Record_info& ri=*static_cast(info); - String& column_name=*static_cast(item); - Value *value; - 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); - ri.hash->put(column_name, value); -} -static void _record(Request& r, const String& method_name, MethodParams *) { - Table& table=static_cast(r.self)->table(); - if(const Array *columns=table.columns()) { - Pool& pool=r.pool(); - Value& result=*new(pool) VHash(pool); - Record_info record_info={&pool, &table, result.get_hash()}; - columns->for_each(store_column_item_to_hash, &record_info); - result.set_name(method_name); - r.write_no_lang(result); - } -} - /// used by table: _hash / table_row_to_hash struct Row_info { Table *table; @@ -280,7 +369,7 @@ static void table_row_to_hash(Array::Ite int value_field=ri.value_fields->get_int(i); if(value_fieldcolumns()->get_string(value_field), + *ri.table->columns()->get_string(value_field), new(pool) VString(*row.get_string(value_field))); } @@ -288,12 +377,13 @@ static void table_row_to_hash(Array::Ite } } static void _hash(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); Table& table=static_cast(r.self)->table(); + Value& result=*new(pool) VHash(pool); if(const Array *columns=table.columns()) 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 +393,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); } @@ -314,12 +404,11 @@ static void _hash(Request& r, const Stri } // integers: key_field & value_fields - Value& result=*new(pool) VHash(pool); Row_info row_info={&table, key_field, &value_fields, result.get_hash()}; table.for_each(table_row_to_hash, &row_info); - result.set_name(method_name); - r.write_no_lang(result); } + result.set_name(method_name); + r.write_no_lang(result); } /// used by table: _sort / sort_cmp_string|sort_cmp_double @@ -347,10 +436,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(); @@ -395,7 +484,7 @@ static void _locate(Request& r, const St VTable& vtable=*static_cast(r.self); Table& table=vtable.table(); Value& result=*new(pool) VBool(pool, table.locate( - params->get(0).as_string(), + params->get(0).as_string(), params->get(1).as_string())); result.set_name(method_name); r.write_no_lang(result); @@ -425,7 +514,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,17 +530,17 @@ 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, + PTHROW(0, 0, + &method_name, "source is not a table"); Table& src=*maybe_src; Table& dest=static_cast(r.self)->table(); if(&src == &dest) - PTHROW(0, 0, - &method_name, + PTHROW(0, 0, + &method_name, "source and destination are same table"); if(const Array *dest_columns=dest.columns()) { // dest is named @@ -470,25 +559,73 @@ static void _join(Request& r, const Stri } } +#ifndef DOXYGEN +class Table_sql_event_handlers : public SQL_Driver_query_event_handlers { +public: + Table_sql_event_handlers(Pool& apool, const String& amethod_name, + const String& astatement_string, const char *astatement_cstr) : + pool(apool), + method_name(amethod_name), + statement_string(astatement_string), + statement_cstr(astatement_cstr), + columns(*new(pool) Array(pool)), + row(0), row_index(0), + table(0) + { + } + + void add_column(void *ptr, size_t size) { + String *column=new(pool) String(pool); + column->APPEND_TAINTED( + (const char *)ptr, size, + statement_cstr, 0); + columns+=column; + } + void before_rows() { + table=new(pool) Table(pool, &method_name, &columns); + } + void add_row() { + (*table)+=(row=new(pool) Array(pool)); + } + void add_row_cell(void *ptr, size_t size) { + String *cell=new(pool) String(pool); + if(size) + cell->APPEND_TAINTED( + (const char *)ptr, size, + statement_cstr, row_index++); + (*row)+=cell; + } + +private: + Pool& pool; + const String& method_name; + const String& statement_string; const char *statement_cstr; + Array& columns; + Array *row; + uint row_index; +public: + Table *table; +}; +#endif static void _sql(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); if(!r.connection) - PTHROW(0, 0, - &method_name, + PTHROW(0, 0, + &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(); } @@ -496,77 +633,53 @@ static void _sql(Request& r, const Strin const String& statement_string=r.process(statement).as_string(); const char *statement_cstr= statement_string.cstr(String::UL_UNSPECIFIED, r.connection); - unsigned int sql_column_count; SQL_Driver::Cell *sql_columns; - unsigned long sql_row_count; SQL_Driver::Cell **sql_rows; + Table_sql_event_handlers handlers(pool, method_name, + statement_string, statement_cstr); bool need_rethrow=false; Exception rethrow_me; PTRY { r.connection->query( - statement_cstr, offset, limit, - &sql_column_count, &sql_columns, - &sql_row_count, &sql_rows); + statement_cstr, offset, limit, + handlers); } - PCATCH(e) { // connect/process problem + PCATCH(e) { // query problem rethrow_me=e; need_rethrow=true; } PEND_CATCH if(need_rethrow) - PTHROW(rethrow_me.type(), rethrow_me.code(), + PTHROW(rethrow_me.type(), rethrow_me.code(), &statement_string, // setting more specific source [were url] rethrow_me.comment()); - - Array& table_columns=*new(pool) Array(pool); - for(unsigned int i=0; i(r.self)->set_table(table); + static_cast(r.self)->set_table(*result); } static void _dir(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - Value& relative_path=params->get_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; int erroffset; regexp_code=pcre_compile(pattern, PCRE_EXTRA | PCRE_DOTALL, - &errptr, &erroffset, + &errptr, &erroffset, r.pcre_tables); if(!regexp_code) - PTHROW(0, 0, - ®exp->mid(erroffset, regexp->size()), + PTHROW(0, 0, + ®exp->mid(erroffset, regexp->size()), "regular expression syntax error - %s", errptr); ovector=(int *)malloc(sizeof(int)*(ovecsize=(1/*match*/)*3)); @@ -585,16 +698,16 @@ static void _dir(Request& r, const Strin size_t file_name_size=strlen(ffblk.ff_name); bool suits=true; if(regexp_code) { - int exec_result=pcre_exec(regexp_code, 0, - ffblk.ff_name, file_name_size, 0, + int exec_result=pcre_exec(regexp_code, 0, + ffblk.ff_name, file_name_size, 0, 0, ovector, ovecsize); if(exec_result==PCRE_ERROR_NOMATCH) suits=false; else if(exec_result<0) { (*pcre_free)(regexp_code); - PTHROW(0, 0, - regexp, + PTHROW(0, 0, + regexp, "regular expression execute (%d)", exec_result); } @@ -604,7 +717,7 @@ static void _dir(Request& r, const Strin char *file_name_cstr=(char *)r.malloc(file_name_size); memcpy(file_name_cstr, ffblk.ff_name, file_name_size); String &file_name=*new(pool) String(pool); - file_name.APPEND(file_name_cstr, file_name_size, String::UL_FILE_NAME, + file_name.APPEND(file_name_cstr, file_name_size, String::UL_FILE_NAME, method_name.origin().file, method_name.origin().line); Array& row=*new(pool) Array(pool); @@ -620,6 +733,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()) { + Array_iter i(*source_columns); + while(i.has_next()) { + Array& result_row=*new(pool) Array(pool); + result_row+=i.next(); + 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) { @@ -633,6 +768,10 @@ MTable::MTable(Pool& apool) : Methoded(a // ^table:load[nameless;file] add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2); + // ^table:calendar[month|montheng;year;month] + // ^table:calendar[week|weekeng;year;month;day] + add_native_method("calendar", Method::CT_DYNAMIC, _calendar, 3, 4); + // ^table.save[file] // ^table.save[nameless;file] add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2); @@ -654,13 +793,8 @@ MTable::MTable(Pool& apool) : Methoded(a // ^table.empty[] add_native_method("empty", Method::CT_DYNAMIC, _empty, 0, 0); - // ^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 +820,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