1 /* PostgresTest.cs - based on the postgres-test.c in libgda
3 * Copyright (C) 2002 Gonzalo Paniagua Javier
4 * Copyright (C) 2002 Daniel Morgan
7 * Gonzalo Paniagua Javier <gonzalo@gnome-db.org>
8 * PORTING FROM C TO C# AUTHOR:
9 * Daniel Morgan <danmorg@sc.rr.com>
11 * Permission was given from the original author, Gonzalo Paniagua Javier,
12 * to port and include his original work in Mono.
14 * The original work falls under the LGPL, but the port to C# falls
15 * under the X11 license.
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.
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.
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.
35 using System.Data.SqlClient;
37 namespace TestSystemDataSqlClient {
41 static void CreateTable (IDbConnection cnc) {
43 IDbCommand createCommand = cnc.CreateCommand();
45 createCommand.CommandText =
46 "create table mono_postgres_test (" +
47 "boolean_value boolean, " +
48 "int2_value smallint, " +
49 "int4_value integer, " +
50 "bigint_value bigint, " +
51 "float_value real, " +
52 "double_value double precision, " +
53 "numeric_value numeric(15, 3), " +
54 "char_value char(50), " +
55 "varchar_value varchar(20), " +
57 "point_value point, " +
60 "timestamp_value timestamp, " +
61 "null_boolean_value boolean, " +
62 "null_int2_value smallint, " +
63 "null_int4_value integer, " +
64 "null_bigint_value bigint, " +
65 "null_float_value real, " +
66 "null_double_value double precision, " +
67 "null_numeric_value numeric(15, 3), " +
68 "null_char_value char(50), " +
69 "null_varchar_value varchar(20), " +
70 "null_text_value text, " +
71 "null_point_value point, " +
72 "null_time_value time, " +
73 "null_date_value date, " +
74 "null_timestamp_value timestamp " +
77 createCommand.ExecuteNonQuery ();
80 static void DropTable (IDbConnection cnc) {
82 IDbCommand dropCommand = cnc.CreateCommand ();
84 dropCommand.CommandText =
85 "drop table mono_postgres_test";
87 dropCommand.ExecuteNonQuery ();
90 static object CallStoredProcedure (IDbConnection cnc) {
92 IDbCommand callStoredProcCommand = cnc.CreateCommand ();
95 callStoredProcCommand.CommandText =
98 data = callStoredProcCommand.ExecuteScalar ();
103 static void InsertData (IDbConnection cnc) {
105 IDbCommand insertCommand = cnc.CreateCommand();
107 insertCommand.CommandText =
108 "insert into mono_postgres_test (" +
121 "timestamp_value, " +
127 "123456789012345, " +
129 "3.1415926969696, " +
130 "123456789012.345, " +
131 "'This is a char', " +
132 "'This is a varchar', " +
133 "'This is a text', " +
136 "'2004-02-29 14:00:11.31', " +
140 insertCommand.ExecuteNonQuery ();
143 static IDataReader SelectData (IDbConnection cnc) {
145 IDbCommand selectCommand = cnc.CreateCommand();
148 // FIXME: System.Data classes need to handle NULLs
149 // FIXME: System.Data needs to handle more data types
151 selectCommand.CommandText =
153 "from mono_postgres_test";
156 selectCommand.CommandText =
171 "timestamp_value, " +
172 "null_boolean_value, " +
173 "null_int2_value, " +
174 "null_int4_value, " +
175 "null_bigint_value, " +
176 "null_float_value, " +
177 "null_double_value, " +
178 "null_numeric_value, " +
179 "null_char_value, " +
180 "null_varchar_value, " +
181 "null_text_value, " +
182 "null_point_value, " +
183 "null_time_value, " +
184 "null_date_value, " +
185 "null_timestamp_value " +
186 "from mono_postgres_test";
188 reader = selectCommand.ExecuteReader ();
193 static void UpdateData (IDbConnection cnc) {
195 IDbCommand updateCommand = cnc.CreateCommand();
197 updateCommand.CommandText =
198 "update mono_postgres_test " +
200 "boolean_value = 'F', " +
203 "bigint_value = 9, " +
204 "char_value = 'Mono.Data!' , " +
205 "varchar_value = 'It was not me!', " +
206 "text_value = 'We got data!' " +
207 "where int2_value = -22";
209 updateCommand.ExecuteNonQuery ();
212 // used to do a min(), max(), count(), sum(), or avg()
213 static object SelectAggregate (IDbConnection cnc, String agg) {
215 IDbCommand selectCommand = cnc.CreateCommand();
218 Console.WriteLine("Aggregate: " + agg);
220 selectCommand.CommandText =
222 "from mono_postgres_test";
224 data = selectCommand.ExecuteScalar ();
226 Console.WriteLine("Agg Result: " + data);
231 /* Postgres provider tests */
232 static void DoPostgresTest (IDbConnection cnc) {
237 Console.WriteLine ("\tPostgres provider specific tests...\n");
239 /* Drops the gda_postgres_test table. */
240 Console.WriteLine ("\t\tDrop table: ");
243 Console.WriteLine ("OK");
245 catch (SqlException e) {
246 Console.WriteLine("Error (don't worry about this one)" + e);
250 /* Creates a table with all supported data types */
251 Console.WriteLine ("\t\tCreate table with all supported types: ");
253 Console.WriteLine ("OK");
256 Console.WriteLine ("\t\tInsert values for all known types: ");
258 Console.WriteLine ("OK");
261 Console.WriteLine ("\t\tUpdate values: ");
263 Console.WriteLine ("OK");
266 Console.WriteLine ("\t\tInsert values for all known types: ");
268 Console.WriteLine ("OK");
270 /* Select aggregates */
271 SelectAggregate (cnc, "count(*)");
272 //SelectAggregate (cnc, "avg(int4_value)");
273 SelectAggregate (cnc, "min(text_value)");
274 SelectAggregate (cnc, "max(int4_value)");
275 SelectAggregate (cnc, "sum(int4_value)");
278 Console.WriteLine ("\t\tSelect values from the database: ");
279 reader = SelectData (cnc);
281 // get the DataTable that holds
\r
283 Console.WriteLine("\t\tGet Schema.");
\r
284 DataTable dt = reader.GetSchemaTable();
\r
286 // number of columns in the table
\r
287 Console.WriteLine("dt.Columns.Count: " +
\r
291 // display the schema
\r
292 for(c = 0; c < dt.Columns.Count; c++) {
\r
293 Console.WriteLine("* Column Name: " +
\r
294 dt.Columns[c].ColumnName);
\r
295 Console.WriteLine(" MaxLength: " +
\r
296 dt.Columns[c].MaxLength);
\r
297 Console.WriteLine(" Type: " +
\r
298 dt.Columns[c].DataType);
\r
302 string metadataValue;
\r
305 // Read and display the rows
\r
306 while(reader.Read()) {
\r
307 Console.WriteLine ("Row " + nRows + ":");
\r
308 for(c = 0; c < reader.FieldCount; c++) {
\r
310 // column meta data
\r
314 dt.Columns[c].ColumnName;
\r
317 if(reader.IsDBNull(c) == true)
\r
318 dataValue = " is NULL";
\r
322 reader.GetValue(c);
\r
324 // display column meta data and data
\r
325 output = metadataValue + dataValue;
\r
326 Console.WriteLine(output);
\r
332 Console.WriteLine ("Rows: " + nRows);
\r
334 // Call a Stored Procedure named Version()
\r
335 Console.WriteLine("\t\tCalling stored procedure version()");
\r
336 object obj = CallStoredProcedure(cnc);
\r
337 Console.WriteLine("Result: " + obj);
\r
339 Console.WriteLine("Database Server Version: " +
\r
340 ((SqlConnection)cnc).ServerVersion);
\r
343 Console.WriteLine ("Clean up...");
344 Console.WriteLine ("\t\tDrop table...");
346 Console.WriteLine("OK");
349 Console.WriteLine("Exception caught: " + e);
354 static void Main(string[] args)
\r
356 SqlConnection cnc = new SqlConnection ();
\r
359 string connectionString =
366 string connectionString =
371 cnc.ConnectionString = connectionString;
\r