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.

117 lines
4.3 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SQLite;
  5. namespace SqlSugar
  6. {
  7. public partial class SqliteProvider : AdoProvider
  8. {
  9. public SqliteProvider() { }
  10. public override IDbConnection Connection
  11. {
  12. get
  13. {
  14. if (base._DbConnection == null)
  15. {
  16. try
  17. {
  18. var SQLiteConnectionString = base.Context.CurrentConnectionConfig.ConnectionString;
  19. base._DbConnection = new SQLiteConnection(SQLiteConnectionString);
  20. }
  21. catch (Exception ex)
  22. {
  23. Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message);
  24. }
  25. }
  26. return base._DbConnection;
  27. }
  28. set
  29. {
  30. base._DbConnection = value;
  31. }
  32. }
  33. public override void BeginTran(string transactionName)
  34. {
  35. base.BeginTran();
  36. }
  37. /// <summary>
  38. /// Only SqlServer
  39. /// </summary>
  40. /// <param name="iso"></param>
  41. /// <param name="transactionName"></param>
  42. public override void BeginTran(IsolationLevel iso, string transactionName)
  43. {
  44. base.BeginTran(iso);
  45. }
  46. public override IDataAdapter GetAdapter()
  47. {
  48. return new SQLiteDataAdapter();
  49. }
  50. public override IDbCommand GetCommand(string sql, SugarParameter[] parameters)
  51. {
  52. var sqlCommand = new SQLiteCommand(sql, (SQLiteConnection)this.Connection)
  53. {
  54. CommandType = this.CommandType,
  55. CommandTimeout = this.CommandTimeOut
  56. };
  57. if (this.Transaction != null)
  58. {
  59. sqlCommand.Transaction = (SQLiteTransaction)this.Transaction;
  60. }
  61. if (parameters.HasValue())
  62. {
  63. var ipars = ToIDbDataParameter(parameters);
  64. sqlCommand.Parameters.AddRange((SQLiteParameter[])ipars);
  65. }
  66. CheckConnection();
  67. return sqlCommand;
  68. }
  69. public override void SetCommandToAdapter(IDataAdapter dataAdapter, IDbCommand command)
  70. {
  71. ((SQLiteDataAdapter)dataAdapter).SelectCommand = (SQLiteCommand)command;
  72. }
  73. /// <summary>
  74. /// if SQLite return SQLiteParameter[] pars
  75. /// if sqlerver return SqlParameter[] pars ...
  76. /// </summary>
  77. /// <param name="parameters"></param>
  78. /// <returns></returns>
  79. public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters)
  80. {
  81. if (parameters == null || parameters.Length == 0) return null;
  82. var result = new SQLiteParameter[parameters.Length];
  83. var index = 0;
  84. foreach (var parameter in parameters)
  85. {
  86. if (parameter.Value == null) parameter.Value = DBNull.Value;
  87. if (parameter.Value.GetType() == UtilConstants.GuidType)
  88. {
  89. parameter.Value = parameter.Value.ToString();
  90. }
  91. var sqlParameter = new SQLiteParameter
  92. {
  93. ParameterName = parameter.ParameterName,
  94. Size = parameter.Size,
  95. Value = parameter.Value,
  96. DbType = parameter.DbType
  97. };
  98. result[index] = sqlParameter;
  99. if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput))
  100. {
  101. if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
  102. this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
  103. this.OutputParameters.Add(sqlParameter);
  104. }
  105. if (sqlParameter.DbType == System.Data.DbType.Guid)
  106. {
  107. sqlParameter.DbType = System.Data.DbType.String;
  108. sqlParameter.Value = sqlParameter.Value.ObjToString();
  109. }
  110. ++index;
  111. }
  112. return result;
  113. }
  114. }
  115. }