|
|
| version 1.65, 2001/04/26 14:55:13 | version 1.65.2.2, 2001/04/27 17:17:58 |
|---|---|
| Line 12 | Line 12 |
| #include "pcre.h" | #include "pcre.h" |
| #include "classes.h" | |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "_table.h" | |
| #include "pa_vtable.h" | #include "pa_vtable.h" |
| #include "pa_vint.h" | #include "pa_vint.h" |
| #include "pa_sql_connection.h" | #include "pa_sql_connection.h" |
| #include "pa_dir.h" | #include "pa_dir.h" |
| // global var | // defines |
| VStateless_class *table_class; | #define TABLE_CLASS_NAME "table" |
| // class | |
| class MTable : public Methoded { | |
| public: // VStateless_class | |
| Value *create_new_value(Pool& pool) { return new(pool) VTable(pool); } | |
| public: | |
| MTable(Pool& pool); | |
| bool used_directly() { return true; } | |
| }; | |
| // methods | // methods |
| static void _set(Request& r, const String& method_name, MethodParams *params) { | |
| void _set(Methoded& m, Request& r, const String& method_name, MethodParams *params) { | |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| // data is last parameter | // data is last parameter |
| Value& vdata=params->get_junction(params->size()-1, "body must be code"); | Value& vdata=params->get_junction(params->size()-1, "body must be code"); |
| Line 67 static void _set(Request& r, const Strin | Line 81 static void _set(Request& r, const Strin |
| static_cast<VTable *>(r.self)->set_table(table); | static_cast<VTable *>(r.self)->set_table(table); |
| } | } |
| static void _load(Request& r, const String& method_name, MethodParams *params) { | void _load(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| // filename is last parameter | // filename is last parameter |
| Value& vfilename=params->get_no_junction(params->size()-1, | Value& vfilename=params->get_no_junction(params->size()-1, |
| Line 116 static void _load(Request& r, const Stri | Line 130 static void _load(Request& r, const Stri |
| static_cast<VTable *>(r.self)->set_table(table); | static_cast<VTable *>(r.self)->set_table(table); |
| } | } |
| static void _save(Request& r, const String& method_name, MethodParams *params) { | void _save(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& vtable_name=params->get_no_junction(params->size()-1, | Value& vtable_name=params->get_no_junction(params->size()-1, |
| "file name must not be code"); | "file name must not be code"); |
| Line 163 static void _save(Request& r, const Stri | Line 177 static void _save(Request& r, const Stri |
| sdata.cstr(), sdata.size(), true); | sdata.cstr(), sdata.size(), true); |
| } | } |
| static void _count(Request& r, const String&, MethodParams *) { | void _count(Methoded& m, Request& r, const String&, MethodParams *) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size()); | Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size()); |
| r.write_no_lang(value); | r.write_no_lang(value); |
| } | } |
| static void _line(Request& r, const String& method_name, MethodParams *) { | void _line(Methoded& m, Request& r, const String& method_name, MethodParams *) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().current()); | Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().current()); |
| r.write_no_lang(value); | r.write_no_lang(value); |
| } | } |
| static void _offset(Request& r, const String& method_name, MethodParams *params) { | void _offset(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Table& table=static_cast<VTable *>(r.self)->table(); | Table& table=static_cast<VTable *>(r.self)->table(); |
| if(params->size()) | if(params->size()) |
| Line 186 static void _offset(Request& r, const St | Line 200 static void _offset(Request& r, const St |
| } | } |
| } | } |
| static void _menu(Request& r, const String& method_name, MethodParams *params) { | void _menu(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Value& body_code=params->get_junction(0, "body must be code"); | Value& body_code=params->get_junction(0, "body must be code"); |
| Value *delim_code=params->size()==2?¶ms->get(1):0; | Value *delim_code=params->size()==2?¶ms->get(1):0; |
| Line 210 static void _menu(Request& r, const Stri | Line 224 static void _menu(Request& r, const Stri |
| table.set_current(saved_current); vtable.unlock(); | table.set_current(saved_current); vtable.unlock(); |
| } | } |
| static void _empty(Request& r, const String& method_name, MethodParams *params) { | void _empty(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Table& table=static_cast<VTable *>(r.self)->table(); | Table& table=static_cast<VTable *>(r.self)->table(); |
| if(table.size()==0) | if(table.size()==0) |
| r.write_pass_lang(r.process(params->get(0))); | r.write_pass_lang(r.process(params->get(0))); |
| Line 235 static void store_column_item_to_hash(Ar | Line 249 static void store_column_item_to_hash(Ar |
| value=new(*ri.pool) VUnknown(*ri.pool); | value=new(*ri.pool) VUnknown(*ri.pool); |
| ri.hash->put(column_name, value); | ri.hash->put(column_name, value); |
| } | } |
| static void _record(Request& r, const String& method_name, MethodParams *params) { | void _record(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Table& table=static_cast<VTable *>(r.self)->table(); | Table& table=static_cast<VTable *>(r.self)->table(); |
| if(const Array *columns=table.columns()) { | if(const Array *columns=table.columns()) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Line 270 static int sort_cmp_double(const void *a | Line 284 static int sort_cmp_double(const void *a |
| else | else |
| return 0; | return 0; |
| } | } |
| static void _sort(Request& r, const String& method_name, MethodParams *params) { | void _sort(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Value& key_maker=params->get_junction(0, "key-maker must be code"); | Value& key_maker=params->get_junction(0, "key-maker must be code"); |
| bool reverse=params->size()==2/*..[asc|desc]*/? | bool reverse=params->size()==2/*..[asc|desc]*/? |
| Line 313 static void _sort(Request& r, const Stri | Line 327 static void _sort(Request& r, const Stri |
| table.set_current(0); | table.set_current(0); |
| } | } |
| static void _locate(Request& r, const String& method_name, MethodParams *params) { | void _locate(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| VTable& vtable=*static_cast<VTable *>(r.self); | VTable& vtable=*static_cast<VTable *>(r.self); |
| Table& table=vtable.table(); | Table& table=vtable.table(); |
| vtable.last_locate_was_successful=table.locate( | vtable.last_locate_was_successful=table.locate( |
| Line 321 static void _locate(Request& r, const St | Line 335 static void _locate(Request& r, const St |
| params->get(1).as_string()); | params->get(1).as_string()); |
| } | } |
| static void _found(Request& r, const String& method_name, MethodParams *params) { | void _found(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| if(static_cast<VTable *>(r.self)->last_locate_was_successful) | if(static_cast<VTable *>(r.self)->last_locate_was_successful) |
| r.write_pass_lang(r.process(params->get_junction(0, "found-parameter must be code"))); | r.write_pass_lang(r.process(params->get_junction(0, "found-parameter must be code"))); |
| else if(params->size()==2) | else if(params->size()==2) |
| r.write_pass_lang(r.process(params->get_junction(0, "else-parameter must be code"))); | r.write_pass_lang(r.process(params->get_junction(0, "else-parameter must be code"))); |
| } | } |
| static void _flip(Request& r, const String& method_name, MethodParams *params) { | void _flip(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| VTable& vtable=*static_cast<VTable *>(r.self); | VTable& vtable=*static_cast<VTable *>(r.self); |
| Line 348 static void _flip(Request& r, const Stri | Line 362 static void _flip(Request& r, const Stri |
| vtable.set_table(new_table); | vtable.set_table(new_table); |
| } | } |
| static void _append(Request& r, const String& method_name, MethodParams *params) { | void _append(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| // data is last parameter | // data is last parameter |
| const String& string= | const String& string= |
| Line 361 static void _append(Request& r, const St | Line 375 static void _append(Request& r, const St |
| static_cast<VTable *>(r.self)->table()+=&row; | static_cast<VTable *>(r.self)->table()+=&row; |
| } | } |
| static void _join(Request& r, const String& method_name, MethodParams *params) { | void _join(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Table *maybe_src=params->get_no_junction(0, "table ref must not be code").get_table(); | Table *maybe_src=params->get_no_junction(0, "table ref must not be code").get_table(); |
| Line 394 static void _join(Request& r, const Stri | Line 408 static void _join(Request& r, const Stri |
| } | } |
| /// ^table:sql{query}[(count[;offset])] | /// ^table:sql{query}[(count[;offset])] |
| static void _sql(Request& r, const String& method_name, MethodParams *params) { | void _sql(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| if(!r.connection) | if(!r.connection) |
| Line 471 static void _sql(Request& r, const Strin | Line 485 static void _sql(Request& r, const Strin |
| /// ^table:dir[path] | /// ^table:dir[path] |
| /// ^table:dir[path][regexp] | /// ^table:dir[path][regexp] |
| static void _dir(Request& r, const String& method_name, MethodParams *params) { | void _dir(Methoded& m, Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& relative_path=params->get_no_junction(0, "path must not be code"); | Value& relative_path=params->get_no_junction(0, "path must not be code"); |
| Line 546 static void _dir(Request& r, const Strin | Line 560 static void _dir(Request& r, const Strin |
| static_cast<VTable *>(r.self)->set_table(table); | static_cast<VTable *>(r.self)->set_table(table); |
| } | } |
| // initialize | // constructor |
| void initialize_table_class(Pool& pool, VStateless_class& vclass) { | |
| MTable::MTable(Pool& pool) : Methoded(pool) { | |
| set_name(NEW String(pool, TABLE_CLASS_NAME)); | |
| // ^table:set{data} | // ^table:set{data} |
| // ^table:set[nameless]{data} | // ^table:set[nameless]{data} |
| vclass.add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2); | add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2); |
| // ^table:load[file] | // ^table:load[file] |
| // ^table:load[nameless;file] | // ^table:load[nameless;file] |
| vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2); | add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2); |
| // ^table.save[file] | // ^table.save[file] |
| // ^table.save[nameless;file] | // ^table.save[nameless;file] |
| vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2); | add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2); |
| // ^table.count[] | // ^table.count[] |
| vclass.add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0); | add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0); |
| // ^table.line[] | // ^table.line[] |
| vclass.add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0); | add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0); |
| // ^table.offset[] | // ^table.offset[] |
| // ^table.offset[offset] | // ^table.offset[offset] |
| vclass.add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1); | add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1); |
| // ^table.menu{code} | // ^table.menu{code} |
| // ^table.menu{code}[delim] | // ^table.menu{code}[delim] |
| vclass.add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2); | add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2); |
| // ^table.empty{code-when-empty} | // ^table.empty{code-when-empty} |
| // ^table.empty{code-when-empty}{code-when-not} | // ^table.empty{code-when-empty}{code-when-not} |
| vclass.add_native_method("empty", Method::CT_DYNAMIC, _empty, 1, 2); | add_native_method("empty", Method::CT_DYNAMIC, _empty, 1, 2); |
| // ^table.record[] | // ^table.record[] |
| vclass.add_native_method("record", Method::CT_DYNAMIC, _record, 0, 0); | add_native_method("record", Method::CT_DYNAMIC, _record, 0, 0); |
| // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc] | // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc] |
| // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc] | // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc] |
| vclass.add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2); | add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2); |
| // ^table.locate[field;value] | // ^table.locate[field;value] |
| vclass.add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2); | add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2); |
| // ^table.found{when-found} | // ^table.found{when-found} |
| // ^table.found{when-found}{when-not-found} | // ^table.found{when-found}{when-not-found} |
| vclass.add_native_method("found", Method::CT_DYNAMIC, _found, 1, 2); | add_native_method("found", Method::CT_DYNAMIC, _found, 1, 2); |
| // ^table.flip[] | // ^table.flip[] |
| vclass.add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0); | add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0); |
| // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d} | // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d} |
| vclass.add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1); | add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1); |
| // ^table.join[table] | // ^table.join[table] |
| vclass.add_native_method("join", Method::CT_DYNAMIC, _join, 1, 1); | add_native_method("join", Method::CT_DYNAMIC, _join, 1, 1); |
| // ^table:sql[query][(count[;offset])] | // ^table:sql[query][(count[;offset])] |
| vclass.add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3); | add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3); |
| // ^table:dir[path] | // ^table:dir[path] |
| // ^table:dir[path][regexp] | // ^table:dir[path][regexp] |
| vclass.add_native_method("dir", Method::CT_DYNAMIC, _dir, 1, 2); | add_native_method("dir", Method::CT_DYNAMIC, _dir, 1, 2); |
| } | |
| // global variable | |
| Methoded *table_class; | |
| // creator | |
| Methoded *MTable_create(Pool& pool) { | |
| return table_class=new(pool) MTable(pool); | |
| } | } |