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 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 #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> 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);
        }
Beispiel #3
0
        /// <summary>
        /// Delete an existing instance from database.
        /// </summary>
        /// <param name="instance">Instance unique identifier.</param>
        private static int DeleteDatabaseForeignRecords(long id)
        {
            int        rowsAffected = 0;
            object     dummy;
            T          instance = default;
            Type       foreignType;
            MethodInfo deleteMethod;

            // Check if there are any foreign collection to delete in cascade
            foreach (ORMEntityMember member in ORMEntity <T> .ORMStructure.ForeignCollections)
            {
                // Get the owner instance
                if (instance == null)
                {
                    instance = ORMEntity <T> .Get(id);
                }

                if (((ORMForeignCollection)member.Attribute).OnDeleteAction == OnDeleteActionTypes.DeleteInCascade)
                {
                    // Get the method GET(long)
                    foreignType  = member.ForeignCollectionType;
                    foreignType  = typeof(ORMEntity <>).MakeGenericType(foreignType);
                    deleteMethod = foreignType.GetMethod("Delete", new Type[] { typeof(Int64) });

                    // Delete each instance in collection
                    foreach (long objId in ORMEntity <T> .GetForeignCollectionIdentifiers(instance, member))
                    {
                        // Invoke the method
                        dummy         = Activator.CreateInstance(foreignType);
                        rowsAffected += (int)deleteMethod.Invoke(dummy, new object[] { objId });
                    }
                }
            }

            return(rowsAffected);
        }
Beispiel #4
0
 /// <summary>
 /// Get the requested instance.
 /// </summary>
 /// <param name="id">Instance unique identifier (DB).</param>
 /// <returns>The requested instance or <c>null</c> if the instance cannot be found.</returns>
 public static T Get(Int32 id)
 {
     return(ORMEntity <T> .Get((Int64)id, true));
 }