Пример #1
0
        public List <DbTableTypeDeclaration> Parse(string sql)
        {
            var decls = new List <DbTableTypeDeclaration>();

            var text    = sql.Replace('\n', ' ').Replace('\r', ' ');
            var matches = Regex.Matches(text,
                                        @"CREATE\s+TYPE\s+(\[dbo\]\.)?\[(\w+)\]\s?AS\s?TABLE\s?\((.+?)[^0-9]\)");

            foreach (Match match in matches)
            {
                var decl = new DbTableTypeDeclaration();
                decl.TypeName = match.Groups[2].Value;
                decl.Fields   = new List <DbField>();

                var paramsText = match.Groups[3].Value.Trim();
                if (paramsText.StartsWith("(") && paramsText.EndsWith(")"))
                {
                    paramsText = paramsText.Substring(1, paramsText.Length - 2);
                }

                foreach (var p in paramsText.Split(','))
                {
                    var fields = p.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)
                                 .Where(w => w.ToLower() != "as").ToArray();
                    if (fields.Length == 0)
                    {
                        continue;
                    }

                    var fieldName = fields[0];
                    var fieldType = fields[1];

                    var type = DbTypeHelper.GetTypeFromSqlType(fieldType);
                    if (type == null)
                    {
                        throw new Exception("Cannot resolve type: " + fieldType);
                    }

                    decl.Fields.Add(new DbField
                    {
                        Name = fieldName.Replace("[", "").Replace("]", ""),
                        Type = type.Item1,
                        Len  = type.Item2,
                        Dir  = ""
                    });
                }
                decls.Add(decl);
            }

            return(decls);
        }
        public void Generate(DbTableTypeDeclaration d, CodeWriter.CodeWriter w)
        {
            using (w.B($"public class {d.ClassName}"))
            {
                w._($"public DataTable Table {{ get; set; }}");
                w._();

                using (w.B($"public {d.ClassName}()"))
                {
                    w._($"Table = new DataTable();");
                    foreach (var field in d.Fields)
                    {
                        w._($"Table.Columns.Add(`{field.Name}`, typeof({field.Type}));");
                    }
                }

                var funcDecl   = string.Join(", ", d.Fields.Select(f => f.Type + " " + f.Name));
                var funcParams = string.Join(", ", d.Fields.Select(f => f.Name));
                using (w.B($"public void Add({funcDecl})"))
                {
                    w._($"Table.Rows.Add({funcParams});");
                }
            }
        }