예제 #1
0
        private static SQLiteDataReader ExecuteReader(string sql)
        {
            using (SQLiteCommand cmd = new SQLiteCommand(sql, PersistentEntity <T> .Connection))
            {
                PersistentEntity <T> .SetCommandParameters(cmd);

                return(cmd.ExecuteReader());
            }
        }
예제 #2
0
        public static T MapData(Type type, SQLiteDataReader reader)
        {
            T instance = (T)Activator.CreateInstance(typeof(T), new object[] { });

            foreach (var prop in type.GetProperties())
            {
                PersistentEntity <T> .SetValue(instance, prop, reader);
            }

            return(instance);
        }
예제 #3
0
        /// <summary>
        /// Get the specified instance of the type.
        /// </summary>
        /// <param name="id">Instance primary key.</param>
        /// <returns>The specified instance.</returns>
        public static T Get(long id)
        {
            T      instance  = default(T);
            String tableName = ORMSqlManager <T> .GetTypeTableName();

            // Check if the instance is in memory
            if (PersistentEntity <T> .database == null)
            {
                PersistentEntity <T> .database = new Dictionary <long, object>();
            }
            if (PersistentEntity <T> .database.ContainsKey(id))
            {
                return((T)PersistentEntity <T> .database[id]);
            }

            // Get the SQL sentence
            if (PersistentEntity <T> .SqlSelectByPrimaryKey == null)
            {
                // PersistentEntity<T>.SqlSelectByPrimaryKey = ORMSqlManager<T>.SelectByPrimaryKey(typeof(T));
            }

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

            //PersistentEntity<T>.

            //// Execute the SELECT sentence to retrieve the instance properties
            //using (SQLiteDataReader reader = ExecuteReader(PersistentEntity<T>.SqlSelectByPrimaryKey))
            //{
            //   if (reader.Read())
            //   {
            //      instance = PersistentEntity<T>.MapData(typeof(T), reader);
            //   }
            //}

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

            // Store instance in memory database
            PersistentEntity <T> .database.Add(id, instance);

            return(instance);
        }
예제 #4
0
        private static void SetValue(object instance, PropertyInfo propertyInfo, SQLiteDataReader reader)
        {
            ORMProperty[] props = (ORMProperty[])propertyInfo.GetCustomAttributes(typeof(ORMProperty), false);
            if (props.Length > 0)
            {
                if (PersistentEntity <T> .IsOrmInstance(propertyInfo.PropertyType))
                {
                    //var methodInfo = propertyInfo.PropertyType.GetMethod("Get", System.Reflection.BindingFlags.FlattenHierarchy
                    //                                                            | System.Reflection.BindingFlags.Public
                    //                                                            | System.Reflection.BindingFlags.Static);
                    //methodInfo.Invoke(this, new object[] { (long)reader[props[0].FieldName] });


                    //PersistentEntity<T> refInstance = (PersistentEntity<T>)Activator.CreateInstance(propertyInfo.PropertyType, new object[] { });
                    //propertyInfo.SetValue(refInstance, PersistentEntity<T>.Get((long)reader[props[0].FieldName]));
                }
                else
                {
                    propertyInfo.SetValue(instance, reader[props[0].FieldName]);
                }
            }

            propertyInfo.SetValue(instance, reader[props[0].FieldName]);
        }