Beispiel #1
0
 internal static object ConvertEnumParameter(object value,ConnectionPool pool)
 {
     if ((value.GetType().IsArray) || (value is IEnumerable))
     {
         ArrayList tmp = new ArrayList();
         foreach (Enum en in (IEnumerable)value)
             tmp.Add(pool.GetEnumID(en.GetType(), en.ToString()));
         return tmp;
     }
     else
         return pool.GetEnumID(value.GetType(), value.ToString());
 }
Beispiel #2
0
        public static bool BackupDataToStream(ConnectionPool pool, ref Stream outputStream)
        {
            Logger.LogLine("Locking down "+pool.ConnectionName+" database for backing up...");
            Connection c = pool.LockDownForBackupRestore();
            System.Threading.Thread.Sleep(500);
            c.StartTransaction();
            Logger.LogLine("Database locked down for backing up");
            List<Type> types = pool.Mapping.Types;
            List<Type> enums = new List<Type>();
            List<Type> basicTypes = new List<Type>();
            List<Type> complexTypes = new List<Type>();
            sTable tbl;
            Logger.LogLine("Loading all required types...");
            foreach (Type t in types)
            {
                tbl = pool.Mapping[t];
                foreach(sTableField fld in tbl.Fields)
                {
                    if (fld.Type == Org.Reddragonit.Dbpro.Structure.Attributes.FieldType.ENUM)
                    {
                        PropertyInfo pi = t.GetProperty(fld.ClassProperty, Utility._BINDING_FLAGS);
                        if (!enums.Contains((pi.PropertyType.IsArray ? pi.PropertyType.GetElementType() : pi.PropertyType)))
                            enums.Add((pi.PropertyType.IsArray ? pi.PropertyType.GetElementType() : pi.PropertyType));
                    }
                }
                if (tbl.ForeignTableProperties.Length > 0)
                {
                    if (!complexTypes.Contains(t))
                        recurAddRelatedTypes(ref basicTypes, ref complexTypes, tbl, t,pool);
                }
                else
                {
                    if (!basicTypes.Contains(t))
                        basicTypes.Add(t);
                }
            }

            //begin data dumping and outputting
            ZipFile zf = new ZipFile(outputStream,false);
            byte[] buff = new byte[1024];
            XmlDocument doc;
            XmlElement elem;
            MemoryStream ms;
            int len;
            object obj;
            int cnt = 0;

            //output all enumerations
            Logger.LogLine("Extracting enumerations for backup...");
            foreach (Type t in enums)
            {
                doc = new XmlDocument();
                elem = doc.CreateElement("enums");
                doc.AppendChild(elem);
                foreach (string str in Enum.GetNames(t))
                {
                    elem = doc.CreateElement("enum");
                    elem.Attributes.Append(CreateAttributeWithValue(doc, "Name", str));
                    elem.Attributes.Append(CreateAttributeWithValue(doc,"Value",pool.GetEnumID(t,str).ToString()));
                    doc.DocumentElement.AppendChild(elem);
                }
                zf.AppendFile(cnt.ToString("0000000") + "_" + t.FullName + ".xml", XMLCompressor.CompressXMLDocument(doc));
                cnt++;
            }

            //output all basic types
            Logger.LogLine("Backing up the basic types for database...");
            foreach (Type t in basicTypes)
            {
                Logger.LogLine("Backing up basic type: " + t.FullName);
                doc = new XmlDocument();
                elem = doc.CreateElement("entries");
                doc.AppendChild(elem);
                tbl = pool.Mapping[t];
                Logger.LogLine("Extracting basic type: " + t.FullName + " from the database and writing it to the xml document.");
                foreach (Table table in c.SelectAll(t))
                {
                    elem = doc.CreateElement("entry");
                    foreach (string str in tbl.Properties)
                    {
                        obj = table.GetField(str);
                        if (obj != null)
                            elem.AppendChild(CreateElementWithValue(doc, str, obj));
                    }
                    doc.DocumentElement.AppendChild(elem);
                }
                Logger.LogLine("Compressing basic type: " + t.FullName + " data and appending it into the zip file.");
                zf.AppendFile(cnt.ToString("0000000") + "_" + t.FullName + ".xml", XMLCompressor.CompressXMLDocument(doc));
                cnt++;
                c.ResetConnection(false);
            }

            //output all complex types
            Logger.LogLine("Backing up complex types for database...");
            foreach (Type t in complexTypes)
            {
                Logger.LogLine("Backing up complex type: " + t.FullName);
                doc = new XmlDocument();
                elem = doc.CreateElement("entries");
                doc.AppendChild(elem);
                tbl = pool.Mapping[t];
                Logger.LogLine("Extracting complex type: " + t.FullName + " from the database and writing it to the xml document.");
                foreach (Table table in c.SelectAll(t))
                {
                    elem = doc.CreateElement("entry");
                    foreach (string str in tbl.Properties)
                    {
                        obj = table.GetField(str);
                        if (obj != null)
                        {
                            if (tbl.GetRelationForProperty(str).HasValue)
                            {
                                PropertyInfo pi = t.GetProperty(str, Utility._BINDING_FLAGS);
                                if (pi.PropertyType.IsArray)
                                {
                                    elem.AppendChild(doc.CreateElement(str));
                                    foreach (object o in (Array)obj)
                                    {
                                        elem.ChildNodes[elem.ChildNodes.Count - 1].AppendChild(CreateRealtedXMLElement((Table)o, doc, "child",pool));
                                    }
                                }else
                                    elem.AppendChild(CreateRealtedXMLElement((Table)obj, doc,str,pool));
                            }else
                                elem.AppendChild(CreateElementWithValue(doc, str, obj));
                        }
                    }
                    doc.DocumentElement.AppendChild(elem);
                }
                Logger.LogLine("Compressing complex type: " + t.FullName + " data and appending it into the zip file.");
                zf.AppendFile(cnt.ToString("0000000") + "_" + t.FullName + ".xml", XMLCompressor.CompressXMLDocument(doc));
                cnt++;
                c.ResetConnection(false);
            }

            zf.Flush();
            zf.Close();
            c.Reset();
            Logger.LogLine("Backup of database complete, re-enabling pool.");
            c.Disconnect();
            pool.UnlockPoolPostBackupRestore();
            return true;
        }