2
0

PostgresTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* PostgresTest.cs - based on the postgres-test.c in libgda
  2. *
  3. * Copyright (C) 2002 Gonzalo Paniagua Javier
  4. * Copyright (C) 2002 Daniel Morgan
  5. *
  6. * ORIGINAL AUTHOR:
  7. * Gonzalo Paniagua Javier <[email protected]>
  8. * PORTING FROM C TO C# AUTHOR:
  9. * Daniel Morgan <[email protected]>
  10. *
  11. * Permission was given from the original author, Gonzalo Paniagua Javier,
  12. * to port and include his original work in Mono.
  13. *
  14. * The original work falls under the LGPL, but the port to C# falls
  15. * under the X11 license.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of the
  20. * License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * Library General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public
  28. * License along with this program; see the file COPYING. If not,
  29. * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  30. * Boston, MA 02111-1307, USA.
  31. */
  32. using System;
  33. using System.Data;
  34. using Mono.Data.PostgreSqlClient;
  35. namespace Test.Mono.Data.PostgreSqlClient {
  36. class PostgresTest {
  37. // execute SQL CREATE TABLE Command using ExecuteNonQuery()
  38. static void CreateTable (IDbConnection cnc) {
  39. IDbCommand createCommand = cnc.CreateCommand();
  40. createCommand.CommandText =
  41. "create table mono_postgres_test (" +
  42. "boolean_value boolean, " +
  43. "int2_value smallint, " +
  44. "int4_value integer, " +
  45. "bigint_value bigint, " +
  46. "float_value real, " +
  47. "double_value double precision, " +
  48. "numeric_value numeric(15, 3), " +
  49. "char_value char(50), " +
  50. "varchar_value varchar(20), " +
  51. "text_value text, " +
  52. "point_value point, " +
  53. "time_value time, " +
  54. "date_value date, " +
  55. "timestamp_value timestamp, " +
  56. "null_boolean_value boolean, " +
  57. "null_int2_value smallint, " +
  58. "null_int4_value integer, " +
  59. "null_bigint_value bigint, " +
  60. "null_float_value real, " +
  61. "null_double_value double precision, " +
  62. "null_numeric_value numeric(15, 3), " +
  63. "null_char_value char(50), " +
  64. "null_varchar_value varchar(20), " +
  65. "null_text_value text, " +
  66. "null_point_value point, " +
  67. "null_time_value time, " +
  68. "null_date_value date, " +
  69. "null_timestamp_value timestamp " +
  70. ")";
  71. createCommand.ExecuteNonQuery ();
  72. }
  73. // execute SQL DROP TABLE Command using ExecuteNonQuery
  74. static void DropTable (IDbConnection cnc) {
  75. IDbCommand dropCommand = cnc.CreateCommand ();
  76. dropCommand.CommandText =
  77. "drop table mono_postgres_test";
  78. dropCommand.ExecuteNonQuery ();
  79. }
  80. // execute stored procedure using ExecuteScalar()
  81. static object CallStoredProcedure (IDbConnection cnc) {
  82. IDbCommand callStoredProcCommand = cnc.CreateCommand ();
  83. object data;
  84. callStoredProcCommand.CommandType =
  85. CommandType.StoredProcedure;
  86. callStoredProcCommand.CommandText =
  87. "version";
  88. data = callStoredProcCommand.ExecuteScalar ();
  89. return data;
  90. }
  91. // execute SQL INSERT Command using ExecuteNonQuery()
  92. static void InsertData (IDbConnection cnc) {
  93. IDbCommand insertCommand = cnc.CreateCommand();
  94. insertCommand.CommandText =
  95. "insert into mono_postgres_test (" +
  96. "boolean_value, " +
  97. "int2_value, " +
  98. "int4_value, " +
  99. "bigint_value, " +
  100. "float_value, " +
  101. "double_value, " +
  102. "numeric_value, " +
  103. "char_value, " +
  104. "varchar_value, " +
  105. "text_value, " +
  106. "time_value, " +
  107. "date_value, " +
  108. "timestamp_value, " +
  109. "point_value " +
  110. ") values (" +
  111. "'T', " +
  112. "-22, " +
  113. "1048000, " +
  114. "123456789012345, " +
  115. "3.141592, " +
  116. "3.1415926969696, " +
  117. "123456789012.345, " +
  118. "'This is a char', " +
  119. "'This is a varchar', " +
  120. "'This is a text', " +
  121. "'21:13:14', " +
  122. "'2000-02-29', " +
  123. "'2004-02-29 14:00:11.31', " +
  124. "'(1,0)' " +
  125. ")";
  126. insertCommand.ExecuteNonQuery ();
  127. }
  128. // execute a SQL SELECT Query using ExecuteReader() to retrieve
  129. // a IDataReader so we retrieve data
  130. static IDataReader SelectData (IDbConnection cnc) {
  131. IDbCommand selectCommand = cnc.CreateCommand();
  132. IDataReader reader;
  133. // FIXME: System.Data classes need to handle NULLs
  134. // this would be done by System.DBNull ?
  135. // FIXME: System.Data needs to handle more data types
  136. /*
  137. selectCommand.CommandText =
  138. "select * " +
  139. "from mono_postgres_test";
  140. */
  141. selectCommand.CommandText =
  142. "select " +
  143. "boolean_value, " +
  144. "int2_value, " +
  145. "int4_value, " +
  146. "bigint_value, " +
  147. "float_value, " +
  148. "double_value, " +
  149. "numeric_value, " +
  150. "char_value, " +
  151. "varchar_value, " +
  152. "text_value, " +
  153. "point_value, " +
  154. "time_value, " +
  155. "date_value, " +
  156. "timestamp_value, " +
  157. "null_boolean_value, " +
  158. "null_int2_value, " +
  159. "null_int4_value, " +
  160. "null_bigint_value, " +
  161. "null_float_value, " +
  162. "null_double_value, " +
  163. "null_numeric_value, " +
  164. "null_char_value, " +
  165. "null_varchar_value, " +
  166. "null_text_value, " +
  167. "null_point_value, " +
  168. "null_time_value, " +
  169. "null_date_value, " +
  170. "null_timestamp_value " +
  171. "from mono_postgres_test";
  172. reader = selectCommand.ExecuteReader ();
  173. return reader;
  174. }
  175. // Tests a SQL Command (INSERT, UPDATE, DELETE)
  176. // executed via ExecuteReader
  177. static IDataReader SelectDataUsingInsertCommand (IDbConnection cnc) {
  178. IDbCommand selectCommand = cnc.CreateCommand();
  179. IDataReader reader;
  180. // This is a SQL INSERT Command, not a Query
  181. selectCommand.CommandText =
  182. "insert into mono_postgres_test (" +
  183. "boolean_value, " +
  184. "int2_value, " +
  185. "int4_value, " +
  186. "bigint_value, " +
  187. "float_value, " +
  188. "double_value, " +
  189. "numeric_value, " +
  190. "char_value, " +
  191. "varchar_value, " +
  192. "text_value, " +
  193. "time_value, " +
  194. "date_value, " +
  195. "timestamp_value, " +
  196. "point_value " +
  197. ") values (" +
  198. "'T', " +
  199. "-22, " +
  200. "1048000, " +
  201. "123456789012345, " +
  202. "3.141592, " +
  203. "3.1415926969696, " +
  204. "123456789012.345, " +
  205. "'This is a char', " +
  206. "'This is a varchar', " +
  207. "'This is a text', " +
  208. "'21:13:14', " +
  209. "'2000-02-29', " +
  210. "'2004-02-29 14:00:11.31', " +
  211. "'(1,0)' " +
  212. ")";
  213. reader = selectCommand.ExecuteReader ();
  214. return reader;
  215. }
  216. // Tests a SQL Command not (INSERT, UPDATE, DELETE)
  217. // executed via ExecuteReader
  218. static IDataReader SelectDataUsingCommand (IDbConnection cnc) {
  219. IDbCommand selectCommand = cnc.CreateCommand();
  220. IDataReader reader;
  221. // This is a SQL Command, not a Query
  222. selectCommand.CommandText =
  223. "SET DATESTYLE TO 'ISO'";
  224. reader = selectCommand.ExecuteReader ();
  225. return reader;
  226. }
  227. // execute an SQL UPDATE Command using ExecuteNonQuery()
  228. static void UpdateData (IDbConnection cnc) {
  229. IDbCommand updateCommand = cnc.CreateCommand();
  230. updateCommand.CommandText =
  231. "update mono_postgres_test " +
  232. "set " +
  233. "boolean_value = 'F', " +
  234. "int2_value = 5, " +
  235. "int4_value = 3, " +
  236. "bigint_value = 9, " +
  237. "char_value = 'Mono.Data!' , " +
  238. "varchar_value = 'It was not me!', " +
  239. "text_value = 'We got data!' " +
  240. "where int2_value = -22";
  241. updateCommand.ExecuteNonQuery ();
  242. }
  243. // used to do a min(), max(), count(), sum(), or avg()
  244. // execute SQL SELECT Query using ExecuteScalar
  245. static object SelectAggregate (IDbConnection cnc, String agg) {
  246. IDbCommand selectCommand = cnc.CreateCommand();
  247. object data;
  248. Console.WriteLine("Aggregate: " + agg);
  249. selectCommand.CommandType = CommandType.Text;
  250. selectCommand.CommandText =
  251. "select " + agg +
  252. "from mono_postgres_test";
  253. data = selectCommand.ExecuteScalar ();
  254. Console.WriteLine("Agg Result: " + data);
  255. return data;
  256. }
  257. // used internally by ReadData() to read each result set
  258. static void ReadResult(IDataReader rdr, DataTable dt) {
  259. // number of columns in the table
  260. Console.WriteLine(" Total Columns: " +
  261. dt.Rows.Count);
  262. // display the schema
  263. foreach (DataRow schemaRow in dt.Rows) {
  264. foreach (DataColumn schemaCol in dt.Columns)
  265. Console.WriteLine(schemaCol.ColumnName +
  266. " = " +
  267. schemaRow[schemaCol]);
  268. Console.WriteLine();
  269. }
  270. int nRows = 0;
  271. int c = 0;
  272. string output, metadataValue, dataValue;
  273. // Read and display the rows
  274. Console.WriteLine("Gonna do a Read() now...");
  275. while(rdr.Read()) {
  276. Console.WriteLine(" Row " + nRows + ": ");
  277. for(c = 0; c < rdr.FieldCount; c++) {
  278. // column meta data
  279. DataRow dr = dt.Rows[c];
  280. metadataValue =
  281. " Col " +
  282. c + ": " +
  283. dr["ColumnName"];
  284. // column data
  285. if(rdr.IsDBNull(c) == true)
  286. dataValue = " is NULL";
  287. else
  288. dataValue =
  289. ": " +
  290. rdr.GetValue(c);
  291. // display column meta data and data
  292. output = metadataValue + dataValue;
  293. Console.WriteLine(output);
  294. }
  295. nRows++;
  296. }
  297. Console.WriteLine(" Total Rows Retrieved: " +
  298. nRows);
  299. }
  300. // Used to read data from IDataReader after calling IDbCommand:ExecuteReader()
  301. static void ReadData(IDataReader rdr) {
  302. int results = 0;
  303. if(rdr == null) {
  304. Console.WriteLine("IDataReader has a Null Reference.");
  305. }
  306. else {
  307. do {
  308. DataTable dt = rdr.GetSchemaTable();
  309. if(rdr.RecordsAffected != -1) {
  310. // Results for
  311. // SQL INSERT, UPDATE, DELETE Commands
  312. // have RecordsAffected >= 0
  313. Console.WriteLine("Result is from a SQL Command (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
  314. }
  315. else if(dt == null)
  316. // Results for
  317. // SQL Commands not INSERT, UPDATE, nor DELETE
  318. // have RecordsAffected == -1
  319. // and GetSchemaTable() returns a null reference
  320. Console.WriteLine("Result is from a SQL Command not (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
  321. else {
  322. // Results for
  323. // SQL SELECT Queries
  324. // have RecordsAffected = -1
  325. // and GetSchemaTable() returns a reference to a DataTable
  326. Console.WriteLine("Result is from a SELECT SQL Query. Records Affected: " + rdr.RecordsAffected);
  327. results++;
  328. Console.WriteLine("Result Set " + results + "...");
  329. ReadResult(rdr, dt);
  330. }
  331. } while(rdr.NextResult());
  332. Console.WriteLine("Total Result sets: " + results);
  333. rdr.Close();
  334. }
  335. }
  336. /* Postgres provider tests */
  337. static void DoPostgresTest (IDbConnection cnc) {
  338. IDataReader reader;
  339. Object oDataValue;
  340. Console.WriteLine ("\tPostgres provider specific tests...\n");
  341. /* Drops the gda_postgres_test table. */
  342. Console.WriteLine ("\t\tDrop table: ");
  343. try {
  344. DropTable (cnc);
  345. Console.WriteLine ("OK");
  346. }
  347. catch (PgSqlException e) {
  348. Console.WriteLine("Error (don't worry about this one)" + e);
  349. }
  350. try {
  351. /* Creates a table with all supported data types */
  352. Console.WriteLine ("\t\tCreate table with all supported types: ");
  353. CreateTable (cnc);
  354. Console.WriteLine ("OK");
  355. /* Inserts values */
  356. Console.WriteLine ("\t\tInsert values for all known types: ");
  357. InsertData (cnc);
  358. Console.WriteLine ("OK");
  359. /* Update values */
  360. Console.WriteLine ("\t\tUpdate values: ");
  361. UpdateData (cnc);
  362. Console.WriteLine ("OK");
  363. /* Inserts values */
  364. Console.WriteLine ("\t\tInsert values for all known types: ");
  365. InsertData (cnc);
  366. Console.WriteLine ("OK");
  367. /* Select aggregates */
  368. SelectAggregate (cnc, "count(*)");
  369. // FIXME: still having a problem with avg()
  370. // because it returns a decimal.
  371. // It may have something to do
  372. // with culture not being set
  373. // properly.
  374. //SelectAggregate (cnc, "avg(int4_value)");
  375. SelectAggregate (cnc, "min(text_value)");
  376. SelectAggregate (cnc, "max(int4_value)");
  377. SelectAggregate (cnc, "sum(int4_value)");
  378. /* Select values */
  379. Console.WriteLine ("\t\tSelect values from the database: ");
  380. reader = SelectData (cnc);
  381. ReadData(reader);
  382. /* SQL Command via ExecuteReader/SqlDataReader */
  383. /* Command is not INSERT, UPDATE, or DELETE */
  384. Console.WriteLine("\t\tCall ExecuteReader with a SQL Command. (Not INSERT,UPDATE,DELETE).");
  385. reader = SelectDataUsingCommand(cnc);
  386. ReadData(reader);
  387. /* SQL Command via ExecuteReader/SqlDataReader */
  388. /* Command is INSERT, UPDATE, or DELETE */
  389. Console.WriteLine("\t\tCall ExecuteReader with a SQL Command. (Is INSERT,UPDATE,DELETE).");
  390. reader = SelectDataUsingInsertCommand(cnc);
  391. ReadData(reader);
  392. // Call a Stored Procedure named Version()
  393. Console.WriteLine("\t\tCalling stored procedure version()");
  394. object obj = CallStoredProcedure(cnc);
  395. Console.WriteLine("Result: " + obj);
  396. Console.WriteLine("Database Server Version: " +
  397. ((PgSqlConnection)cnc).ServerVersion);
  398. /* Clean up */
  399. Console.WriteLine ("Clean up...");
  400. Console.WriteLine ("\t\tDrop table...");
  401. DropTable (cnc);
  402. Console.WriteLine("OK");
  403. }
  404. catch(Exception e) {
  405. Console.WriteLine("Exception caught: " + e);
  406. }
  407. }
  408. [STAThread]
  409. static void Main(string[] args) {
  410. Console.WriteLine("Tests Start.");
  411. Console.WriteLine("Creating PgSqlConnectioin...");
  412. PgSqlConnection cnc = new PgSqlConnection ();
  413. // possible PostgreSQL Provider ConnectionStrings
  414. //string connectionString =
  415. // "Server=hostname;" +
  416. // "Database=database;" +
  417. // "User ID=userid;" +
  418. // "Password=password";
  419. // or
  420. //string connectionString =
  421. // "host=hostname;" +
  422. // "dbname=database;" +
  423. // "user=userid;" +
  424. // "password=password";
  425. string connectionString =
  426. "host=localhost;" +
  427. "dbname=test;" +
  428. "user=postgres";
  429. Console.WriteLine("Setting ConnectionString: " +
  430. connectionString);
  431. cnc.ConnectionString = connectionString;
  432. Console.WriteLine("Opening database connection...");
  433. cnc.Open();
  434. Console.WriteLine("Do Tests....");
  435. DoPostgresTest(cnc);
  436. Console.WriteLine("Close database connection...");
  437. cnc.Close();
  438. Console.WriteLine("Tests Done.");
  439. }
  440. }
  441. }