Esempio n. 1
0
        public static String BuildUpdateSql(Object obj, String ID, out SqlParameter[] parameters)
        {
            StringBuilder            set        = new StringBuilder();
            List <PropertyAttribute> attributes = ORMUtils.GetFieldAttributes(obj);

            parameters = new SqlParameter[0];
            List <SqlParameter> listParam = parameters.ToList <SqlParameter>();

            for (int i = 0; i < attributes.Count; i++)
            {
                PropertyAttribute attribute = attributes[i];
                if (attribute.Property.Name.Equals("ID"))
                {
                    continue;
                }
                if (ORMUtils.IsDefaultValue(obj, attribute.Property))
                {
                    continue;
                }
                String paramName = String.Format("@{0}", attribute.Property.Name);
                listParam.Add(new SqlParameter(paramName, attribute.Value));
                set.Append(String.Format("{0} = {1} , ", attribute.Property.Name, paramName));
            }
            set.Length = set.Length - 2;
            String whereParamName = "@ID";

            listParam.Add(new SqlParameter(whereParamName, ID));
            parameters = listParam.ToArray <SqlParameter>();
            return(String.Format("update {0} set {1} where ID = {2}", ORMUtils.GetTableName(obj), set.ToString(), whereParamName));;
        }
Esempio n. 2
0
 private void SaveAllocationTable(ISession session)
 {
     try
     {
         QueryParams <PersistentStorageAllocationTable> query = new QueryParams <PersistentStorageAllocationTable>(
             GetAllocationTableCriteria(), 1);
         IList <PersistentStorageAllocationTable> resultList = QueryHelper.Query <PersistentStorageAllocationTable>(session, query, LOG_QUERY);
         PersistentStorageAllocationTable         table      = null;
         if (resultList.Count == 0)
         {
             table         = new PersistentStorageAllocationTable();
             table.Id      = new EntityId(SYSTEM_ID, mDeviceId, 0L);
             table.Version = new EntityVersion(mDeviceId);
         }
         else
         {
             table = resultList[0];
             table.Version.IncSeqNumber();
         }
         using (MemoryStream ms = new MemoryStream())
         {
             SerializationHelper.Write <ItemTable>(mAllocationTable, ms, mTableFormatter, false);
             table.ItemAllocationTableData = ms.ToArray();
         }
         ORMUtils.SaveEntity(table, session);
     }
     catch (Exception ex)
     {
         throw new PersistenceException("Unable to persist allocation table.", ex);
     }
 }
Esempio n. 3
0
        public static String BuildDeleteSql(Object obj, String ID, out SqlParameter parameter)
        {
            String paramName = "@ID";

            parameter = new SqlParameter(paramName, ID);
            return(String.Format("delete from {0} where ID = {1}", ORMUtils.GetTableName(obj), paramName));
        }
Esempio n. 4
0
        public static String BuildCreateSql(Object obj)
        {
            StringBuilder create = new StringBuilder();

            create.Append(String.Format("create table {0} (", ORMUtils.GetTableName(obj)));
            List <PropertyAttribute> attributes = ORMUtils.GetFieldAttributes(obj);

            foreach (PropertyAttribute attribute in attributes)
            {
                String restrain = "";
                if (attribute.Colmun == null)
                {
                    continue;
                }
                if (attribute.Colmun.PrimaryKey)
                {
                    restrain = "primary key";
                }
                String IsNull = "not null";
                if (attribute.Colmun.IsNull)
                {
                    IsNull = "";
                }
                String col = String.Format("{0} {1} {2} {3}, ", attribute.Property.Name, attribute.Colmun.Type, restrain, IsNull);
                create.Append(col);
            }
            create.Length = create.Length - 2;
            create.Append(")");
            return(create.ToString());
        }
Esempio n. 5
0
        private void AsyncResultCall(IAsyncResult asyncResult)
        {
            SqlCommand    command    = asyncResult.AsyncState as SqlCommand;
            SqlDataReader dataReader = command.EndExecuteReader(asyncResult);
            List <T>      list       = ORMUtils.SetModelListValue <T>(dataReader);

            Rows = list;
            dataReader.Close();
            synchronization.Post(new SendOrPostCallback(SendMethod), null);
        }
Esempio n. 6
0
        public int CreateTable()
        {
            if (SqlHelper.ExistTable(ORMUtils.GetTableName(this)))
            {
                return(0);
            }
            String create = ORMFactory.BuildCreateSql(this);

            return(SqlHelper.ExecuteNonQuery(create));
        }
Esempio n. 7
0
        public static long Count(Object obj, Where where)
        {
            String count = String.Format("select Count(ID) from {0}", ORMUtils.GetTableName(obj));

            if (where == null)
            {
                return(Convert.ToInt64(SqlHelper.ExecuteScalar(count)));
            }
            SqlParameter[] sqlParameters;
            count = String.Format("{0} {1}", count, where.Build(out sqlParameters));
            return(Convert.ToInt64(SqlHelper.ExecuteScalar(count, sqlParameters)));
        }
Esempio n. 8
0
        public virtual bool Find(Where where)
        {
            if (where.Count() == 0)
            {
                return(false);
            }
            SqlParameter[] parameters;
            String         whereStr   = where.Build(out parameters);
            String         find       = String.Format("select * from {0} {1}", ORMUtils.GetTableName(this), whereStr);
            SqlDataReader  dataReader = SqlHelper.ExecuteReader(find, parameters);

            return(ORMUtils.SetModelValue(dataReader, this));
        }
Esempio n. 9
0
        private void ExceAsync()
        {
            SqlParameter[] parameters = null;
            String         whereStr   = Where.Build(out parameters);

            Total = ORMSupport.Count(new T(), Where);
            if (Total == 0)
            {
                return;
            }
            String select = String.Format("select * from {0} {1} ",
                                          ORMUtils.GetTableName(typeof(T)), whereStr);
            String pageSelect = Page.BuildSql(select, Order);

            SqlHelper.BeginExecuteReader(pageSelect, parameters, AsyncResultCall);
        }
Esempio n. 10
0
        private void Exce()
        {
            SqlParameter[] parameters = null;
            String         whereStr   = Where.Build(out parameters);

            Total = ORMSupport.Count(new T(), Where);
            if (Total == 0)
            {
                return;
            }
            String select = String.Format("select * from {0} {1}",
                                          ORMUtils.GetTableName(typeof(T)), whereStr);
            String        pageSelect = Page.BuildSql(select, Order);
            SqlDataReader dataReader = SqlHelper.ExecuteReader(pageSelect, parameters);
            List <T>      list       = ORMUtils.SetModelListValue <T>(dataReader);

            Rows = list;
            dataReader.Close();
        }
Esempio n. 11
0
        private void RemoveObject(EntityId entityId, ISession session)
        {
            QueryParams <PersistentStorageItem> query = new QueryParams <PersistentStorageItem>(
                new GroupCriteria(
                    new ArithmeticCriteria("id.systemId", entityId.SystemId),
                    new ArithmeticCriteria("id.deviceId", entityId.DeviceId),
                    new ArithmeticCriteria("id.id", entityId.Id)), 1);
            IList <PersistentStorageItem> resultList = QueryHelper.Query <PersistentStorageItem>(session, query, LOG_QUERY);
            PersistentStorageItem         item       = null;

            if (resultList.Count == 0)
            {
                throw new Exception(String.Format("Unable to remove object. Id: {0}", entityId));
            }
            else
            {
                item = resultList[0];
                ORMUtils.DeleteEntity(item, session);
            }
        }
Esempio n. 12
0
 private void Reset()
 {
     using (ISession session = GetSession())
     {
         try
         {
             using (ITransaction transaction = session.BeginTransaction())
             {
                 {
                     QueryParams <PersistentStorageAllocationTable> query = new QueryParams <PersistentStorageAllocationTable>(
                         GetAllocationTableCriteria(), 1);
                     IList <PersistentStorageAllocationTable> resultList = QueryHelper.Query <PersistentStorageAllocationTable>(session, query, LOG_QUERY);
                     if (resultList.Count > 0)
                     {
                         ORMUtils.DeleteEntity(resultList[0], session);
                     }
                 }
                 {
                     QueryParams <PersistentStorageItem> query = new QueryParams <PersistentStorageItem>(
                         new GroupCriteria(
                             new ArithmeticCriteria("id.systemId", SYSTEM_ID),
                             new ArithmeticCriteria("id.deviceId", mDeviceId)));
                     IList <PersistentStorageItem> resultList = QueryHelper.Query <PersistentStorageItem>(session, query, LOG_QUERY);
                     if (resultList.Count > 0)
                     {
                         foreach (PersistentStorageItem item in resultList)
                         {
                             ORMUtils.DeleteEntity(item, session);
                         }
                         resultList.Clear();
                     }
                 }
                 transaction.Commit();
             }
         }
         catch (Exception ex)
         {
             throw new PersistenceException("Unable to reset storage provider.", ex);
         }
     }
 }
Esempio n. 13
0
 private void SaveObject(EntityId entityId, T o, ISession session)
 {
     try
     {
         QueryParams <PersistentStorageItem> query = new QueryParams <PersistentStorageItem>(
             new GroupCriteria(
                 new ArithmeticCriteria("id.systemId", entityId.SystemId),
                 new ArithmeticCriteria("id.deviceId", entityId.DeviceId),
                 new ArithmeticCriteria("id.id", entityId.Id)), 1);
         IList <PersistentStorageItem> resultList = QueryHelper.Query <PersistentStorageItem>(session, query, LOG_QUERY);
         PersistentStorageItem         item       = null;
         if (resultList.Count == 0)
         {
             item         = new PersistentStorageItem();
             item.Id      = entityId;
             item.Version = new EntityVersion(mDeviceId);
         }
         else
         {
             item = resultList[0];
         }
         if (o == null)
         {
             item.EntryData = null;
         }
         else
         {
             using (MemoryStream ms = new MemoryStream())
             {
                 SerializationHelper.Write <T>(o, ms, DataFormatter, CompressContent);
                 item.EntryData = ms.ToArray();
             }
         }
         ORMUtils.SaveEntity(item, session);
     }
     catch (Exception ex)
     {
         throw new PersistenceException(String.Format("Unable to save object. Id: {0}", entityId), ex);
     }
 }
Esempio n. 14
0
        public static String BuildInsertSql(Object obj, out SqlParameter[] parameters)
        {
            StringBuilder            clomun     = new StringBuilder("(");
            StringBuilder            value      = new StringBuilder("values(");
            List <PropertyAttribute> attributes = ORMUtils.GetFieldAttributes(obj);

            parameters = new SqlParameter[attributes.Count];
            for (int i = 0; i < attributes.Count; i++)
            {
                PropertyAttribute attribute = attributes[i];
                clomun.Append(String.Format("{0} , ", attribute.Property.Name));
                String paramName = String.Format("@{0}", attribute.Property.Name);
                value.Append(String.Format("{0} , ", paramName));
                parameters[i] = new SqlParameter(paramName, attribute.Value);
            }
            clomun.Length = clomun.Length - 2;
            clomun.Append(")");
            value.Length = value.Length - 2;
            value.Append(")");
            String insert = String.Format("insert into {0} {1} {2}", ORMUtils.GetTableName(obj),
                                          clomun.ToString(), value.ToString());

            return(insert);
        }
Esempio n. 15
0
        public void TestEntitySave()
        {
            using (ISession session = mSessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    // create products if they are not exist...
                    Product apple = null;
                    Product pear  = null;
                    Product peach = null;

                    IListSpecialized <Product> productList = QueryHelper.Query <Product>(session, new QueryParams <Product>(new ArithmeticCriteria("name", "Apple"), 1));
                    if (productList.Count > 0)
                    {
                        apple = productList[0];
                    }
                    else
                    {
                        apple = new Product();
                        //tp.Id = 1;
                        apple.Name = "Apple";
                        ORMUtils.SaveEntity(apple, session);
                    }

                    productList = QueryHelper.Query <Product>(session, new QueryParams <Product>(new ArithmeticCriteria("name", "Pear"), 1));
                    if (productList.Count > 0)
                    {
                        pear = productList[0];
                    }
                    else
                    {
                        pear = new Product();
                        //tp.Id = 1;
                        pear.Name = "Pear";
                        ORMUtils.SaveEntity(pear, session);
                    }

                    productList = QueryHelper.Query <Product>(session, new QueryParams <Product>(new ArithmeticCriteria("name", "Peach"), 1));
                    if (productList.Count > 0)
                    {
                        peach = productList[0];
                    }
                    else
                    {
                        peach = new Product();
                        //tp.Id = 1;
                        peach.Name = "Peach";
                        ORMUtils.SaveEntity(peach, session);
                    }

                    Customer customer1 = null;
                    Customer customer2 = null;

                    IListSpecialized <Customer> customerList = QueryHelper.Query <Customer>(session, new QueryParams <Customer>(new ArithmeticCriteria("name", "David"), 1));
                    if (customerList.Count > 0)
                    {
                        customer1 = customerList[0];
                    }
                    else
                    {
                        customer1 = new Customer();
                        //tp.Id = 1;
                        customer1.Name = "David";
                        ORMUtils.SaveEntity(customer1, session);
                    }

                    customerList = QueryHelper.Query <Customer>(session, new QueryParams <Customer>(new ArithmeticCriteria("name", "Thomas"), 1));
                    if (customerList.Count > 0)
                    {
                        customer2 = customerList[0];
                    }
                    else
                    {
                        customer2 = new Customer();
                        //tp.Id = 1;
                        customer2.Name = "Thomas";
                        ORMUtils.SaveEntity(customer2, session);
                    }

                    // create a shopping cart
                    ShoppingCart cart1 = new ShoppingCart();
                    cart1.Customer = customer1;
                    cart1.Products.Add(apple);
                    cart1.Products.Add(pear);
                    ORMUtils.SaveEntity(cart1, session);

                    ShoppingCart cart2 = new ShoppingCart();
                    cart2.Customer = customer2;
                    cart2.Products.Add(apple);
                    cart2.Products.Add(peach);
                    ORMUtils.SaveEntity(cart2, session);

                    transaction.Commit();
                }
            }
        }