DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
QueryTest.cs
Go to the documentation of this file.
1 //
2 // QueryTest.cs
3 //
4 // Author:
5 // Antonello Provenzano <antonello@deveel.com>
6 //
7 // Copyright (c) 2009 Deveel
8 //
9 // This program is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU Lesser General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 using System;
23 using System.Data;
24 
25 using NUnit.Framework;
26 
27 
28 namespace Deveel.Data {
29  [TestFixture]
30  public class QueryTest : TestBase {
31  [Test(Description = "Counts the people in the table 'Person' with a SQL COUNT statement")]
32  public void CountPeople() {
33  Console.Out.WriteLine("Counting the number of rows in 'Person' table...");
34 
35  // Create a Statement object to execute the queries on,
36  IDbCommand statement = Connection.CreateCommand();
37 
38  // How many rows are in the 'Person' table?
39  statement.CommandText = "SELECT COUNT(*) FROM Person";
40  IDataReader result = statement.ExecuteReader();
41  if (result.Read()) {
42  Console.Out.WriteLine("Rows in 'Person' table: " + result.GetInt32(0));
43  }
44  }
45 
46  [Test]
47  public void AvgAge() {
48  Console.Out.WriteLine("Computing the average ages of people in 'Person' table...");
49 
50  IDbCommand command = Connection.CreateCommand();
51  command.CommandText = "SELECT AVG(age) FROM Person";
52  IDataReader reader = command.ExecuteReader();
53 
54  if (reader.Read())
55  Console.Out.WriteLine("Average age of people: {0}", reader.GetDouble(0));
56 
57  reader.Close();
58  }
59 
60  [Test]
61  public void PeopleInAfrica() {
62  Console.Out.WriteLine("Selecting all the people in 'Person' table who live in Africa...");
63 
64  IDbCommand command = Connection.CreateCommand();
65  command.CommandText = "SELECT name FROM Person WHERE lives_in = 'Africa' ORDER BY name";
66 
67  IDataReader reader = command.ExecuteReader();
68  Console.Out.WriteLine("All people that live in Africa:");
69  while (reader.Read()) {
70  Console.Out.WriteLine(" " + reader.GetString(0));
71  }
72  Console.Out.WriteLine();
73  }
74 
75  [Test]
76  public void OasisOrBeatles() {
77  // List the name and music group of all the people that listen to
78  // either 'Oasis' or 'Beatles'
79  IDbCommand command = Connection.CreateCommand();
80  command.CommandText = " SELECT Person.name, MusicGroup.name " +
81  " FROM Person, ListensTo, MusicGroup " +
82  " WHERE MusicGroup.name IN ( 'Oasis', 'Beatles' ) " +
83  " AND Person.name = ListensTo.person_name " +
84  " AND ListensTo.music_group_name = MusicGroup.name " +
85  " ORDER BY MusicGroup.name, Person.name ";
86  IDataReader result = command.ExecuteReader();
87  Console.Out.WriteLine("All people that listen to either Beatles or Oasis:");
88  while (result.Read()) {
89  Console.Out.Write(" " + result.GetString(0));
90  Console.Out.Write(" listens to ");
91  Console.Out.WriteLine(result.GetString(1));
92  }
93  Console.Out.WriteLine();
94  }
95 
96  [Test]
97  public void ListIdentities() {
98  IDbCommand command = Connection.CreateCommand();
99  command.CommandText = "SELECT IDENTITY FROM Person";
100 
101  using (IDataReader reader = command.ExecuteReader()) {
102  if (reader.Read())
103  Console.Out.WriteLine("The latest identity for the table Person : {0}", reader.GetInt32(0));
104  }
105  }
106  }
107 }