Skip to content

Commit 9547729

Browse files
committed
Shorten query command names.
1 parent 95dc6c1 commit 9547729

11 files changed

Lines changed: 122 additions & 82 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using CommandLine;
2+
using Serilog;
3+
4+
namespace Unitfly.MFiles.DevTools.SqlGenerator.App.Commands
5+
{
6+
[Verb("create", HelpText = "Generate sql CREATE query for an M-Files class.")]
7+
public class CreateQuery : Query
8+
{
9+
protected override void PrintQuery(Table table)
10+
{
11+
Log.Information(table.CreateQuery);
12+
}
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using CommandLine;
2+
using Serilog;
3+
4+
namespace Unitfly.MFiles.DevTools.SqlGenerator.App.Commands
5+
{
6+
[Verb("delete", HelpText = "Generate sql DELETE query for an M-Files class.")]
7+
public class DeleteQuery : Query
8+
{
9+
protected override void PrintQuery(Table table)
10+
{
11+
Log.Information(table.DeleteQuery);
12+
}
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using CommandLine;
2+
using Serilog;
3+
4+
namespace Unitfly.MFiles.DevTools.SqlGenerator.App.Commands
5+
{
6+
[Verb("insert", HelpText = "Generate sql INSERT query for an M-Files class.")]
7+
public class InsertQuery : Query
8+
{
9+
protected override void PrintQuery(Table table)
10+
{
11+
Log.Information(table.InsertIntoQuery);
12+
}
13+
}
14+
}

Unitfly.MFiles.DevTools.SqlGenerator.App/Commands/Login.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public static int Execute(ref AppSettings appSettings, ref SqlGenerator Converte
1313
{
1414
try
1515
{
16-
Converter = new SqlGenerator(loginType: appSettings.Vault.LoginType,
16+
Converter = new SqlGenerator(Log.Logger,
17+
loginType: appSettings.Vault.LoginType,
1718
vaultName: appSettings.Vault.VaultName,
1819
username: appSettings.Vault.Username,
1920
password: appSettings.Vault.Password,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using CommandLine;
2+
using Serilog;
3+
using System;
4+
using Unitfly.MFiles.DevTools.SqlGenerator.App.Configuration;
5+
6+
namespace Unitfly.MFiles.DevTools.SqlGenerator.App.Commands
7+
{
8+
public abstract class Query
9+
{
10+
[Value(0, MetaName = "Class", Required = true, HelpText = "M-Files class to generate sql query for.")]
11+
public string Class { get; set; }
12+
13+
public int Execute(ref AppSettings appSettings, ref SqlGenerator converter, Query opts)
14+
{
15+
try
16+
{
17+
if (Login.Execute(ref appSettings, ref converter) != 0)
18+
{
19+
return 1;
20+
}
21+
22+
var table = converter.ConvertClassToTable(opts.Class, appSettings.ItemNameTransform.CasingConverter, appSettings.IgnoreBuiltinProperties);
23+
PrintQuery(table);
24+
}
25+
catch (Exception e)
26+
{
27+
Log.Error(e, "Error generating sql queries for class.");
28+
return 1;
29+
}
30+
return 0;
31+
}
32+
33+
protected abstract void PrintQuery(Table table);
34+
}
35+
}

Unitfly.MFiles.DevTools.SqlGenerator.App/Commands/Run.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using CommandLine;
2+
using Serilog;
3+
4+
namespace Unitfly.MFiles.DevTools.SqlGenerator.App.Commands
5+
{
6+
[Verb("update", HelpText = "Generate sql UPDATE query for an M-Files class.")]
7+
public class UpdateQuery : Query
8+
{
9+
protected override void PrintQuery(Table table)
10+
{
11+
Log.Information(table.UpdateQuery);
12+
}
13+
}
14+
}

Unitfly.MFiles.DevTools.SqlGenerator.App/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ static void Main(string[] args)
3333
: new string[] { input };
3434

3535
Parser.Default
36-
.ParseArguments<Exit, Login, CreateQuery, UpdateQuery, InsertQuery, DeleteQuery, Reload, Print>(arg)
36+
.ParseArguments<Login, CreateQuery, UpdateQuery, InsertQuery, DeleteQuery, Print, Reload, Exit>(arg)
3737
.MapResult(
3838
(Login opts) => Login.Execute(ref AppSettings, ref Generator, opts),
3939
(CreateQuery opts) => new CreateQuery().Execute(ref AppSettings, ref Generator, opts),
4040
(UpdateQuery opts) => new UpdateQuery().Execute(ref AppSettings, ref Generator, opts),
4141
(InsertQuery opts) => new InsertQuery().Execute(ref AppSettings, ref Generator, opts),
4242
(DeleteQuery opts) => new DeleteQuery().Execute(ref AppSettings, ref Generator, opts),
43-
(Reload opts) => Reload.Execute(ref AppSettings, ref Generator, opts),
4443
(Print opts) => Print.Execute(ref AppSettings, opts),
44+
(Reload opts) => Reload.Execute(ref AppSettings, ref Generator, opts),
4545
(Exit opts) => Exit.Execute(opts),
4646
errs => 1);
4747

Unitfly.MFiles.DevTools.SqlGenerator.App/Unitfly.MFiles.DevTools.SqlGenerator.App.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,20 @@
103103
<Reference Include="System.Xml" />
104104
</ItemGroup>
105105
<ItemGroup>
106+
<Compile Include="Commands\CreateQuery.cs" />
107+
<Compile Include="Commands\InsertQuery.cs" />
106108
<Compile Include="Commands\Print.cs" />
109+
<Compile Include="Commands\Query.cs" />
107110
<Compile Include="Commands\Reload.cs" />
111+
<Compile Include="Commands\UpdateQuery.cs" />
108112
<Compile Include="Configuration\AppSettings.cs" />
109113
<Compile Include="Commands\Exit.cs" />
110114
<Compile Include="Commands\Login.cs" />
111115
<Compile Include="Configuration\NameTransform.cs" />
112116
<Compile Include="Configuration\Vault.cs" />
113117
<Compile Include="Program.cs" />
114118
<Compile Include="Properties\AssemblyInfo.cs" />
115-
<Compile Include="Commands\Run.cs" />
119+
<Compile Include="Commands\DeleteQuery.cs" />
116120
</ItemGroup>
117121
<ItemGroup>
118122
<None Include="App.config" />

Unitfly.MFiles.DevTools.SqlGenerator/SqlGenerator.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,48 @@
33
using System.Data;
44
using System.Linq;
55
using MFilesAPI;
6+
using Serilog;
67
using Unitfly.MFiles.DevTools.Common;
78
using Unitfly.MFiles.DevTools.Common.CaseConverters;
89

910
namespace Unitfly.MFiles.DevTools.SqlGenerator
1011
{
1112
public class SqlGenerator : ServerApplication
1213
{
13-
public SqlGenerator(LoginType loginType, string vaultName, string username, string password, string domain = null,
14-
string protocolSequence = "ncacn_ip_tcp", string networkAddress = "localhost", string endpoint = "2266",
15-
bool encryptedConnection = false, string localComputerName = "") :
16-
base(loginType, vaultName, username, password, domain, protocolSequence,
14+
public SqlGenerator(ILogger logger, LoginType loginType, string vaultName, string username, string password, string domain = null,
15+
string protocolSequence = "ncacn_ip_tcp", string networkAddress = "localhost", string endpoint = "2266",
16+
bool encryptedConnection = false, string localComputerName = "") :
17+
base(loginType, vaultName, username, password, domain, protocolSequence,
1718
networkAddress, endpoint, encryptedConnection, localComputerName)
1819
{
20+
Log.Logger = logger;
21+
Log.Information("Logged in to vault {vault} as {loginType} user {user}.",
22+
vaultName, loginType, string.IsNullOrWhiteSpace(domain) ? username : $"{domain}\\{username}");
1923
}
2024

2125
public Table ConvertClassToTable(string @class, CaseConverter converter, bool ignoreBuiltinProperties)
2226
{
23-
var classObj = Vault.ClassOperations.GetAllObjectClasses().Cast<ObjectClass>().FirstOrDefault(c => c.Name.ToLower() == @class.ToLower());
27+
var classObj = Vault.ClassOperations.GetAllObjectClasses()?
28+
.Cast<ObjectClass>()?
29+
.FirstOrDefault(c => c?.Name?.ToLower() == @class?.ToLower());
30+
2431
if (classObj is null)
2532
{
2633
throw new Exception($"Unable to fetch class {@class} from vault.");
2734
}
35+
2836
return ConvertClassToTable(classObj, converter, ignoreBuiltinProperties);
2937
}
3038

3139
public Table ConvertClassToTable(ObjectClass @class, CaseConverter converter, bool ignoreBuiltinProperties)
3240
{
3341
var propertyDefs = Vault.PropertyDefOperations.GetPropertyDefs().Cast<PropertyDef>();
34-
return new Table(
42+
var table = new Table(
3543
GetTableName(converter, @class),
3644
GetTableColumns(converter, @class, propertyDefs, ignoreBuiltinProperties)
3745
);
46+
Log.Debug("Converted M-Files class {class} to an sql table.", @class?.Name);
47+
return table;
3848
}
3949

4050
public IEnumerable<Table> ConvertAllClassesToTables(CaseConverter converter, bool ignoreBuiltinProperties)
@@ -48,8 +58,9 @@ public IEnumerable<Table> ConvertAllClassesToTables(CaseConverter converter, boo
4858
{
4959
result.Add(ConvertClassToTable(objClass, converter, ignoreBuiltinProperties));
5060
}
51-
catch (Exception)
61+
catch (Exception e)
5262
{
63+
Log.Warning(e, "Error converting M-Files class {class} to an sql table.", objClass?.Name);
5364
continue;
5465
}
5566
}

0 commit comments

Comments
 (0)