예제 #1
0
        public override DbCommand GetInsertCommand(ModelBase value, ModelMapping mapping)
        {
            lock (DbCommand)
            {
                DataTable schema = m_connection.GetSchema("Columns", new string[] { null, null, mapping.TableName });
                string    sql    = "insert into ";
                sql += mapping.TableName;
                sql += " values (";
                sql += value.Rid;
                List <PropertyFieldPair> listPair = new List <PropertyFieldPair>();
                foreach (DataRow row in schema.Rows)
                {
                    string columnName = row["COLUMN_NAME"].ToString();
                    if (columnName.ToLower() == "rid")
                    {
                        continue;
                    }
                    foreach (PropertyFieldPair pair in mapping.PropertyFields)
                    {
                        if (pair.FieldName.ToLower() == columnName.ToLower())
                        {
                            sql += ",";
                            //sql += "?";
                            object v = pair.Property.GetValue(value, null);
                            if (v == null)
                            {
                                sql += "''";
                            }
                            else if (row["DATA_TYPE"].ToString() == "12")
                            {
                                sql += string.Format("'{0}'", v.ToString());
                            }
                            else if (row["DATA_TYPE"].ToString() == "-5" && v.GetType() == typeof(DateTime))
                            {
                                DateTime dt = (DateTime)v;
                                TimeSpan ts = dt.Subtract(new DateTime(1970, 1, 1));
                                long     t  = Convert.ToInt64(ts.TotalMilliseconds);
                                sql += t.ToString();
                            }
                            else
                            {
                                sql += v.ToString();
                            }
                            //listPair.Add(pair);
                            goto Next;
                        }
                    }
                    sql += ",";
                    sql += GetDefaultValue(row["DATA_TYPE"].ToString());
Next:
                    continue;
                }
                sql += ");";
                DbCommand.CommandText = sql;
                DbCommand.Parameters.Clear();
                //foreach (PropertyFieldPair pair in listPair)
                //{
                //    DbCommand.Parameters.Add(GetParameter(value, pair));
                //}
            }
            return(DbCommand);
        }
예제 #2
0
 /// <summary>
 ///  创建ChildrenModel对象
 /// </summary>
 /// <param name="value">模型对象</param>
 internal ChildrenModel(ModelBase value)
 {
     model = value;
 }
예제 #3
0
        public static int AppendModel(ModelBase value, ModelMapping mapping, out Exception exception)
        {
            int count = 0;

            lock (s_locker)
            {
                exception = null;
                ModelFactory[] factoryList = GetFactories(mapping.ModelType);
                if (factoryList.Length <= 1)
                {
                    try
                    {
                        foreach (ModelFactory factory in factoryList)
                        {
                            DbCommand cmd = factory.GetAppendCommand(value, mapping);
                            if (factory.DbConnection.State == ConnectionState.Closed)
                            {
                                factory.DbConnection.Open();
                            }
                            count += cmd.ExecuteNonQuery();
                            if (factory.DbConnection.State != ConnectionState.Closed)
                            {
                                factory.DbConnection.Close();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }
                else
                {
                    //foreach (ModelFactory factory in factories)
                    //{
                    //    factory.DbCommand.Transaction = factory.DbConnection.BeginTransaction();
                    //}
                    HashSet <ModelFactory> factories = new HashSet <ModelFactory>();
                    foreach (ModelFactory f in factoryList)
                    {
                        factories.Add(f);
                    }
                    //foreach (ModelFactory factory in factories)
                    //{
                    //    if (factory.DbConnection.State == ConnectionState.Closed)
                    //    {
                    //        factory.DbConnection.Open();
                    //    }
                    //    factory.DbCommand.Transaction = factory.DbConnection.BeginTransaction();

                    //    if (factory.DbConnection.State != ConnectionState.Closed)
                    //    {
                    //        factory.DbConnection.Close();
                    //    }
                    //}
                    try
                    {
                        foreach (ModelFactory factory in factories)
                        {
                            if (factory.DbConnection.State == ConnectionState.Closed)
                            {
                                factory.DbConnection.Open();
                            }
                            DbCommand cmd = factory.GetAppendCommand(value, mapping);
                            count += cmd.ExecuteNonQuery();
                            if (factory.DbConnection.State != ConnectionState.Closed)
                            {
                                factory.DbConnection.Close();
                            }
                        }
                        //foreach (ModelFactory factory in factories)
                        //{
                        //    if (factory.DbConnection.State == ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Open();
                        //    }
                        //    factory.DbCommand.Transaction.Commit();

                        //    if (factory.DbConnection.State != ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Close();
                        //    }
                        //}
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                        //foreach (ModelFactory factory in factories)
                        //{
                        //    if (factory.DbConnection.State == ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Open();
                        //    }
                        //    factory.DbCommand.Transaction.Rollback();

                        //    if (factory.DbConnection.State != ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Close();
                        //    }
                        //}
                    }
                    finally
                    {
                        //foreach (ModelFactory factory in factories)
                        //{
                        //    if (factory.DbConnection.State == ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Open();
                        //    }
                        //    factory.DbCommand.Transaction = null;
                        //    if (factory.DbConnection.State != ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Close();
                        //    }
                        //}
                    }
                }
            }
            return(count);
        }
예제 #4
0
 public override DbCommand GetAppendCommand(ModelBase value, ModelMapping mapping)
 {
     return(GetInsertCommand(value, mapping));
 }
예제 #5
0
        public static int DeleteModel(ModelBase value, ModelMapping mapping, out Exception exception)
        {
            int count = 0;

            lock (s_locker)
            {
                exception = null;
                if (mapping.Parent == null)
                {
                    try
                    {
                        ModelFactory factory = GetFactory(mapping.ModelType, value.Rid);
                        DbCommand    cmd     = factory.GetDeleteCommand(value, mapping);
                        // Console.WriteLine(cmd.CommandText);
                        if (factory.DbConnection.State == ConnectionState.Closed)
                        {
                            factory.DbConnection.Open();
                        }
                        count = cmd.ExecuteNonQuery();
                        if (factory.DbConnection.State != ConnectionState.Closed)
                        {
                            factory.DbConnection.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }
                else
                {
                    List <ModelFactoryMapping> factoryList = GetFactories(mapping, value.Rid);
                    HashSet <ModelFactory>     factories   = new HashSet <ModelFactory>();
                    foreach (ModelFactoryMapping mfm in factoryList)
                    {
                        factories.Add(mfm.m_factory);
                    }
                    //foreach (ModelFactory factory in factories)
                    //{
                    //    if (factory.DbConnection.State == ConnectionState.Closed)
                    //    {
                    //        factory.DbConnection.Open();
                    //    }
                    //    factory.DbCommand.Transaction = factory.DbConnection.BeginTransaction();
                    //    if (factory.DbConnection.State != ConnectionState.Closed)
                    //    {
                    //        factory.DbConnection.Close();
                    //    }
                    //}
                    try
                    {
                        foreach (ModelFactoryMapping mfm in factoryList)
                        {
                            if (mfm.m_factory.DbConnection.State == ConnectionState.Closed)
                            {
                                mfm.m_factory.DbConnection.Open();
                            }
                            DbCommand cmd = mfm.m_factory.GetDeleteCommand(value, mfm.m_mapping);
                            count += cmd.ExecuteNonQuery();
                            if (mfm.m_factory.DbConnection.State != ConnectionState.Closed)
                            {
                                mfm.m_factory.DbConnection.Close();
                            }
                        }
                        //foreach (ModelFactoryMapping mfm in factoryList)
                        //{
                        //    mfm.m_factory.DbCommand.Transaction.Commit();
                        //}
                        //foreach (ModelFactory factory in factories)
                        //{
                        //    if (factory.DbConnection.State == ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Open();
                        //    }
                        //   factory.DbCommand.Transaction.Commit();
                        //    if (factory.DbConnection.State != ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Close();
                        //    }
                        //}
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                        //foreach (ModelFactoryMapping mfm in factoryList)
                        //{
                        //    mfm.m_factory.DbCommand.Transaction.Rollback();
                        //}
                        //foreach (ModelFactory factory in factories)
                        //{
                        //    if (factory.DbConnection.State == ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Open();
                        //    }
                        //    factory.DbCommand.Transaction.Rollback();
                        //    if (factory.DbConnection.State != ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Close();
                        //    }
                        //}
                    }
                    finally
                    {
                        //foreach (ModelFactoryMapping mfm in factoryList)
                        //{
                        //    mfm.m_factory.DbCommand.Transaction = null;
                        //}
                        //foreach (ModelFactory factory in factories)
                        //{
                        //    if (factory.DbConnection.State == ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Open();
                        //    }
                        //    factory.DbCommand.Transaction = null;
                        //    if (factory.DbConnection.State != ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Close();
                        //    }
                        //}
                    }
                }
            }
            return(count);
        }
예제 #6
0
        public static int InsertModel(ModelBase value, ModelMapping mapping, out Exception exception)
        {
            int count = 0;

            lock (s_locker)
            {
                exception = null;
                if (mapping.Parent == null)
                {
                    try
                    {
                        ModelFactory factory = GetFactory(mapping.ModelType, value.Rid);
                        //田濛 12.11.11
                        if (factory.DbConnection.State == ConnectionState.Closed)
                        {
                            factory.DbConnection.Open();
                        }
                        if (value.Rid == 0)
                        {
                            value.Rid = factory.GetMaxRid(mapping.ModelType);
                        }
                        if (factory.DbConnection.State == ConnectionState.Closed)
                        {
                            factory.DbConnection.Open();
                        }

                        DbCommand cmd = factory.GetInsertCommand(value, mapping);
                        //string sql = cmd.CommandText;
                        //StringBuilder str = new StringBuilder();
                        //string[] ss = sql.Split('@');
                        //str.Append(ss[0]);
                        //foreach (DbParameter dd in cmd.Parameters)
                        //{
                        //    str.Append("'");
                        //    str.Append(dd.Value);
                        //    str.Append("'");
                        //    str.Append(",");
                        //}
                        //sql = string.Empty;
                        //sql = str.ToString();
                        //sql = sql.Substring(0, sql.Length - 1);
                        //sql = sql + ");";

                        //string sqlname = sql.Substring(12, 16);
                        //if (sqlname == "EnergyRegistrationData")
                        //{
                        //    string filename = string.Format("d:\\sql\\EnergyRegistrationData_{0}.txt", DateTime.Now.Ticks);
                        //    FileStream fs = new FileStream(filename, FileMode.Append);
                        //    StreamWriter sw = new StreamWriter(fs);
                        //    sw.BaseStream.Seek(0, SeekOrigin.End);
                        //    sw.WriteLine(sql);
                        //    sw.Close();
                        //    fs.Close();
                        //}
                        count = cmd.ExecuteNonQuery();
                        if (factory.DbConnection.State != ConnectionState.Closed)
                        {
                            factory.DbConnection.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }
                else
                {
                    List <ModelFactoryMapping> factoryList = GetFactories(mapping, value.Rid);
                    //foreach (ModelFactoryMapping mfm in factoryList)
                    //{
                    //    mfm.m_factory.DbCommand.Transaction = mfm.m_factory.DbConnection.BeginTransaction();
                    //}
                    HashSet <ModelFactory> factories = new HashSet <ModelFactory>();
                    foreach (ModelFactoryMapping mfm in factoryList)
                    {
                        factories.Add(mfm.m_factory);
                    }
                    //foreach (ModelFactory factory in factories)
                    //{
                    //    if (factory.DbConnection.State == ConnectionState.Closed)
                    //    {
                    //        factory.DbConnection.Open();
                    //    }
                    //    //factory.DbCommand.Transaction = factory.DbConnection.BeginTransaction();
                    //    if (factory.DbConnection.State != ConnectionState.Closed)
                    //    {
                    //        factory.DbConnection.Close();
                    //    }
                    //}
                    try
                    {
                        foreach (ModelFactoryMapping mfm in factoryList)
                        {
                            if (mfm.m_factory.DbConnection.State == ConnectionState.Closed)
                            {
                                mfm.m_factory.DbConnection.Open();
                            }
                            if (value.Rid == 0)
                            {
                                value.Rid = mfm.m_factory.GetMaxRid(mapping.ModelType);
                            }
                            if (mfm.m_factory.DbConnection.State == ConnectionState.Closed)
                            {
                                mfm.m_factory.DbConnection.Open();
                            }
                            DbCommand cmd = mfm.m_factory.GetInsertCommand(value, mfm.m_mapping);
                            count += cmd.ExecuteNonQuery();

                            if (mfm.m_factory.DbConnection.State != ConnectionState.Closed)
                            {
                                mfm.m_factory.DbConnection.Close();
                            }
                        }
                        //foreach (ModelFactoryMapping mfm in factoryList)
                        //{
                        //    mfm.m_factory.DbCommand.Transaction.Commit();
                        //}
                        //foreach (ModelFactory factory in factories)
                        //{
                        //    if (factory.DbConnection.State == ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Open();
                        //    }
                        //      factory.DbCommand.Transaction.Commit();
                        //    if (factory.DbConnection.State != ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Close();
                        //    }
                        //}
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                        //foreach (ModelFactoryMapping mfm in factoryList)
                        //{
                        //    mfm.m_factory.DbCommand.Transaction.Rollback();
                        //}
                        //foreach (ModelFactory factory in factories)
                        //{
                        //    if (factory.DbConnection.State == ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Open();
                        //    }
                        //    factory.DbCommand.Transaction.Rollback();
                        //    if (factory.DbConnection.State != ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Close();
                        //    }
                        //}
                    }
                    finally
                    {
                        //foreach (ModelFactoryMapping mfm in factoryList)
                        //{
                        //    mfm.m_factory.DbCommand.Transaction = null;
                        //}
                        //foreach (ModelFactory factory in factories)
                        //{
                        //    if (factory.DbConnection.State == ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Open();
                        //    }
                        //    factory.DbCommand.Transaction = null;
                        //    if (factory.DbConnection.State != ConnectionState.Closed)
                        //    {
                        //        factory.DbConnection.Close();
                        //    }
                        //}
                    }
                }
            }
            return(count);
        }
예제 #7
0
 /// <summary>
 /// 创建ParentModel对象
 /// </summary>
 /// <param name="value">模型对象</param>
 internal ParentModel(ModelBase value)
 {
     model = value;
 }
예제 #8
0
        public static void Excute(ExcuteAction[] actions, IExcuteNotify notify, out bool result)
        {
            result = true;
            foreach (ExcuteAction ea in actions)
            {
                switch (ea.ExcuteType)
                {
                case ExcuteType.Insert:
                {
                    ModelBase model = ea.ExcuteObject as ModelBase;
                    if (model != null)
                    {
                        Exception ex;
                        int       id;
                        InsertModel(model, out ex, out id);
                        if (ex != null || id < 0)
                        {
                            notify.ErrorNotify(ex.Message, ExcuteType.Insert);
                            result = false;
                            return;
                        }
                        else
                        {
                            model.Rid = id;
                        }
                    }
                    break;
                }

                case ExcuteType.Delete:
                {
                    ModelBase model = ea.ExcuteObject as ModelBase;
                    if (model != null)
                    {
                        Exception ex;
                        DeleteModel(model, out ex);
                        if (ex != null)
                        {
                            notify.ErrorNotify(ex.Message, ExcuteType.Delete);
                            result = false;
                            return;
                        }
                    }
                    break;
                }

                case ExcuteType.Update:
                {
                    ModelBase model = ea.ExcuteObject as ModelBase;
                    if (model != null)
                    {
                        Exception ex;
                        UpdateModel(model, out ex);
                        if (ex != null)
                        {
                            notify.ErrorNotify(ex.Message, ExcuteType.Update);
                            result = false;
                            return;
                        }
                    }
                    break;
                }

                case ExcuteType.CacheSave:
                {
                    ModelBase model = ea.ExcuteObject as ModelBase;
                    if (model != null)
                    {
                        try
                        {
                            ModelCacheManager.Instance.Save(model);
                        }
                        catch (Exception ex)
                        {
                            notify.ErrorNotify(ex.Message, ExcuteType.Update);
                            result = false;
                            return;
                        }
                    }
                    break;
                }

                case ExcuteType.CacheRemove:
                {
                    break;
                }
                }
            }
            // notify.ExcuteNotify(actions);
        }
예제 #9
0
        /// <summary>
        /// 存储一个模型对象,如果存根管理器中没有该对象则增加该对象
        /// </summary>
        /// <param name="value">要存储的模型对象</param>
        public virtual void Save(ModelBase value)
        {
            Type            type            = value.GetType();
            ModelCollection modelCollection = this[type];

            if (modelCollection == null)
            {
                return;
            }
            if (modelCollection.Contains(value.Rid))
            {
                bool      altKeyChanged = false;
                ModelBase model         = modelCollection[value.Rid];
                if (model.AlternateKey != value.AlternateKey)
                {
                    modelCollection.RemoveAltKey(model);
                    altKeyChanged = true;
                }
                ModelMapping mf = modelCollection.ModelField;
                foreach (PropertyFieldPair pfp in mf.PropertyFields)
                {
                    object obj = pfp.Property.GetValue(value, null);
                    pfp.Property.SetValue(model, obj, null);
                }
                while (mf.Parent != null)
                {
                    foreach (PropertyFieldPair pfp in mf.Parent.PropertyFields)
                    {
                        object obj = pfp.Property.GetValue(value, null);
                        pfp.Property.SetValue(model, obj, null);
                    }
                    mf = mf.Parent;
                }
                if (altKeyChanged)
                {
                    modelCollection.AddAltKey(value);
                }
            }
            else
            {
                modelCollection.Add(value);
                modelCollection.AddAltKey(value);
                while (modelCollection.ModelField.Parent != null)
                {
                    modelCollection = this[modelCollection.ModelField.Parent.ModelType];
                    if (modelCollection.Contains(value.Rid))
                    {
                        ModelBase    model = modelCollection[value.Rid];
                        ModelMapping mf    = modelCollection.ModelField;
                        foreach (PropertyFieldPair pfp in mf.PropertyFields)
                        {
                            object obj = pfp.Property.GetValue(model, null);
                            pfp.Property.SetValue(value, obj, null);
                        }
                        modelCollection.Remove(value.Rid);
                        modelCollection.RemoveAltKey(value);
                    }
                    modelCollection.Add(value);
                    modelCollection.AddAltKey(value);
                    type = type.BaseType;
                }
            }
        }