Ejemplo n.º 1
0
        /// <summary>
        /// Delete by primary key
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="id">The primary key to be searched.</param>
        /// <returns>Record ID if successfull, -1 otherwise</returns>
        public static int Delete <T>(int id) where T : ModelBase, new()
        {
            var obj    = new T();
            int result = -1;

            if (obj != null)
            {
                string query = String.Format(Queries.DeleteByID, obj.TableNameFullyQualified, obj.PrimaryKeyFullyQualified);

                using (Cinch cinch = new Cinch(query, CommandType.Text))
                {
                    cinch.AddParameter("id", SqlDbType.Int, id);

                    try
                    {
                        result = cinch.ExecuteScalarInt();
                    }
                    catch
                    {
                        throw;
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// UPDATE
        /// </summary>
        /// <param name="obj">The object to be updated.</param>
        /// <param name="cols">The whitelist of columns to be inserted.</param>
        /// <returns>Record ID if successfull, -1 otherwise</returns>
        public static int Update <T>(T obj, List <string> cols = null) where T : ModelBase
        {
            int result = -1;

            if (obj != null)
            {
                CinchMapping mappings = Mapper.MapProperties(obj, cols);
                string       query    = String.Format(Queries.Update, obj.TableNameFullyQualified, mappings.UpdateValuesQueryParamsString, obj.PrimaryKeyFullyQualified);

                using (Cinch cinch = new Cinch(query, CommandType.Text))
                {
                    cinch.AddParameters(mappings.SqlParams);
                    cinch.AddParameter("id", SqlDbType.Int, obj.ID);

                    try
                    {
                        result = cinch.ExecuteScalarInt();
                    }
                    catch
                    {
                        throw;
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Find first by primary key
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="id">The primary key to be searched.</param>
        /// <returns>The object if found; null, otherwise.</returns>
        public static T FindFirst <T>(int id) where T : ModelBase, new()
        {
            var obj = new T();

            if (obj != null)
            {
                string query = String.Format(Queries.FindFirst, obj.ColumnsFullyQualified, obj.TableNameFullyQualified, obj.TableName, obj.PrimaryKeyFullyQualified);

                using (Cinch cinch = new Cinch(query, CommandType.Text))
                {
                    cinch.AddParameter("id", SqlDbType.Int, id);
                    obj = cinch.FillObject <T>();
                }
            }

            return(obj);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Checks if an object exists by primary key
        /// </summary>
        /// <param name="obj">The object to be searched.</param>
        /// <param name="column"></param>
        /// <param name="value"></param>
        /// <returns>The number of existent objects.</returns>
        public static int Exists <T>(int id) where T : ModelBase, new()
        {
            var obj    = new T();
            int result = -1;

            if (obj != null)
            {
                string query = String.Format(Queries.FindFirst, obj.ColumnsFullyQualified, obj.TableNameFullyQualified, obj.TableName, obj.PrimaryKeyFullyQualified);

                using (Cinch cinch = new Cinch(query, CommandType.Text))
                {
                    cinch.AddParameter("id", SqlDbType.Int, id);
                    result = cinch.ExecuteScalarInt();
                }
            }

            return(result);
        }