PostgresTest.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 System.Data.SqlClient;
  35. namespace TestSystemDataSqlClient {
  36. class PostgresTest {
  37. static void CreateTable (IDbConnection cnc) {
  38. IDbCommand createCommand = cnc.CreateCommand();
  39. createCommand.CommandText =
  40. "create table mono_postgres_test (" +
  41. "boolean_value boolean, " +
  42. "int2_value smallint, " +
  43. "int4_value integer, " +
  44. "bigint_value bigint, " +
  45. "float_value real, " +
  46. "double_value double precision, " +
  47. "numeric_value numeric(15, 3), " +
  48. "char_value char(50), " +
  49. "varchar_value varchar(20), " +
  50. "text_value text, " +
  51. "point_value point, " +
  52. "time_value time, " +
  53. "date_value date, " +
  54. "timestamp_value timestamp, " +
  55. "null_boolean_value boolean, " +
  56. "null_int2_value smallint, " +
  57. "null_int4_value integer, " +
  58. "null_bigint_value bigint, " +
  59. "null_float_value real, " +
  60. "null_double_value double precision, " +
  61. "null_numeric_value numeric(15, 3), " +
  62. "null_char_value char(50), " +
  63. "null_varchar_value varchar(20), " +
  64. "null_text_value text, " +
  65. "null_point_value point, " +
  66. "null_time_value time, " +
  67. "null_date_value date, " +
  68. "null_timestamp_value timestamp " +
  69. ")";
  70. createCommand.ExecuteNonQuery ();
  71. }
  72. static void DropTable (IDbConnection cnc) {
  73. IDbCommand dropCommand = cnc.CreateCommand ();
  74. dropCommand.CommandText =
  75. "drop table mono_postgres_test";
  76. dropCommand.ExecuteNonQuery ();
  77. }
  78. static object CallStoredProcedure (IDbConnection cnc) {
  79. IDbCommand callStoredProcCommand = cnc.CreateCommand ();
  80. object data;
  81. callStoredProcCommand.CommandText =
  82. "select version()";
  83. data = callStoredProcCommand.ExecuteScalar ();
  84. return data;
  85. }
  86. static void InsertData (IDbConnection cnc) {
  87. IDbCommand insertCommand = cnc.CreateCommand();
  88. insertCommand.CommandText =
  89. "insert into mono_postgres_test (" +
  90. "boolean_value, " +
  91. "int2_value, " +
  92. "int4_value, " +
  93. "bigint_value, " +
  94. "float_value, " +
  95. "double_value, " +
  96. "numeric_value, " +
  97. "char_value, " +
  98. "varchar_value, " +
  99. "text_value, " +
  100. "time_value, " +
  101. "date_value, " +
  102. "timestamp_value, " +
  103. "point_value " +
  104. ") values (" +
  105. "'T', " +
  106. "-22, " +
  107. "1048000, " +
  108. "123456789012345, " +
  109. "3.141592, " +
  110. "3.1415926969696, " +
  111. "123456789012.345, " +
  112. "'This is a char', " +
  113. "'This is a varchar', " +
  114. "'This is a text', " +
  115. "'21:13:14', " +
  116. "'2000-02-29', " +
  117. "'2004-02-29 14:00:11.31', " +
  118. "'(1,0)' " +
  119. ")";
  120. insertCommand.ExecuteNonQuery ();
  121. }
  122. static IDataReader SelectData (IDbConnection cnc) {
  123. IDbCommand selectCommand = cnc.CreateCommand();
  124. IDataReader reader;
  125. // FIXME: System.Data classes need to handle NULLs
  126. // FIXME: System.Data needs to handle more data types
  127. /*
  128. selectCommand.CommandText =
  129. "select * " +
  130. "from mono_postgres_test";
  131. */
  132. selectCommand.CommandText =
  133. "select " +
  134. "boolean_value, " +
  135. "int2_value, " +
  136. "int4_value, " +
  137. "bigint_value, " +
  138. "float_value, " +
  139. "double_value, " +
  140. "numeric_value, " +
  141. "char_value, " +
  142. "varchar_value, " +
  143. "text_value, " +
  144. "point_value, " +
  145. "time_value, " +
  146. "date_value, " +
  147. "timestamp_value, " +
  148. "null_boolean_value, " +
  149. "null_int2_value, " +
  150. "null_int4_value, " +
  151. "null_bigint_value, " +
  152. "null_float_value, " +
  153. "null_double_value, " +
  154. "null_numeric_value, " +
  155. "null_char_value, " +
  156. "null_varchar_value, " +
  157. "null_text_value, " +
  158. "null_point_value, " +
  159. "null_time_value, " +
  160. "null_date_value, " +
  161. "null_timestamp_value " +
  162. "from mono_postgres_test";
  163. reader = selectCommand.ExecuteReader ();
  164. return reader;
  165. }
  166. static void UpdateData (IDbConnection cnc) {
  167. IDbCommand updateCommand = cnc.CreateCommand();
  168. updateCommand.CommandText =
  169. "update mono_postgres_test " +
  170. "set " +
  171. "boolean_value = 'F', " +
  172. "int2_value = 5, " +
  173. "int4_value = 3, " +
  174. "bigint_value = 9, " +
  175. "char_value = 'Mono.Data!' , " +
  176. "varchar_value = 'It was not me!', " +
  177. "text_value = 'We got data!' " +
  178. "where int2_value = -22";
  179. updateCommand.ExecuteNonQuery ();
  180. }
  181. // used to do a min(), max(), count(), sum(), or avg()
  182. static object SelectAggregate (IDbConnection cnc, String agg) {
  183. IDbCommand selectCommand = cnc.CreateCommand();
  184. object data;
  185. Console.WriteLine("Aggregate: " + agg);
  186. selectCommand.CommandText =
  187. "select " + agg +
  188. "from mono_postgres_test";
  189. data = selectCommand.ExecuteScalar ();
  190. Console.WriteLine("Agg Result: " + data);
  191. return data;
  192. }
  193. /* Postgres provider tests */
  194. static void DoPostgresTest (IDbConnection cnc) {
  195. IDataReader reader;
  196. Object oDataValue;
  197. Console.WriteLine ("\tPostgres provider specific tests...\n");
  198. /* Drops the gda_postgres_test table. */
  199. Console.WriteLine ("\t\tDrop table: ");
  200. try {
  201. DropTable (cnc);
  202. Console.WriteLine ("OK");
  203. }
  204. catch (SqlException e) {
  205. Console.WriteLine("Error (don't worry about this one)" + e);
  206. }
  207. try {
  208. /* Creates a table with all supported data types */
  209. Console.WriteLine ("\t\tCreate table with all supported types: ");
  210. CreateTable (cnc);
  211. Console.WriteLine ("OK");
  212. /* Inserts values */
  213. Console.WriteLine ("\t\tInsert values for all known types: ");
  214. InsertData (cnc);
  215. Console.WriteLine ("OK");
  216. /* Update values */
  217. Console.WriteLine ("\t\tUpdate values: ");
  218. UpdateData (cnc);
  219. Console.WriteLine ("OK");
  220. /* Inserts values */
  221. Console.WriteLine ("\t\tInsert values for all known types: ");
  222. InsertData (cnc);
  223. Console.WriteLine ("OK");
  224. /* Select aggregates */
  225. SelectAggregate (cnc, "count(*)");
  226. //SelectAggregate (cnc, "avg(int4_value)");
  227. SelectAggregate (cnc, "min(text_value)");
  228. SelectAggregate (cnc, "max(int4_value)");
  229. SelectAggregate (cnc, "sum(int4_value)");
  230. /* Select values */
  231. Console.WriteLine ("\t\tSelect values from the database: ");
  232. reader = SelectData (cnc);
  233. // get the DataTable that holds
  234. // the schema
  235. Console.WriteLine("\t\tGet Schema.");
  236. DataTable dt = reader.GetSchemaTable();
  237. // number of columns in the table
  238. Console.WriteLine("dt.Columns.Count: " +
  239. dt.Columns.Count);
  240. int c;
  241. // display the schema
  242. for(c = 0; c < dt.Columns.Count; c++) {
  243. Console.WriteLine("* Column Name: " +
  244. dt.Columns[c].ColumnName);
  245. Console.WriteLine(" MaxLength: " +
  246. dt.Columns[c].MaxLength);
  247. Console.WriteLine(" Type: " +
  248. dt.Columns[c].DataType);
  249. }
  250. int nRows = 0;
  251. string metadataValue;
  252. string dataValue;
  253. string output;
  254. // Read and display the rows
  255. while(reader.Read()) {
  256. Console.WriteLine ("Row " + nRows + ":");
  257. for(c = 0; c < reader.FieldCount; c++) {
  258. // column meta data
  259. metadataValue =
  260. " Col " +
  261. c + ": " +
  262. dt.Columns[c].ColumnName;
  263. // column data
  264. if(reader.IsDBNull(c) == true)
  265. dataValue = " is NULL";
  266. else
  267. dataValue =
  268. ": " +
  269. reader.GetValue(c);
  270. // display column meta data and data
  271. output = metadataValue + dataValue;
  272. Console.WriteLine(output);
  273. }
  274. nRows++;
  275. }
  276. reader.Close();
  277. Console.WriteLine ("Rows: " + nRows);
  278. // Call a Stored Procedure named Version()
  279. Console.WriteLine("\t\tCalling stored procedure version()");
  280. object obj = CallStoredProcedure(cnc);
  281. Console.WriteLine("Result: " + obj);
  282. /* Clean up */
  283. Console.WriteLine ("Clean up...");
  284. Console.WriteLine ("\t\tDrop table...");
  285. DropTable (cnc);
  286. Console.WriteLine("OK");
  287. }
  288. catch(Exception e) {
  289. Console.WriteLine("Exception caught: " + e);
  290. }
  291. }
  292. [STAThread]
  293. static void Main(string[] args)
  294. {
  295. SqlConnection cnc = new SqlConnection ();
  296. /*
  297. string connectionString =
  298. "host=localhost;" +
  299. "dbname=test;" +
  300. "user=userid;" +
  301. "password=password";
  302. */
  303. string connectionString =
  304. "host=localhost;" +
  305. "dbname=test;" +
  306. "user=postgres;";
  307. cnc.ConnectionString = connectionString;
  308. cnc.Open();
  309. DoPostgresTest(cnc);
  310. cnc.Close();
  311. }
  312. }
  313. }