----------------------------------------------------------------------------- * array_prepend : * push an element onto the front of a one-dimensional array *---------------------------------------------------------------------------- */
| 172 | *---------------------------------------------------------------------------- |
| 173 | */ |
| 174 | Datum |
| 175 | array_prepend(PG_FUNCTION_ARGS) |
| 176 | { |
| 177 | ExpandedArrayHeader *eah; |
| 178 | Datum newelem; |
| 179 | bool isNull; |
| 180 | Datum result; |
| 181 | int *lb; |
| 182 | int indx; |
| 183 | int lb0; |
| 184 | ArrayMetaState *my_extra; |
| 185 | |
| 186 | isNull = PG_ARGISNULL(0); |
| 187 | if (isNull) |
| 188 | newelem = (Datum) 0; |
| 189 | else |
| 190 | newelem = PG_GETARG_DATUM(0); |
| 191 | eah = fetch_array_arg_replace_nulls(fcinfo, 1); |
| 192 | |
| 193 | if (eah->ndims == 1) |
| 194 | { |
| 195 | /* prepend newelem */ |
| 196 | lb = eah->lbound; |
| 197 | lb0 = lb[0]; |
| 198 | |
| 199 | if (pg_sub_s32_overflow(lb0, 1, &indx)) |
| 200 | ereport(ERROR, |
| 201 | (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), |
| 202 | errmsg("integer out of range"))); |
| 203 | } |
| 204 | else if (eah->ndims == 0) |
| 205 | { |
| 206 | indx = 1; |
| 207 | lb0 = 1; |
| 208 | } |
| 209 | else |
| 210 | ereport(ERROR, |
| 211 | (errcode(ERRCODE_DATA_EXCEPTION), |
| 212 | errmsg("argument must be empty or one-dimensional array"))); |
| 213 | |
| 214 | /* Perform element insertion */ |
| 215 | my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; |
| 216 | |
| 217 | result = array_set_element(EOHPGetRWDatum(&eah->hdr), |
| 218 | 1, &indx, newelem, isNull, |
| 219 | -1, my_extra->typlen, my_extra->typbyval, my_extra->typalign); |
| 220 | |
| 221 | /* Readjust result's LB to match the input's, as expected for prepend */ |
| 222 | Assert(result == EOHPGetRWDatum(&eah->hdr)); |
| 223 | if (eah->ndims == 1) |
| 224 | { |
| 225 | /* This is ok whether we've deconstructed or not */ |
| 226 | eah->lbound[0] = lb0; |
| 227 | } |
| 228 | |
| 229 | PG_RETURN_DATUM(result); |
| 230 | } |
| 231 |
nothing calls this directly
no test coverage detected