Пример #1
0
        public override string ToString()
        {
            string str = @"using System;
using __NAMESPACE__.Models;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
	
namespace __NAMESPACE__.Repositories
{
	public class __NAME__ : BaseMySqlRepository<__MODEL_NAME__>
	{
		public __NAME__()
		{
		}
		
		public override void Save(__MODEL_NAME__ __VARIABLE__)
		{
			string query = @""
INSERT INTO __TABLE_NAME__(
__COLUMNS__
)
VALUES(
__PARAMETERIZED_COLUMNS__
)"";
			ExecuteNonQuery(
				query,
__SQL_PARAMETERS__
			);
		}
		
		public override void Update(__MODEL_NAME__ __VARIABLE__, int id)
		{
			string query = @""
UPDATE __TABLE_NAME__ SET
__COLUMNS_AND_PARAMETERIZED_COLUMNS__
WHERE __MODEL_NAME__ID = @__MODEL_NAME__ID"";
			ExecuteNonQuery(
				query,
__SQL_PARAMETERS__
			);
		}
		
		public override void Delete(int id)
		{
			string query = @""
DELETE FROM __TABLE_NAME__
WHERE __MODEL_NAME__ID = @__MODEL_NAME__ID"";
			ExecuteNonQuery(
				query,
				new MySqlParameter(""@__MODEL_NAME__ID"", id)
			);
		}
		
		public override __MODEL_NAME__ Read(int id)
		{
			string query = @""
SELECT __COLUMNS__
FROM __TABLE_NAME__
WHERE __MODEL_NAME__ID = @__MODEL_NAME__ID"";
			__MODEL_NAME__ __VARIABLE__ = null;
			using (var rs = ExecuteReader(query, new MySqlParameter(""@__MODEL_NAME__ID"", id))) {
				if (rs.Read()) {
					__VARIABLE__ = new __MODEL_NAME__ {
__ASSIGNED_PROPERTIES__
					};
				}
			}
			return __VARIABLE__;
		}
		
		public override IList<__MODEL_NAME__> FindAll()
		{
			string query = @""
SELECT __COLUMNS__
FROM __TABLE_NAME__"";
			var __VARIABLE__s = new List<__MODEL_NAME__>();
			using (var rs = ExecuteReader(query)) {
				while (rs.Read()) {
					__VARIABLE__s.Add(new __MODEL_NAME__ {
__ASSIGNED_PROPERTIES__
					});
				}
			}
			return __VARIABLE__s;
		}
	}
}";

            str = str.Replace("__NAMESPACE__", Namespace);
            str = str.Replace("__TABLE_NAME__", ModelName);
            str = str.Replace("__NAME__", Name);
            str = str.Replace("__MODEL_NAME__", ModelName.ToSingularize().ToPascalCase());
            str = str.Replace("__VARIABLE__", ModelName.ToSingularize().ToCamelCase());

            string columns = "";
            int    i       = 1;

            foreach (var c in table.Columns)
            {
                columns += "	"+ c.Name;
                columns += i++ < table.Columns.Count ? ", " + Environment.NewLine : "";
            }
            str = str.Replace("__COLUMNS__", columns);

            string parameterizedColumns = "";

            i = 1;
            foreach (var c in table.Columns)
            {
                parameterizedColumns += "	@"+ c.Name;
                parameterizedColumns += i++ < table.Columns.Count ? ", " + Environment.NewLine : "";
            }
            str = str.Replace("__PARAMETERIZED_COLUMNS__", parameterizedColumns);

            string sqlParameters = "";

            i = 1;
            foreach (var c in table.Columns)
            {
                sqlParameters += string.Format(@"				new MySqlParameter(""@{0}"", {1}.{2})", c.Name, ModelName.ToSingularize().ToCamelCase(), c.Name.ToPascalCase());
                sqlParameters += i++ < table.Columns.Count ? "," + Environment.NewLine : "";
            }
            str = str.Replace("__SQL_PARAMETERS__", sqlParameters);

            string columnsAndParameterizedColumns = "";

            i = 1;
            foreach (var c in table.Columns)
            {
                columnsAndParameterizedColumns += string.Format("	{0} = @{0}", c.Name);
                columnsAndParameterizedColumns += i++ < table.Columns.Count ? "," + Environment.NewLine : "";
            }
            str = str.Replace("__COLUMNS_AND_PARAMETERIZED_COLUMNS__", columnsAndParameterizedColumns);

            string assignedProperties = "";

            i = 1;
            foreach (var c in table.Columns)
            {
                assignedProperties += string.Format(@"						{0} = {1}", c.Name.ToPascalCase(), GetAssigneeReader(c, i));
                assignedProperties += i++ < table.Columns.Count ? "," + Environment.NewLine : "";
            }
            str = str.Replace("__ASSIGNED_PROPERTIES__", assignedProperties);

            return(str);
        }