Пример #1
0
        public static IActTResult <TEntity> Map <TEntity>(IDataSource dataSource) where TEntity : class
        {
            var entityType = typeof(TEntity);
            var pmc        = EntityMapCache.GetEntityMap(entityType).PropertyMapList;

            if (pmc == null || pmc.Count == 0)
            {
                throw new Exception("no property.");
            }
            var entity = (TEntity)Activator.CreateInstance(entityType);

            foreach (var p in pmc)
            {
                string columnName;
                if (p.Column != null && !StringHelper.IsNullOrEmptyOrBlankString(p.Column.ColumnName))
                {
                    columnName = p.Column.ColumnName;
                }
                else
                {
                    columnName = p.Property.PropertyName;
                }
                if (!dataSource.HasField(columnName))
                {
                    continue;
                }
                var pValue = dataSource.GetFieldValue(columnName);
                p.Property.SetValue(entity, pValue);
            }
            return(new ActTResult <TEntity>(entity));
        }
Пример #2
0
 public ITableQueryWhere <T> From(string tableAlias)
 {
     if (StringHelper.IsNullOrEmptyOrBlankString(tableAlias))
     {
         var entityType = typeof(T);
         var em         = EntityMapCache.GetEntityMap(entityType);
         tableAlias = em.Table == null ? null : em.Table.TableName;
     }
     this.Command.CommandText_From = this.DataManager.DataProvider.TableCommandGenerator.From(tableAlias);
     return(new TableQueryWhere <T>(this.DataManager, this.Command));
 }
Пример #3
0
        public ITableActResult Insert <T>(string connectionString, T entity, string tableAlias,
                                          bool includeIdAutoIncrease = false) where T : class
        {
            var entityType = typeof(T);
            var em         = EntityMapCache.GetEntityMap(entityType);

            if (StringHelper.IsNullOrEmptyOrBlankString(tableAlias) && em != null && em.Table != null)
            {
                tableAlias = em.Table.TableName;
            }
            List <DbParameter> pList = new List <DbParameter>();
            List <string>      fc    = new List <string>();

            foreach (
                var pm in
                em.PropertyMapList.Where(
                    p =>
                    p.Column == null || (p.Column != null && p.Column.IsIdAutoIncrease == includeIdAutoIncrease))
                )
            {
                var wName = pm.Column != null ? pm.Column.ColumnName : pm.Property.PropertyName;
                fc.Add(wName);

                var p = this.DataProvider.GenerateParam(pm.Property.GetValue(entity), wName);
                if (p != null && !pList.Contains(p))
                {
                    pList.Add(p);
                }
            }

            ITableInsertCommand command = new TableInsertCommand();

            command.CommandText_Insert    = this.DataProvider.TableCommandGenerator.Insert(tableAlias);
            command.CommandText_IntoValue = this.DataProvider.TableCommandGenerator.IntoValue(fc.ToArray());
            var commandText = this.DataProvider.TableCommandGenerator.CommandText(command);

            try
            {
                var result = new TableActionResult();
                //result.EffectedRowNo = this.DataProvider.ExecuteNonQuery(commandText, pList.ToArray());
                var ds = this.DataProvider.ExecuteDataset(connectionString, CommandType.Text, commandText,
                                                          pList.ToArray());
                if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    result.IdentityRowNo = Convert.ToInt64(ds.Tables[0].Rows[0][0]);
                }
                return(result);
            }
            catch (Exception ex)
            {
                return(new TableActionResult(ex.Message));
            }
        }
Пример #4
0
        public ITableDelete <T> Delete <T>(string connectionString, string tableAlias) where T : class
        {
            var entityType = typeof(T);
            var em         = EntityMapCache.GetEntityMap(entityType);

            if (StringHelper.IsNullOrEmptyOrBlankString(tableAlias) && em != null && em.Table != null)
            {
                tableAlias = em.Table.TableName;
            }
            ITableDeleteCommand command = new TableDeleteCommand();

            command.CommandTextDelete = this.DataProvider.TableCommandGenerator.Delete(tableAlias);
            command.ConnectionString  = connectionString;
            return(new TableDelete <T>(this, command));
        }
Пример #5
0
        public ITableActResult Delete <T>(string connectionString, T entity, string tableAlias) where T : class
        {
            var entityType = typeof(T);
            var em         = EntityMapCache.GetEntityMap(entityType);

            if (StringHelper.IsNullOrEmptyOrBlankString(tableAlias) && em != null && em.Table != null)
            {
                tableAlias = em.Table.TableName;
            }
            var whereFields =
                em.PropertyMapList.Where(p => p.Column != null && p.Column.IsPrimaryKey == true).ToList <PropertyMap>();
            List <DbParameter> pList = new List <DbParameter>();
            List <string>      fc    = new List <string>();

            foreach (var wp in whereFields)
            {
                var wName = wp.Column != null ? wp.Column.ColumnName : wp.Property.PropertyName;
                fc.Add(wName);

                var p = this.DataProvider.GenerateParam(wp.Property.GetValue(entity), wName);
                if (p != null && !pList.Contains(p))
                {
                    pList.Add(p);
                }
            }

            ITableDeleteCommand command = new TableDeleteCommand();

            command.CommandTextDelete = this.DataProvider.TableCommandGenerator.Delete(tableAlias);
            command.CommandText_Where = this.DataProvider.TableCommandGenerator.Where(fc.ToArray());
            var commandText = this.DataProvider.TableCommandGenerator.CommandText(command);

            try
            {
                this.DataProvider.ExecuteNonQuery(connectionString, CommandType.Text, commandText, pList.ToArray());
                return(new TableActionResult());
            }
            catch (Exception ex)
            {
                return(new TableActionResult(ex.Message));
            }
        }
Пример #6
0
        public static IActTResult <TEntity> Map <TEntity>(NameValueCollection collection) where TEntity : class
        {
            var entityType = typeof(TEntity);
            var pmc        = EntityMapCache.GetEntityMap(entityType).PropertyMapList;

            if (pmc == null || pmc.Count == 0)
            {
                throw new Exception("no property.");
            }
            var entity = (TEntity)Activator.CreateInstance(entityType);

            foreach (var p in pmc)
            {
                var pId    = string.Format("{0}_{1}", p.TypeName, p.Property.PropertyName);
                var pValue = collection[pId];
                if (pValue != null)
                {
                    p.Property.SetValue(entity, pValue);
                }
            }
            return(new ActTResult <TEntity>(entity));
        }
Пример #7
0
        public virtual string IntoValue <T>(List <T> entityList, string tableAlias, out List <DbParameter> paraList)
        {
            var entityType = typeof(T);
            var em         = EntityMapCache.GetEntityMap(entityType);

            if (StringHelper.IsNullOrEmptyOrBlankString(tableAlias) && em != null && em.Table != null)
            {
                tableAlias = em.Table.TableName;
            }
            var propertyList = em.PropertyMapList.Where(
                p => p.Column == null || (p.Column != null && p.Column.IsIdAutoIncrease == false)).ToList();

            var columnSql = new StringBuilder();

            columnSql.Append("(");
            var sql = new StringBuilder();

            bool isColumnGenerated = false;
            var  pIndex            = 0;
            var  pList             = new List <DbParameter>();

            for (int j = 0; j < entityList.Count; j++)
            {
                sql.Append("(");
                for (int i = 0; i < propertyList.Count; i++)
                {
                    var p = propertyList[i];
                    if (!isColumnGenerated)
                    {
                        var columnName = p.Column != null ? p.Column.ColumnName : p.Property.PropertyName;
                        columnSql.Append(columnName);
                        if (i == propertyList.Count - 1)
                        {
                            columnSql.Append(")");
                        }
                        else
                        {
                            columnSql.Append(",");
                        }
                    }
                    var pName = string.Format("@P{0}", pIndex);
                    sql.Append(pName);
                    pIndex++;
                    pList.Add(LFFK.DataManager.DataProvider.GenerateParam(p.Property.GetValue(entityList[j]), pName));
                    if (i == propertyList.Count - 1)
                    {
                        isColumnGenerated = true;
                    }
                    else
                    {
                        sql.Append(",");
                    }
                }
                sql.Append(")");
                if (j < entityList.Count - 1)
                {
                    sql.Append(",");
                }
                else
                {
                    sql.Append(";");
                }
            }

            paraList = pList;
            return(string.Format("START TRANSACTION; INSERT INTO {0}{1} VALUES {2} COMMIT;", tableAlias, columnSql, sql));
        }
Пример #8
0
        public virtual string Update <T>(List <T> entityList, string tableAlias, out List <DbParameter> paraList,
                                         params Expression <Func <T, object> >[] columnNameFileterExps) where T : class
        {
            var entityType = typeof(T);
            var em         = EntityMapCache.GetEntityMap(entityType);

            if (StringHelper.IsNullOrEmptyOrBlankString(tableAlias) && em != null && em.Table != null)
            {
                tableAlias = em.Table.TableName;
            }

            List <PropertyMap> propertyList = new List <PropertyMap>();

            if (columnNameFileterExps != null && columnNameFileterExps.Length > 0)
            {
                var fields = ExpressionHelper.GetPropertyMapNamesFromExps(columnNameFileterExps);
                for (var i = 0; i < fields.Length; i++)
                {
                    propertyList.Add(DataMapper.FindColumnByPropertyName <T>(fields[i], em.PropertyMapList));
                }
            }
            else
            {
                propertyList = em.PropertyMapList.ToList();
            }

            var columnSql = new StringBuilder();

            columnSql.Append("(");
            var columnUpdateSql = new StringBuilder();
            var sql             = new StringBuilder();

            bool isColumnGenerated = false;
            var  pIndex            = 0;
            var  pList             = new List <DbParameter>();

            for (int j = 0; j < entityList.Count; j++)
            {
                sql.Append("(");
                for (int i = 0; i < propertyList.Count; i++)
                {
                    var p = propertyList[i];
                    if (!isColumnGenerated)
                    {
                        var columnName = p.Column != null ? p.Column.ColumnName : p.Property.PropertyName;
                        columnSql.Append(columnName);
                        columnUpdateSql.Append(columnName);
                        columnUpdateSql.Append("=VALUES(");
                        columnUpdateSql.Append(columnName);
                        columnUpdateSql.Append(")");
                        if (i == propertyList.Count - 1)
                        {
                            columnSql.Append(")");
                            columnUpdateSql.Append(";");
                        }
                        else
                        {
                            columnSql.Append(",");
                            columnUpdateSql.Append(",");
                        }
                    }
                    var pName = string.Format("@P{0}", pIndex);
                    sql.Append(pName);
                    pIndex++;

                    pList.Add(LFFK.DataManager.DataProvider.GenerateParam(p.Property.GetValue(entityList[j]), pName));

                    if (i == propertyList.Count - 1)
                    {
                        isColumnGenerated = true;
                    }
                    else
                    {
                        sql.Append(",");
                    }
                }
                sql.Append(")");
                if (j < entityList.Count - 1)
                {
                    sql.Append(",");
                }
            }
            paraList = pList;
            return
                (string.Format("START TRANSACTION; INSERT INTO {0}{1} VALUES {2} ON DUPLICATE KEY UPDATE {3} COMMIT;",
                               tableAlias, columnSql.ToString(), sql.ToString(), columnUpdateSql.ToString()));
        }
Пример #9
0
        public static PropertyMapCollection FindPropertyList <TEnttiy>()
        {
            var entityType = typeof(TEnttiy);

            return(EntityMapCache.GetEntityMap(entityType).PropertyMapList);
        }
Пример #10
0
        public ITableActResult Update <T>(string connectionString, T entity, string tableAlias,
                                          params System.Linq.Expressions.Expression <Func <T, object> >[]
                                          columnNameFileterExps) where T : class
        {
            var entityType = typeof(T);
            var em         = EntityMapCache.GetEntityMap(entityType);

            if (StringHelper.IsNullOrEmptyOrBlankString(tableAlias) && em != null && em.Table != null)
            {
                tableAlias = em.Table.TableName;
            }

            List <PropertyMap> setFields = new List <PropertyMap>();

            if (columnNameFileterExps != null && columnNameFileterExps.Length > 0)
            {
                var fields = ExpressionHelper.GetPropertyMapNamesFromExps(columnNameFileterExps);
                for (var i = 0; i < fields.Length; i++)
                {
                    var p = DataMapper.FindColumnByPropertyName <T>(fields[i], em.PropertyMapList);
                    if (p != null && (p.Column == null || (p.Column != null && p.Column.IsIdAutoIncrease == false)))
                    {
                        setFields.Add(p);
                    }
                }
            }

            var whereFields =
                em.PropertyMapList.Where(p => p.Column != null && p.Column.IsPrimaryKey == true).ToList <PropertyMap>();

            ITableUpdateCommand updateCommand = new TableUpdateCommand();

            updateCommand.CommandText_Update = this.DataProvider.TableCommandGenerator.Update(tableAlias);
            List <DbParameter> pList = new List <DbParameter>();

            PropertyMap[] pmList;
            if (setFields != null && setFields.Count > 0)
            {
                pmList = setFields.ToArray();
            }
            else
            {
                pmList =
                    em.PropertyMapList.Where(
                        p => p.Column == null || (p.Column != null && p.Column.IsIdAutoIncrease == false))
                    .ToArray <PropertyMap>();
            }
            List <string> fc       = new List <string>();
            List <string> fcParaed = new List <string>();

            for (var i = 0; i < pmList.Length; i++)
            {
                var cName = pmList[i].Column != null ? pmList[i].Column.ColumnName : pmList[i].Property.PropertyName;
                fc.Add(cName);
                var p = this.DataProvider.GenerateParam(pmList[i].Property.GetValue(entity), cName);
                if (p != null && !pList.Contains(p))
                {
                    pList.Add(p);
                    fcParaed.Add(cName);
                }
            }
            updateCommand.CommandText_Set = this.DataProvider.TableCommandGenerator.Set(fc.ToArray());
            fc.Clear();
            foreach (var wp in whereFields)
            {
                var wName = wp.Column != null ? wp.Column.ColumnName : wp.Property.PropertyName;
                fc.Add(wName);
                if (!fcParaed.Contains(wName))
                {
                    var p = this.DataProvider.GenerateParam(wp.Property.GetValue(entity), wName);
                    if (p != null)
                    {
                        pList.Add(p);
                    }
                }
            }
            updateCommand.CommandText_Where = this.DataProvider.TableCommandGenerator.Where(fc.ToArray());

            var commandText = this.DataProvider.TableCommandGenerator.CommandText(updateCommand);

            try
            {
                this.DataProvider.ExecuteNonQuery(connectionString, CommandType.Text, commandText, pList.ToArray());
                return(new TableActionResult());
            }
            catch (Exception ex)
            {
                return(new TableActionResult(ex.Message));
            }
        }