/// <summary>
        /// Add Insert Model to Script Model
        /// </summary>
        /// <param name="model">Insert Model</param>
        /// <param name="suffix">suffix to use for declared variable name</param>
        /// <param name="injects">Inject values</param>
        /// <param name="skipComment">Include comments</param>
        /// <returns>Declared variable name if used</returns>
        public string AddInsert(InsertModel model, string suffix, IEnumerable <KeyValuePair <string, string> > injects = null, bool skipComment = false)
        {
            if (!skipComment)
            {
                this.Content.Add($"-- {model.FullName}");
                this.Content.Add("--------------------------------------------------");
            }

            var declare = string.Empty;

            if (model.HasId)
            {
                declare = ($"i_{model.Table}_id_{suffix}").Trim('_');
                this.Declare.Add(new DeclareModel(declare));
                this.Content.Add($"{model.Build(true, injects).TrimEnd(';')} INTO {declare};");
                this.Content.Add($"RAISE NOTICE '{model.FullName} ID: %', {declare};");
            }
            else
            {
                this.Content.Add($"{model.Build(true, injects)}");
            }

            this.Content.Add(string.Empty);
            return(declare);
        }
Beispiel #2
0
        /// <summary>
        /// Read Table and Column attributes and create InsertModel with filled data
        /// </summary>
        /// <typeparam name="TModel">Model Type</typeparam>
        /// <param name="model">model</param>
        /// <returns></returns>
        public static InsertModel Create <TModel>(TModel model)
            where TModel : new()
        {
            var result     = new InsertModel();
            var properties = typeof(TModel).GetProperties().Where(prop => prop.IsDefined(typeof(ColumnAttribute), false));
            var table      = (TableAttribute)typeof(TModel).GetTypeInfo().GetCustomAttribute(typeof(TableAttribute));

            if (table != null)
            {
                result.Table  = table.Name;
                result.Schema = table.Schema;
            }

            foreach (var item in properties)
            {
                ColumnAttribute[] attributes = (ColumnAttribute[])item.GetCustomAttributes(typeof(ColumnAttribute), false);
                if (null != attributes[0])
                {
                    result.Columns.Add(attributes[0].Name);
                    result.Values.Add(Common.GetSqlValue(item, item.GetValue(model, null)));
                }
            }

            return(result);
        }