Example #1
0
        public void Insert(T genericObject)
        {
            string       columns = BuildInsertColumns(out Type type);
            string       values  = BuildInsertValues(genericObject, type);
            OrmAttribute orm     = type.GetCustomAttribute <OrmAttribute>();
            string       query   = $"INSERT INTO {GetTableName(ref orm)} ({ columns }) VALUES ({ values })";

            using (SqlConnection connection = new SqlConnection(ConnectionString))
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    Debug.WriteLine(query);
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            reader.Close();
                        }
                    }

                    finally
                    {
                        connection?.Close();
                    }
                }
        }
Example #2
0
        public T Select(string id)
        {
            Type         type  = typeof(T);
            OrmAttribute orm   = type.GetCustomAttribute <OrmAttribute>();
            string       query = $"SELECT * FROM {GetTableName(ref orm)} WHERE {orm.IdName} = '{id}'";

            T result = default;

            using (SqlConnection connection = new SqlConnection(ConnectionString))
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    Debug.WriteLine(query);
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                result = ConvertToObject(reader, orm);
                            }

                            reader.Close();
                        }
                    }

                    finally
                    {
                        connection?.Close();
                    }
                }

            return(result);
        }
Example #3
0
        public IList <T> SelectAll()
        {
            IList <T>    result = new List <T>();
            OrmAttribute orm    = new OrmAttribute();

            string query = $"SELECT * FROM {GetTableName(ref orm)}";

            using (SqlConnection connection = new SqlConnection(ConnectionString))
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    Debug.WriteLine(query);
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                T entity = ConvertToObject(reader, orm);
                                result.Add(entity);
                            }

                            reader.Close();
                        }
                    }

                    finally
                    {
                        connection?.Close();
                    }
                }

            return(result);
        }
Example #4
0
        protected virtual object ConvertValue(SqlDataReader dataReader, PropertyInfo property, string entityIdName)
        {
            OrmAttribute orm        = property.GetCustomAttribute <OrmAttribute>();
            string       columnName = (entityIdName == null && orm != null) ? orm.Name.ToLower() : property.Name.ToLower();;

            object rawValue = entityIdName == null ? dataReader[columnName] : dataReader[entityIdName.ToLower()];
            object value;
            Type   valueType = property.PropertyType;

            if (valueType == typeof(Guid))
            {
                value = ConvertToGuid(rawValue);
            }
            else if (valueType.IsEnum)
            {
                value = Convert.ChangeType(Enum.ToObject(property.PropertyType, rawValue), property.PropertyType);
            }
            else if (valueType == typeof(QueryFreightResponseRoot))
            {
                value = Parser <QueryFreightResponseRoot> .FromJson((string)rawValue);
            }
            else
            {
                value = Convert.ChangeType(rawValue, property.PropertyType);
            };
            return(value);
        }
Example #5
0
        protected virtual T ConvertToObject(SqlDataReader dataReader, OrmAttribute orm)
        {
            Type type   = typeof(T);
            T    entity = (T)Activator.CreateInstance(type);

            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (property.GetCustomAttribute <OrmAttribute>().Ignore)
                {
                    continue;
                }

                object value;

                if (property.Name == "Id")
                {
                    value = ConvertValue(dataReader, property, orm.IdName);
                }
                else
                {
                    value = ConvertValue(dataReader, property, null);
                }

                SetValue(property, entity, value);
            }

            return(entity);
        }
Example #6
0
        public List <T> Query(string query)
        {
            List <T>     result = new List <T>();
            OrmAttribute orm    = typeof(T).GetCustomAttribute <OrmAttribute>();

            using (SqlConnection connection = new SqlConnection(ConnectionString))
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    Debug.WriteLine(query);
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                T entity = ConvertToObject(reader, orm);
                                if (entity != null)
                                {
                                    result.Add(entity);
                                }
                            }

                            reader.Close();
                        }
                    }

                    finally
                    {
                        connection?.Close();
                    }
                }

            return(result);
        }
Example #7
0
        public void Update(T genericObject, string id, string query = "")
        {
            if (query?.Length <= 0 || query == null)
            {
                string       updateParameters = BuildUpdateParameters(genericObject, out Type type);
                OrmAttribute orm = type.GetCustomAttribute <OrmAttribute>();
                query = $"UPDATE {GetTableName(ref orm)} SET { updateParameters } WHERE { orm.IdName } = { ConvertInputToSqlQuery(id) }";
            }

            using (SqlConnection connection = new SqlConnection(ConnectionString))
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    Debug.WriteLine(query);
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            reader.Close();
                        }
                    }

                    finally
                    {
                        connection?.Close();
                    }
                }
        }
Example #8
0
        protected virtual string GetTableName(ref OrmAttribute ormAttribute)
        {
            Type         type  = typeof(T);
            OrmAttribute orm   = type.GetCustomAttribute <OrmAttribute>();
            string       table = orm != null ? orm.Name : typeof(T).Name.ToLower();

            string schemaTable = Schema?.Length > 0 ? $"{Schema}.{table}" : table;

            ormAttribute = orm;
            return(schemaTable);
        }
Example #9
0
        protected virtual string BuildInsertColumns(out Type externalType)
        {
            Type type = typeof(T);

            PropertyInfo[] properties    = type.GetProperties();
            List <string>  propertyNames = new List <string>();

            foreach (PropertyInfo property in properties)
            {
                OrmAttribute propertyAttributes = property.GetCustomAttribute <OrmAttribute>();
                string       propertyName;

                if (propertyAttributes.Ignore == true)
                {
                    continue;
                }
                else if (propertyAttributes.Name == null)
                {
                    propertyName = type.GetCustomAttribute <OrmAttribute>().IdName.ToLower();
                }
                else
                {
                    propertyName = propertyAttributes.Name.ToLower();
                }

                propertyNames.Add(propertyName);
            }

            StringBuilder insertColumnsListBuilder = new StringBuilder();

            for (int i = 0; i < propertyNames.Count; i++)
            {
                insertColumnsListBuilder.Append(propertyNames[i]);

                if ((i + 1) < propertyNames.Count)
                {
                    insertColumnsListBuilder.Append(", ");
                }
            }

            externalType = type;
            return(insertColumnsListBuilder.ToString());
        }
Example #10
0
        public void Delete(string id)
        {
            OrmAttribute orm   = new OrmAttribute();
            string       query = $"DELETE {GetTableName(ref orm)} WHERE { orm.IdName } = { ConvertInputToSqlQuery(id) }";

            using (SqlConnection connection = new SqlConnection(ConnectionString))
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    Debug.WriteLine(query);
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            reader.Close();
                        }
                    }

                    finally
                    {
                        connection?.Close();
                    }
                }
        }