Esempio n. 1
0
        public void AddDelete(ClrClass @class, DbTable table)
        {
            if (@class == null)
            {
                throw new ArgumentNullException("class");
            }
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }

            var varName = NameProvider.ToParameterName(@class.Name);

            _buffer.AppendLine(string.Format(@"public void Delete({0} {1})", @class.Name, varName));

            this.BeginBlock();

            _buffer.AppendLine(this.GetParameterCheck(varName));
            this.AddEmptyLine();

            _buffer.AppendLine(string.Format(@"var query = @""{0}"";", QueryCreator.GetDelete(table).Statement));
            this.AddEmptyLine();
            _buffer.AppendLine(@"var sqlParams = new []");

            this.BeginBlock();
            _buffer.AppendLine(string.Format(@"QueryHelper.Parameter(@""{0}"", {1}.{0}),", NameProvider.IdName, varName));
            this.EndBlockWith();

            this.AddEmptyLine();

            _buffer.AppendLine(@"QueryHelper.ExecuteQuery(query, sqlParams);");

            this.EndBlock();
        }
Esempio n. 2
0
        public static Field[] GetDictionaryFields(DbTable[] tables, DbTable detailsTable = null)
        {
            if (tables == null)
            {
                throw new ArgumentNullException("tables");
            }

            var totalFields = tables.Length;

            if (detailsTable != null)
            {
                totalFields++;
            }
            var fields = new Field[totalFields];

            for (var index = 0; index < tables.Length; index++)
            {
                var table = tables[index];
                fields[index] = new Field(table.ClassName, NameProvider.ToParameterName(table.Name));
            }

            if (detailsTable != null)
            {
                fields[fields.Length - 1] = new Field(detailsTable.Name + @"Adapter", @"adapter", false);
            }

            return(fields);
        }
Esempio n. 3
0
        public void AddInsert(ClrClass @class, DbTable table)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }

            var className = table.ClassName;

            var varName = NameProvider.ToParameterName(className);

            _buffer.AppendLine(string.Format(@"public void Insert({0} {1})", className, varName));

            this.BeginBlock();

            _buffer.AppendLine(this.GetParameterCheck(varName));
            this.AddEmptyLine();

            _buffer.AppendLine(string.Format(@"var query = @""{0}"";", QueryCreator.GetInsert(table).Statement));
            this.AddEmptyLine();
            _buffer.AppendLine(@"var sqlParams = new []");

            this.BeginBlock();
            var index = 0;
            var names = QueryCreator.GetParametersWithoutPrimaryKey(table);

            foreach (var property in @class.Properties)
            {
                var type = property.Type;
                if (type.IsCollection)
                {
                    continue;
                }
                var name = property.Name;
                if (name != NameProvider.IdName)
                {
                    if (!type.IsBuiltIn)
                    {
                        name += @"." + NameProvider.IdName;
                    }
                    _buffer.AppendLine(string.Format(@"QueryHelper.Parameter(@""{0}"", {1}.{2}),", names[index++], varName, name));
                }
            }
            this.EndBlockWith();

            this.AddEmptyLine();

            _buffer.AppendLine(@"QueryHelper.ExecuteQuery(query, sqlParams);");
            _buffer.AppendLine(string.Format(@"{0}.Id = Convert.ToInt64(QueryHelper.ExecuteScalar(@""SELECT LAST_INSERT_ROWID()""));", varName));

            this.EndBlock();
        }
Esempio n. 4
0
        public void AddUpdate(ClrClass @class, DbTable table)
        {
            if (@class == null)
            {
                throw new ArgumentNullException("class");
            }
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }

            var varName = NameProvider.ToParameterName(@class.Name);

            _buffer.AppendLine(string.Format(@"public void Update({0} {1})", @class.Name, varName));

            this.BeginBlock();

            _buffer.AppendLine(this.GetParameterCheck(varName));
            this.AddEmptyLine();

            _buffer.AppendLine(string.Format(@"var query = @""{0}"";", QueryCreator.GetUpdate(table).Statement));
            this.AddEmptyLine();
            _buffer.AppendLine(@"var sqlParams = new []");

            this.BeginBlock();
            var index      = 0;
            var parameters = QueryCreator.GetParameters(table.Columns);

            foreach (var property in @class.Properties)
            {
                var type = property.Type;
                if (type.IsCollection)
                {
                    continue;
                }

                var name = property.Name;
                if (!type.IsBuiltIn)
                {
                    name += @"." + NameProvider.IdName;
                }
                _buffer.AppendLine(string.Format(@"QueryHelper.Parameter(@""{0}"", {1}.{2}),", parameters[index++].Name, varName, name));
            }
            this.EndBlockWith();

            this.AddEmptyLine();

            _buffer.AppendLine(@"QueryHelper.ExecuteQuery(query, sqlParams);");

            this.EndBlock();
        }
Esempio n. 5
0
        public void AddFillMethod(string className, DbTable table)
        {
            var name = NameProvider.ToParameterName(table.Name);

            _buffer.AppendLine(string.Format(@"public void Fill({0} {1})", GetDictionaryField(className), name));

            this.BeginBlock();

            _buffer.AppendLine(this.GetParameterCheck(name));
            this.AddEmptyLine();
            _buffer.AppendLine(string.Format(@"var query = @""{0}"";", QueryCreator.GetSelect(table).Statement));
            this.AddEmptyLine();
            _buffer.AppendLine(string.Format(@"QueryHelper.Fill({0}, query, this.Creator, this.Selector);", name));

            this.EndBlock();
        }
Esempio n. 6
0
        private static ClrProperty[] GetProperties(DbTable table, DbTable[] tables)
        {
            var columns            = table.Columns;
            var collectionProperty = FindCollectionProperty(table, tables);

            ClrProperty[] properties;
            if (collectionProperty == null)
            {
                properties = new ClrProperty[columns.Length];
            }
            else
            {
                properties = new ClrProperty[columns.Length + 1];
                properties[properties.Length - 1] = collectionProperty;
            }

            // Table properties
            for (var i = 0; i < columns.Length; i++)
            {
                var column     = columns[i];
                var foreignKey = column.DbForeignKey;

                var type = Types[column.Type.Sequence];
                var name = NameProvider.GetPropertyName(column.Name, foreignKey != null);

                if (foreignKey != null)
                {
                    var classType = string.Empty;
                    var tableName = foreignKey.Table;
                    foreach (var t in tables)
                    {
                        if (t.Name == tableName)
                        {
                            classType = t.ClassName;
                            break;
                        }
                    }
                    type = ClrType.UserType(classType, !column.AllowNull);
                }

                properties[i] = new ClrProperty(type, name);
            }

            return(properties);
        }
Esempio n. 7
0
        public static string GenerateCode(DbTable table)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }

            var template = @"	public sealed class {0}Helper
	{{
		private readonly Dictionary<long, {1}> _{2} = new Dictionary<long, {1}>();

		public Dictionary<long, {1}> {0} {{ get {{ return _{2}; }} }}

		public void Load({0}Adapter adapter)
		{{
			if (adapter == null) throw new ArgumentNullException(""adapter"");

			adapter.Fill(this.{0});
		}}
	}}"    ;

            return(string.Format(template, table.Name, table.ClassName, NameProvider.ToParameterName(table.Name)));
        }
Esempio n. 8
0
        public static string GenerateCode(DbSchema schema)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            var buffer = new StringBuilder(1024);

            buffer.AppendLine(string.Format(@"public sealed class {0}App", schema.Name));
            buffer.AppendLine(@"{");

            buffer.AppendLine(@"public event EventHandler<HelperLoadedEventArgs> HelperLoaded;");
            buffer.AppendLine();

            buffer.AppendLine(@"private void OnHelperLoaded(HelperLoadedEventArgs e)");
            buffer.AppendLine(@"{");
            buffer.AppendLine(@"var handler = HelperLoaded;");
            buffer.AppendLine(@"if (handler != null) handler(this, e);");
            buffer.AppendLine(@"}");
            buffer.AppendLine();

            var tables = new List <DbTable>(schema.Tables);

            tables.Sort((x, y) =>
            {
                var cmp = HasForeignKey(x).CompareTo(HasForeignKey(y));
                if (cmp == 0)
                {
                    cmp = string.Compare(x.Name, y.Name, StringComparison.Ordinal);
                }
                return(cmp);
            });

            foreach (var table in tables)
            {
                if (table.IsReadOnly)
                {
                    var name = NameProvider.ToParameterName(table.Name);

                    buffer.AppendLine(string.Format(@"private readonly {0}Helper _{1}Helper = new {0}Helper();", table.Name, name));
                    buffer.AppendLine(string.Format(@"public {0}Helper {0}Helper", table.Name));
                    buffer.AppendLine(@"{");
                    buffer.AppendLine(string.Format(@"get {{ return _{0}Helper; }}", name));
                    buffer.AppendLine(@"}");
                    buffer.AppendLine();
                }
            }

            buffer.AppendLine(@"public void Load()");
            buffer.AppendLine(@"{");
            foreach (var table in tables)
            {
                if (table.IsReadOnly)
                {
                    var parameters = new StringBuilder();
                    foreach (var column in table.Columns)
                    {
                        var foreignKey = column.DbForeignKey;
                        if (foreignKey != null)
                        {
                            if (parameters.Length > 0)
                            {
                                parameters.Append(@", ");
                            }
                            parameters.Append(@"this.");
                            parameters.Append(foreignKey.Table);
                            parameters.Append(@"Helper.");
                            parameters.Append(foreignKey.Table);
                        }
                    }
                    buffer.AppendLine(string.Format(@"this.{0}Helper.Load(new {0}Adapter({1}));", table.Name, parameters));
                    buffer.AppendLine(string.Format(@"this.OnHelperLoaded(new HelperLoadedEventArgs(@""{0}""));", table.Name));
                    buffer.AppendLine();
                }
            }
            buffer.AppendLine(@"}");

            buffer.AppendLine(@"}");

            return(buffer.ToString());
        }
Esempio n. 9
0
        private static void AppendContructor(string name, ClrProperty[] properties, StringBuilder buffer)
        {
            buffer.Append(Tab);
            buffer.Append(@"public");
            buffer.Append(Space);
            buffer.Append(name);
            buffer.Append(@"(");

            // Add parameters
            var parameterNames = new string[properties.Length];

            for (var i = 0; i < parameterNames.Length; i++)
            {
                parameterNames[i] = NameProvider.ToParameterName(properties[i].Name);
            }
            for (var i = 0; i < parameterNames.Length; i++)
            {
                var parameterName = parameterNames[i];
                if (i > 0)
                {
                    buffer.Append(Comma);
                    buffer.Append(Space);
                }
                buffer.Append(properties[i].Type.Name);
                buffer.Append(Space);
                buffer.Append(parameterName);
            }

            buffer.AppendLine(@")");

            buffer.Append(Tab);
            buffer.AppendLine(@"{");

            // Add parameter checks
            var hasChecks = false;

            for (var i = 0; i < properties.Length; i++)
            {
                var property = properties[i];
                if (property.Type.CheckValue)
                {
                    hasChecks = true;
                    buffer.Append(Tab);
                    buffer.Append(Tab);
                    buffer.AppendFormat(@"if ({0} == null) throw new ArgumentNullException(""{0}"");", parameterNames[i]);
                    buffer.AppendLine();
                }
            }

            if (hasChecks)
            {
                buffer.AppendLine();
            }

            for (var i = 0; i < properties.Length; i++)
            {
                buffer.Append(Tab);
                buffer.Append(Tab);
                buffer.Append(@"this.");
                buffer.Append(properties[i].Name);
                buffer.Append(@" = ");
                buffer.Append(parameterNames[i]);
                buffer.Append(Semicolon);
                buffer.AppendLine();
            }

            buffer.Append(Tab);
            buffer.AppendLine(@"}");
        }
Esempio n. 10
0
        public void AddCreator(ClrClass @class, Field[] fields, int readerIndexOffset = 0)
        {
            var properties = @class.Properties;

            var index = 0;
            var names = new string[properties.Length];

            foreach (var property in properties)
            {
                var name = NameProvider.ToParameterName(property.Name);
                var type = property.Type;
                if (type.IsCollection)
                {
                    name = @"new " + type.Name + @"()";
                }
                names[index++] = name;
            }

            Field parameter = null;

            for (var i = 0; i < properties.Length; i++)
            {
                var property  = properties[i];
                var name      = names[i];
                var type      = property.Type;
                var readValue = type.IsBuiltIn || (Field.FindFieldByType(fields, property.Type)) != null;
                if (!readValue && !type.IsCollection)
                {
                    parameter = new Field(type.Name, name);
                }
            }

            _buffer.AppendLine(parameter == null
                                ? string.Format(@"private {0} Creator(IDataReader r)", @class.Name)
                                : string.Format(@"public {0} Creator(IDataReader r, {1} {2})", @class.Name, parameter.Type, parameter.Name));

            this.BeginBlock();

            if (parameter != null)
            {
                _buffer.AppendLine(this.GetParameterCheck(@"r"));
                _buffer.AppendLine(this.GetParameterCheck(parameter.Name));
                this.AddEmptyLine();
            }

            var readerIndex = 0;

            for (var i = 0; i < properties.Length; i++)
            {
                var property = properties[i];
                var name     = names[i];
                var type     = property.Type;

                Field field     = null;
                var   readValue = type.IsBuiltIn || (field = Field.FindFieldByType(fields, property.Type)) != null;
                if (readValue)
                {
                    var value = readerIndex + readerIndexOffset;

                    _buffer.AppendLine(string.Format(@"var {0} = {1};", name, type.DefaultValue));
                    _buffer.AppendLine(string.Format(@"if (!r.IsDBNull({0}))", value));

                    this.BeginBlock();

                    if (type.IsBuiltIn)
                    {
                        _buffer.AppendLine(string.Format(@"{0} = r.{1}({2});", name, type.ReaderMethod, value));
                    }
                    else
                    {
                        _buffer.AppendLine(string.Format(@"{0} = _{1}[r.{2}({3})];", name, field.Name, type.ReaderMethod, value));
                    }

                    this.EndBlock();
                    readerIndex++;
                }
            }

            this.AddEmptyLine();

            _buffer.AppendLine(string.Format(@"return new {0}({1});", @class.Name, string.Join(@", ", names)));

            this.EndBlock();
        }