DIPSAS / Dapper.Oracle

Oracle support for Dapper Micro ORM.
MIT License
113 stars 43 forks source link

GUID issues #69

Open martinRocks opened 5 months ago

martinRocks commented 5 months ago

Hi, I love your library. Would it be possible to add a mapping for GUIDs? We store GUIDs in an oracle RAW 16. Why oracle doesn't have a specific field type for GUIDs, I don't understand. However, I need to convert the byte array to GUID. And in my case, I have to do Endian flip. I don't know if others have to do this Endian flip.

Below is an example of what I am doing. The AWN_SA_ID is the column name that is mapped by dapper. AwnSaId is the guid that I actually use.

public class Dto
        {
            public byte[] AWN_SA_ID
            {
                set => AwnSaId = FlipEndian(new Guid(value));
            }
            public Guid AwnSaId { get; set; }
            public string Jcn { get; set; }
            public string Hsc { get; set; }
        }

        private static Guid FlipEndian(Guid guid)
        {
            var newBytes = new byte[16];
            var oldBytes = guid.ToByteArray();
            for (var i = 8; i < 16; i++)
            {
                newBytes[i] = oldBytes[i];
            }

            newBytes[3] = oldBytes[0];
            newBytes[2] = oldBytes[1];
            newBytes[1] = oldBytes[2];
            newBytes[0] = oldBytes[3];
            newBytes[5] = oldBytes[4];
            newBytes[4] = oldBytes[5];
            newBytes[6] = oldBytes[7];
            newBytes[7] = oldBytes[6];
            return new Guid(newBytes);
        }

public void DapperStoredProcedure()
        {
            List<Dto> data;
            var connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            using (var connection = new OracleConnection(connStr))
            {
                var values = new OracleDynamicParameters();
                values.Add("i_uic", Uic, OracleMappingType.Varchar2, ParameterDirection.Input);
                values.Add("o_cur", dbType: OracleMappingType.RefCursor, direction: ParameterDirection.Output);

                data = connection.Query<Dto>("jcn_exports.get_work_notifications", values, commandType: CommandType.StoredProcedure).ToList();
            }
            Console.WriteLine($"The count is {data.Count}");
            WriteGuid(data);
        }
gwinnem commented 1 month ago

Oracle does not support GUID's out of the box. Read this: https://stackoverflow.com/questions/153815/how-should-i-store-a-guid-in-oracle