PostgresTest.cs 9.8 KB

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