Skip to content

Commit 3dc34c5

Browse files
author
Roberto Garcia
committed
WL10034 DEV API Views DDL Support.
Implemented Drop View support and unit tests.
1 parent b72b25c commit 3dc34c5

File tree

6 files changed

+83
-4
lines changed

6 files changed

+83
-4
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
7.0.7
22
- MySqlX: Fixed configuration handling paths in Linux and MacOS (Oracle Bug #25423724).
33
- MySqlX: Fixed Dispose error in Linux and MacOS (Oracle Bug #25423700).
4+
- MySqlX: Added support for creating, modifying, and dropping Views.
45

56

67
7.0.6

Source/MySql.Data/X/Protocol/X/XProtocol.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,5 +574,13 @@ internal void SendModifyView(string schema, string name, string definer, ViewAlg
574574
}
575575
_writer.Write((int)ClientMessages.Types.Type.CrudModifyView, builder);
576576
}
577+
578+
internal void SendDropView(string schema, string name, bool ifExists)
579+
{
580+
var builder = new DropView();
581+
builder.Collection = ExprUtil.BuildCollection(schema, name);
582+
builder.IfExists = ifExists;
583+
_writer.Write((int)ClientMessages.Types.Type.CrudDropView, builder);
584+
}
577585
}
578586
}

Source/MySql.Data/X/Session/XInternalSession.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,5 +347,12 @@ public Result ViewAlter(ViewAlterStatement statement)
347347
statement.columns, statement.queryStatement);
348348
return new Result(this);
349349
}
350+
351+
public Result ViewDrop(ViewDropStatement statement)
352+
{
353+
protocol.SendDropView(statement.Target.Schema.Name,
354+
statement.name, statement.ifExists);
355+
return new Result(this);
356+
}
350357
}
351358
}

Source/MySql.Data/X/XDevAPI/Common/ViewDropStatement.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,34 @@
2727

2828
namespace MySqlX.XDevAPI.Common
2929
{
30-
public class ViewDropStatement : BaseStatement<Result>
30+
public class ViewDropStatement : TargetedBaseStatement<Schema, Result>
3131
{
32-
internal ViewDropStatement(BaseSession session, string name) : base(session)
32+
internal string name;
33+
internal bool ifExists = false;
34+
35+
internal ViewDropStatement(Schema schema, string name) : base(schema)
3336
{
37+
this.name = name;
38+
}
3439

40+
/// <summary>
41+
/// Supress error if the view to drop doesn't exist.
42+
/// </summary>
43+
/// <returns></returns>
44+
public ViewDropStatement IfExists()
45+
{
46+
ifExists = true;
47+
return this;
3548
}
3649

50+
/// <summary>
51+
/// Executes the view drop statement
52+
/// </summary>
53+
/// <returns>Result of execution</returns>
3754
public override Result Execute()
3855
{
39-
throw new NotImplementedException();
56+
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException("Name");
57+
return Session.XSession.ViewDrop(this);
4058
}
4159
}
4260
}

Source/MySql.Data/X/XDevAPI/Schema.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public ViewAlterStatement AlterView(string name)
192192
/// <returns>ViewDrop chaining object</returns>
193193
public ViewDropStatement DropView(string name)
194194
{
195-
return new ViewDropStatement(Session, name);
195+
return new ViewDropStatement(this, name);
196196
}
197197

198198
#endregion

Tests/MySqlX.Data.Tests/CrudViewsTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,21 @@ public void CreateViewNullValidations()
173173
Assert.Equal("Definer", Assert.ThrowsAny<ArgumentNullException>(() => db.CreateView("myview").DefinedAs(table.Select()).Definer(null).Execute()).ParamName);
174174
}
175175

176+
[Fact]
177+
public void CreateViewReplace()
178+
{
179+
CreateBasicViewFromTable();
180+
Schema db = GetSession().Schema;
181+
Table table = db.GetTable(tableName);
182+
db.CreateView("myview", true)
183+
.DefinedAs(table.Select("id"))
184+
.Execute();
185+
186+
var result = GetNodeSession().SQL("DESC myview").Execute().FetchAll();
187+
Assert.Equal(1, result.Count);
188+
Assert.Equal("id", result[0]["field"]);
189+
}
190+
176191
#endregion
177192

178193
#region Alter View
@@ -225,5 +240,35 @@ public void AlterViewFromCollection()
225240
}
226241

227242
#endregion
243+
244+
#region Drop View
245+
246+
[Fact]
247+
public void DropTableView()
248+
{
249+
CreateBasicViewFromTable();
250+
Assert.True(GetSession().Schema.GetTable("myview").ExistsInDatabase());
251+
GetSession().Schema.DropView("myview").Execute();
252+
Assert.False(GetSession().Schema.GetTable("myview").ExistsInDatabase());
253+
}
254+
255+
[Fact]
256+
public void DropCollectionView()
257+
{
258+
CreateBasicViewFromCollection();
259+
Assert.True(GetSession().Schema.GetTable("myview").ExistsInDatabase());
260+
GetSession().Schema.DropView("myview").Execute();
261+
Assert.False(GetSession().Schema.GetTable("myview").ExistsInDatabase());
262+
}
263+
264+
[Fact]
265+
public void DropViewIfExists()
266+
{
267+
Assert.False(GetSession().Schema.GetTable("viewNotExists").ExistsInDatabase());
268+
Assert.ThrowsAny<MySqlException>(() => GetSession().Schema.DropView("viewNotExists").Execute());
269+
GetSession().Schema.DropView("viewNotExists").IfExists().Execute();
270+
}
271+
272+
#endregion
228273
}
229274
}

0 commit comments

Comments
 (0)