Exemplo n.º 1
0
        public void CodeBlockForStatement()
        {
            var cb = new CodeBlock(SourceCodeLanguage.CSharp, "for (int i = 0; i < length; i++)");
            cb.CurlyBracket = true;
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "x = x + 1;"));

            Assert.AreEqual("for (int i = 0; i < length; i++)" + SourceCodeGenerator.NewLine
                + "{" + SourceCodeGenerator.NewLine
                + "    x = x + 1;" + SourceCodeGenerator.NewLine
                + "}" + SourceCodeGenerator.NewLine
                , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, cb));
        }
Exemplo n.º 2
0
        public void CodeBlockIfStatement()
        {
            var cb = new CodeBlock(SourceCodeLanguage.CSharp, "if (bl == true)");
            cb.CurlyBracket = true;
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "MessageBox.Show(\"Update successfully!\");"));

            Assert.AreEqual("if (bl == true)" + SourceCodeGenerator.NewLine
                + "{" + SourceCodeGenerator.NewLine
                + "    MessageBox.Show(\"Update successfully!\");" + SourceCodeGenerator.NewLine
                + "}" + SourceCodeGenerator.NewLine
                , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, cb));
        }
Exemplo n.º 3
0
        public void CodeBlockIndentIsTwoWhitespace()
        {
            var cb = new CodeBlock(SourceCodeLanguage.CSharp, "if (bl == true)");
            cb.CurlyBracket = true;
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "MessageBox.Show(\"Update successfully!\");"));

            var cs = new CSharpSourceCodeGenerator();
            cs.Indent = "  ";
            cs.Write(cb);

            Assert.AreEqual("if (bl == true)" + SourceCodeGenerator.NewLine
                + "{" + SourceCodeGenerator.NewLine
                + "  MessageBox.Show(\"Update successfully!\");" + SourceCodeGenerator.NewLine
                + "}" + SourceCodeGenerator.NewLine
                , cs.ToString());
        }
Exemplo n.º 4
0
        public void CodeBlockBracket()
        {
            var cb = new CodeBlock(SourceCodeLanguage.CSharp, "catch (InvalidCastException ex)");
            cb.CurlyBracket = true;
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "throw new StoredProcedureSchemaMismatchedException(this, ex);"));

            var cs = new CSharpSourceCodeGenerator();
            cs.CurrentIndentLevel = 3;
            cs.Indent = "    ";
            cs.Write(cb);

            Assert.AreEqual("            catch (InvalidCastException ex)" + SourceCodeGenerator.NewLine
                + "            {" + SourceCodeGenerator.NewLine
                + "                throw new StoredProcedureSchemaMismatchedException(this, ex);" + SourceCodeGenerator.NewLine
                + "            }" + SourceCodeGenerator.NewLine
                , cs.ToString());
        }
        public IEnumerable<CodeBlock> CreateSetOutputParameterValueMethodBody()
        {
            var t = this.Table;
            CodeBlock cb = null;

            yield return new CodeBlock(SourceCodeLanguage.CSharp, "var spInsert = storedProcedure as {0}Insert;", t.Name);
            cb = new CodeBlock(SourceCodeLanguage.CSharp, "if (spInsert != null)");
            cb.CurlyBracket = true;
            foreach (var column in t.Columns.FindAll(el => el.IsServerAutomaticallyInsertValueColumn()))
            {
                cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "record.{0} = spInsert.{0};", column.Name));
            }
            yield return cb;

            yield return new CodeBlock(SourceCodeLanguage.CSharp, "var spUpdate = storedProcedure as {0}Update;", t.Name);
            cb = new CodeBlock(SourceCodeLanguage.CSharp, "if (spUpdate != null)");
            cb.CurlyBracket = true;
            foreach (var column in t.Columns.FindAll(el => el.IsServerAutomaticallyInsertValueColumn()))
            {
                cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "record.{0} = spUpdate.{0};", column.Name));
            }
            yield return cb;
        }
        public IEnumerable<CodeBlock> CreateSetDataRowMethodBody()
        {
            var t = this.Table;

            var cb = new CodeBlock(SourceCodeLanguage.CSharp, "if (saveMode != SaveMode.Insert)");
            cb.CurlyBracket = true;
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "if (record.OldRecord == null) throw new OldRecordIsNullException();"));
            foreach (var column in t.GetColumns(true, true))
            {
                cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "dataRow[\"@PK_{0}\"] = GetValueOrDBNull(record.OldRecord.{0});", column.Name));
            }
            yield return cb;

            foreach (var column in t.Columns)
            {
                yield return new CodeBlock(SourceCodeLanguage.CSharp, "dataRow[\"@{0}\"] = GetValueOrDBNull(record.{0});", column.Name);
            }
            yield return new CodeBlock(SourceCodeLanguage.CSharp, "return dataRow;");
        }
        public IEnumerable<CodeBlock> CreateCreateStoredProcedureWithResultSetMethodBody()
        {
            var t = this.Table;

            var cb = new CodeBlock(SourceCodeLanguage.CSharp, "switch (type)", t.Name);
            cb.CurlyBracket = true;
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp
                , String.Format("case TableStoredProcedureTypeWithResultSet.SelectAll: return CreateSelectAllStoredProcedure();")));
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp
                , String.Format("case TableStoredProcedureTypeWithResultSet.SelectByPrimaryKey: return this.CreateSelectByPrimaryKeyStoredProcedure(record);", t.Name)));
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "default: throw new InvalidOperationException();"));
            yield return cb;
        }
        public IEnumerable<CodeBlock> CreateCreateStoredProcedureMethodBody()
        {
            var t = this.Table;

            var cb = new CodeBlock(SourceCodeLanguage.CSharp, "switch (type)", t.Name);
            cb.CurlyBracket = true;
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp
                , String.Format("case TableStoredProcedureType.Insert: return this.CreateInsertStoredProcedure(record);", t.Name)));
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp
                , String.Format("case TableStoredProcedureType.Update: return this.CreateUpdateStoredProcedure(record);", t.Name)));
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp
                , String.Format("case TableStoredProcedureType.Delete: return this.CreateDeleteStoredProcedure(record);", t.Name)));
            cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "default: throw new InvalidOperationException();"));
            yield return cb;
        }
        public Method CreateSetValueMethod()
        {
            var t = this.Table;
            Method md = new Method(MethodAccessModifier.Public, "SetValue");
            md.Modifier.Polymophism = MethodPolymophism.Override;
            md.ReturnTypeName = new TypeName("Boolean");
            md.Parameters.Add(new MethodParameter("Int32", "index"));
            md.Parameters.Add(new MethodParameter("Object", "value"));

            var cb = new CodeBlock(SourceCodeLanguage.CSharp, "switch (index)");
            cb.CurlyBracket = true;

            Int32 index = 0;
            CodeBlock cbIf = null;
            CodeBlock cbElse = null;
            Boolean useCast = false;
            foreach (var item in t.Columns)
            {
                var tp = item.GetClassNameType();
                useCast = item.ConvertType == SqlParameterConvertType.Default &&
                    (tp == ClassNameType.Object ||
                    tp == ClassNameType.String ||
                    tp == ClassNameType.ByteArray ||
                    tp == ClassNameType.MySqlGeometry ||
                    tp == ClassNameType.Geography ||
                    tp == ClassNameType.Geometry ||
                    tp == ClassNameType.HierarchyId);

                cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "case {0}:", index));
                cbIf = new CodeBlock(SourceCodeLanguage.CSharp, "if (value == null)");
                cbIf.IndentLevel = 1;
                cbIf.CurlyBracket = true;
                if (item.AllowNull == true)
                {
                    cbIf.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "this.{0} = null;", item.Name));
                    cbIf.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "return true;"));
                }
                else
                {
                    cbIf.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "return false;"));
                }
                cb.CodeBlocks.Add(cbIf);

                cbElse = new CodeBlock(SourceCodeLanguage.CSharp, "else");
                cbElse.IndentLevel = 1;
                cbElse.CurlyBracket = true;

                if (useCast == true)
                {
                    if (tp.IsStructure() == true)
                    {
                        cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp
                            , "var newValue = value as {0}?;", tp.ToClassNameString()));
                    }
                    else
                    {
                        cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp
                            , "var newValue = value as {0};", item.GetClassName()));
                    }
                }
                else
                {
                    if (item.ConvertType == SqlParameterConvertType.Default)
                    {
                        cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp
                           , "var newValue = TableRecord.TypeConverter.To{0}(value);", tp));
                    }
                    else if (item.ConvertType == SqlParameterConvertType.Enum)
                    {
                        cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp
                           , "var newValue = TableRecord.TypeConverter.ToEnum<{0}>(value);", item.EnumName));
                    }
                }
                cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "if (newValue == null) return false;"));

                if (tp.IsStructure() == true ||
                    item.ConvertType == SqlParameterConvertType.Enum)
                {
                    cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "this.{0} = newValue.Value;", item.Name));
                }
                else
                {
                    cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "this.{0} = newValue;", item.Name));
                }
                cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "return true;"));
                cb.CodeBlocks.Add(cbElse);

                index += 1;
            }
            md.Body.Add(cb);
            md.Body.Add(SourceCodeLanguage.CSharp
                , String.Format("throw new ArgumentOutOfRangeException(\"index\", index, \"index must be 0-{0}\");"
                , t.Columns.Count - 1));

            return md;
        }
        public Method CreateGetValueMethod()
        {
            var t = this.Table;
            Method md = new Method(MethodAccessModifier.Public, "GetValue");
            md.Modifier.Polymophism = MethodPolymophism.Override;
            md.ReturnTypeName = new TypeName("Object");
            md.Parameters.Add(new MethodParameter("Int32", "index"));

            var cb = new CodeBlock(SourceCodeLanguage.CSharp, "switch (index)");
            cb.CurlyBracket = true;
            Int32 index = 0;
            foreach (var item in t.Columns)
            {
                cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "case {0}: return this.{1};", index, item.Name));
                index += 1;
            }
            md.Body.Add(cb);
            md.Body.Add(SourceCodeLanguage.CSharp, "throw new ArgumentOutOfRangeException();");

            return md;
        }
Exemplo n.º 11
0
 public abstract void Write(CodeBlock codeBlock);
Exemplo n.º 12
0
 public void CodeBlockBasicFeature()
 {
     var cb = new CodeBlock(SourceCodeLanguage.CSharp, "Int32 x = 12;");
     Assert.AreEqual("Int32 x = 12;" + SourceCodeGenerator.NewLine
         , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, cb));
 }
 public override void Write(CodeBlock codeBlock)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 14
0
 public static String Write(SourceCodeLanguage language, CodeBlock codeBlock)
 {
     var sc = Create(language);
     sc.Write(codeBlock);
     return sc.TextWriter.ToString();
 }
Exemplo n.º 15
0
 public abstract void Write(CodeBlock codeBlock);