Example #1
0
        public string ToTypeName(ISqlFormater formater)
        {
            var ef     = EnumCache.Get <DataFormat>();
            var format = formater.ToDataFormat(DataType);
            var ei     = ef.Get(format.ToStringX());
            var type   = ei.Description;

            return(type);
        }
Example #2
0
 public static string ToStr <T>(T key)
 {
     if (key is string str)
     {
         return(str);
     }
     if (typeof(T) == typeof(object))
     {
         return(key.GetType().FullName + "-" + key.ToString());
     }
     if (EnumCache <T> .IsEnum)
     {
         return(EnumCache <T> .Get(key));
     }
     else
     {
         return(key.GetType().FullName + "-" + key.ToString());
     }
 }
Example #3
0
        private string GeneratePackageSchema(string name)
        {
            var pattern =
                @"// {PackageClassName}.cs
//
// Generated by Kuicker.Tools.EntityGenerator.
// {Today}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Kuicker;
using Kuicker.Data;

namespace {Namespace}
{
	public partial class {PackageClassName} : IPackage
	{
		public static string __PackageName
		{
			get
			{
				return ""{PackageName}"";
			}
		}
{Procedures}
	}
}";
            var now              = DateTime.Now.yyyy_MM_dd_HH_mm_ss_fff();
            var nameSpace        = txtNamespace.Text;
            var packageName      = name;
            var packageClassName = packageName
                                   .TrimStart(txtPackageTrimPrefix.Text);

            packageClassName = NamingFilter(packageClassName)
                               .AppendSuffix(txtPackageAppendSuffix.Text);
            if (packageClassName.IsNullOrEmpty())
            {
                packageClassName = packageName;
            }

            var packageSchema = PackageSchemas.FirstOrDefault(x =>
                                                              x.PackageName == packageName
                                                              );

            var ef           = EnumCache.Get <DataFormat>();
            var sbProcedures = new StringBuilder();

            foreach (var procedure in packageSchema.Procedures)
            {
                var propertyName = procedure.ProcedureName
                                   .TrimStart(txtProcedureTrimPrefix.Text);
                propertyName = NamingFilter(propertyName);
                if (propertyName.IsNullOrEmpty())
                {
                    propertyName = procedure.ProcedureName;
                }

                if (sbProcedures.Length > 0)
                {
                    sbProcedures.AppendLine();
                }

                sbProcedures.AppendFormat(@"
		#region {2}
		/// <summary>
		/// {0}
		/// </summary>
		public static {1} {2}({3})
		{{
			return {2}(
				new EntityApi(__PackageName){4}
			);
		}}

		public static {1} {2}(
			EntityApi api{5})
		{{
			return api.{6}(
				""{7}""{8}
			){9};
		}}
		#endregion"        ,
                                          // 0
                                          procedure.DbFullName,
                                          // 1
                                          procedure.ToOutType(Formater),
                                          // 2
                                          propertyName,
                                          // 3
                                          (!procedure.Ins.IsNullOrEmpty()).If(
                                              procedure
                                              .Ins
                                              .Select(x => x.ToMethodArgument(Formater))
                                              .Join(
                                                  "," + Environment.NewLine,
                                                  "			",
                                                  string.Empty
                                                  )
                                              .AppendPrefix(Environment.NewLine)
                                              ),
                                          // 4
                                          (!procedure.Ins.IsNullOrEmpty()).If(
                                              procedure
                                              .Ins
                                              .Select(x => x.ArgumentName)
                                              .Join(
                                                  "," + Environment.NewLine,
                                                  "				",
                                                  string.Empty
                                                  )
                                              .AppendPrefix("," + Environment.NewLine)
                                              ),
                                          // 5
                                          (!procedure.Ins.IsNullOrEmpty()).If(
                                              procedure
                                              .Ins
                                              .Select(x => x.ToMethodArgument(Formater))
                                              .Join(
                                                  "," + Environment.NewLine,
                                                  "			",
                                                  string.Empty
                                                  )
                                              .AppendPrefix("," + Environment.NewLine)
                                              ),
                                          // 6
                                          procedure.ToApiMethod(),
                                          // 7
                                          procedure.ProcedureName,
                                          // 8
                                          (!procedure.Ins.IsNullOrEmpty()).If(
                                              procedure
                                              .Ins
                                              .Select(x => string.Format(
                                                          @"new Any(""{0}"", {0})",
                                                          x.ArgumentName
                                                          ))
                                              .Join(
                                                  "," + Environment.NewLine,
                                                  "				",
                                                  string.Empty
                                                  )
                                              .AppendPrefix("," + Environment.NewLine)
                                              ),
                                          // 9
                                          procedure.ToOutFormatMethod(Formater)
                                          );
            }

            var sb = new StringBuilder();

            sb.Append(pattern);
            sb
            .Replace("{Comments}", packageSchema.PackageName)
            .Replace("{Today}", now)
            .Replace("{Namespace}", nameSpace)
            .Replace("{PackageClassName}", packageClassName)
            .Replace("{PackageName}", packageName)
            .Replace("{Procedures}", sbProcedures.ToString());

            return(sb.ToString());
        }
Example #4
0
        private string GenerateEntitySchema(string name)
        {
            var pattern =
                @"// {EntityName}.cs
//
// Generated by Kuicker.Tools.EntityGenerator.
// {Today}

using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Collections.Generic;
using Kuicker;
using Kuicker.Data;

namespace {Namespace}
{
	public partial class {EntityName} : Entity<{EntityName}>
	{
		public override string __TableName
		{
			get
			{
				return ""{TableName}"";
			}
		}
{Properties}
	}
}";
            var now        = DateTime.Now.yyyy_MM_dd_HH_mm_ss_fff();
            var nameSpace  = txtNamespace.Text;
            var tableName  = name;
            var entityName = tableName
                             .TrimStart(txtTableTrimPrefix.Text);

            entityName = NamingFilter(entityName)
                         .AppendSuffix(txtTableAppendSuffix.Text);
            if (entityName.IsNullOrEmpty())
            {
                entityName = tableName;
            }

            var tableSchema = TableSchemas.FirstOrDefault(x =>
                                                          x.TableName == tableName
                                                          );

            var ef           = EnumCache.Get <DataFormat>();
            var sbProperties = new StringBuilder();

            foreach (var column in tableSchema.Columns)
            {
                var propertyName = column.ColumnName
                                   .TrimStart(txtColumnTrimPrefix.Text);
                propertyName = NamingFilter(propertyName)
                               .AppendSuffix(txtColumnAppendSuffix.Text);
                if (propertyName.IsNullOrEmpty())
                {
                    propertyName = column.ColumnName;
                }

                var columnSchema = tableSchema.Columns.FirstOrDefault(x =>
                                                                      x.ColumnName == column.ColumnName
                                                                      );
                if (columnSchema == null)
                {
                    continue;
                }

                if (sbProperties.Length > 0)
                {
                    sbProperties.AppendLine();
                }

                sbProperties.AppendFormat(@"
		/// <summary>
		/// {0}
		/// </summary>
		[Description(""{0}"")]
		[DataMember, Column(""{1}""){2}{3}]
		public {4} {5} {{ get; set; }}"        ,
                                          column.Comments.AirBag(column.ColumnName),
                                          column.ColumnName,
                                          tableSchema
                                          .IsPrimaryKey(column.ColumnName)
                                          .If(", PrimaryKey"),
                                          column
                                          .Nullable
                                          .If(", AllowDBNull"),
                                          columnSchema.ToTypeName(Formater),
                                          propertyName
                                          );
            }

            var sb = new StringBuilder();

            sb.Append(pattern);
            sb
            .Replace(
                "{Comments}",
                tableSchema.Comments.AirBag(tableSchema.TableName)
                )
            .Replace("{Today}", now)
            .Replace("{Namespace}", nameSpace)
            .Replace("{EntityName}", entityName)
            .Replace("{TableName}", tableName)
            .Replace("{Properties}", sbProperties.ToString());

            return(sb.ToString());
        }
Example #5
0
        // Bind
        internal void Bind(IntervalLogger il, IEntity instance)
        {
            this.CurrentSchema = instance;

            foreach (Column column in instance.__Columns)
            {
                this.CurrentColumn = column;

                string asName = Contains(column.ColumnName)
                                        ? column.ColumnName
                                        : Contains(column.Alias)
                                                ? column.Alias
                                                : null;
                if (null == asName)
                {
                    continue;
                }

                try {
                    object val = null;

                    switch (column.Format)
                    {
                    case DataFormat.String:
                        val = this.GetString(asName);
                        break;

                    case DataFormat.DateTime:
                        val = this.GetDateTime(asName);
                        break;

                    case DataFormat.Boolean:
                        val = this.GetInteger(asName) == 1
                                                                ? true
                                                                : false;
                        break;

                    case DataFormat.Char:
                        val = this.GetString(asName);
                        break;

                    case DataFormat.Byte:
                        val = this.GetByte(asName);
                        break;

                    case DataFormat.Short:
                        val = this.GetShort(asName);
                        break;

                    case DataFormat.Integer:
                        val = this.GetInteger(asName);
                        break;

                    case DataFormat.Long:
                        val = this.GetLong(asName);
                        break;

                    case DataFormat.Float:
                        val = this.GetFloat(asName);
                        break;

                    case DataFormat.Double:
                        val = this.GetDouble(asName);
                        break;

                    case DataFormat.Decimal:
                        val = this.GetDecimal(asName);
                        break;

                    case DataFormat.ByteArray:
                        val = this.GetBytes(asName);
                        break;

                    case DataFormat.Color:
                        int color = this.GetInteger(asName);
                        val = Color.FromArgb(color);
                        break;

                    case DataFormat.Guid:
                        val = this.GetGuid(asName);
                        break;

                    case DataFormat.Enum:
                        string original = this.GetString(asName);
                        if (original.IsNullOrEmpty())
                        {
                            original = string.Empty;
                        }

                        var enumRef = EnumCache.Get(
                            column.Property.PropertyType
                            );
                        var enumItem = enumRef.Get(original);

                        if (
                            !original.IsNullOrEmpty()
                            &&
                            null == enumItem)
                        {
                            LogRecord
                            .Create()
                            .SetMessage(
                                "Dirty enum value alert!"
                                )
                            .AddRange(Currents)
                            .Add("Dirty Value", original)
                            .Add(
                                "Possible Values",
                                enumRef
                                .Items
                                .Join(x => x.Name, ", ")
                                )
                            .Error();
                        }

                        var enumVal = null == enumItem
                                                                ? enumRef.DefaultValue
                                                                : enumItem.Name;
                        val = Enum.Parse(
                            column.Property.PropertyType,
                            enumVal,
                            true
                            );
                        break;

                    case DataFormat.TimeSpan:
                        val = this.GetValue(asName).ToTimeSpan();
                        break;

                    default:
                        throw new NotImplementedException(
                                  LogRecord
                                  .Create()
                                  .SetMessage(
                                      "Unhandled Property Type: ",
                                      column.Property.PropertyType.Name,
                                      " at ",
                                      column.EntityFullName
                                      )
                                  .Error()
                                  .Message
                                  );
                    }
                    column.Property.SetValue(
                        instance, val, new Object[0]
                        );
                } catch (Exception ex) {
                    WriteErrorMessage("Bind", ex);
                }
            }
        }