예제 #1
0
        protected virtual string CreateAppendSqlString(ModelBase value, ModelMapping mapping)
        {
            string sql = "insert into ";

            sql += mapping.TableName;
            sql += " (";
            foreach (PropertyFieldPair pair in mapping.PropertyFields)
            {
                sql += string.Format(EscapeExpr, pair.FieldName);
                sql += ",";
            }
            sql  = sql.TrimEnd(',');
            sql += ") values (";
            foreach (PropertyFieldPair pair in mapping.PropertyFields)
            {
                sql += "@";
                sql += pair.FieldName;
                sql += ",";
            }
            sql  = sql.TrimEnd(',');
            sql += ");";
            return(sql);
        }
예제 #2
0
        protected override string CreateSelectSqlString(ModelMapping mapping, string where, bool hasRid)
        {
            string sql = "select ";

            if (hasRid)
            {
                sql += "Rid,";
            }
            foreach (PropertyFieldPair pair in mapping.PropertyFields)
            {
                sql += pair.FieldName;
                sql += ",";
            }
            sql  = sql.TrimEnd(',');
            sql += " from ";
            sql += mapping.TableName;
            if (!string.IsNullOrWhiteSpace(where))
            {
                sql += " where ";
                sql += where;
            }
            sql += ";";
            return(sql);
        }
예제 #3
0
 public static void UpdateModel(ModelBase item, out Exception e)
 {
     lock (m_lock)
     {
         e = null;
         try
         {
             ModelMapping mp = new ModelMapping(item.GetType());
             ModelFactoryCollection.UpdateModel(item, mp, out e);
             if (e == null)
             {
                 ModelCacheManager.Instance.Save(item);
             }
             else
             {
                 return;
             }
         }
         catch (Exception ex)
         {
             e = ex;
         }
     }
 }
예제 #4
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);
        }
예제 #5
0
 protected override string CreateSelectModelSqlString(ModelMapping mapping, int rid)
 {
     return(CreateSelectSqlString(mapping, string.Format("Rid={0}", rid), true));
 }
예제 #6
0
 public override DbCommand GetAppendCommand(ModelBase value, ModelMapping mapping)
 {
     return(GetInsertCommand(value, mapping));
 }
예제 #7
0
 public ObjectDatabaseReader(Type type, DbCommand command)
 {
     m_type    = type;
     m_field   = new ModelMapping(type);
     DbCommand = command;
 }
예제 #8
0
 public ModelFactoryCollection(Type modelType)
 {
     m_mapping = new ModelMapping(modelType);
 }
예제 #9
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);
        }
예제 #10
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);
        }
예제 #11
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);
        }
예제 #12
0
 internal int GetMaxRid(ModelMapping mapping)
 {
     return(GetMaxRid(mapping.Root.TableName));
 }
예제 #13
0
 public virtual DbCommand GetSelectCommand(ModelMapping mapping)
 {
     string where = string.Format("Rid <= {0} and Rid > {1}", MaxRid, MinRid);
     return(GetSelectCommand(mapping, where, true));
 }
예제 #14
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;
                }
            }
        }
예제 #15
0
 /// <summary>
 /// 我自己加的带条件查询
 /// </summary>
 public ModelDatabaseReader(Type type, ModelFactory factory, string where)
 {
     m_type    = type;
     m_field   = new ModelMapping(type);
     DbCommand = factory.GetSelectCommand(m_field, where, true);
 }