Beispiel #1
0
        /// <summary>
        /// Get the specified instance of the type.
        /// </summary>
        /// <param name="id">Instance primary key.</param>
        /// <returns>The specified instance.</returns>
        private static T ReadFromDatabase(long id, bool closeConnection = true)
        {
            T             instance = default;
            ORMSqlCommand cmd      = ORMEntity <T> .SqlDialect.GetSelectCommand();

            // Connecto to database
            ORMEntity <T> .Connect();

            // Set command parameters
            ORMEntity <T> .SetParameter(cmd.PrimaryKeyName, id);

            // Execute the SELECT sentence to retrieve the instance properties
            using (DbDataReader reader = ORMEntity <T> .ExecuteReader(cmd.SqlCommand))
            {
                if (reader.Read())
                {
                    instance = ORMEntity <T> .MapData(reader);
                }
            }

            // Close the connection to database
            if (closeConnection)
            {
                ORMEntity <T> .Disconnect();
            }

            return(instance);
        }
Beispiel #2
0
        /// <summary>
        /// Get the specified instance of the type.
        /// </summary>
        /// <param name="id">Instance primary key.</param>
        /// <returns>The specified instance.</returns>
        private static ICollection <T> ReadRecords(string propertyName, object value, bool closeConnection = true)
        {
            List <long>   ids  = new List <long>();
            List <T>      list = new List <T>();
            ORMSqlCommand cmd  = ORMEntity <T> .SqlDialect.GetSelectByFieldCommand(propertyName);

            // Connecto to database
            ORMEntity <T> .Connect();

            // Set filter parameter
            ORMEntity <T> .SetParameter(cmd.Parameters[0], value);

            // Execute the SELECT sentence to retrieve the instance properties
            using (DbDataReader reader = ORMEntity <T> .ExecuteReader(cmd.SqlCommand))
            {
                while (reader.Read())
                {
                    ids.Add((long)reader[0]);
                }
            }

            // Close the connection to database
            if (closeConnection)
            {
                ORMEntity <T> .Disconnect();
            }

            // Get all instances
            foreach (long id in ids)
            {
                if (!ORMEntity <T> .InMemoryTable.ContainsKey(id))
                {
                    T instance = ORMEntity <T> .Get(id, closeConnection);

                    list.Add(instance);
                }
                else
                {
                    list.Add(ORMEntity <T> .InMemoryTable[id]);
                }
            }

            return(list);
        }
Beispiel #3
0
        /// <summary>
        /// Get the specified instance of the type.
        /// </summary>
        /// <param name="id">Instance primary key.</param>
        /// <returns>The specified instance.</returns>
        private static ICollection <T> ReadAllRecords(ICollection <String> sortProperties = null)
        {
            List <long>   ids  = new List <long>();
            List <T>      list = new List <T>();
            ORMSqlCommand cmd  = ORMEntity <T> .SqlDialect.GetSelectAllCommand(sortProperties);

            // Connecto to database
            ORMEntity <T> .Connect();

            // Execute the SELECT sentence to retrieve the instance properties
            using (DbDataReader reader = ORMEntity <T> .ExecuteReader(cmd.SqlCommand))
            {
                while (reader.Read())
                {
                    ids.Add((long)reader[0]);
                }
            }

            // Close the connection to database
            ORMEntity <T> .Disconnect();

            // Get all instances
            foreach (long id in ids)
            {
                if (!ORMEntity <T> .InMemoryTable.ContainsKey(id))
                {
                    T instance = ORMEntity <T> .Get(id);

                    list.Add(instance);
                }
                else
                {
                    list.Add(ORMEntity <T> .InMemoryTable[id]);
                }
            }

            return(list);
        }