--- parser3/src/classes/array.C 2024/09/28 21:28:57 1.12 +++ parser3/src/classes/array.C 2024/10/02 02:40:26 1.15 @@ -17,7 +17,7 @@ #include "pa_vbool.h" #include "pa_vmethod_frame.h" -volatile const char * IDENT_ARRAY_C="$Id: array.C,v 1.12 2024/09/28 21:28:57 moko Exp $"; +volatile const char * IDENT_ARRAY_C="$Id: array.C,v 1.15 2024/10/02 02:40:26 moko Exp $"; // class @@ -46,10 +46,17 @@ static void _create_or_add(Request& r, M if(VArray* src=dynamic_cast(&vsrc)) { if(src==&self) - throw Exception(PARSER_RUNTIME, 0, "source and destination are the same array"); - self_array.append(src->array()); + return; + if(self_array.count()){ + for(ArrayValue::Iterator i(src->array()); i; i.next()){ + if(i.value()) + self_array.put(i.index(), i.value()); + } + } else { + self_array.append(src->array()); + } } else { - HashStringValue* src_hash=vsrc.get_hash(); + HashStringValue* src_hash=vsrc.as_hash("param must be array or"); if(!src_hash) return; for(HashStringValue::Iterator i(*src_hash); i; i.next()){ @@ -119,7 +126,7 @@ static void _join(Request& r, MethodPara } } } else { - HashStringValue* src_hash=vsrc.get_hash(); + HashStringValue* src_hash=vsrc.as_hash("param must be array or"); if(!src_hash) return; if(o.defined){ @@ -445,6 +452,7 @@ static void _sql(Request& r, MethodParam if(params.count()>1) if(HashStringValue* options=params.as_hash(1, "sql options")) { int valid_options=0; + bool distinct_specified=false; for(HashStringValue::Iterator i(*options); i; i.next() ){ String::Body key=i.key(); Value* value=i.value(); @@ -459,6 +467,7 @@ static void _sql(Request& r, MethodParam valid_options++; } else if (key == sql_distinct_name) { distinct=r.process(*value).as_bool(); + distinct_specified=true; valid_options++; } else if (key == sql_value_type_name) { value_type=get_value_type(r.process(*value)); @@ -470,6 +479,8 @@ static void _sql(Request& r, MethodParam } if(valid_options!=options->count()) throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + if(distinct_specified && !sparse) + throw Exception(PARSER_RUNTIME, 0, "'distinct' option can only be used when $.sparse(true) is specified"); } SQL_Driver::Placeholder* placeholders=0; @@ -481,7 +492,7 @@ static void _sql(Request& r, MethodParam const char* statement_cstr=statement_string.untaint_cstr(String::L_SQL, r.connection()); VArray& self=GET_SELF(r, VArray); - + self.array().clear(); self.invalidate(); // just in case if called as method if(sparse){ @@ -1088,7 +1099,7 @@ MArray::MArray(): Methoded(VARRAY_TYPE) #ifdef FEATURE_GET_ELEMENT4CALL // aliases without "_" add_native_method("keys", Method::CT_DYNAMIC, _keys, 0, 1); - add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0); + add_native_method("count", Method::CT_DYNAMIC, _count, 0, 1); add_native_method("at", Method::CT_DYNAMIC, _at, 1, 2); #endif