Example #1
0
        public void Update(object o)
        {
            //NextSeq(o);

            var type = o.GetType();

            var mem = loadedTypes[type];

            string tableName = mem.TableName;

            string upd = "update " + tableName + " set ";

            List <object> pps = new List <object>();
            int           k   = 0;

            foreach (var c in mem.fields)
            {
                string ctype = GetSqlTypeByMem(c.Mem);

                var cl = GetTypeClassByMem(c.Mem);

                string name = c.colName;

                upd += name + " = " + conf.parPre + "p" + k++ + ",";

                switch (cl)
                {
                case HidTypeInfo.HibTypeClass.Def:
                    pps.Add(c.GetValue(o));
                    break;

                case HidTypeInfo.HibTypeClass.External:
                    var idb = loadedTypes[c.type].idInfo.GetValue(c.GetValue(o));
                    if (InBase(idb, c.type))
                    {
                        pps.Add(idb);
                    }
                    else
                    {
                        throw new NonLoadedExternalDate();
                    }
                    break;

                case HidTypeInfo.HibTypeClass.Xml:
                    pps.Add(XmlSer.ToXmlString(c.GetValue(o), c.type));
                    break;

                default:
                    throw new HidException();
                }
            }

            upd  = upd.TrimEnd(',');
            upd += " where " + mem.idInfo.colName + " = " + conf.parPre + "pid";

            using (var com = m_connection.CreateCommand())
            {
                DbParameter p;

                com.Transaction = m_tranaction;
                com.CommandText = upd;
                for (int i = 0; i < k; i++)
                {
                    p = com.CreateParameter();
                    p.ParameterName = "p" + i;
                    p.Value         = pps[i];

                    com.Parameters.Add(p);
                }

                p = com.CreateParameter();
                p.ParameterName = "pid";
                p.Value         = mem.idInfo.GetValue(o);
                com.Parameters.Add(p);

                com.ExecuteNonQuery();
            }
        }
Example #2
0
        object Load(object id, Type type)
        {
            if (!loadedTypes.ContainsKey(type))
            {
                throw new NonLOadadTypeError();
            }

            object ret = Activator.CreateInstance(type);
            // Type type = typeof(T);

            var mem = loadedTypes[type];

            string tableName = mem.TableName;

            using (var com = m_connection.CreateCommand())
            {
                com.Transaction = m_tranaction;
                com.CommandText = "select * from " + tableName + " where " + IdByType(type) + " = " + conf.parPre + "p";

                com.Parameters.Clear();
                var p = com.CreateParameter();
                // p.DbType = System.Data.DbType.
                p.ParameterName = "p";
                p.Value         = id;
                com.Parameters.Add(p);

                using (var rd = com.ExecuteReader())
                {
                    if (!rd.Read())
                    {
                        return(ret);
                    }

                    var r = UtilsClass.ReaderToDic(rd); // да костыль но в некоторых БД по другому ни как.
                    rd.Close();

                    foreach (var c in mem.fields)
                    {
                        string name = c.colName;

                        var cl = GetTypeClassByMem(c.Mem);

                        switch (cl)
                        {
                        case HidTypeInfo.HibTypeClass.Def:
                            c.SetValue(ret, Convert.ChangeType(r[name], c.type));
                            break;

                        case HidTypeInfo.HibTypeClass.External:
                            if (!loadedTypes.ContainsKey(c.type))
                            {
                                throw new NonLOadadTypeError();
                            }
                            var i = Convert.ChangeType(r[name], loadedTypes[c.type].idInfo.type);
                            c.SetValue(ret, Load(i, c.type));
                            break;

                        case HidTypeInfo.HibTypeClass.Xml:
                            c.SetValue(ret, XmlSer.FromString(r[name].ToString(), c.type));
                            break;

                        default:
                            throw new HidException();
                        }
                    }
                }
            }


            return(ret);
        }
Example #3
0
        public void Save(object o)
        {
            NextSeq(o);

            var type = o.GetType();

            var mem = loadedTypes[type];

            string tableName = mem.TableName;

            string        inst  = "insert into " + tableName + " (";
            string        inst2 = " VALUES ( ";
            List <object> pps   = new List <object>();
            int           k     = 0;

            foreach (var c in mem.fields)
            {
                string ctype = GetSqlTypeByMem(c.Mem);

                var cl = GetTypeClassByMem(c.Mem);

                string name = c.colName;

                inst  += name + ",";
                inst2 += conf.parPre + "p" + k++ + ",";

                switch (cl)
                {
                case HidTypeInfo.HibTypeClass.Def:
                    pps.Add(c.GetValue(o));
                    break;

                case HidTypeInfo.HibTypeClass.External:
                    var idb = loadedTypes[c.type].idInfo.GetValue(c.GetValue(o));
                    if (InBase(idb, c.type))
                    {
                        pps.Add(idb);
                    }
                    else
                    {
                        throw new NonLoadedExternalDate();
                    }
                    break;

                case HidTypeInfo.HibTypeClass.Xml:
                    pps.Add(XmlSer.ToXmlString(c.GetValue(o), c.type));
                    break;

                default:
                    throw new HidException();
                }
            }

            inst  = inst.TrimEnd(',');
            inst2 = inst2.TrimEnd(',');

            inst += ") " + inst2 + ")";


            using (var com = m_connection.CreateCommand())
            {
                com.Transaction = m_tranaction;
                com.CommandText = inst;
                for (int i = 0; i < k; i++)
                {
                    var p = com.CreateParameter();
                    p.ParameterName = "p" + i;
                    p.Value         = pps[i];

                    com.Parameters.Add(p);
                }
                com.ExecuteNonQuery();
            }
        }