2
0

PostgresLibrary.cs 14 KB

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