Exemple #1
0
        public virtual DbCommandSource CreateDbCommandForAddEntity <TEntity>(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            var mapping = this.DbMappings.GetDbMapping(typeof(TEntity));

            if (mapping == null)
            {
                throw new ArgumentException(string.Format("Cannot get the mapping of type {0}", typeof(TEntity).FullName), "TEntity");
            }

            var dbCommandSource = new DbCommandSource();
            var sqlText         = new StringBuilder();

            sqlText.AppendLine("INSERT INTO {0}", mapping.TableName);
            var columns = new List <string>();

            foreach (var column in mapping.Columns)
            {
                var objVal = column.PropertyInfo.GetValue(entity, null);
                if (column.IngoreOnInsert)
                {
                    continue;
                }
                columns.Add(column.ColumnName);
                dbCommandSource.Parameters.Add(column.ColumnName, objVal);
            }
            sqlText.AppendLine("            ({0})", string.Join(", ", columns));
            sqlText.AppendLine("     VALUES ({0})", string.Join(", ", columns.Select(c => this.ParameterSymbol + c)));

            dbCommandSource.CommandText = sqlText.ToString();
            return(dbCommandSource);
        }
Exemple #2
0
        public virtual DbCommandSource CreateDbCommandForGetList <TEntity>(object conditions = null, object order = null)
        {
            var mapping = this.DbMappings.GetDbMapping(typeof(TEntity));

            if (mapping == null)
            {
                throw new ArgumentException(string.Format("Cannot get the mapping of type {0}", typeof(TEntity).FullName), "TEntity");
            }

            var sqlText = new StringBuilder();

            sqlText.AppendLine("SELECT {0}", string.Join(", ", mapping.Columns.Select(c => c.ColumnName)));
            sqlText.AppendLine("  FROM {0}", mapping.TableName);

            var dbCommandSource             = new DbCommandSource();
            var dbCommandSourceForCondition = this.GetDbConditions(conditions);

            if (dbCommandSourceForCondition != null)
            {
                sqlText.AppendLine(" WHERE {0}", dbCommandSourceForCondition.CommandText);
                foreach (var param in dbCommandSourceForCondition.Parameters)
                {
                    dbCommandSource.Parameters.Add(param.Key, param.Value);
                }
            }
            var orderString = this.GetOrders(order);

            if (orderString != null)
            {
                sqlText.AppendLine(" ORDER BY {0}", orderString);
            }
            dbCommandSource.CommandText = sqlText.ToString();
            return(dbCommandSource);
        }
Exemple #3
0
        public virtual DbCommand CreateDbCommand(DbCommandSource dbCommandSource)
        {
            var dbCommand = this.CreateDbCommand(dbCommandSource.CommandText);

            foreach (var parameter in dbCommandSource.Parameters)
            {
                dbCommand.AppendParameter(parameter.Key, parameter.Value);
            }
            return(dbCommand);
        }
Exemple #4
0
        public virtual DbCommandSource CreateDbCommandForUpdateEntity <TEntity>(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            var mapping = this.DbMappings.GetDbMapping(typeof(TEntity));

            if (mapping == null)
            {
                throw new ArgumentException(string.Format("Cannot get the mapping of type {0}", typeof(TEntity).FullName), "TEntity");
            }

            var dbCommandSource = new DbCommandSource();
            var sqlText         = new StringBuilder();

            sqlText.AppendLine("UPDATE {0}", mapping.TableName);
            var columns = new List <string>();
            var isFirst = true;

            foreach (var column in mapping.Columns)
            {
                var objVal = column.PropertyInfo.GetValue(entity, null);
                if (column.IsPrimaryKey)
                {
                    columns.Add(string.Format("{0} = {1}{0}", column.ColumnName, this.ParameterSymbol));
                }
                else
                {
                    if (isFirst)
                    {
                        sqlText.AppendLine("   SET {0} = {1}{0}", column.ColumnName, this.ParameterSymbol);
                        isFirst = false;
                    }
                    else
                    {
                        sqlText.AppendLine("      ,{0} = {1}{0}", column.ColumnName, this.ParameterSymbol);
                    }
                }
                dbCommandSource.Parameters.Add(column.ColumnName, objVal);
            }
            if (columns != null && columns.Any())
            {
                sqlText.AppendLine(" WHERE {0}", string.Join(" AND ", columns));
            }
            else
            {
                throw new ArgumentException(string.Format("The type {0} must has one or more primary key(s)", typeof(TEntity).FullName), "TEntity");
            }

            dbCommandSource.CommandText = sqlText.ToString();
            return(dbCommandSource);
        }
Exemple #5
0
        public virtual DbCommandSource CreateDbCommandForDeleteEntity(string tableName, object conditions = null)
        {
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentNullException("tableName");
            }

            var dbCommandSource = new DbCommandSource();
            var sqlText         = new StringBuilder();

            sqlText.AppendLine("DELETE FROM {0}", tableName);
            var columns = new List <string>();
            var isFirst = true;

            if (conditions != null)
            {
                if (conditions is string)
                {
                    sqlText.AppendLine(" WHERE {0}", conditions);
                }
                else
                {
                    isFirst = true;
                    foreach (var columnProperty in conditions.GetType().GetProperties().Where(p => p.CanRead))
                    {
                        var objVal = columnProperty.GetValue(conditions, null);
                        if (isFirst)
                        {
                            sqlText.AppendLine(" WHERE {0} = {1}get_{0}", columnProperty.Name, this.ParameterSymbol);
                            isFirst = false;
                        }
                        else
                        {
                            sqlText.AppendLine("   AND {0} = {1}get_{0}", columnProperty.Name, this.ParameterSymbol);
                        }
                        dbCommandSource.Parameters.Add("get_" + columnProperty.Name, objVal);
                    }
                }
            }

            dbCommandSource.CommandText = sqlText.ToString();
            return(dbCommandSource);
        }