Exemplo n.º 1
0
        /// <summary>
        /// 针对符合要求的Model对象进行单字段条件的查询操作,返回对象DataTable(条件部分只能进行And合并)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <param name="exp_Where"></param>
        /// <param name="exps_Sel"></param>
        /// <returns></returns>
        public DataTable SelectDataTable <T>(Expression <Func <T, bool> >[] exp_Where, params Expression <Func <T, object> >[] exps_Sel) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(TableModelMapper.GetTableName(typeof(T)));

            if (exps_Sel == null || exps_Sel.Count() <= 0)
            {
                sqlBuilder = sqlBuilder.Select();
            }
            else
            {
                sqlBuilder = sqlBuilder.Select(exps_Sel);
            }
            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            string sql = sqlBuilder.GetSql();

            return(ExecuteDataTable(sql));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 以指定倒序排序字段查询数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exp_Order"></param>
        /// <param name="exp_Where"></param>
        /// <returns></returns>
        public List <T> SelectOrderByDescending <T>(Expression <Func <T, object> > exp_Order, params Expression <Func <T, bool> >[] exp_Where) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(TableModelMapper.GetTableName(typeof(T)));

            sqlBuilder = sqlBuilder.Select();
            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            if (exp_Order != null)
            {
                sqlBuilder = sqlBuilder.OrderBy(exp_Order, SortType.Desc);
            }

            string sql = sqlBuilder.GetSql();

            using (IDataReader dr = ExecuteDataReader(sql))
            {
                return(GetObjectList <T>(dr));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 针对符合要求的Model对象进行单字段条件检查是否存在(条件部分只能进行And合并)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <param name="exp_Where"></param>
        /// <returns></returns>
        public bool Exists <T>(params Expression <Func <T, bool> >[] exp_Where) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(TableModelMapper.GetTableName(typeof(T)))
                                         .Select("1");

            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            string sql = sqlBuilder.GetSql();
            object ret = ExecuteScalar(sql);

            if (ret != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 针对符合要求的Model对象进行单字段条件删除操作(条件部分只能进行And合并)
        /// </summary>
        /// <param name="info"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public ResultInfo <int> Delete <T>(params Expression <Func <T, bool> >[] exp_Where) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(TableModelMapper.GetTableName(typeof(T)))
                                         .Delete();

            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            string sql = sqlBuilder.GetSql();
            int    ret = ExecuteNonQuery(sql);

            if (ret > 0)
            {
                return(new ResultInfo <int>(false, "", "0", int.Parse(ret.ToString())));
            }
            else
            {
                return(new ResultInfo <int>(true, "删除记录失败,删除记录数为0", GetErrCode("100001"), -1));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 针对符合要求的Model对象进行Update操作(条件部分只能进行And合并)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <param name="exp_Where"></param>
        /// <param name="exp_Sel"></param>
        /// <returns></returns>
        public ResultInfo <int> Update <T>(T info, Expression <Func <T, bool> >[] exp_Where, params Expression <Func <T, object> >[] exp_Sel) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(info, TableModelMapper.GetTableName(typeof(T)))
                                         .Update()
                                         .Set(exp_Sel);

            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            string sql = sqlBuilder.GetSql();

            int ret = ExecuteNonQuery(sql);

            if (ret > 0)
            {
                return(new ResultInfo <int>(false, "", "0", ret));
            }
            else
            {
                return(new ResultInfo <int>(true, "更新记录失败", GetErrCode("100001"), -1));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 针对符合要求的Model对象进行Insert操作
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <param name="expressions"></param>
        /// <returns></returns>
        public ResultInfo <int> Insert <T>(T info, params Expression <Func <T, object> >[] expressions) where T : ModelBase, new()
        {
            string sql = CreateSqlBuilder <T>(info, TableModelMapper.GetTableName(typeof(T)))
                         .Insert()
                         .AddInsert(expressions)
                         .WithIdentity()
                         .GetSql();
            object ret = ExecuteScalar(sql);

            if (ret != null)
            {
                return(new ResultInfo <int>(false, "", "0", int.Parse(ret.ToString())));
            }
            else
            {
                return(new ResultInfo <int>(true, "新增记录失败", GetErrCode("100001"), -1));
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 针对符合要求的Model对象进行单字段条件的查询操作,返回INT(条件部分只能进行And合并)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public int Count <T>(params Expression <Func <T, bool> >[] exp_Where) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(TableModelMapper.GetTableName(typeof(T)))
                                         .SelectCount();

            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            string sql = sqlBuilder.GetSql();

            return(int.Parse(ExecuteScalar(sql).ToString()));
        }
Exemplo n.º 8
0
        public async Task RenderAsync(CancellationToken cancellationToken = default)
        {
            var relationshipFinder = new RelationshipFinder(Tables);
            var mapper             = new TableModelMapper(IdentifierDefaults, RowCounts, relationshipFinder);

            var graphvizFactory = new GraphvizExecutableFactory();

            using var graphviz = graphvizFactory.GetExecutable();

            var tableTasks = Tables.Select(async table =>
            {
                var tableModel = mapper.Map(table);
                var outputPath = Path.Combine(ExportDirectory.FullName, table.Name.ToSafeKey() + ".html");
                if (!ExportDirectory.Exists)
                {
                    ExportDirectory.Create();
                }

                XNamespace svgNs   = "http://www.w3.org/2000/svg";
                XNamespace xlinkNs = "http://www.w3.org/1999/xlink";

                var dotRenderer = new DotSvgRenderer(graphviz.DotPath);
                foreach (var diagram in tableModel.Diagrams)
                {
                    var svgFilePath = Path.Combine(ExportDirectory.FullName, diagram.ContainerId + ".svg");
                    var svg         = await dotRenderer.RenderToSvgAsync(diagram.Dot, cancellationToken).ConfigureAwait(false);

                    // ensure links open in new window with right attrs
                    var doc = XDocument.Parse(svg, LoadOptions.PreserveWhitespace);
                    doc.ReplaceTitlesWithTableNames();

                    var linkNodes = doc.Descendants(svgNs + "a");
                    foreach (var linkNode in linkNodes)
                    {
                        linkNode.SetAttributeValue("target", "_blank");
                        linkNode.SetAttributeValue("rel", "noopener noreferrer");
                        linkNode.SetAttributeValue(xlinkNs + "show", "new");
                    }

                    using (var writer = new StringWriter())
                    {
                        var svgRoot = doc.Root;
                        svgRoot.Attribute("width")?.Remove();
                        svgRoot.Attribute("height")?.Remove();

                        svgRoot.Save(writer, SaveOptions.DisableFormatting);
                        var svgText = writer.ToString();
                        if (svgText.StartsWith(XmlDeclaration))
                        {
                            svgText = svgText.Substring(XmlDeclaration.Length);
                        }
                        diagram.Svg = svgText;
                    }

                    // disable links, replace them with a <g>, i.e. a dummy element
                    linkNodes = doc.Descendants(svgNs + "a");
                    foreach (var linkNode in linkNodes)
                    {
                        linkNode.RemoveAttributes();
                        linkNode.Name = svgNs + "g";
                    }

                    using (var writer = File.CreateText(svgFilePath))
                        doc.Save(writer, SaveOptions.DisableFormatting);
                }

                var renderedTable = await Formatter.RenderTemplateAsync(tableModel, cancellationToken).ConfigureAwait(false);

                var databaseName = !IdentifierDefaults.Database.IsNullOrWhiteSpace()
                    ? IdentifierDefaults.Database + " Database"
                    : "Database";
                var pageTitle      = table.Name.ToVisibleName() + " · Table · " + databaseName;
                var tableContainer = new Container(renderedTable, pageTitle, "../");
                var renderedPage   = await Formatter.RenderTemplateAsync(tableContainer, cancellationToken).ConfigureAwait(false);

                using (var writer = File.CreateText(outputPath))
                {
                    await writer.WriteAsync(renderedPage.AsMemory(), cancellationToken).ConfigureAwait(false);
                    await writer.FlushAsync().ConfigureAwait(false);
                }
            });

            await Task.WhenAll(tableTasks).ConfigureAwait(false);
        }
Exemplo n.º 9
0
 /// <summary>
 /// 根据类型获取对应的表名
 /// </summary>
 /// <param name="t"></param>
 /// <returns></returns>
 protected string GetTableName(Type t)
 {
     return(TableModelMapper.GetTableName(t));
 }