PostgresLibrary.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. // PostgreSQL Type (oid and typname from pg_type)
  22. internal enum PgType {
  23. ABSTIME = 702,
  24. ACLITEM = 1033,
  25. BIT = 1560,
  26. BOOL = 16,
  27. BOX = 603,
  28. BPCHAR = 1042,
  29. BYTEA = 17,
  30. CHAR = 18,
  31. CIDR = 650,
  32. CIRCLE = 718,
  33. DATE = 1082,
  34. FLOAT4 = 700,
  35. FLOAT8 = 701,
  36. INET = 869,
  37. INT2 = 21,
  38. INT4 = 23,
  39. INT8 = 20,
  40. INTERVAL = 1186,
  41. LINE = 628,
  42. LSEG = 601,
  43. MACADDR = 829,
  44. MONEY = 790,
  45. NAME = 19,
  46. NUMERIC = 1700,
  47. OID = 26,
  48. PATH = 602,
  49. POINT = 600,
  50. POLYGON = 604,
  51. REFCURSOR = 1790,
  52. RELTIME = 703,
  53. TEXT = 25,
  54. TIME = 1083,
  55. TIMESTAMP = 1114,
  56. TIMESTAMPTZ = 1184,
  57. TIMETZ = 1266,
  58. TINTERVAL = 704,
  59. VARBIT = 1562,
  60. VARCHAR = 1043
  61. }
  62. /* IMPORTANT: DO NOT CHANGE ANY OF THESE ENUMS */
  63. internal enum ConnStatusType
  64. {
  65. CONNECTION_OK,
  66. CONNECTION_BAD,
  67. CONNECTION_STARTED,
  68. CONNECTION_MADE,
  69. CONNECTION_AWAITING_RESPONSE,
  70. CONNECTION_AUTH_OK,
  71. CONNECTION_SETENV
  72. }
  73. internal enum PostgresPollingStatusType
  74. {
  75. PGRES_POLLING_FAILED = 0,
  76. PGRES_POLLING_READING,
  77. PGRES_POLLING_WRITING,
  78. PGRES_POLLING_OK,
  79. PGRES_POLLING_ACTIVE
  80. }
  81. internal enum ExecStatusType
  82. {
  83. PGRES_EMPTY_QUERY = 0,
  84. PGRES_COMMAND_OK,
  85. PGRES_TUPLES_OK,
  86. PGRES_COPY_OUT,
  87. PGRES_COPY_IN,
  88. PGRES_BAD_RESPONSE,
  89. PGRES_NONFATAL_ERROR,
  90. PGRES_FATAL_ERROR
  91. }
  92. sealed internal class PostgresHelper {
  93. /// <summary>
  94. /// Convert a PostgreSQL Type to a .NET System type.
  95. /// </summary>
  96. /// <param name="oid"></param>
  97. /// <param name="value"></param>
  98. /// <returns></returns>
  99. public static object ConvertPgTypeToSystem (int oid, String value) {
  100. object obj = null;
  101. // FIXME: more types need
  102. // to be converted
  103. // from PostgreSQL oid type
  104. // to .NET System.<type>
  105. switch((PgType) oid) {
  106. case PgType.VARCHAR:
  107. case PgType.BPCHAR:
  108. case PgType.TEXT:
  109. case PgType.CHAR:
  110. obj = (object) String.Copy(value);
  111. break;
  112. case PgType.BOOL:
  113. obj = (object) Boolean.Parse(value);
  114. break;
  115. case PgType.INT2:
  116. obj = (object) Int16.Parse(value);
  117. break;
  118. case PgType.INT4:
  119. obj = (object) Int32.Parse(value);
  120. break;
  121. case PgType.INT8:
  122. obj = (object) Int64.Parse(value);
  123. break;
  124. default:
  125. throw new NotImplementedException(
  126. "PGNI1: PostgreSQL oid data type " + oid +
  127. " not mapped to .NET System data type.");
  128. }
  129. return obj;
  130. }
  131. /// <summary>
  132. /// Convert the PostgreSQL Type oid to the .NET System.Type.
  133. /// </summary>
  134. /// <param name="oid"></param>
  135. /// <returns></returns>
  136. public static Type OidToType (int oid) {
  137. // FIXME: more types need
  138. // to be mapped
  139. // from PostgreSQL oid type
  140. // to .NET System.<type>
  141. Type typ = null;
  142. switch((PgType) oid) {
  143. case PgType.VARCHAR:
  144. case PgType.BPCHAR:
  145. case PgType.TEXT:
  146. case PgType.CHAR:
  147. typ = typeof(String);
  148. break;
  149. case PgType.BOOL:
  150. typ = typeof(Boolean);
  151. break;
  152. case PgType.INT2:
  153. typ = typeof(Int16);
  154. break;
  155. case PgType.INT4:
  156. typ = typeof(Int32);
  157. break;
  158. case PgType.INT8:
  159. typ = typeof(Int64);
  160. break;
  161. default:
  162. throw new NotImplementedException(
  163. "PGNI2: PostgreSQL oid type " + oid +
  164. " not mapped to .NET System Type.");
  165. }
  166. return typ;
  167. }
  168. }
  169. sealed internal class PostgresLibrary
  170. {
  171. #region PInvoke Functions
  172. // pinvoke prototypes to PostgreSQL client library
  173. // pq.dll on windows and libpq.so on linux
  174. [DllImport("pq")]
  175. public static extern IntPtr PQconnectStart (string conninfo);
  176. // PGconn *PQconnectStart(const char *conninfo);
  177. [DllImport("pq")]
  178. public static extern PostgresPollingStatusType PQconnectPoll (IntPtr conn);
  179. // PostgresPollingStatusType PQconnectPoll(PGconn *conn);
  180. [DllImport("pq")]
  181. public static extern IntPtr PQconnectdb (string conninfo);
  182. // PGconn *PQconnectdb(const char *conninfo);
  183. [DllImport("pq")]
  184. public static extern IntPtr PQsetdbLogin (string pghost,
  185. string pgport, string pgoptions,
  186. string pgtty, string dbName,
  187. string login, string pwd);
  188. // PGconn *PQsetdbLogin(const char *pghost,
  189. // const char *pgport, const char *pgoptions,
  190. // const char *pgtty, const char *dbName,
  191. // const char *login, const char *pwd);
  192. [DllImport("pq")]
  193. public static extern void PQfinish (IntPtr conn);
  194. // void PQfinish(PGconn *conn);
  195. [DllImport("pq")]
  196. public static extern IntPtr PQconndefaults ();
  197. // PQconninfoOption *PQconndefaults(void);
  198. [DllImport("pq")]
  199. public static extern void PQconninfoFree (IntPtr connOptions);
  200. // void PQconninfoFree(PQconninfoOption *connOptions);
  201. [DllImport("pq")]
  202. public static extern int PQresetStart (IntPtr conn);
  203. // int PQresetStart(PGconn *conn);
  204. [DllImport("pq")]
  205. public static extern IntPtr PQresetPoll (IntPtr conn);
  206. // PostgresPollingStatusType PQresetPoll(PGconn *conn);
  207. [DllImport("pq")]
  208. public static extern void PQreset (IntPtr conn);
  209. // void PQreset(PGconn *conn);
  210. [DllImport("pq")]
  211. public static extern int PQrequestCancel (IntPtr conn);
  212. // int PQrequestCancel(PGconn *conn);
  213. [DllImport("pq")]
  214. public static extern string PQdb (IntPtr conn);
  215. // char *PQdb(const PGconn *conn);
  216. [DllImport("pq")]
  217. public static extern string PQuser (IntPtr conn);
  218. // char *PQuser(const PGconn *conn);
  219. [DllImport("pq")]
  220. public static extern string PQpass (IntPtr conn);
  221. // char *PQpass(const PGconn *conn);
  222. [DllImport("pq")]
  223. public static extern string PQhost (IntPtr conn);
  224. // char *PQhost(const PGconn *conn);
  225. [DllImport("pq")]
  226. public static extern string PQport (IntPtr conn);
  227. // char *PQport(const PGconn *conn);
  228. [DllImport("pq")]
  229. public static extern string PQtty (IntPtr conn);
  230. // char *PQtty(const PGconn *conn);
  231. [DllImport("pq")]
  232. public static extern string PQoptions (IntPtr conn);
  233. // char *PQoptions(const PGconn *conn);
  234. [DllImport("pq")]
  235. public static extern ConnStatusType PQstatus (IntPtr conn);
  236. // ConnStatusType PQstatus(const PGconn *conn);
  237. [DllImport("pq")]
  238. public static extern string PQerrorMessage (IntPtr conn);
  239. // char *PQerrorMessage(const PGconn *conn);
  240. [DllImport("pq")]
  241. public static extern int PQsocket (IntPtr conn);
  242. // int PQsocket(const PGconn *conn);
  243. [DllImport("pq")]
  244. public static extern int PQbackendPID (IntPtr conn);
  245. // int PQbackendPID(const PGconn *conn);
  246. [DllImport("pq")]
  247. public static extern int PQclientEncoding (IntPtr conn);
  248. // int PQclientEncoding(const PGconn *conn);
  249. [DllImport("pq")]
  250. public static extern int PQsetClientEncoding (IntPtr conn,
  251. string encoding);
  252. // int PQsetClientEncoding(PGconn *conn,
  253. // const char *encoding);
  254. //FIXME: when loading, causes runtime exception
  255. //[DllImport("pq")]
  256. //public static extern IntPtr PQgetssl (IntPtr conn);
  257. // SSL *PQgetssl(PGconn *conn);
  258. [DllImport("pq")]
  259. public static extern void PQtrace (IntPtr conn,
  260. IntPtr debug_port);
  261. // void PQtrace(PGconn *conn,
  262. // FILE *debug_port);
  263. [DllImport("pq")]
  264. public static extern void PQuntrace (IntPtr conn);
  265. // void PQuntrace(PGconn *conn);
  266. [DllImport("pq")]
  267. public static extern IntPtr PQsetNoticeProcessor (IntPtr conn,
  268. IntPtr proc, IntPtr arg);
  269. // PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
  270. // PQnoticeProcessor proc, void *arg);
  271. [DllImport("pq")]
  272. public static extern int PQescapeString (string to,
  273. string from, int length);
  274. // size_t PQescapeString(char *to,
  275. // const char *from, size_t length);
  276. [DllImport("pq")]
  277. public static extern string PQescapeBytea (string bintext,
  278. int binlen, IntPtr bytealen);
  279. // unsigned char *PQescapeBytea(unsigned char *bintext,
  280. // size_t binlen, size_t *bytealen);
  281. [DllImport("pq")]
  282. public static extern IntPtr PQexec (IntPtr conn,
  283. string query);
  284. // PGresult *PQexec(PGconn *conn,
  285. // const char *query);
  286. [DllImport("pq")]
  287. public static extern IntPtr PQnotifies (IntPtr conn);
  288. // PGnotify *PQnotifies(PGconn *conn);
  289. [DllImport("pq")]
  290. public static extern void PQfreeNotify (IntPtr notify);
  291. // void PQfreeNotify(PGnotify *notify);
  292. [DllImport("pq")]
  293. public static extern int PQsendQuery (IntPtr conn,
  294. string query);
  295. // int PQsendQuery(PGconn *conn,
  296. // const char *query);
  297. [DllImport("pq")]
  298. public static extern IntPtr PQgetResult (IntPtr conn);
  299. // PGresult *PQgetResult(PGconn *conn);
  300. [DllImport("pq")]
  301. public static extern int PQisBusy (IntPtr conn);
  302. // int PQisBusy(PGconn *conn);
  303. [DllImport("pq")]
  304. public static extern int PQconsumeInput (IntPtr conn);
  305. // int PQconsumeInput(PGconn *conn);
  306. [DllImport("pq")]
  307. public static extern int PQgetline (IntPtr conn,
  308. string str, int length);
  309. // int PQgetline(PGconn *conn,
  310. // char *string, int length);
  311. [DllImport("pq")]
  312. public static extern int PQputline (IntPtr conn,
  313. string str);
  314. // int PQputline(PGconn *conn,
  315. // const char *string);
  316. [DllImport("pq")]
  317. public static extern int PQgetlineAsync (IntPtr conn,
  318. string buffer, int bufsize);
  319. // int PQgetlineAsync(PGconn *conn, char *buffer,
  320. // int bufsize);
  321. [DllImport("pq")]
  322. public static extern int PQputnbytes (IntPtr conn,
  323. string buffer, int nbytes);
  324. // int PQputnbytes(PGconn *conn,
  325. //const char *buffer, int nbytes);
  326. [DllImport("pq")]
  327. public static extern int PQendcopy (IntPtr conn);
  328. // int PQendcopy(PGconn *conn);
  329. [DllImport("pq")]
  330. public static extern int PQsetnonblocking (IntPtr conn,
  331. int arg);
  332. // int PQsetnonblocking(PGconn *conn, int arg);
  333. [DllImport("pq")]
  334. public static extern int PQisnonblocking (IntPtr conn);
  335. // int PQisnonblocking(const PGconn *conn);
  336. [DllImport("pq")]
  337. public static extern int PQflush (IntPtr conn);
  338. // int PQflush(PGconn *conn);
  339. [DllImport("pq")]
  340. public static extern IntPtr PQfn (IntPtr conn, int fnid,
  341. IntPtr result_buf, IntPtr result_len,
  342. int result_is_int, IntPtr args,
  343. int nargs);
  344. // PGresult *PQfn(PGconn *conn, int fnid,
  345. // int *result_buf, int *result_len,
  346. // int result_is_int, const PQArgBlock *args,
  347. // int nargs);
  348. [DllImport("pq")]
  349. public static extern ExecStatusType PQresultStatus (IntPtr res);
  350. // ExecStatusType PQresultStatus(const PGresult *res);
  351. [DllImport("pq")]
  352. public static extern string PQresStatus (ExecStatusType status);
  353. // char *PQresStatus(ExecStatusType status);
  354. [DllImport("pq")]
  355. public static extern string PQresultErrorMessage (IntPtr res);
  356. // char *PQresultErrorMessage(const PGresult *res);
  357. [DllImport("pq")]
  358. public static extern int PQntuples (IntPtr res);
  359. // int PQntuples(const PGresult *res);
  360. [DllImport("pq")]
  361. public static extern int PQnfields (IntPtr res);
  362. // int PQnfields(const PGresult *res);
  363. [DllImport("pq")]
  364. public static extern int PQbinaryTuples (IntPtr res);
  365. // int PQbinaryTuples(const PGresult *res);
  366. [DllImport("pq")]
  367. public static extern string PQfname (IntPtr res,
  368. int field_num);
  369. // char *PQfname(const PGresult *res,
  370. // int field_num);
  371. [DllImport("pq")]
  372. public static extern int PQfnumber (IntPtr res,
  373. string field_name);
  374. // int PQfnumber(const PGresult *res,
  375. // const char *field_name);
  376. [DllImport("pq")]
  377. public static extern int PQftype (IntPtr res,
  378. int field_num);
  379. // Oid PQftype(const PGresult *res,
  380. // int field_num);
  381. [DllImport("pq")]
  382. public static extern int PQfsize (IntPtr res,
  383. int field_num);
  384. // int PQfsize(const PGresult *res,
  385. // int field_num);
  386. [DllImport("pq")]
  387. public static extern int PQfmod (IntPtr res, int field_num);
  388. // int PQfmod(const PGresult *res, int field_num);
  389. [DllImport("pq")]
  390. public static extern string PQcmdStatus (IntPtr res);
  391. // char *PQcmdStatus(PGresult *res);
  392. [DllImport("pq")]
  393. public static extern string PQoidStatus (IntPtr res);
  394. // char *PQoidStatus(const PGresult *res);
  395. [DllImport("pq")]
  396. public static extern int PQoidValue (IntPtr res);
  397. // Oid PQoidValue(const PGresult *res);
  398. [DllImport("pq")]
  399. public static extern string PQcmdTuples (IntPtr res);
  400. // char *PQcmdTuples(PGresult *res);
  401. [DllImport("pq")]
  402. public static extern string PQgetvalue (IntPtr res,
  403. int tup_num, int field_num);
  404. // char *PQgetvalue(const PGresult *res,
  405. // int tup_num, int field_num);
  406. [DllImport("pq")]
  407. public static extern int PQgetlength (IntPtr res,
  408. int tup_num, int field_num);
  409. // int PQgetlength(const PGresult *res,
  410. // int tup_num, int field_num);
  411. [DllImport("pq")]
  412. public static extern int PQgetisnull (IntPtr res,
  413. int tup_num, int field_num);
  414. // int PQgetisnull(const PGresult *res,
  415. // int tup_num, int field_num);
  416. [DllImport("pq")]
  417. public static extern void PQclear (IntPtr res);
  418. // void PQclear(PGresult *res);
  419. [DllImport("pq")]
  420. public static extern IntPtr PQmakeEmptyPGresult (IntPtr conn,
  421. IntPtr status);
  422. // PGresult *PQmakeEmptyPGresult(PGconn *conn,
  423. // ExecStatusType status);
  424. [DllImport("pq")]
  425. public static extern void PQprint (IntPtr fout,
  426. IntPtr res, IntPtr ps);
  427. // void PQprint(FILE *fout,
  428. // const PGresult *res, const PQprintOpt *ps);
  429. [DllImport("pq")]
  430. public static extern void PQdisplayTuples (IntPtr res,
  431. IntPtr fp, int fillAlign, string fieldSep,
  432. int printHeader, int quiet);
  433. // void PQdisplayTuples(const PGresult *res,
  434. // FILE *fp, int fillAlign, const char *fieldSep,
  435. // int printHeader, int quiet);
  436. [DllImport("pq")]
  437. public static extern void PQprintTuples (IntPtr res,
  438. IntPtr fout, int printAttName, int terseOutput,
  439. int width);
  440. // void PQprintTuples(const PGresult *res,
  441. // FILE *fout, int printAttName, int terseOutput,
  442. // int width);
  443. [DllImport("pq")]
  444. public static extern int lo_open (IntPtr conn,
  445. int lobjId, int mode);
  446. // int lo_open(PGconn *conn,
  447. // Oid lobjId, int mode);
  448. [DllImport("pq")]
  449. public static extern int lo_close (IntPtr conn, int fd);
  450. // int lo_close(PGconn *conn, int fd);
  451. [DllImport("pq")]
  452. public static extern int lo_read (IntPtr conn,
  453. int fd, string buf, int len);
  454. // int lo_read(PGconn *conn,
  455. // int fd, char *buf, size_t len);
  456. [DllImport("pq")]
  457. public static extern int lo_write (IntPtr conn,
  458. int fd, string buf, int len);
  459. // int lo_write(PGconn *conn,
  460. // int fd, char *buf, size_t len);
  461. [DllImport("pq")]
  462. public static extern int lo_lseek (IntPtr conn,
  463. int fd, int offset, int whence);
  464. // int lo_lseek(PGconn *conn,
  465. // int fd, int offset, int whence);
  466. [DllImport("pq")]
  467. public static extern int lo_creat (IntPtr conn,
  468. int mode);
  469. // Oid lo_creat(PGconn *conn,
  470. // int mode);
  471. [DllImport("pq")]
  472. public static extern int lo_tell (IntPtr conn, int fd);
  473. // int lo_tell(PGconn *conn, int fd);
  474. [DllImport("pq")]
  475. public static extern int lo_unlink (IntPtr conn,
  476. int lobjId);
  477. // int lo_unlink(PGconn *conn,
  478. // Oid lobjId);
  479. [DllImport("pq")]
  480. public static extern int lo_import (IntPtr conn,
  481. string filename);
  482. // Oid lo_import(PGconn *conn,
  483. // const char *filename);
  484. [DllImport("pq")]
  485. public static extern int lo_export (IntPtr conn,
  486. int lobjId, string filename);
  487. // int lo_export(PGconn *conn,
  488. // Oid lobjId, const char *filename);
  489. [DllImport("pq")]
  490. public static extern int PQmblen (string s,
  491. int encoding);
  492. // int PQmblen(const unsigned char *s,
  493. // int encoding);
  494. [DllImport("pq")]
  495. public static extern int PQenv2encoding ();
  496. // int PQenv2encoding(void);
  497. #endregion
  498. }
  499. }