DBGuids.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using OpenMetaverse;
  5. namespace OpenSim.Data
  6. {
  7. public static class DBGuid
  8. {
  9. /// <summary>This function converts a value returned from the database in one of the
  10. /// supported formats into a UUID. This function is not actually DBMS-specific right
  11. /// now
  12. ///
  13. /// </summary>
  14. /// <param name="id"></param>
  15. /// <returns></returns>
  16. public static UUID FromDB(object id)
  17. {
  18. if( (id == null) || (id == DBNull.Value))
  19. return UUID.Zero;
  20. if (id.GetType() == typeof(Guid))
  21. return new UUID((Guid)id);
  22. if (id.GetType() == typeof(byte[]))
  23. {
  24. if (((byte[])id).Length == 0)
  25. return UUID.Zero;
  26. else if (((byte[])id).Length == 16)
  27. return new UUID((byte[])id, 0);
  28. }
  29. else if (id.GetType() == typeof(string))
  30. {
  31. if (((string)id).Length == 0)
  32. return UUID.Zero;
  33. else if (((string)id).Length == 36)
  34. return new UUID((string)id);
  35. }
  36. throw new Exception("Failed to convert db value to UUID: " + id.ToString());
  37. }
  38. }
  39. }