|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Apache.Ignite.Examples.Datagrid |
| 19 | +{ |
| 20 | + using System; |
| 21 | + using Apache.Ignite.Core; |
| 22 | + using Apache.Ignite.Core.Cache; |
| 23 | + using Apache.Ignite.Core.Cache.Configuration; |
| 24 | + using Apache.Ignite.Core.Cache.Query; |
| 25 | + using Apache.Ignite.ExamplesDll.Binary; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// This example showcases DML capabilities of Ignite's SQL engine. |
| 29 | + /// <para /> |
| 30 | + /// 1) Build the project Apache.Ignite.ExamplesDll (select it -> right-click -> Build). |
| 31 | + /// Apache.Ignite.ExamplesDll.dll must appear in %IGNITE_HOME%/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/bin/${Platform]/${Configuration} folder. |
| 32 | + /// 2) Set this class as startup object (Apache.Ignite.Examples project -> right-click -> Properties -> |
| 33 | + /// Application -> Startup object); |
| 34 | + /// 3) Start example (F5 or Ctrl+F5). |
| 35 | + /// <para /> |
| 36 | + /// This example can be run with standalone Apache Ignite.NET node: |
| 37 | + /// 1) Run %IGNITE_HOME%/platforms/dotnet/bin/Apache.Ignite.exe: |
| 38 | + /// Apache.Ignite.exe -configFileName=platforms\dotnet\examples\apache.ignite.examples\app.config -assembly=[path_to_Apache.Ignite.ExamplesDll.dll] |
| 39 | + /// 2) Start example. |
| 40 | + /// </summary> |
| 41 | + public class QueryDmlExample |
| 42 | + { |
| 43 | + /// <summary>Organization cache name.</summary> |
| 44 | + private const string OrganizationCacheName = "dotnet_cache_query_dml_organization"; |
| 45 | + |
| 46 | + /// <summary>Employee cache name.</summary> |
| 47 | + private const string EmployeeCacheName = "dotnet_cache_query_dml_employee"; |
| 48 | + |
| 49 | + [STAThread] |
| 50 | + public static void Main() |
| 51 | + { |
| 52 | + using (var ignite = Ignition.StartFromApplicationConfiguration()) |
| 53 | + { |
| 54 | + Console.WriteLine(); |
| 55 | + Console.WriteLine(">>> Cache query DML example started."); |
| 56 | + |
| 57 | + var employeeCache = ignite.GetOrCreateCache<int, Employee>( |
| 58 | + new CacheConfiguration(EmployeeCacheName, new QueryEntity(typeof(int), typeof(Employee)))); |
| 59 | + |
| 60 | + var organizationCache = ignite.GetOrCreateCache<int, Organization>(new CacheConfiguration( |
| 61 | + OrganizationCacheName, new QueryEntity(typeof(int), typeof(Organization)))); |
| 62 | + |
| 63 | + Insert(organizationCache, employeeCache); |
| 64 | + Select(employeeCache, "Inserted data"); |
| 65 | + |
| 66 | + Update(employeeCache); |
| 67 | + Select(employeeCache, "Update salary for ASF employees"); |
| 68 | + |
| 69 | + Delete(employeeCache); |
| 70 | + Select(employeeCache, "Delete non-ASF employees"); |
| 71 | + |
| 72 | + Console.WriteLine(); |
| 73 | + } |
| 74 | + |
| 75 | + Console.WriteLine(); |
| 76 | + Console.WriteLine(">>> Example finished, press any key to exit ..."); |
| 77 | + Console.ReadKey(); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Selects and displays Employee data. |
| 82 | + /// </summary> |
| 83 | + /// <param name="employeeCache">Employee cache.</param> |
| 84 | + /// <param name="message">Message.</param> |
| 85 | + private static void Select(ICache<int, Employee> employeeCache, string message) |
| 86 | + { |
| 87 | + Console.WriteLine("\n>>> {0}", message); |
| 88 | + |
| 89 | + var qry = new SqlFieldsQuery(string.Format( |
| 90 | + "select emp._key, emp.name, org.name, emp.salary " + |
| 91 | + "from Employee as emp, " + |
| 92 | + "\"{0}\".Organization as org " + |
| 93 | + "where emp.organizationId = org._key", OrganizationCacheName)); |
| 94 | + |
| 95 | + using (var cursor = employeeCache.QueryFields(qry)) |
| 96 | + { |
| 97 | + foreach (var row in cursor) |
| 98 | + { |
| 99 | + Console.WriteLine(">>> {0}: {1}, {2}, {3}", row[0], row[1], row[2], row[3]); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Populate cache with test data. |
| 106 | + /// </summary> |
| 107 | + /// <param name="organizationCache">Organization cache.</param> |
| 108 | + /// <param name="employeeCache">Employee cache.</param> |
| 109 | + private static void Insert(ICache<int, Organization> organizationCache, ICache<int, Employee> employeeCache) |
| 110 | + { |
| 111 | + // Insert organizations. |
| 112 | + var qry = new SqlFieldsQuery("insert into Organization (_key, name) values (?, ?)", 1, "ASF"); |
| 113 | + organizationCache.QueryFields(qry); |
| 114 | + |
| 115 | + qry.Arguments = new object[] {2, "Eclipse"}; |
| 116 | + organizationCache.QueryFields(qry); |
| 117 | + |
| 118 | + // Insert employees. |
| 119 | + qry = new SqlFieldsQuery("insert into Employee (_key, name, organizationId, salary) values (?, ?, ?, ?)"); |
| 120 | + |
| 121 | + qry.Arguments = new object[] {1, "John Doe", 1, 4000}; |
| 122 | + employeeCache.QueryFields(qry); |
| 123 | + |
| 124 | + qry.Arguments = new object[] {2, "Jane Roe", 1, 5000}; |
| 125 | + employeeCache.QueryFields(qry); |
| 126 | + |
| 127 | + qry.Arguments = new object[] {3, "Mary Major", 2, 2000}; |
| 128 | + employeeCache.QueryFields(qry); |
| 129 | + |
| 130 | + qry.Arguments = new object[] {4, "Richard Miles", 2, 3000}; |
| 131 | + employeeCache.QueryFields(qry); |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Conditional UPDATE query: raise salary for ASF employees. |
| 136 | + /// </summary> |
| 137 | + /// <param name="employeeCache">Employee cache.</param> |
| 138 | + private static void Update(ICache<int, Employee> employeeCache) |
| 139 | + { |
| 140 | + var qry = new SqlFieldsQuery("update Employee set salary = salary * 1.1 where organizationId = ?", 1); |
| 141 | + |
| 142 | + employeeCache.QueryFields(qry); |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Conditional DELETE query: remove non-ASF employees. |
| 147 | + /// </summary> |
| 148 | + /// <param name="employeeCache">Employee cache.</param> |
| 149 | + private static void Delete(ICache<int, Employee> employeeCache) |
| 150 | + { |
| 151 | + var qry = new SqlFieldsQuery(string.Format( |
| 152 | + "delete from Employee where _key in (" + |
| 153 | + "select emp._key from Employee emp, \"{0}\".Organization org " + |
| 154 | + "where org.Name != ? and org._key = emp.organizationId)", OrganizationCacheName), "ASF"); |
| 155 | + |
| 156 | + employeeCache.QueryFields(qry); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments