PostgresLibrary.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. //
  2. // System.Data.SqlClient.PostgresLibrary.cs
  3. //
  4. // PInvoke methods to libpq
  5. // which is PostgreSQL client library
  6. //
  7. // May also contain enumerations,
  8. // data types, or wrapper methods.
  9. //
  10. // Author:
  11. // Rodrigo Moya ([email protected])
  12. // Daniel Morgan ([email protected])
  13. //
  14. // (C) Ximian, Inc 2002
  15. //
  16. using System;
  17. using System.Data;
  18. using System.Runtime.InteropServices;
  19. using System.Diagnostics;
  20. namespace System.Data.SqlClient
  21. {
  22. /* IMPORTANT: DO NOT CHANGE ANY OF THESE ENUMS */
  23. internal enum ConnStatusType
  24. {
  25. CONNECTION_OK,
  26. CONNECTION_BAD,
  27. CONNECTION_STARTED,
  28. CONNECTION_MADE,
  29. CONNECTION_AWAITING_RESPONSE,
  30. CONNECTION_AUTH_OK,
  31. CONNECTION_SETENV
  32. }
  33. internal enum PostgresPollingStatusType
  34. {
  35. PGRES_POLLING_FAILED = 0,
  36. PGRES_POLLING_READING,
  37. PGRES_POLLING_WRITING,
  38. PGRES_POLLING_OK,
  39. PGRES_POLLING_ACTIVE
  40. }
  41. internal enum ExecStatusType
  42. {
  43. PGRES_EMPTY_QUERY = 0,
  44. PGRES_COMMAND_OK,
  45. PGRES_TUPLES_OK,
  46. PGRES_COPY_OUT,
  47. PGRES_COPY_IN,
  48. PGRES_BAD_RESPONSE,
  49. PGRES_NONFATAL_ERROR,
  50. PGRES_FATAL_ERROR
  51. }
  52. sealed internal class PostgresLibrary
  53. {
  54. #region PInvoke Functions
  55. // pinvoke prototypes to PostgreSQL client library
  56. // pq.dll on windows and libpq.so on linux
  57. [DllImport("pq")]
  58. public static extern IntPtr PQconnectStart (string conninfo);
  59. // PGconn *PQconnectStart(const char *conninfo);
  60. [DllImport("pq")]
  61. public static extern PostgresPollingStatusType PQconnectPoll (IntPtr conn);
  62. // PostgresPollingStatusType PQconnectPoll(PGconn *conn);
  63. [DllImport("pq")]
  64. public static extern IntPtr PQconnectdb (string conninfo);
  65. // PGconn *PQconnectdb(const char *conninfo);
  66. [DllImport("pq")]
  67. public static extern IntPtr PQsetdbLogin (string pghost,
  68. string pgport, string pgoptions,
  69. string pgtty, string dbName,
  70. string login, string pwd);
  71. // PGconn *PQsetdbLogin(const char *pghost,
  72. // const char *pgport, const char *pgoptions,
  73. // const char *pgtty, const char *dbName,
  74. // const char *login, const char *pwd);
  75. [DllImport("pq")]
  76. public static extern void PQfinish (IntPtr conn);
  77. // void PQfinish(PGconn *conn);
  78. [DllImport("pq")]
  79. public static extern IntPtr PQconndefaults ();
  80. // PQconninfoOption *PQconndefaults(void);
  81. [DllImport("pq")]
  82. public static extern void PQconninfoFree (IntPtr connOptions);
  83. // void PQconninfoFree(PQconninfoOption *connOptions);
  84. [DllImport("pq")]
  85. public static extern int PQresetStart (IntPtr conn);
  86. // int PQresetStart(PGconn *conn);
  87. [DllImport("pq")]
  88. public static extern IntPtr PQresetPoll (IntPtr conn);
  89. // PostgresPollingStatusType PQresetPoll(PGconn *conn);
  90. [DllImport("pq")]
  91. public static extern void PQreset (IntPtr conn);
  92. // void PQreset(PGconn *conn);
  93. [DllImport("pq")]
  94. public static extern int PQrequestCancel (IntPtr conn);
  95. // int PQrequestCancel(PGconn *conn);
  96. [DllImport("pq")]
  97. public static extern string PQdb (IntPtr conn);
  98. // char *PQdb(const PGconn *conn);
  99. [DllImport("pq")]
  100. public static extern string PQuser (IntPtr conn);
  101. // char *PQuser(const PGconn *conn);
  102. [DllImport("pq")]
  103. public static extern string PQpass (IntPtr conn);
  104. // char *PQpass(const PGconn *conn);
  105. [DllImport("pq")]
  106. public static extern string PQhost (IntPtr conn);
  107. // char *PQhost(const PGconn *conn);
  108. [DllImport("pq")]
  109. public static extern string PQport (IntPtr conn);
  110. // char *PQport(const PGconn *conn);
  111. [DllImport("pq")]
  112. public static extern string PQtty (IntPtr conn);
  113. // char *PQtty(const PGconn *conn);
  114. [DllImport("pq")]
  115. public static extern string PQoptions (IntPtr conn);
  116. // char *PQoptions(const PGconn *conn);
  117. [DllImport("pq")]
  118. public static extern ConnStatusType PQstatus (IntPtr conn);
  119. // ConnStatusType PQstatus(const PGconn *conn);
  120. [DllImport("pq")]
  121. public static extern string PQerrorMessage (IntPtr conn);
  122. // char *PQerrorMessage(const PGconn *conn);
  123. [DllImport("pq")]
  124. public static extern int PQsocket (IntPtr conn);
  125. // int PQsocket(const PGconn *conn);
  126. [DllImport("pq")]
  127. public static extern int PQbackendPID (IntPtr conn);
  128. // int PQbackendPID(const PGconn *conn);
  129. [DllImport("pq")]
  130. public static extern int PQclientEncoding (IntPtr conn);
  131. // int PQclientEncoding(const PGconn *conn);
  132. [DllImport("pq")]
  133. public static extern int PQsetClientEncoding (IntPtr conn,
  134. string encoding);
  135. // int PQsetClientEncoding(PGconn *conn,
  136. // const char *encoding);
  137. //FIXME: when loading, causes runtime exception
  138. //[DllImport("pq")]
  139. //public static extern IntPtr PQgetssl (IntPtr conn);
  140. // SSL *PQgetssl(PGconn *conn);
  141. [DllImport("pq")]
  142. public static extern void PQtrace (IntPtr conn,
  143. IntPtr debug_port);
  144. // void PQtrace(PGconn *conn,
  145. // FILE *debug_port);
  146. [DllImport("pq")]
  147. public static extern void PQuntrace (IntPtr conn);
  148. // void PQuntrace(PGconn *conn);
  149. [DllImport("pq")]
  150. public static extern IntPtr PQsetNoticeProcessor (IntPtr conn,
  151. IntPtr proc, IntPtr arg);
  152. // PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
  153. // PQnoticeProcessor proc, void *arg);
  154. [DllImport("pq")]
  155. public static extern int PQescapeString (string to,
  156. string from, int length);
  157. // size_t PQescapeString(char *to,
  158. // const char *from, size_t length);
  159. [DllImport("pq")]
  160. public static extern string PQescapeBytea (string bintext,
  161. int binlen, IntPtr bytealen);
  162. // unsigned char *PQescapeBytea(unsigned char *bintext,
  163. // size_t binlen, size_t *bytealen);
  164. [DllImport("pq")]
  165. public static extern IntPtr PQexec (IntPtr conn,
  166. string query);
  167. // PGresult *PQexec(PGconn *conn,
  168. // const char *query);
  169. [DllImport("pq")]
  170. public static extern IntPtr PQnotifies (IntPtr conn);
  171. // PGnotify *PQnotifies(PGconn *conn);
  172. [DllImport("pq")]
  173. public static extern void PQfreeNotify (IntPtr notify);
  174. // void PQfreeNotify(PGnotify *notify);
  175. [DllImport("pq")]
  176. public static extern int PQsendQuery (IntPtr conn,
  177. string query);
  178. // int PQsendQuery(PGconn *conn,
  179. // const char *query);
  180. [DllImport("pq")]
  181. public static extern IntPtr PQgetResult (IntPtr conn);
  182. // PGresult *PQgetResult(PGconn *conn);
  183. [DllImport("pq")]
  184. public static extern int PQisBusy (IntPtr conn);
  185. // int PQisBusy(PGconn *conn);
  186. [DllImport("pq")]
  187. public static extern int PQconsumeInput (IntPtr conn);
  188. // int PQconsumeInput(PGconn *conn);
  189. [DllImport("pq")]
  190. public static extern int PQgetline (IntPtr conn,
  191. string str, int length);
  192. // int PQgetline(PGconn *conn,
  193. // char *string, int length);
  194. [DllImport("pq")]
  195. public static extern int PQputline (IntPtr conn,
  196. string str);
  197. // int PQputline(PGconn *conn,
  198. // const char *string);
  199. [DllImport("pq")]
  200. public static extern int PQgetlineAsync (IntPtr conn,
  201. string buffer, int bufsize);
  202. // int PQgetlineAsync(PGconn *conn, char *buffer,
  203. // int bufsize);
  204. [DllImport("pq")]
  205. public static extern int PQputnbytes (IntPtr conn,
  206. string buffer, int nbytes);
  207. // int PQputnbytes(PGconn *conn,
  208. //const char *buffer, int nbytes);
  209. [DllImport("pq")]
  210. public static extern int PQendcopy (IntPtr conn);
  211. // int PQendcopy(PGconn *conn);
  212. [DllImport("pq")]
  213. public static extern int PQsetnonblocking (IntPtr conn,
  214. int arg);
  215. // int PQsetnonblocking(PGconn *conn, int arg);
  216. [DllImport("pq")]
  217. public static extern int PQisnonblocking (IntPtr conn);
  218. // int PQisnonblocking(const PGconn *conn);
  219. [DllImport("pq")]
  220. public static extern int PQflush (IntPtr conn);
  221. // int PQflush(PGconn *conn);
  222. [DllImport("pq")]
  223. public static extern IntPtr PQfn (IntPtr conn, int fnid,
  224. IntPtr result_buf, IntPtr result_len,
  225. int result_is_int, IntPtr args,
  226. int nargs);
  227. // PGresult *PQfn(PGconn *conn, int fnid,
  228. // int *result_buf, int *result_len,
  229. // int result_is_int, const PQArgBlock *args,
  230. // int nargs);
  231. [DllImport("pq")]
  232. public static extern ExecStatusType PQresultStatus (IntPtr res);
  233. // ExecStatusType PQresultStatus(const PGresult *res);
  234. [DllImport("pq")]
  235. public static extern string PQresStatus (ExecStatusType status);
  236. // char *PQresStatus(ExecStatusType status);
  237. [DllImport("pq")]
  238. public static extern string PQresultErrorMessage (IntPtr res);
  239. // char *PQresultErrorMessage(const PGresult *res);
  240. [DllImport("pq")]
  241. public static extern int PQntuples (IntPtr res);
  242. // int PQntuples(const PGresult *res);
  243. [DllImport("pq")]
  244. public static extern int PQnfields (IntPtr res);
  245. // int PQnfields(const PGresult *res);
  246. [DllImport("pq")]
  247. public static extern int PQbinaryTuples (IntPtr res);
  248. // int PQbinaryTuples(const PGresult *res);
  249. [DllImport("pq")]
  250. public static extern string PQfname (IntPtr res,
  251. int field_num);
  252. // char *PQfname(const PGresult *res,
  253. // int field_num);
  254. [DllImport("pq")]
  255. public static extern int PQfnumber (IntPtr res,
  256. string field_name);
  257. // int PQfnumber(const PGresult *res,
  258. // const char *field_name);
  259. [DllImport("pq")]
  260. public static extern int PQftype (IntPtr res,
  261. int field_num);
  262. // Oid PQftype(const PGresult *res,
  263. // int field_num);
  264. [DllImport("pq")]
  265. public static extern int PQfsize (IntPtr res,
  266. int field_num);
  267. // int PQfsize(const PGresult *res,
  268. // int field_num);
  269. [DllImport("pq")]
  270. public static extern int PQfmod (IntPtr res, int field_num);
  271. // int PQfmod(const PGresult *res, int field_num);
  272. [DllImport("pq")]
  273. public static extern string PQcmdStatus (IntPtr res);
  274. // char *PQcmdStatus(PGresult *res);
  275. [DllImport("pq")]
  276. public static extern string PQoidStatus (IntPtr res);
  277. // char *PQoidStatus(const PGresult *res);
  278. [DllImport("pq")]
  279. public static extern int PQoidValue (IntPtr res);
  280. // Oid PQoidValue(const PGresult *res);
  281. [DllImport("pq")]
  282. public static extern string PQcmdTuples (IntPtr res);
  283. // char *PQcmdTuples(PGresult *res);
  284. [DllImport("pq")]
  285. public static extern string PQgetvalue (IntPtr res,
  286. int tup_num, int field_num);
  287. // char *PQgetvalue(const PGresult *res,
  288. // int tup_num, int field_num);
  289. [DllImport("pq")]
  290. public static extern int PQgetlength (IntPtr res,
  291. int tup_num, int field_num);
  292. // int PQgetlength(const PGresult *res,
  293. // int tup_num, int field_num);
  294. [DllImport("pq")]
  295. public static extern int PQgetisnull (IntPtr res,
  296. int tup_num, int field_num);
  297. // int PQgetisnull(const PGresult *res,
  298. // int tup_num, int field_num);
  299. [DllImport("pq")]
  300. public static extern void PQclear (IntPtr res);
  301. // void PQclear(PGresult *res);
  302. [DllImport("pq")]
  303. public static extern IntPtr PQmakeEmptyPGresult (IntPtr conn,
  304. IntPtr status);
  305. // PGresult *PQmakeEmptyPGresult(PGconn *conn,
  306. // ExecStatusType status);
  307. [DllImport("pq")]
  308. public static extern void PQprint (IntPtr fout,
  309. IntPtr res, IntPtr ps);
  310. // void PQprint(FILE *fout,
  311. // const PGresult *res, const PQprintOpt *ps);
  312. [DllImport("pq")]
  313. public static extern void PQdisplayTuples (IntPtr res,
  314. IntPtr fp, int fillAlign, string fieldSep,
  315. int printHeader, int quiet);
  316. // void PQdisplayTuples(const PGresult *res,
  317. // FILE *fp, int fillAlign, const char *fieldSep,
  318. // int printHeader, int quiet);
  319. [DllImport("pq")]
  320. public static extern void PQprintTuples (IntPtr res,
  321. IntPtr fout, int printAttName, int terseOutput,
  322. int width);
  323. // void PQprintTuples(const PGresult *res,
  324. // FILE *fout, int printAttName, int terseOutput,
  325. // int width);
  326. [DllImport("pq")]
  327. public static extern int lo_open (IntPtr conn,
  328. int lobjId, int mode);
  329. // int lo_open(PGconn *conn,
  330. // Oid lobjId, int mode);
  331. [DllImport("pq")]
  332. public static extern int lo_close (IntPtr conn, int fd);
  333. // int lo_close(PGconn *conn, int fd);
  334. [DllImport("pq")]
  335. public static extern int lo_read (IntPtr conn,
  336. int fd, string buf, int len);
  337. // int lo_read(PGconn *conn,
  338. // int fd, char *buf, size_t len);
  339. [DllImport("pq")]
  340. public static extern int lo_write (IntPtr conn,
  341. int fd, string buf, int len);
  342. // int lo_write(PGconn *conn,
  343. // int fd, char *buf, size_t len);
  344. [DllImport("pq")]
  345. public static extern int lo_lseek (IntPtr conn,
  346. int fd, int offset, int whence);
  347. // int lo_lseek(PGconn *conn,
  348. // int fd, int offset, int whence);
  349. [DllImport("pq")]
  350. public static extern int lo_creat (IntPtr conn,
  351. int mode);
  352. // Oid lo_creat(PGconn *conn,
  353. // int mode);
  354. [DllImport("pq")]
  355. public static extern int lo_tell (IntPtr conn, int fd);
  356. // int lo_tell(PGconn *conn, int fd);
  357. [DllImport("pq")]
  358. public static extern int lo_unlink (IntPtr conn,
  359. int lobjId);
  360. // int lo_unlink(PGconn *conn,
  361. // Oid lobjId);
  362. [DllImport("pq")]
  363. public static extern int lo_import (IntPtr conn,
  364. string filename);
  365. // Oid lo_import(PGconn *conn,
  366. // const char *filename);
  367. [DllImport("pq")]
  368. public static extern int lo_export (IntPtr conn,
  369. int lobjId, string filename);
  370. // int lo_export(PGconn *conn,
  371. // Oid lobjId, const char *filename);
  372. [DllImport("pq")]
  373. public static extern int PQmblen (string s,
  374. int encoding);
  375. // int PQmblen(const unsigned char *s,
  376. // int encoding);
  377. [DllImport("pq")]
  378. public static extern int PQenv2encoding ();
  379. // int PQenv2encoding(void);
  380. #endregion
  381. }
  382. }