|
|
| version 1.35, 2024/11/04 03:53:25 | version 1.43, 2026/04/25 13:38:46 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b array parser class. | Parser: @b array parser class. |
| Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) |
| Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| Line 452 static void _sql(Request& r, MethodParam | Line 452 static void _sql(Request& r, MethodParam |
| Table2hash_value_type value_type=C_HASH; | Table2hash_value_type value_type=C_HASH; |
| if(params.count()>1) | if(params.count()>1) |
| if(HashStringValue* options=params.as_hash(1, "sql options")) { | if(HashStringValue* options=params.as_hash(1, "sql options")) { |
| int valid_options=0; | |
| bool distinct_specified=false; | bool distinct_specified=false; |
| for(HashStringValue::Iterator i(*options); i; i.next() ){ | for(HashStringValue::Iterator i(*options); i; i.next() ){ |
| String::Body key=i.key(); | String::Body key=i.key(); |
| Value* value=i.value(); | Value* value=i.value(); |
| if(key == sql_bind_name) { | if(key == sql_bind_name) { |
| bind=value->get_hash(); | bind=value->get_hash(); |
| valid_options++; | |
| } else if(key == sql_limit_name) { | } else if(key == sql_limit_name) { |
| limit=(ulong)r.process(*value).as_double(); | limit=(ulong)r.process(*value).as_double(); |
| valid_options++; | |
| } else if(key == sql_offset_name) { | } else if(key == sql_offset_name) { |
| offset=(ulong)r.process(*value).as_double(); | offset=(ulong)r.process(*value).as_double(); |
| valid_options++; | |
| } else if (key == sql_distinct_name) { | } else if (key == sql_distinct_name) { |
| distinct=r.process(*value).as_bool(); | distinct=r.process(*value).as_bool(); |
| distinct_specified=true; | distinct_specified=true; |
| valid_options++; | |
| } else if (key == sql_value_type_name) { | } else if (key == sql_value_type_name) { |
| value_type=get_value_type(r.process(*value)); | value_type=get_value_type(r.process(*value)); |
| valid_options++; | |
| } else if (key == "sparse") { | } else if (key == "sparse") { |
| sparse=r.process(*value).as_bool(); | sparse=r.process(*value).as_bool(); |
| valid_options++; | } else |
| } | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| } | } |
| if(valid_options!=options->count()) | |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | |
| if(distinct_specified && !sparse) | if(distinct_specified && !sparse) |
| throw Exception(PARSER_RUNTIME, 0, "'distinct' option can only be used when $.sparse(true) is specified"); | throw Exception(PARSER_RUNTIME, 0, "'distinct' option can only be used when $.sparse(true) is specified"); |
| } | } |
| Line 588 static void _keys(Request& r, MethodPara | Line 580 static void _keys(Request& r, MethodPara |
| for(ArrayValue::Iterator i(array); i; i.next()){ | for(ArrayValue::Iterator i(array); i; i.next()){ |
| if(i.value()){ | if(i.value()){ |
| Table::element_type row(new ArrayString(1)); | Table::element_type row(new ArrayString(1)); |
| *row+=new String(i.key(), String::L_TAINTED); | *row+=new String(pa_uitoa(i.index())); |
| *table+=row; | *table+=row; |
| } | } |
| } | } |
| Line 657 static void _remove(Request& r, MethodPa | Line 649 static void _remove(Request& r, MethodPa |
| array.invalidate(); | array.invalidate(); |
| } | } |
| static void _pop(Request& r, MethodParams& params) { | static void _pop(Request& r, MethodParams&) { |
| ArrayValue& array=GET_SELF(r, VArray).array(); | ArrayValue& array=GET_SELF(r, VArray).array(); |
| Value *result=array.pop(); | Value *result=array.pop(); |
| if(result){ | if(result){ |
| Line 691 static void _for(Request& r, MethodParam | Line 683 static void _for(Request& r, MethodParam |
| if(delim_maybe_code){ // delimiter set | if(delim_maybe_code){ // delimiter set |
| bool need_delim=false; | bool need_delim=false; |
| for(ArrayValue::Iterator i(array); i; i.next()){ | for(ArrayValue::RobustIterator i(array); i; i.next()){ |
| if(key_var_name){ | if(key_var_name) |
| VString* vkey=new VString(*new String(i.key(), String::L_TAINTED)); | r.put_element(caller, *key_var_name, VString::uitoa(i.index())); |
| r.put_element(caller, *key_var_name, vkey); | |
| } | |
| if(value_var_name) | if(value_var_name) |
| r.put_element(caller, *value_var_name, i.value() ? i.value() : VVoid::get()); | r.put_element(caller, *value_var_name, i.value() ? i.value() : VVoid::get()); |
| Line 717 static void _for(Request& r, MethodParam | Line 706 static void _for(Request& r, MethodParam |
| break; | break; |
| } | } |
| } else { | } else { |
| for(ArrayValue::Iterator i(array); i; i.next()){ | for(ArrayValue::RobustIterator i(array); i; i.next()){ |
| if(key_var_name){ | if(key_var_name) |
| VString* vkey=new VString(*new String(i.key(), String::L_TAINTED)); | r.put_element(caller, *key_var_name, VString::uitoa(i.index())); |
| r.put_element(caller, *key_var_name, vkey); | |
| } | |
| if(value_var_name) | if(value_var_name) |
| r.put_element(caller, *value_var_name, i.value() ? i.value() : VVoid::get()); | r.put_element(caller, *value_var_name, i.value() ? i.value() : VVoid::get()); |
| Line 750 static void _foreach(Request& r, MethodP | Line 736 static void _foreach(Request& r, MethodP |
| if(delim_maybe_code){ // delimiter set | if(delim_maybe_code){ // delimiter set |
| bool need_delim=false; | bool need_delim=false; |
| for(ArrayValue::Iterator i(array); i; i.next()){ | for(ArrayValue::RobustIterator i(array); i; i.next()){ |
| if(i.value()){ | if(i.value()){ |
| if(key_var_name){ | if(key_var_name) |
| VString* vkey=new VString(*new String(i.key(), String::L_TAINTED)); | r.put_element(caller, *key_var_name, VString::uitoa(i.index())); |
| r.put_element(caller, *key_var_name, vkey); | |
| } | |
| 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()); |
| Line 778 static void _foreach(Request& r, MethodP | Line 761 static void _foreach(Request& r, MethodP |
| } | } |
| } | } |
| } else { | } else { |
| for(ArrayValue::Iterator i(array); i; i.next()){ | for(ArrayValue::RobustIterator i(array); i; i.next()){ |
| if(i.value()){ | if(i.value()){ |
| if(key_var_name){ | if(key_var_name) |
| VString* vkey=new VString(*new String(i.key(), String::L_TAINTED)); | r.put_element(caller, *key_var_name, VString::uitoa(i.index())); |
| r.put_element(caller, *key_var_name, vkey); | |
| } | |
| 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()); |
| Line 842 static void _sort(Request& r, MethodPara | Line 822 static void _sort(Request& r, MethodPara |
| int pos=0; | int pos=0; |
| bool key_values_are_strings=true; | bool key_values_are_strings=true; |
| for(ArrayValue::Iterator i(array); i; i.next() ){ | for(ArrayValue::RobustIterator i(array); i; i.next() ){ |
| if(i.value()){ | if(i.value()){ |
| if(key_var) | if(key_var) |
| r.put_element(*context, *key_var, new VString(*new String(i.key(), String::L_TAINTED))); | r.put_element(*context, *key_var, VString::uitoa(i.index())); |
| if(value_var) | if(value_var) |
| r.put_element(*context, *value_var, i.value()); | r.put_element(*context, *value_var, i.value()); |
| Line 916 static void _at(Request& r, MethodParams | Line 896 static void _at(Request& r, MethodParams |
| if(count == array.count()){ | if(count == array.count()){ |
| l1: switch(result_type) { | l1: switch(result_type) { |
| case AtResultTypeKey: | case AtResultTypeKey: |
| r.write(*new VString(*new String(pa_uitoa(pos), String::L_TAINTED))); | r.write(*VString::uitoa(pos)); |
| break; | break; |
| case AtResultTypeValue: | case AtResultTypeValue: |
| r.write(*array.get(pos)); | r.write(*array.get(pos)); |
| break; | break; |
| case AtResultTypeHash: | case AtResultTypeHash: |
| r.write(SingleElementHash(pa_uitoa(pos), array.get(pos))); | r.write(SingleElementHash(String::Body::uitoa(pos), array.get(pos))); |
| break; | break; |
| } | } |
| } else if((size_t)pos == count-1){ | } else if((size_t)pos == count-1){ |
| Line 933 l1: switch(result_type) { | Line 913 l1: switch(result_type) { |
| if(i.value() && !(pos--)){ | if(i.value() && !(pos--)){ |
| switch(result_type) { | switch(result_type) { |
| case AtResultTypeKey: | case AtResultTypeKey: |
| r.write(*new VString(*new String(i.key(), String::L_TAINTED))); | r.write(*VString::uitoa(i.index())); |
| break; | break; |
| case AtResultTypeValue: | case AtResultTypeValue: |
| r.write(*i.value()); | r.write(*i.value()); |
| break; | break; |
| case AtResultTypeHash: | case AtResultTypeHash: |
| r.write(SingleElementHash(i.key(), i.value())); | r.write(SingleElementHash(String::Body::uitoa(i.index()), i.value())); |
| break; | break; |
| } | } |
| break; | break; |
| Line 1017 static void _select(Request& r, MethodPa | Line 997 static void _select(Request& r, MethodPa |
| for(ArrayValue::ReverseIterator i(source_array); i; ){ | for(ArrayValue::ReverseIterator i(source_array); i; ){ |
| if(Value *value=i.prev()){ // here for correct i.key() | if(Value *value=i.prev()){ // here for correct i.key() |
| if(key_var_name) | if(key_var_name) |
| r.put_element(caller, *key_var_name, new VString(*new String(i.key(), String::L_TAINTED))); | r.put_element(caller, *key_var_name, VString::uitoa(i.index())); |
| if(value_var_name) | if(value_var_name) |
| r.put_element(caller, *value_var_name, value); | r.put_element(caller, *value_var_name, value); |
| Line 1034 static void _select(Request& r, MethodPa | Line 1014 static void _select(Request& r, MethodPa |
| } | } |
| } | } |
| } else { | } else { |
| for(ArrayValue::Iterator i(source_array); i; i.next() ){ | for(ArrayValue::RobustIterator i(source_array); i; i.next() ){ |
| if(Value *value=i.value()){ | if(Value *value=i.value()){ |
| if(key_var_name) | if(key_var_name) |
| r.put_element(caller, *key_var_name, new VString(*new String(i.key(), String::L_TAINTED))); | r.put_element(caller, *key_var_name, VString::uitoa(i.index())); |
| if(value_var_name) | if(value_var_name) |
| r.put_element(caller, *value_var_name, value); | r.put_element(caller, *value_var_name, value); |