コード例 #1
0
        public void GenerateMethods(DbRowsetDeclaration d, CodeWriter.CodeWriter w)
        {
            using (w.B($"public async Task<Row> NextAsync()"))
            {
                w._($"if (await _reader.ReadAsync() == false) return null;",
                    $"var r = new Row();");

                var fidx = 0;
                foreach (var field in d.Fields)
                {
                    w._($"var v{fidx} = _reader.GetValue({fidx});");
                    if (field.Nullable)
                    {
                        var ntype = field.Type + (DbTypeHelper.IsValueType(field.Type) ? "?" : "");
                        w._($"r.{field.Name} = (v{fidx} is DBNull) ? ({ntype})null : ({field.Type})v{fidx};");
                    }
                    else
                    {
                        var ivalue = DbTypeHelper.GetInitValue(field.Type);
                        w._($"r.{field.Name} = (v{fidx} is DBNull) ? {ivalue} : ({field.Type})v{fidx};");
                    }
                    fidx += 1;
                }

                w._($"return r;");
            }

            using (w.B($"public async Task<List<Row>> FetchAllRowsAndDisposeAsync()"))
            {
                w._($"var rows = new List<Row>();");
                using (w.b($"while (true)"))
                {
                    w._($"var row = await NextAsync();",
                        $"if (row == null) break;",
                        $"rows.Add(row);");
                }
                w._($"Dispose();",
                    $"return rows;");
            }

            using (w.B($"public async Task<List<T>> FetchAllRowsAndDisposeAsync<T>(Func<Row, T> selector)"))
            {
                w._($"var rows = new List<T>();");
                using (w.b($"while (true)"))
                {
                    w._($"var row = await NextAsync();",
                        $"if (row == null) break;",
                        $"rows.Add(selector(row));");
                }
                w._($"Dispose();",
                    $"return rows;");
            }

            using (w.B($"public void Dispose()"))
            {
                w._($"_reader.Dispose();");
            }
        }
コード例 #2
0
        public void Generate(DbRowsetDeclaration d, CodeWriter.CodeWriter w)
        {
            using (w.B($"public class {d.ClassName} : IDisposable"))
            {
                w._($"private DbDataReader _reader;");
                w._();

                using (w.B($"public {d.ClassName}(DbDataReader reader)"))
                {
                    w._($"_reader = reader;");
                }

                using (w.B($"public class Row"))
                {
                    foreach (var field in d.Fields)
                    {
                        w._($"public {DbTypeHelper.GetMemberDecl(field)};");
                    }
                }

                GenerateMethods(d, w);
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: tteogi/SqlProcBinder
        private static void ReadOption(string optionFile,
                                       Options options,
                                       List <DbProcDeclaration> procs,
                                       List <DbRowsetDeclaration> rowsets,
                                       List <DbTableTypeDeclaration> tableTypes)
        {
            var joption = JsonConvert.DeserializeObject <JsonOption>(File.ReadAllText(optionFile));
            var baseDir = Path.GetDirectoryName(optionFile);

            if (joption.Procs != null)
            {
                var parser = new DbProcParser();
                foreach (var jproc in joption.Procs)
                {
                    List <DbProcDeclaration> decls;

                    if (string.IsNullOrEmpty(jproc.Path) == false)
                    {
                        decls = parser.Parse(File.ReadAllText(Path.Combine(baseDir, jproc.Path)));
                    }
                    else if (string.IsNullOrEmpty(jproc.Source) == false)
                    {
                        decls = parser.Parse(jproc.Source);
                    }
                    else
                    {
                        throw new ArgumentException("Path or Source is required: " + jproc);
                    }

                    decls.ForEach(d =>
                    {
                        d.ClassName   = jproc.ClassName;
                        d.Return      = jproc.Return;
                        d.Rowset      = jproc.Rowset;
                        d.RowsetFetch = jproc.RowsetFetch;
                        d.Params.ForEach(p => p.Nullable = jproc.Nullable ?? options.Nullable);
                    });
                    procs.AddRange(decls);
                }
            }

            if (joption.Rowsets != null)
            {
                foreach (var jrowset in joption.Rowsets)
                {
                    var rowset = new DbRowsetDeclaration();
                    rowset.ClassName = jrowset.Name;
                    rowset.Fields    = jrowset.Fields.Select(s =>
                    {
                        var strs = s.Split();
                        if (strs[0].EndsWith("?"))
                        {
                            return(new DbField
                            {
                                Type = strs[0].Substring(0, strs[0].Length - 1),
                                Name = strs[1],
                                Nullable = true
                            });
                        }
                        else
                        {
                            return(new DbField
                            {
                                Type = strs[0],
                                Name = strs[1]
                            });
                        }
                    }).ToList();
                    rowsets.Add(rowset);
                }
            }

            if (joption.TableTypes != null)
            {
                var parser = new DbTableTypeParser();
                foreach (var jtype in joption.TableTypes)
                {
                    List <DbTableTypeDeclaration> decls;

                    if (string.IsNullOrEmpty(jtype.Path) == false)
                    {
                        decls = parser.Parse(File.ReadAllText(Path.Combine(baseDir, jtype.Path)));
                    }
                    else if (string.IsNullOrEmpty(jtype.Source) == false)
                    {
                        decls = parser.Parse(jtype.Source);
                    }
                    else
                    {
                        throw new ArgumentException("Path or Source is required: " + jtype);
                    }

                    decls.ForEach(d =>
                    {
                        d.ClassName = jtype.ClassName;
                    });
                    tableTypes.AddRange(decls);
                }
            }
        }