Example #1
0
        public void Insert(object entity)
        {
            var entityType = typeof(TEntity);

            var tableName = GetTableName(entityType, true);

            var props = entityType.GetProperties();

            StringBuilder columns = new StringBuilder().Append("(");
            StringBuilder values  = new StringBuilder().Append("(");

            foreach (var prop in props)
            {
                PrepareInsertArguments(entity, prop, values, columns);
            }

            columns.Remove(columns.Length - 1, 1).Append(')');
            values.Remove(values.Length - 1, 1).Append(')');
            try
            {
                ExecuteNonQueryCommand(Statements.CreateInsertStatement(tableName, columns.ToString(), values.ToString()));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Example #2
0
        public void Update(object entity)
        {
            var entityType = typeof(TEntity);

            var tableAlias = TableEntityAliasCache.GetOrAddAlias(entityType);

            var           tableName = GetTableName(entityType);
            var           props     = entityType.GetProperties();
            StringBuilder arguments = new StringBuilder();

            foreach (var prop in props)
            {
                PrepareUpdateArguments(entity, prop, arguments);
            }

            arguments.Remove(arguments.Length - 1, 1);

            var updateSql = Statements.CreateUpdateStatement(tableAlias, tableName, arguments.ToString(), ConditionSql);

            try
            {
                ExecuteNonQueryCommand(updateSql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Example #3
0
        public void Delete()
        {
            var entityType = typeof(TEntity);
            var tableAlias = TableEntityAliasCache.GetOrAddAlias(entityType);
            var tableName  = GetTableName(entityType);

            try
            {
                ExecuteNonQueryCommand(Statements.CreateDeleteStatement(tableAlias, ConditionSql, tableName));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Example #4
0
        private IEnumerable <TEntity> Select()
        {
            var entityType = typeof(TEntity);

            var entityAlias = TableEntityAliasCache.GetOrAddAlias(entityType);

            var tableName = GetTableName(entityType);

            var entityProperties = entityType.GetProperties();


            var columnNames = (from entityProperty in entityProperties
                               where entityProperty.GetCustomAttribute(typeof(ForeignKeyAttribute)) == null
                               select GetColumnName(entityProperty, true) into column
                               where column != null select $"{entityAlias}.{column}").ToList();

            LoadColumnsFromJoinedEntities(columnNames);
            var leftJoins = LoadLeftJoins();

            var sqlStatement = EntityJoinRequests.Count == 0 ? Statements.CreateSelectStatement(columnNames, tableName, ConditionSql) : Statements.CreateSelectWithJoinsStatement(columnNames, tableName, leftJoins, ConditionSql);


            return(GetEntities(sqlStatement, columnNames, entityType, entityProperties));
        }