Beispiel #1
0
        public static T Find(long id)
        {
            if (id == null)
            {
                return(null);
            }

            ActiveRecordMap activeRecordMap = ActiveRecord <T> .GetMap();

            string query = @"SELECT " + SqlBuilderServices.BuildSqlColumnList(activeRecordMap.ColumnList(true), activeRecordMap.TableName, SqlBuilderServices.ColumnListType.List)
                           + " FROM " + activeRecordMap.TableName
                           + " WHERE " + activeRecordMap.PrimaryKeyColumnName + " = @" + activeRecordMap.PrimaryKeyColumnName;


            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter(activeRecordMap.PrimaryKeyColumnName
                                 , SqlBuilderServices.GetDBTypeFromSystemType(activeRecordMap.PrimaryKeyColumnMap.Property.PropertyType)
                                 , id)
            };

            IDataReader dataReader = _activeRecordSession.DatabaseServices.ExecuteDataReader(query, parameters);

            return(ObjectRetrievalServices.RetrieveObject <T>(dataReader, activeRecordMap));
        }
        protected override void Retrieve()
        {
            String sqlQuery;

            SqlParameter[] parameters;

            sqlQuery = @"SELECT " + SqlBuilderServices.BuildSqlColumnList(ActiveRecord <T> .GetMap().ColumnList(true), ActiveRecord <T> .GetMap().TableName, SqlBuilderServices.ColumnListType.List)
                       + " FROM " + ActiveRecord <T> .GetMap().TableName
                       + " INNER JOIN " + RelationshipTableName + " ON " + RelationshipTableName + "." + ChildKeyColumnName + " = " + ActiveRecord <T> .GetMap().TableName + "." + ChildKeyColumnName
                       + " WHERE " + ParentKeyColumnName + " = @" + ParentKeyColumnName;

            parameters = new SqlParameter[]
            {
                new SqlParameter(ParentKeyColumnName
                                 , SqlBuilderServices.GetDBTypeFromSystemType(_parentObject.ActiveRecordMap.PrimaryKeyColumnMap.Property.PropertyType)
                                 , _parentObject.ActiveRecordMap.PrimaryKeyColumnMap.Property.GetValue(_parentObject, null))
            };

            ObjectList = ActiveRecord <T> .FindManyWithSql(sqlQuery, parameters);

            foreach (T obj in ObjectList)
            {
                _retrivedObjects.Add(obj.Id);
            }

            _loadStatus = LoadStatuses.Loaded;
        }
Beispiel #3
0
        public void Update()
        {
            string command = @"UPDATE " + _activeRecordMap.TableName
                             + " SET " + SqlBuilderServices.BuildSqlColumnList(_activeRecordMap.ColumnList(false), _activeRecordMap.TableName, SqlBuilderServices.ColumnListType.UpdateParameterizedList)
                             + " WHERE " + _activeRecordMap.PrimaryKeyColumnName + " = @" + _activeRecordMap.PrimaryKeyColumnName;

            SqlParameter[] parameters = new SqlParameter[_activeRecordMap.ColumnMappings.Count + 1];

            //Set Column Parameters
            int parameterIndex = 0;

            foreach (ColumnMap columnMap in _activeRecordMap.ColumnMappings.Values)
            {
                parameters[parameterIndex] = new SqlParameter(columnMap.ColumnName
                                                              , SqlBuilderServices.GetDBTypeFromSystemType(columnMap.Property.PropertyType)
                                                              , columnMap.Property.GetValue(this, null));

                if (parameters[parameterIndex].Type == DbType.DateTime && (DateTime)parameters[parameterIndex].Value == DateTime.MinValue)
                {
                    parameters[parameterIndex].Value = DateTime.Parse("1/1/1900");
                }

                parameterIndex++;
            }

            //Set Primary Key Parameter
            parameters[parameterIndex] = new SqlParameter(_activeRecordMap.PrimaryKeyColumnName
                                                          , SqlBuilderServices.GetDBTypeFromSystemType(_activeRecordMap.PrimaryKeyColumnMap.Property.PropertyType)
                                                          , _activeRecordMap.PrimaryKeyColumnMap.Property.GetValue(this, null));

            _activeRecordSession.DatabaseServices.ExecuteNonQuery(command, parameters);
        }
Beispiel #4
0
        public void Insert()
        {
            string command = @"INSERT INTO " + _activeRecordMap.TableName
                             + "(" + SqlBuilderServices.BuildSqlColumnList(_activeRecordMap.ColumnList(false), _activeRecordMap.TableName, SqlBuilderServices.ColumnListType.List) + ")"
                             + " VALUES(" + SqlBuilderServices.BuildSqlColumnList(_activeRecordMap.ColumnList(false), _activeRecordMap.TableName, SqlBuilderServices.ColumnListType.ParameterizedList) + ")";

            SqlParameter[] parameters = new SqlParameter[_activeRecordMap.ColumnMappings.Count];

            //Set Column Parameters
            int parameterIndex = 0;

            foreach (ColumnMap columnMap in _activeRecordMap.ColumnMappings.Values)
            {
                SqlParameter parameter = new SqlParameter(columnMap.ColumnName
                                                          , SqlBuilderServices.GetDBTypeFromSystemType(columnMap.Property.PropertyType)
                                                          , columnMap.Property.GetValue(this, null));

                if (parameter.Type == DbType.DateTime && (DateTime)parameter.Value == DateTime.MinValue)
                {
                    parameter.Value = DateTime.Parse("1/1/1900");
                }

                parameters[parameterIndex] = parameter;
                parameterIndex++;
            }
            //Execute Insert Command
            _activeRecordSession.DatabaseServices.ExecuteNonQuery(command, parameters);

            //Retrive Primary Key
            string query = @"SELECT MAX(" + _activeRecordMap.PrimaryKeyColumnName + ")"
                           + " FROM " + _activeRecordMap.TableName
                           + " WHERE " + SqlBuilderServices.BuildSqlColumnList(_activeRecordMap.ColumnList(false), _activeRecordMap.TableName, SqlBuilderServices.ColumnListType.WhereParameterizedList);

            _activeRecordMap.PrimaryKeyColumnMap.Property.SetValue(this, _activeRecordSession.DatabaseServices.ExecuteScalar(query, parameters), null);
        }
Beispiel #5
0
        public static List <T> FindAll()
        {
            ActiveRecordMap activeRecordMap = ActiveRecord <T> .GetMap();

            string query = @"SELECT " + SqlBuilderServices.BuildSqlColumnList(activeRecordMap.ColumnList(true), activeRecordMap.TableName, SqlBuilderServices.ColumnListType.List)
                           + " FROM " + activeRecordMap.TableName;

            IDataReader dataReader = _activeRecordSession.DatabaseServices.ExecuteDataReader(query);

            return(ObjectRetrievalServices.RetrieveObjects <T>(dataReader, activeRecordMap));
        }
Beispiel #6
0
        public static T FindFirstWhere(String where, SqlParameter[] parameters)
        {
            ActiveRecordMap activeRecordMap = ActiveRecord <T> .GetMap();

            string query = @"SELECT " + SqlBuilderServices.BuildSqlColumnList(activeRecordMap.ColumnList(true), activeRecordMap.TableName, SqlBuilderServices.ColumnListType.List)
                           + " FROM " + activeRecordMap.TableName
                           + " WHERE " + where;

            IDataReader dataReader = _activeRecordSession.DatabaseServices.ExecuteDataReader(query, parameters);

            return(ObjectRetrievalServices.RetrieveObject <T>(dataReader, activeRecordMap));
        }
Beispiel #7
0
        public void Refresh()
        {
            ActiveRecordMap activeRecordMap = ActiveRecord <T> .GetMap();

            string query = @"SELECT " + SqlBuilderServices.BuildSqlColumnList(activeRecordMap.ColumnList(true), activeRecordMap.TableName, SqlBuilderServices.ColumnListType.List)
                           + " FROM " + activeRecordMap.TableName
                           + " WHERE " + activeRecordMap.PrimaryKeyColumnName + " = @" + activeRecordMap.PrimaryKeyColumnName;


            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter(activeRecordMap.PrimaryKeyColumnName
                                 , SqlBuilderServices.GetDBTypeFromSystemType(activeRecordMap.PrimaryKeyColumnMap.Property.PropertyType)
                                 , this.Id)
            };

            IDataReader dataReader = _activeRecordSession.DatabaseServices.ExecuteDataReader(query, parameters);

            ObjectRetrievalServices.RetrieveObject <T>(dataReader, activeRecordMap, true);
        }