|
|
| version 1.147, 2020/12/29 13:56:18 | version 1.154, 2021/12/21 14:24:54 |
|---|---|
| Line 622 static void _at(Request& r, MethodParams | Line 622 static void _at(Request& r, MethodParams |
| extern String table_reverse_name; | extern String table_reverse_name; |
| static void _select(Request& r, MethodParams& params) { | static void _select(Request& r, MethodParams& params) { |
| InCycle temp(r); | |
| const String* key_var_name=¶ms.as_string(0, "key-var name must be string"); | const String* key_var_name=¶ms.as_string(0, "key-var name must be string"); |
| const String* value_var_name=¶ms.as_string(1, "value-var name must be string"); | const String* value_var_name=¶ms.as_string(1, "value-var name must be string"); |
| Value& vcondition=params.as_expression(2, "condition must be number, bool or expression"); | Value& vcondition=params.as_expression(2, "condition must be number, bool or expression"); |
| Line 634 static void _select(Request& r, MethodPa | Line 635 static void _select(Request& r, MethodPa |
| int limit=source_hash.count(); | int limit=source_hash.count(); |
| bool reverse=false; | bool reverse=false; |
| bool copy_default=false; | |
| if(params.count()>3) | if(params.count()>3) |
| if(HashStringValue* options=params.as_hash(3)) { | if(HashStringValue* options=params.as_hash(3)) { |
| Line 646 static void _select(Request& r, MethodPa | Line 648 static void _select(Request& r, MethodPa |
| valid_options++; | valid_options++; |
| reverse=r.process(*vreverse).as_bool(); | reverse=r.process(*vreverse).as_bool(); |
| } | } |
| if(Value* vcopy_default=options->get(sql_default_name)) { | |
| valid_options++; | |
| copy_default=r.process(*vcopy_default).as_bool(); | |
| } | |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| } | } |
| Line 653 static void _select(Request& r, MethodPa | Line 659 static void _select(Request& r, MethodPa |
| HashStringValue& result_hash=*new HashStringValue(); | HashStringValue& result_hash=*new HashStringValue(); |
| if(limit>0){ | if(limit>0){ |
| #ifdef HASH_ORDER | #ifdef HASH_ORDER |
| if(reverse){ | if(reverse){ |
| for(HashStringValue::ReverseIterator i(source_hash); i; i.prev()){ | for(HashStringValue::ReverseIterator i(source_hash); i; i.prev()){ |
| Line 661 static void _select(Request& r, MethodPa | Line 668 static void _select(Request& r, MethodPa |
| if(value_var_name) | if(value_var_name) |
| r.put_element(caller, *value_var_name, i.value()); | r.put_element(caller, *value_var_name, i.value()); |
| if(r.process(vcondition).as_bool()){ | bool condition=r.process(vcondition).as_bool(); |
| if(r.check_skip_break()) | |
| break; | |
| if(condition){ | |
| result_hash.put(i.key(), i.value()); | result_hash.put(i.key(), i.value()); |
| if(!--limit) | if(!--limit) |
| break; | break; |
| Line 677 static void _select(Request& r, MethodPa | Line 689 static void _select(Request& r, MethodPa |
| if(value_var_name) | if(value_var_name) |
| r.put_element(caller, *value_var_name, i.value()); | r.put_element(caller, *value_var_name, i.value()); |
| if(r.process(vcondition).as_bool()){ | bool condition=r.process(vcondition).as_bool(); |
| if(r.check_skip_break()) | |
| break; | |
| if(condition){ | |
| result_hash.put(i.key(), i.value()); | result_hash.put(i.key(), i.value()); |
| if(!--limit) | if(!--limit) |
| break; | break; |
| Line 686 static void _select(Request& r, MethodPa | Line 703 static void _select(Request& r, MethodPa |
| } | } |
| } | } |
| r.write(*new VHash(result_hash)); | VHash *result=new VHash(result_hash); |
| if(copy_default){ | |
| result->set_default(GET_SELF(r, VHashBase).get_default()); | |
| } | |
| r.write(*result); | |
| } | } |
| static void _reverse(Request& r, MethodParams& params) { | static void _reverse(Request& r, MethodParams& params) { |
| HashStringValue& source_hash=GET_SELF(r, VHashBase).hash(); | VHashBase& self=GET_SELF(r, VHashBase); |
| HashStringValue& source_hash=self.hash(); | |
| HashStringValue& result_hash=*new HashStringValue(); | HashStringValue& result_hash=*new HashStringValue(); |
| #ifdef HASH_ORDER | #ifdef HASH_ORDER |
| Line 700 static void _reverse(Request& r, MethodP | Line 723 static void _reverse(Request& r, MethodP |
| for(HashStringValue::Iterator i(source_hash); i; i.next() ) | for(HashStringValue::Iterator i(source_hash); i; i.next() ) |
| result_hash.put(i.key(), i.value()); | result_hash.put(i.key(), i.value()); |
| #endif | #endif |
| r.write(*new VHash(result_hash)); | |
| VHashBase& result=*new VHash(result_hash); | |
| if(Value* vdefault=self.get_default()) | |
| result.set_default(vdefault); | |
| r.write(result); | |
| } | |
| static void _rename(Request& r, MethodParams& params) { | |
| HashStringValue& hash=GET_SELF(r, VHashBase).hash(); | |
| if(params.count()>1){ | |
| const String& key_from=params.as_string(0, "from key must be string"); | |
| const String& key_to=params.as_string(1, "to key must be string"); | |
| hash.rename(key_from, key_to); | |
| } else { | |
| HashStringValue* names=params.as_hash(0,"single parameter"); | |
| for(HashStringValue::Iterator i(*names); i; i.next()) | |
| hash.rename(i.key(), i.value()->as_string()); | |
| } | |
| } | } |
| // constructor | // constructor |
| MHash::MHash(): Methoded("hash") | MHash::MHash(): Methoded("hash") |
| Line 754 MHash::MHash(): Methoded("hash") | Line 800 MHash::MHash(): Methoded("hash") |
| // ^hash._at([-+]offset)[['key'|'value'|'hash']] | // ^hash._at([-+]offset)[['key'|'value'|'hash']] |
| add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 2); | add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 2); |
| // ^hash.rename[from;to] | |
| // ^hash.rename[ $.from[to] ... ] | |
| add_native_method("rename", Method::CT_DYNAMIC, _rename, 1, 2); | |
| #ifdef FEATURE_GET_ELEMENT4CALL | #ifdef FEATURE_GET_ELEMENT4CALL |
| // aliases without "_" | // aliases without "_" |
| add_native_method("keys", Method::CT_DYNAMIC, _keys, 0, 1); | add_native_method("keys", Method::CT_DYNAMIC, _keys, 0, 1); |