コード例 #1
0
ファイル: DBHelper.cs プロジェクト: Amebus/KillersWearsPrada
        //
        /// <summary>
        /// returns an item given a clue ad an item kind
        /// </summary>
        /// <param name="c">a clue object</param>
        /// <param name="itemKind">an itemkind</param>
        /// <returns> an Item object that contains, in order : 
        ///         1. Item code
        ///         2. Barcode
        ///         3. Item Name
        ///         4. Item Price
        ///         5. Item Description
        ///         6. Item Reparto
        ///         7. Texture file name
        ///         8. Image file name
        ///         9. item kind
        /// </returns>
        public Item GetItemFromAbstractItem(AbstractItem Item)
        {
            try
            {
                DBConnection.Open();
            }
            catch
            {
                DBConnection.Close();
                DBConnection.Open();
            }

            // note : we want items of which we have more than 10 available
            string query = "SELECT TOP 1 I.ID, I.Barcode, II.ItemName, II.Price, II.Description, II.Reparto, T.FileName, I.Image";
            query += " FROM Item AS I ,Texture AS T, ItemKind AS IK, TextureKind AS TK, ItemInfo AS II, Color AS C";
            query += " WHERE II.ItemKind = IK.ID AND I.ItemInfo = II.ID AND I.Texture = T.ID AND T.TextureKind = TK.ID AND T.MainColor = C.ID";
            query += " AND I.Available > 10 AND IK.ItemKind = @p5";

            string order = " ORDER BY Rnd(-(10000*I.ID)*Time())";

            string whereLong = " AND II.Long = @p1";
            string whereLight = " AND T.Light = @p2";
            string whereTexture = " AND TK.TextureKind = @p3";
            string whereColor = " AND C.Color = @p4";

            // parameter @p1 - shape
            if(Item.CheckPropertyByKind(E_PropertiesKind.SHAPE))
            {
                if (Item.GetProperty(E_PropertiesKind.SHAPE) == E_Shape.LONG.ToString())
                {
                    whereLong = whereLong.Replace("@p1", true.ToString());
                }
                else
                {
                    whereLong = whereLong.Replace("@p1", false.ToString());
                }
                query += whereLong;
            }

            // parameter @p2 - gradiation
            if (Item.CheckPropertyByKind(E_PropertiesKind.GRADIATION))
            {
                if (Item.GetProperty(E_PropertiesKind.GRADIATION) == E_Gradiation.LIGHT.ToString())
                {
                    whereLight = whereLight.Replace("@p2", true.ToString());
                }
                else
                {
                    whereLight = whereLight.Replace("@p2", false.ToString());
                }
                query += whereLight;
            }

            // parameter @p3 - texture kind
            if (Item.CheckPropertyByKind(E_PropertiesKind.TEXTURE))
            {
                whereTexture = whereTexture.Replace("@p3", "\'" + Item.GetProperty(E_PropertiesKind.TEXTURE).ToLower() + "\'");
                query += whereTexture;
            }

            // parameter @p4 - main color
            if (Item.CheckPropertyByKind(E_PropertiesKind.COLOR))
            {
                whereColor = whereColor.Replace("@p4", "\'" + Item.GetProperty(E_PropertiesKind.COLOR).ToLower() + "\'");
                query += whereColor;
            }

            // parameter @p5 - item kind
            query = query.Replace("@p5", "\'" + Item.ItemKind.ToString().ToLower() + "\'");

            query += order;

            OleDbCommand command = new OleDbCommand(query, DBConnection);

            OleDbDataReader result = command.ExecuteReader();

            result.Read();

            int codice = result.GetInt32(0);
            string barcode = result.GetString(1);
            string name = result.GetString(2);
            double price = result.GetDouble(3);
            string descr = result.GetString(4);
            string rep = result.GetString(5);
            string texture = result.GetString(6);
            string image = result.GetString(7);

            Item i = new Item(codice, barcode, name, price, descr, rep, texture, image, Item);

            DBConnection.Close();

            return i;
        }