Răsfoiți Sursa

Add DbUninstall

Dalibor Votruba 3 ani în urmă
părinte
comite
8366783737

+ 65 - 0
qdr.app.qlbrc.console/Commands/DbUninstallCmd.cs

@@ -0,0 +1,65 @@
+using Microsoft.Data.SqlClient;
+using Quadarax.Application.QLiberace.Base;
+using Quadarax.Application.QLiberace.Console.Commands.Base;
+using Quadarax.Foundation.Core.QConsole;
+using Quadarax.Foundation.Core.QConsole.Attributes;
+using Quadarax.Foundation.Core.Value;
+using static Quadarax.Application.QLiberace.Console.Constants.Commands;
+
+namespace Quadarax.Application.QLiberace.Console.Commands
+{
+    [CommandDefinition]
+    internal class DbUninstallCmd: DbContextCommand
+    {
+        #region *** Properties ***
+
+        public override string Name => DbUninstall.Name;
+        public override string Description => DbUninstall.Description;
+        
+        #endregion
+        #region *** Fields ***
+
+        #endregion
+
+        #region *** Constructor ***
+
+        public DbUninstallCmd(Engine engine) : base(engine)
+        {
+        }
+        #endregion
+        
+        #region *** EXECUTE ***
+        protected override Result OnExecute(QlbrcDbContext context)
+        {
+            WriteInfo("Connecting database...");
+            var resultCode = 0;
+            try
+            {
+                if (context.Database.CanConnect())
+                {
+                    WriteInfo("Deleting database...");
+                    context.Database.EnsureDeleted();
+                }
+                else
+                {
+                    resultCode = DbUninstall.ReturnCodes.DatabaseNotExists;
+                    WriteWarning("Database not exists.");
+                }
+
+            }
+            catch (SqlException e)
+            {
+                return HandleConnectionException(e);
+            }
+
+            return new Result(resultCode);
+        }
+
+        
+        #endregion
+
+        #region *** Private operations ***
+        #endregion
+    }
+
+}

+ 13 - 0
qdr.app.qlbrc.console/Constants.cs

@@ -73,6 +73,19 @@
                 {
                 }
             }
+            internal class DbUninstall
+            {
+                public const string Name = "db_uninstall";
+                public const string Description = "Drop database";
+
+                internal class Arguments
+                {
+                }
+                public class ReturnCodes : Constants.ReturnCodes
+                {
+                    public const int DatabaseNotExists = 1;
+                }
+            }
         }
     }
 }

+ 4 - 0
qdr.app.qlbrc.console/Properties/launchSettings.json

@@ -11,6 +11,10 @@
     "db_clear": {
       "commandName": "Project",
       "commandLineArgs": "db_clear"
+    },
+    "db_uninstall": {
+      "commandName": "Project",
+      "commandLineArgs": "db_uninstall"
     }
   }
 }

+ 16 - 0
qdr.app.qlbrc.console/readme.txt

@@ -73,4 +73,20 @@ db_clear
 	exit codes:
 		0		Ok		
 		10		Cannot connect to server				
+		100		Other unhandled exception
+
+db_uninstall
+----------
+	description:
+		Delete database if exists.
+
+	arguments mandatory:
+
+	arguments optional:
+		
+
+	exit codes:
+		0		Ok		
+		1		Database doesn't exists		
+		10		Cannot connect to server				
 		100		Other unhandled exception