You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.1 KiB

2 years ago
  1. using System;
  2. namespace SqlSugar
  3. {
  4. public class SqliteInsertBuilder : InsertBuilder
  5. {
  6. public override string SqlTemplate
  7. {
  8. get
  9. {
  10. if (IsReturnIdentity)
  11. {
  12. return @"INSERT INTO {0}
  13. ({1})
  14. VALUES
  15. ({2}) ;SELECT LAST_INSERT_ROWID();";
  16. }
  17. else
  18. {
  19. return @"INSERT INTO {0}
  20. ({1})
  21. VALUES
  22. ({2}) ;";
  23. }
  24. }
  25. }
  26. public override string SqlTemplateBatch
  27. {
  28. get
  29. {
  30. return "INSERT INTO {0} ({1})";
  31. }
  32. }
  33. public override object FormatValue(object value)
  34. {
  35. if (value == null)
  36. {
  37. return "NULL";
  38. }
  39. else
  40. {
  41. var type = value.GetType();
  42. if (type == UtilConstants.DateType)
  43. {
  44. var date = value.ObjToDate();
  45. if (date < Convert.ToDateTime("1900-1-1"))
  46. {
  47. date = Convert.ToDateTime("1900-1-1");
  48. }
  49. return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
  50. }
  51. else if (type.IsEnum())
  52. {
  53. return Convert.ToInt64(value);
  54. }
  55. else if (type == UtilConstants.ByteArrayType)
  56. {
  57. var bytesString = "0x" + BitConverter.ToString((byte[])value);
  58. return bytesString;
  59. }
  60. else if (type == UtilConstants.BoolType)
  61. {
  62. return value.ObjToBool() ? "1" : "0";
  63. }
  64. else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
  65. {
  66. return "'" + value.ToString().ToSqlFilter() + "'";
  67. }
  68. else
  69. {
  70. return "'"+value.ToString() + "'";
  71. }
  72. }
  73. }
  74. }
  75. }