Example #1
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();
        }