#region License
// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
#endregion
using System;
using System.Globalization;
using System.Text;
using MiniSqlQuery.Core;
using MiniSqlQuery.Core.DbModel;
namespace MiniSqlQuery.PlugIns.TemplateViewer
{
/// The template host.
public class TemplateHost : IDisposable
{
/// The _database inspector.
private readonly IDatabaseInspector _databaseInspector;
private TemplateData _data;
/// Initializes a new instance of the class.
/// The application services.
/// The database inspector.
public TemplateHost(IApplicationServices applicationServices, IDatabaseInspector databaseInspector)
{
Services = applicationServices;
_databaseInspector = databaseInspector;
}
/// Gets or sets Data.
public TemplateData Data
{
get
{
if (_data == null)
{
_data = Services.Resolve();
}
return _data;
}
set { _data = value; }
}
/// Gets MachineName.
public string MachineName
{
get { return Environment.MachineName; }
}
/// Gets Model.
public DbModelInstance Model
{
get
{
if (_databaseInspector.DbSchema == null)
{
_databaseInspector.LoadDatabaseDetails();
}
return _databaseInspector.DbSchema;
}
}
/// Gets Services.
public IApplicationServices Services { get; private set; }
/// Gets UserName.
public string UserName
{
get { return Environment.UserName; }
}
// to do - helper functions for changing names too code friendly etc
/// The date.
/// The format.
/// The date.
public string Date(string format)
{
return DateTime.Now.ToString(format);
}
/// The to camel case.
/// The text.
/// The to camel case.
public string ToCamelCase(string text)
{
if (text == null)
{
return string.Empty;
}
StringBuilder sb = new StringBuilder();
text = ToPascalCase(text);
for (int i = 0; i < text.Length; i++)
{
if (Char.IsUpper(text, i))
{
sb.Append(Char.ToLower(text[i]));
}
else
{
// allows for names that start with an acronym, e.g. "ABCCode" -> "abcCode"
if (i > 1)
{
i--; // reverse one
sb.Remove(i, 1); // drop last lower cased char
}
sb.Append(text.Substring(i));
break;
}
}
return sb.ToString();
}
/// The to pascal case.
/// The text.
/// The to pascal case.
public string ToPascalCase(string text)
{
if (text == null)
{
return string.Empty;
}
if (text.Contains(" ") || text.Contains("_"))
{
text = text.Replace("_", " ");
text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text);
text = text.Replace(" ", string.Empty);
}
else if (text.Length > 1)
{
text = char.ToUpper(text[0]) + text.Substring(1);
}
return text;
}
/// The dispose.
public void Dispose()
{
if (Data != null)
{
Data.Dispose();
}
}
}
}