Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. public class DBConnection : IDisposable
  2. {
  3. private IntPtr _connection;
  4.  
  5. public void connect(string host, string user, string pass, string db)
  6. {
  7. var connectionStr = $"host={host} dbname={db} user={user} password={pass} client_encoding=utf8";
  8. _connection = Pg.PQconnectdb(connectionStr);
  9. if (Pg.PQstatus(_connection) != ConnStatusType.CONNECTION_OK)
  10. {
  11. close();
  12. throw new Exception($"Could not establish connection with the database. ConnectionStr: {connectionStr}");
  13. }
  14. }
  15.  
  16. public void startTransaction()
  17. {
  18. if (_connection != IntPtr.Zero)
  19. {
  20. var result = Pg.PQexec(_connection, "BEGIN");
  21. if (Pg.PQresultStatus(result) != ExecStatusType.PGRES_COMMAND_OK)
  22. {
  23. Pg.PQclear(result);
  24. close();
  25. throw new Exception("starting transaction failed");
  26. }
  27. }
  28. else
  29. {
  30. throw new Exception("requested start transaction but the database connection is not ready");
  31. }
  32. }
  33.  
  34. public void endTransaction()
  35. {
  36. if (_connection != IntPtr.Zero)
  37. {
  38. var result = Pg.PQexec(_connection, "END");
  39. if (Pg.PQresultStatus(result) != ExecStatusType.PGRES_COMMAND_OK)
  40. {
  41. Pg.PQclear(result);
  42. close();
  43. throw new Exception("ending transaction failed");
  44. }
  45. }
  46. else
  47. {
  48. throw new Exception("requested end transaction but the database connection is not ready");
  49. }
  50. }
  51.  
  52. public IntPtr execute(string query)
  53. {
  54. if (_connection != IntPtr.Zero)
  55. {
  56. var result = Pg.PQexec(_connection, query);
  57. if (Pg.PQresultStatus(result) != ExecStatusType.PGRES_COMMAND_OK &&
  58. Pg.PQresultStatus(result) != ExecStatusType.PGRES_TUPLES_OK)
  59. {
  60. var error = Pg.PQresultErrorMessage(result);
  61. Pg.PQclear(result);
  62. close();
  63. throw new Exception($"query execution failed with message: {error}");
  64. }
  65. else
  66. {
  67. return result;
  68. }
  69. }
  70. else
  71. {
  72. throw new Exception("requested query execution but the database connection is not ready");
  73. }
  74. }
  75.  
  76. public void clearResult(IntPtr result)
  77. {
  78. Pg.PQclear(result);
  79. }
  80.  
  81. public float getFloat(IntPtr result, int row, int col)
  82. {
  83. try
  84. {
  85. var valueStr = Marshal.PtrToStringUTF8(Pg.PQgetvalue(result, row, col));
  86. return float.Parse(valueStr);
  87. }
  88. catch (Exception e)
  89. {
  90. throw new Exception($"Error getting float value. Message: {e}");
  91. }
  92. }
  93.  
  94. public byte getByte(IntPtr result, int row, int col)
  95. {
  96. try
  97. {
  98. var valueStr = Marshal.PtrToStringUTF8(Pg.PQgetvalue(result, row, col));
  99. return byte.Parse(valueStr);
  100. }
  101. catch (Exception e)
  102. {
  103. throw new Exception($"Error getting byte value. Message: {e}");
  104. }
  105. }
  106.  
  107. public short getShort(IntPtr result, int row, int col)
  108. {
  109. try
  110. {
  111. var valueStr = Marshal.PtrToStringUTF8(Pg.PQgetvalue(result, row, col));
  112. return short.Parse(valueStr);
  113. }
  114. catch (Exception e)
  115. {
  116. throw new Exception($"Error getting short value. Message: {e}");
  117. }
  118. }
  119.  
  120. public int getInt(IntPtr result, int row, int col)
  121. {
  122. try
  123. {
  124. var valueStr = Marshal.PtrToStringUTF8(Pg.PQgetvalue(result, row, col));
  125. return int.Parse(valueStr);
  126. }
  127. catch (Exception e)
  128. {
  129. throw new Exception($"Error getting int value. Message: {e}");
  130. }
  131. }
  132.  
  133. public long getLong(IntPtr result, int row, int col)
  134. {
  135. try
  136. {
  137. var valueStr = Marshal.PtrToStringUTF8(Pg.PQgetvalue(result, row, col));
  138. return long.Parse(valueStr);
  139. }
  140. catch (Exception e)
  141. {
  142. throw new Exception($"Error getting long value. Message: {e}");
  143. }
  144. }
  145.  
  146. public bool getBool(IntPtr result, int row, int col)
  147. {
  148. try
  149. {
  150. var valueStr = Marshal.PtrToStringUTF8(Pg.PQgetvalue(result, row, col));
  151. return bool.Parse(valueStr);
  152. }
  153. catch (Exception e)
  154. {
  155. throw new Exception($"Error getting int value. Message: {e}");
  156. }
  157. }
  158.  
  159. public string getString(IntPtr result, int row, int col)
  160. {
  161. try
  162. {
  163. var valueStr = Marshal.PtrToStringUTF8(Pg.PQgetvalue(result, row, col));
  164. return valueStr;
  165. }
  166. catch (Exception e)
  167. {
  168. throw new Exception($"Error getting int value. Message: {e}");
  169. }
  170. }
  171.  
  172. public int getRowCount(IntPtr result)
  173. {
  174. try
  175. {
  176. return Pg.PQntuples(result);
  177. }
  178. catch (Exception e)
  179. {
  180. throw new Exception($"Error getting row count. Message: {e}");
  181. }
  182. }
  183.  
  184. public int getColCount(IntPtr result)
  185. {
  186. try
  187. {
  188. return Pg.PQnfields(result);
  189. }
  190. catch (Exception e)
  191. {
  192. throw new Exception($"Error getting col count. Message: {e}");
  193. }
  194. }
  195.  
  196. private void close()
  197. {
  198. if (_connection != IntPtr.Zero)
  199. {
  200. Pg.PQfinish(_connection);
  201. _connection = IntPtr.Zero;
  202. }
  203. }
  204.  
  205. public void Dispose()
  206. {
  207. close();
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement