public override T Get(Guid id)
 {
     try
     {
         SelectRequest request = new SelectRequest();
         request.SelectExpression = "select * from " + m_objectMapper.TableName + " where itemName() = '" + id + "'";
         SelectResponse response = m_simpleDBClient.Select(request);
         if (response.IsSetSelectResult() && response.SelectResult.Item.Count == 1)
         {
             SimpleDBObjectReader <T> objectReader = new SimpleDBObjectReader <T>(response.SelectResult, m_objectMapper.SetValue);
             return(objectReader.First());
         }
         else if (response.IsSetSelectResult() && response.SelectResult.Item.Count == 1)
         {
             throw new ApplicationException("Multiple rows were returned for Get with id=" + id + ".");
         }
         else
         {
             return(default(T));
         }
     }
     catch (Exception excp)
     {
         logger.Error("Exception SimpleDBAssetPersistor Get(for " + typeof(T).Name + "). " + excp.Message);
         throw;
     }
 }
Exemple #2
0
        public string Request(Domain.Delete pDelete)
        {
            #region Delete conditions validation

            if (pDelete.Conditions.Count == 0)
            {
                throw new Exception("SimpleSQL: unparameterized delete is not supported.");
            }

            #endregion

            string mDomainName = this.GetDomainName(pDelete.Table);

            SelectResponse mSelectResponse = this.ProcessSimpleSelect(mDomainName, null, pDelete.Conditions, pDelete.Table);

            int mTotalDeleteItems = 0;

            if (mSelectResponse.IsSetSelectResult())
            {
                DeleteAttributesRequest mDeleteAction;

                foreach (Item mCurrentItem in mSelectResponse.SelectResult.Item)
                {
                    if (mCurrentItem.IsSetName())
                    {
                        mDeleteAction = new DeleteAttributesRequest().WithDomainName(mDomainName).WithItemName(mCurrentItem.Name);
                        this.aSimpleDBClient.DeleteAttributes(mDeleteAction);

                        mTotalDeleteItems++;
                    }
                }
            }

            return(mTotalDeleteItems.ToString());
        }
Exemple #3
0
        private DataTable TransformSelectResult(string pTableName, List <string> pAttributes, SelectResponse pSelectResponse)
        {
            DateTime mStart = DateTime.Now;

            DataTable mReturn = new DataTable();

            if (pSelectResponse.SelectResult.Item.Count == 0)
            {
                return(null);
            }

            if (pSelectResponse.IsSetSelectResult())
            {
                mReturn.TableName = pTableName;

                bool mAllAttributes = pAttributes.Count == 1 ? pAttributes[0].Equals("*") : false;

                if (!mAllAttributes)
                {
                    pAttributes.ForEach(attr => mReturn.Columns.Add(attr.Trim(), typeof(string)));
                }
                else
                {
                    pSelectResponse.SelectResult.Item[0].Attribute.ForEach(x => mReturn.Columns.Add(x.Name.Trim(), typeof(string)));
                    mReturn.Columns.Remove("SimpleSQL_TableName");
                }

                DataRow mNewRow;

                foreach (Item mCurrentItem in pSelectResponse.SelectResult.Item)
                {
                    if (mCurrentItem.IsSetAttribute())
                    {
                        mNewRow = mReturn.NewRow();

                        foreach (Amazon.SimpleDB.Model.Attribute mCurrentAttribute in mCurrentItem.Attribute)
                        {
                            if (mCurrentAttribute.IsSetName() && mCurrentAttribute.IsSetValue())
                            {
                                if (mReturn.Columns.Contains(mCurrentAttribute.Name.Trim()))
                                {
                                    mNewRow[mCurrentAttribute.Name.Trim()] = mCurrentAttribute.Value;
                                }
                            }
                        }

                        mReturn.Rows.Add(mNewRow);
                    }
                }
            }
            else
            {
                return(null);
            }

            Debug.WriteLine(string.Format("TRANSFORM: {0}", DateTime.Now.Subtract(mStart).ToString()));

            return(mReturn);
        }
Exemple #4
0
        public string Request(Domain.Update pUpdate)
        {
            #region Update conditions validation

            if (pUpdate.Conditions.Count == 0)
            {
                throw new Exception("SimpleSQL: unparameterized update is not supported.");
            }

            #endregion

            string mDomainName = this.GetDomainName(pUpdate.Table);

            SelectResponse mSelectResponse = this.ProcessSimpleSelect(mDomainName, null, pUpdate.Conditions, pUpdate.Table);

            int mTotalUpdatedItems = 0;

            if (mSelectResponse.IsSetSelectResult())
            {
                foreach (Item mCurrentItem in mSelectResponse.SelectResult.Item)
                {
                    if (mCurrentItem.IsSetName())
                    {
                        List <ReplaceableAttribute> mUpdateTargets = new List <ReplaceableAttribute>();

                        foreach (Condition mTarget in pUpdate.Targets)
                        {
                            mUpdateTargets.Add(new ReplaceableAttribute()
                            {
                                Name    = mTarget.Attribute,
                                Value   = mTarget.Values[0].ToString(),
                                Replace = true
                            }
                                               );
                        }

                        PutAttributesRequest mReplaceAction = new PutAttributesRequest().WithDomainName(mDomainName).WithItemName(mCurrentItem.Name).WithAttribute(mUpdateTargets.ToArray());

                        this.aSimpleDBClient.PutAttributes(mReplaceAction);

                        mTotalUpdatedItems++;
                    }
                }
            }

            return(mTotalUpdatedItems.ToString());
        }
 public override object GetProperty(Guid id, string propertyName)
 {
     try
     {
         SelectRequest request = new SelectRequest();
         request.SelectExpression = "select " + propertyName.ToLower() + " from " + m_objectMapper.TableName + " where itemName() = '" + id.ToString() + "'";
         SelectResponse response = m_simpleDBClient.Select(request);
         if (response.IsSetSelectResult())
         {
             return(response.SelectResult.Item[0].Attribute[0].Value);
         }
         return(null);
     }
     catch (Exception excp)
     {
         logger.Error("Exception SimpleDBAssetPersistor GetProperty (for " + typeof(T).Name + "). " + excp.Message);
         throw;
     }
 }
Exemple #6
0
        /// <summary>
        /// Put the user's reco into the ZigMeRecos domain in SimpleDB
        /// </summary>
        /// <returns></returns>
        private bool SaveRecoToSimpleDB(string myFBId)
        {
            AmazonSimpleDB sdb = GetSDB();

            // Creating a domain
            String domainName = "ZigMeRecos";
            CreateDomainRequest createDomain = (new CreateDomainRequest()).WithDomainName(domainName);

            sdb.CreateDomain(createDomain);

            // Check to see how many recos this FB user id has stored in our domain
            String         selectExpression    = "Select * From ZigMeRecos Where FBId = '" + myFBId + "'";
            SelectRequest  selectRequestAction = new SelectRequest().WithSelectExpression(selectExpression);
            SelectResponse selectResponse      = sdb.Select(selectRequestAction);

            int cRecos = 0;

            // Now store the actual recommendation item
            if (selectResponse.IsSetSelectResult())
            {
                SelectResult selectResult = selectResponse.SelectResult;
                cRecos = selectResult.Item.Count;
            }
            cRecos++;
            String recoItem = "Reco_" + myFBId + "_" + cRecos;
            PutAttributesRequest        putAttributesRecoItem = new PutAttributesRequest().WithDomainName(domainName).WithItemName(recoItem);
            List <ReplaceableAttribute> attributesRecoItem    = putAttributesRecoItem.Attribute;

            attributesRecoItem.Add(new ReplaceableAttribute().WithName("FBId").WithValue(myFBId));
            attributesRecoItem.Add(new ReplaceableAttribute().WithName("Name").WithValue(RecoName.Text));
            attributesRecoItem.Add(new ReplaceableAttribute().WithName("Email").WithValue(ContactEmail.Text));
            attributesRecoItem.Add(new ReplaceableAttribute().WithName("City").WithValue(RecoCity.Text));
            attributesRecoItem.Add(new ReplaceableAttribute().WithName("Service").WithValue(RecoService.SelectedValue));
            PutAttributesResponse putAttributesResponse = sdb.PutAttributes(putAttributesRecoItem);

            return(putAttributesResponse.IsSetResponseMetadata());
        }
Exemple #7
0
        public override object Execute(Expression expression)
        {
            try {
                Type   elementType  = TypeSystem.GetElementType(expression.Type);
                string methodName   = ((MethodCallExpression)expression).Method.Name;
                bool   isIQueryable = expression.Type.FullName.StartsWith("System.Linq.IQueryable");
                string queryString  = String.Format(this.Translate(expression), m_domainName);

                if (!OrderBy.IsNullOrBlank())
                {
                    string orderByField = (OrderBy.IndexOf(' ') != -1) ? OrderBy.Substring(0, OrderBy.IndexOf(' ')) : OrderBy;
                    // SimpleDB queries with an order clause must have the order field included as a predicate.
                    // If the select query does not contain the a predicate with the order field add it here.
                    if (!queryString.Contains(orderByField))
                    {
                        queryString += " and " + orderByField + " like '%'";
                    }
                    queryString += " order by " + OrderBy;
                }

                if (Count != Int32.MaxValue)
                {
                    queryString += " limit " + Count;
                }

                //logger.Debug(queryString);

                if (!queryString.IsNullOrBlank())
                {
                    //logger.Debug("SimpleDB select: " + queryString + ".");
                    SelectRequest request = new SelectRequest();
                    request.SelectExpression = queryString;
                    SelectResponse response = m_service.Select(request);
                    if (response.IsSetSelectResult())
                    {
                        if (elementType == typeof(Int32))
                        {
                            return(Convert.ToInt32(response.SelectResult.Item[0].Attribute[0].Value));
                        }
                        else
                        {
                            object result = Activator.CreateInstance(
                                typeof(SimpleDBObjectReader <>).MakeGenericType(elementType),
                                BindingFlags.Instance | BindingFlags.Public, null,
                                new object[] { response.SelectResult, m_setter },
                                null);

                            if (isIQueryable)
                            {
                                return(result);
                            }
                            else
                            {
                                IEnumerator enumerator = ((IEnumerable)result).GetEnumerator();
                                if (enumerator.MoveNext())
                                {
                                    return(enumerator.Current);
                                }
                                else
                                {
                                    return(null);
                                }
                            }
                        }
                    }
                    throw new ApplicationException("No results for SimpleDB query.");
                }
                else
                {
                    throw new ApplicationException("The expression translation by the SimpleDBQueryProvider resulted in an empty select string.");
                }
            }
            catch (Exception excp) {
                logger.Error("Exception SimpleDBQueryProvider Execute. " + expression.ToString() + ". " + excp.Message);
                throw;
            }
        }