--- parser3/src/classes/array.C 2024/09/28 21:28:57 1.12 +++ parser3/src/classes/array.C 2024/10/20 13:20:57 1.19 @@ -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.19 2024/10/20 13:20:57 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,23 +492,25 @@ 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 + ArrayValue& array=self.array(); + + array.clear(); self.invalidate(); // just in case if called as method if(sparse){ - SparseArray_sql_event_handlers handlers(distinct, self.array(), value_type); + SparseArray_sql_event_handlers handlers(distinct, array, value_type); r.connection()->query(statement_cstr, placeholders_count, placeholders, offset, limit, handlers, statement_string); } else { - Array_sql_event_handlers handlers(self.array(), value_type); + Array_sql_event_handlers handlers(array, value_type); r.connection()->query(statement_cstr, placeholders_count, placeholders, offset, limit, handlers, statement_string); } + array.confirm_all_used(); + if(bind) unmarshal_bind_updates(*bind, placeholders_count, placeholders); } - static void mid(Request& r, size_t offset=0, size_t limit=ARRAY_OPTION_LIMIT_ALL) { ArrayValue& array=GET_SELF(r, VArray).array(); if(limit>0){ @@ -514,6 +527,7 @@ static void mid(Request& r, size_t offse result_array+=i.value(); } } + result_array.confirm_all_used(); r.write(*result); } else { r.write(*new VArray); @@ -541,15 +555,11 @@ static void _right(Request& r, MethodPar } static void _mid(Request& r, MethodParams& params) { - const String& string=GET_SELF(r, VString).string(); - int begin=params.as_int(0, "p must be int", r); + if(begin<0) throw Exception(PARSER_RUNTIME, 0, "p(%d) must be >=0", begin); - size_t end; - size_t length=0; - if(params.count()>1) { int n=params.as_int(1, "n must be int", r); if(n<0) @@ -846,6 +856,7 @@ static void _sort(Request& r, MethodPara for(pos=0; pos