postgresql 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. * PostgreSQL Data Provider
  2. <ul>
  3. <li>Exists in namespace Mono.Data.PostgreSql and assembly Mono.Data.PostgreSql
  4. <li>Is a Mono Data Provider for the <a href="http://www.postgresql.org/">PostgreSQL</a>
  5. client/server database management system.
  6. <li>Written in C# and has C# bindings to the PostgreSQL C Client library pq.dll on Windows
  7. and libpq.so on Linux.
  8. </ul>
  9. * Current Status
  10. <ul>
  11. <li>We are able to do simple CREATE TABLE, DROP TABLE, UPDATE, INSERT, and
  12. DELETE SQL commands using the ExecuteNonQuery method in PgSqlCommand.
  13. <li>We can execute multiple queries and do a NextResult() in PgSqlDataReader()
  14. to get the next result set.
  15. <li>We are also able to do simple aggregate functions,
  16. ie, count(), sum(), min(), and max()
  17. in a simple SELECT SQL query using the ExecuteScalar() now.
  18. <li>We are also able to retrieve data with a simple SELECT SQL query
  19. using ExecuteReader() which returns a PgSqlDataReader. We are able to
  20. use GetSchemaTable() to get the meta data about the table columns.
  21. We are able to Read() to get each row from the result set.
  22. <li>We are able to get
  23. String data (char, character, text, varchar), Int16 (smallint),
  24. Int32 (integer), Int64 (bigint), DateTime (time, date, timestamp),
  25. Boolean (boolean), Single (float), and Double (double).
  26. More data types will come later. Note, the types that do work still
  27. need thorough testing.
  28. <li>Rows that are returned which contain columns that are NULL are handled now.
  29. The PgSqlDataReader method IsDBNull() needs to be called to determine
  30. if a field IS NULL before trying to read data from that field.
  31. <li>Calling PostgreSQL stored procedures works. It does not work perfectly.
  32. It may not
  33. even work to specification - yet. If you want to test it yourself, look at
  34. TestSqlDataReader.cs or PostgresTest.cs in
  35. mcs/class/System.Data/Test.
  36. <li>Below, I have some sample code you can
  37. use to call a PostgreSQL stored procedure named "version". This stored
  38. procedure returns a string containing the PostgreSQL server version. Notice
  39. the CommandType is StoredProcedure and the method ExecuteScalar() is called.
  40. <li>ExecuteScalar() is a lightweight method in class PgSqlCommand that only returns
  41. one row and one column as one object - even if there is more than row or column.
  42. <li>We have the beginnings of Parameters support PostgreSQL. Only
  43. Input Parameters are currently supported. Output, Input/Output,
  44. and Return parameters still need to be done.
  45. <li>A lot of functionality in System.Data is missing, but the
  46. infrastructure is starting to come together.
  47. <li>A lot of Exceptions need to be thrown for various exceptions. However,
  48. PgSqlException, PgSqlErrorCollection, and PgSqlError have been partially
  49. implemented.
  50. <li>Tim Coleman and Rodrigo Moya got the beginnings of the
  51. PgSqlDataAdapter/DataSet/DataTable/DataRow to work. Currently,
  52. the PgSqlDataAdapter can Fill() relational data into a DataTable in a DataSet.
  53. See the test mcs/class/System.Data/Test/TestSqlDataAdapter.cs to see it in action.
  54. </ul>
  55. * Testing the PostgreSQL Provider
  56. <ul>
  57. * <p>In order to test Mono.Data.PostgreSqlClient, you will need to have
  58. access to a remote PostgreSQL DBMS, or you will have to install
  59. one locally. PostgreSQL was first ADO.NET provider created in Mono.
  60. <p>Why use PostgreSQL? Because it is free software, has a client
  61. library that is easy to use, PostgreSQL is easy to install on
  62. Unix and Windows (using the Cygwin install program), not difficult to setup after
  63. installation, and it runs under: Linux,
  64. Windows (via cygwin and ipc-daemon), Unix, and
  65. others. This allowed us to create the
  66. System.Data functionality in Mono much quicker.
  67. <p>If you plan on using a remote PostgreSQL DBMS Server,
  68. than you will need to have the PostgreSQL client software on your
  69. local computer that includes libpq.so (pq.dll on Windows).
  70. <p>The System.Data tests use this connection string to connect
  71. to the PostgreSQL database named "test" at host "localhost" as
  72. user "postgres".
  73. <pre>
  74. "host=localhost;dbname=test;user=postgres"
  75. </pre>
  76. </ul>
  77. <p>Installation instructions for PostgreSQL DBMS:
  78. <b>On Unix</b>
  79. <ul>
  80. * Read the PostgreSQL Installation Instructions
  81. at \usr\doc\postgresql-x.x.x\html\installation.html
  82. * Depending on your Unix system,
  83. PostgreSQL maybe already installed, a database user 'postgres' created,
  84. a linux user 'postgres' created and initdb ran. Or maybe not.
  85. <pre>
  86. su
  87. adduser postgres
  88. mkdir /usr/local/pgsql/data
  89. chown postgres /usr/local/pgsql/data
  90. su - postgres
  91. initdb -D /usr/local/pgsql/data
  92. postmaster -i -D /usr/local/pgsql/data
  93. createdb test
  94. psql test
  95. </pre>
  96. * Make sure you have a database user named postgres. It is best to install
  97. the PostgreSQL DBMS under linux user postgres. When you run the postmaster,
  98. run it under the user postgres as well. If this was not done, then you
  99. will need to create a user named postgres for the System.Data tests.
  100. * If you already installed PostgeSQL and you do not have a database
  101. user named postgres, then you can create user postgres using psql:
  102. <pre>
  103. psql test
  104. create user postgres with password 'fun2db';
  105. </pre>
  106. * The postmaster must be run with -i option.
  107. * In the /usr/local/pgsql/data/pg_hba.conf file, you need
  108. to have the AUTH_TYPE set to md5. You can read more on this at
  109. /usr/doc/postgresql-7.2.1/html/client-authentication.html
  110. or wherever your
  111. PostgreSQL html docs are located. See the 2nd line below,
  112. host 127.0.0.1 has an AUTH_TYPE md5 in pg_hba.conf.
  113. <pre>
  114. # TYPE DATABASE IP_ADDRESS MASK AUTH_TYPE
  115. local all trust
  116. host all 127.0.0.1 255.255.255.255 md5
  117. </pre>
  118. * If you can not find your PostgreSQL documentation locally or you
  119. did not install it, then you
  120. can get it <a href="http://www.postgresql.org/idocs/">here</a>.
  121. </ul>
  122. <b>On Windows</b>
  123. <ul>
  124. * Use the <a href="http://www.cygwin.com/">Cygwin</a> installer to
  125. install the PostgreSQL DBMS. It is
  126. found in the database category.
  127. * <p>Read the file postgres-x.x.README at /usr/doc/Cygwin and read
  128. the requirements to install PostgreSQL. Those requirements
  129. are included with cygwin except cygipc. A default installtion
  130. of cygwin does not install everything you will need, so on the
  131. safe side, just include everything when installing cygwin.
  132. * <p>The -x.x in postgres-x.x is the version of your PostgreSQL DBMS.
  133. * <p>Once Cygwin has installed the PostgreSQL DBMS on your computer,
  134. read the file FAQ_MSWIN which is available
  135. in /usr/doc/postgres-x.x
  136. * <p>Important notes from this file are:
  137. <ul>
  138. <p><b>2.</b> - Install the latest <a href="http://www.neuro.gatech.edu/users/cwilson/cygutils/OBSOLETE/V1.1/cygipc/index.html">CygIPC</a> package.
  139. <p>The cygipc package contains the ipc-daemon you will need
  140. to run before you can
  141. run the PostgreSQL DBMS Server daemon (postmaster) or run
  142. initdb which initializes the PostgreSQL database.
  143. <p><b>3.</b> The Cygwin bin directory has to be placed in
  144. the path before the Windows program directories,
  145. for example, C:\cygwin\bin
  146. <p><b>My own note.</b> In the Windows control panel, I set
  147. the environment variables PATH to my cygwin /usr/local/bin,
  148. /usr/bin, and /bin. I also set my LD_LIBRARY_PATH to
  149. /usr/local/lib and /usr/lib. For example:
  150. <p>
  151. <pre>
  152. PATH=c:\cygwin\usr\local\bin;c:\cygwin\usr\bin;c:\cygwin\bin;
  153. LD_LIBRARY_PATH=c:\cygwin\usr\local\lib;c:\cygwin\usr\lib;
  154. </pre>
  155. <p><b>4.</b> Start the ipc-daemon that came with the cygipc
  156. package. There
  157. are two ways to do this: run it from the command line as:
  158. <p>
  159. <pre>
  160. ipc-daemon &
  161. </pre>
  162. <p>or you can set it up as a Windows service. See the
  163. file cygrunsrv.README at /usr/doc/Cygwin on how to do this
  164. for ipc-daemon and postmaster. Note the
  165. troubleshooting section at the end of
  166. the cygrunsrv.README file.
  167. <p>To install ipc-daemon as a service,
  168. you just have to run
  169. <p>
  170. <pre>
  171. ipc-daemon --install-as-service' (--remove-as-service)
  172. </pre>
  173. <p>and then run
  174. <pre>
  175. net start ipc-daemon
  176. </pre>
  177. </ul>
  178. <p>Read the installation.html file
  179. at /usr/doc/postgresql-x.x/html/installation.html
  180. <p>You will see in this file that you will need to
  181. run the following commands:
  182. <p>
  183. <pre>
  184. mkdir /usr/local/pgsql/data
  185. initdb -D /usr/local/pgsql/data
  186. postmaster -D /usr/local/pgsql/data
  187. createdb test
  188. psql test
  189. </pre>
  190. <p>When you need to connect to the database,
  191. you will need ipc-daemon and postmaster running. Start ipc-daemon
  192. before any of the command above. If you restart your computer, you
  193. need to start ipc-daemon and postmaster either manually or as a
  194. service.
  195. <p>psql is a command-line PostgreSQL client tool to
  196. enter and run SQL commands and queries.
  197. <p>If there is no database user named postgres, create a user named
  198. postgres with the following SQL command in the client tool psql:
  199. <p>
  200. <pre>
  201. psql test
  202. create user postgres with password 'fun2db';
  203. </pre>
  204. <p>The only reason I say this is so you can easily use the System.Data tests
  205. without having to change the database, userid, etc.
  206. </ul>
  207. <p>In the path mcs/class/System.Data/Test
  208. there is a PostgreSQL test program named
  209. PostgreTest.cs. Thanks goes to Gonzalo for creating the original
  210. PostgreSQL test.
  211. <p>To use it to test System.Data, you
  212. modify the file to your PostgreSQL database
  213. connection requirements:
  214. <p>
  215. <ul>
  216. <li><b>dbname</b> database, ie., test</li>
  217. <li><b>host</b> hostname of the PostgreSQL DBMS Server, ie., localhost</li>
  218. <li><b>user</b> username, ie., someuser</li>
  219. <li><b>password</b> password, ie., mypass1234</li>
  220. </ul>
  221. <p>The connection string is in OLE-DB connection string format. Internally,
  222. SqlConnection converts this to the PostgreSQL connection string format.
  223. <p>
  224. <pre>
  225. OLE-DB: "host=localhost;dbname=test;user=joe;password=smoe"
  226. PostgreSQL: "host=localhost dbname=test user=joe password=smoe"
  227. </pre>
  228. <p>
  229. Note that OLE-DB includes the semicolons while PostgreSQL's connection
  230. string does not.
  231. <p>
  232. To compile the PostgresTest.cs program, do:
  233. <p>
  234. <pre>
  235. mcs PostgresTest.cs \
  236. -r System.Data.dll \
  237. -r Mono.Data.PostgreSqlClient.dll
  238. </pre>
  239. <p>If there are compile errors, such as, can not convert IDbConnection
  240. to PgSqlConnection, then you need to run mcs like:
  241. <pre>
  242. mono f:/cygwin/home/DanielMorgan/mono/install/bin/mcs.exe \
  243. PostgresTest.cs \
  244. -r System.Data.dll \
  245. -r Mono.Data.PostgreSqlClient.dll
  246. </pre>
  247. <p>
  248. To run using mint, do:
  249. <p>
  250. <pre>
  251. mint PostgresTest.exe
  252. </pre>
  253. <p>
  254. To run using mono, do:
  255. <pre>
  256. mono PostgresTest.exe
  257. </pre>
  258. <p>Below, I show how the output from PostgresTest. I have omitted a lot
  259. of the meta data for the columns except two columns. The classes
  260. used were from Mono.Data.PostgreSqlClient and were used to connect to a
  261. PostgreSQL database and retrieve data.
  262. <p>
  263. <pre>
  264. danmorg@DANPC ~/mono/mcs/class/System.Data/Test
  265. $ mcs PostgresTest.cs -r System.Data.dll
  266. danmorg@DANPC ~/mono/mcs/class/System.Data/Test
  267. $ mono PostgresTest.exe
  268. Postgres provider specific tests...
  269. Drop table:
  270. Error (don't worry about this one)SqlError:PGRES_FATAL_ERROR ERROR:
  271. table "mono_postgres_test" does not exist
  272. <Stack Trace>
  273. Create table with all supported types:
  274. OK
  275. Insert values for all known types:
  276. OK
  277. Update values:
  278. OK
  279. Insert values for all known types:
  280. OK
  281. Aggregate: count(*)
  282. Agg Result: 2
  283. Aggregate: min(text_value)
  284. Agg Result: This is a text
  285. Aggregate: max(int4_value)
  286. Agg Result: 1048000
  287. Aggregate: sum(int4_value)
  288. Agg Result: 1048003
  289. Select values from the database:
  290. Result is from a SELECT SQL Query. Records Affected: -1
  291. Result Set 1...
  292. Total Columns: 28
  293. ColumnName = boolean_value
  294. ColumnOrdinal = 1
  295. ColumnSize = 1
  296. NumericPrecision = 0
  297. NumericScale = 0
  298. IsUnique = False
  299. IsKey =
  300. BaseCatalogName =
  301. BaseColumnName = boolean_value
  302. BaseSchemaName =
  303. BaseTableName =
  304. DataType = System.Boolean
  305. AllowDBNull = False
  306. ProviderType = 16
  307. IsAliased = False
  308. IsExpression = False
  309. IsIdentity = False
  310. IsAutoIncrement = False
  311. IsRowVersion = False
  312. IsHidden = False
  313. IsLong = False
  314. IsReadOnly = False
  315. ...
  316. ColumnName = null_timestamp_value
  317. ColumnOrdinal = 28
  318. ColumnSize = 8
  319. NumericPrecision = 0
  320. NumericScale = 0
  321. IsUnique = False
  322. IsKey =
  323. BaseCatalogName =
  324. BaseColumnName = null_timestamp_value
  325. BaseSchemaName =
  326. BaseTableName =
  327. DataType = System.DateTime
  328. AllowDBNull = False
  329. ProviderType = 1184
  330. IsAliased = False
  331. IsExpression = False
  332. IsIdentity = False
  333. IsAutoIncrement = False
  334. IsRowVersion = False
  335. IsHidden = False
  336. IsLong = False
  337. IsReadOnly = False
  338. Gonna do a Read() now...
  339. Row 0:
  340. Col 0: boolean_value: False
  341. Col 1: int2_value: 5
  342. Col 2: int4_value: 3
  343. Col 3: bigint_value: 9
  344. Col 4: float_value: 3.141590
  345. Col 5: double_value: 3.14159
  346. Col 6: numeric_value: 123456789012.345
  347. Col 7: char_value: Mono.Data!
  348. Col 8: varchar_value: It was not me!
  349. Col 9: text_value: We got data!
  350. Col 10: point_value: (1,0)
  351. Col 11: time_value: 01/01/1 21:13:14
  352. Col 12: date_value: 02/29/2000 00:00:00
  353. Col 13: timestamp_value: 02/29/2004 14:00:11
  354. Col 14: null_boolean_value is NULL
  355. Col 15: null_int2_value is NULL
  356. Col 16: null_int4_value is NULL
  357. Col 17: null_bigint_value is NULL
  358. Col 18: null_float_value is NULL
  359. Col 19: null_double_value is NULL
  360. Col 20: null_numeric_value is NULL
  361. Col 21: null_char_value is NULL
  362. Col 22: null_varchar_value is NULL
  363. Col 23: null_text_value is NULL
  364. Col 24: null_point_value is NULL
  365. Col 25: null_time_value is NULL
  366. Col 26: null_date_value is NULL
  367. Col 27: null_timestamp_value is NULL
  368. Row 1:
  369. Col 0: boolean_value: True
  370. Col 1: int2_value: -22
  371. Col 2: int4_value: 1048000
  372. Col 3: bigint_value: 123456789012345
  373. Col 4: float_value: 3.141590
  374. Col 5: double_value: 3.14159
  375. Col 6: numeric_value: 123456789012.345
  376. Col 7: char_value: This is a char
  377. Col 8: varchar_value: This is a varchar
  378. Col 9: text_value: This is a text
  379. Col 10: point_value: (1,0)
  380. Col 11: time_value: 01/01/1 21:13:14
  381. Col 12: date_value: 02/29/2000 00:00:00
  382. Col 13: timestamp_value: 02/29/2004 14:00:11
  383. Col 14: null_boolean_value is NULL
  384. Col 15: null_int2_value is NULL
  385. Col 16: null_int4_value is NULL
  386. Col 17: null_bigint_value is NULL
  387. Col 18: null_float_value is NULL
  388. Col 19: null_double_value is NULL
  389. Col 20: null_numeric_value is NULL
  390. Col 21: null_char_value is NULL
  391. Col 22: null_varchar_value is NULL
  392. Col 23: null_text_value is NULL
  393. Col 24: null_point_value is NULL
  394. Col 25: null_time_value is NULL
  395. Col 26: null_date_value is NULL
  396. Col 27: null_timestamp_value is NULL
  397. Total Rows Retrieved: 2
  398. Total Result sets: 1
  399. Call ExecuteReader with a SQL Command.
  400. (Not INSERT,UPDATE,DELETE
  401. ).
  402. Result is from a SQL Command not (INSERT,UPDATE,DELETE).
  403. Records Affected: -1
  404. Total Result sets: 0
  405. Call ExecuteReader with a SQL Command.
  406. (Is INSERT,UPDATE,DELETE)
  407. .
  408. Result is from a SQL Command (INSERT,UPDATE,DELETE). Records Affected: 1
  409. Total Result sets: 0
  410. Calling stored procedure version()
  411. Result: PostgreSQL 7.2.1 on i686-pc-cygwin, compiled by GCC 2.95.3-5
  412. Database Server Version: PostgreSQL 7.2.1 on i686-pc-cygwin,
  413. compiled by GCC 2.9
  414. 5.3-5
  415. Clean up...
  416. Drop table...
  417. OK
  418. RESULT: 0
  419. </pre>
  420. * Action Plan
  421. <p>Eventually replace the PostgreSQL data provider in Mono
  422. with <a href="http://gborg.postgresql.org/project/npgsql/projdisplay.php">Npgsql</a>.
  423. Npgsql is a .Net Data Provider for PostgreSQL which implements
  424. the <a href="http://developer.postgresql.org/docs/postgres/protocol.html">PostgreSQL Frontend/Backend Protocol</a>.
  425. Npgsql is implemented in 100% C#. This provider was created by Francisco Figueiredo jr.
  426. and has many programmers developing the provider.