public BuiltInsertCommand <T> AddItem(T item)
        {
            var value = new BuiltValueList <T>(_columns);

            foreach (var p2c in SqlTableHelper.PropertiesToColumnNames <T>())
            {
                if (!_columns.Contains(p2c.Value))
                {
                    continue;
                }

                value.AddValueFor(p2c.Value, p2c.Key.GetValue(item, null).ToString());
            }
            return(this);
        }
Esempio n. 2
0
        /// <summary>
        /// Takes the current row that the reader is on and uses it to construct a new entity of type T.<para />
        /// Does not alter the reader's cursor at all.
        /// May return an incomplete entity in case of failure.
        /// </summary>
        /// <param name="tableType">The type of entity to be constructed.</param>
        /// <param name="reader">The reader to be used. Make sure .HasRows is true before passing this in.</param>
        /// <returns>The constructed entity of type T.</returns>
        internal object BuildSingle(Type tableType, DbDataReader reader)
        {
            var propToCol = SqlTableHelper.PropertiesToColumnNames(tableType);
            var entity    = Activator.CreateInstance(tableType);

            foreach (var property in propToCol)
            {
                var sqlColumnAttribute = property.Key.GetCustomAttribute <SqlColumn>();
                if (sqlColumnAttribute != null && property.Key.GetCustomAttribute <SqlForeignKey>() == null)
                {
                    switch (sqlColumnAttribute.Type)
                    {
                    case DbType.Int16:
                    case DbType.Int32:
                    case DbType.Int64:
                        property.Key.SetValue(entity, Convert.ToInt32(reader[property.Value]), null);
                        break;

                    case DbType.String:
                    case DbType.StringFixedLength:
                    case DbType.AnsiString:
                    case DbType.AnsiStringFixedLength:
                        string value = reader[property.Value].ToString();
                        property.Key.SetValue(entity, value, null);
                        break;

                    case DbType.Date:
                    case DbType.DateTime:
                    case DbType.DateTime2:
                    case DbType.DateTimeOffset:
                        string   stringValue = reader[property.Value].ToString();
                        DateTime dateValue   = DateTime.ParseExact(stringValue, ProviderSpecific.DotNetDateFormatString, CultureInfo.InvariantCulture);
                        property.Key.SetValue(entity, dateValue, null);
                        break;

                    default:
                        throw new Exception("Unsupported column type.");
                    }
                }

                if (sqlColumnAttribute == null)
                {
                    continue;
                }
                var sqlForeignKeyAttributes = property.Key.GetCustomAttributes <SqlForeignKey>();
                foreach (var foreignKey in sqlForeignKeyAttributes)
                {
                    Type foreignTable = foreignKey.ForeignTable;
                    var  properties   = foreignTable.GetProperties();
                    foreach (var prop in properties)
                    {
                        var attrib = prop.GetCustomAttribute <SqlColumn>();
                        if (attrib == null)
                        {
                            continue;
                        }
                        if (attrib.ColumnName == foreignKey.ForeignTableColumnName)
                        {
                            var foreignSelect = SqlBuild.Select(foreignTable)
                                                .Join(tableType, foreignTable, sqlColumnAttribute.ColumnName, foreignKey.ForeignTableColumnName)
                                                .Where(
                                new BuiltSqlCondition()
                                .AddCondition(tableType, sqlColumnAttribute.ColumnName, "=", (string)reader[sqlColumnAttribute.ColumnName], sqlColumnAttribute.Type));
                            var foreignReader = foreignSelect.GenerateCommand(_connection).ExecuteReader();
                            if (!foreignReader.Read())
                            {
                                continue;
                            }

                            var foreignEntity = BuildSingle(foreignTable, foreignReader);
                            property.Key.SetValue(entity, foreignEntity);
                            break;
                        }
                    }
                }
            }
            return(entity);
        }