schotime / NPoco

Simple microORM that maps the results of a query onto a POCO object. Project based on Schotime's branch of PetaPoco
Apache License 2.0
848 stars 302 forks source link

IMapper.GetParameterConverter sourceType parameter Is a different type between Insert and Update #701

Open vllama opened 5 months ago

vllama commented 5 months ago

When updating a table with an array column, malformed sql is being created because the sourceType is passed in as the Array Element Type instead of the Array Type, but only when updating. When Inserting the proper type is being passed in in sourceType

public override Func<object, object> GetParameterConverter(DbCommand command, Type sourceType) {
if (sourceType == typeof(int[])) {  //<-- this is int when updating  should be int[] like when inserting.
                return x => new NpgsqlParameter {
                    NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Integer,
                    Value = (int[])x
                };
}

See parameters 20 and 21 in the update. Sql is not valid. Causes: Npgsql.PostgresException (0x80004005): 42601: syntax error at or near "$22"

UPDATE TBL SET "help" = @p0,...  "status_values" = @p20,@p21, "id_event" = @p22, "deleted" = @p23, "sys_created_by" = @p24, "sys_created_on" = @p25, "sys_modified_by" = @p26, "sys_modified_on" = @p27 WHERE "id" = @p28
...
         -> @p20 [Int32] = "2"
     -> @p21 [Int32] = "3"

INSERT INTO TBL ("help", ... "status_values","id_event","deleted","sys_created_by","sys_created_on","sys_modified_by","sys_modified_on") VALUES (@p0... @p20,@p21,@p22,@p23,@p24,@p25,@p26) returning "id" as NewID
...
     -> @p20 [Int32[]] = "System.Int32[]"
vllama commented 5 months ago

Version 5.7.1