Example #1
0
        public Task ImportToSql(IDatabaseAdapter adapter, MainWindow.UpdateProgressFunc UpdateProgress, string IdKey, string bindingName)
        {
            return(Task.Run(() =>
            {
                var binding = BindingManager.GetInstance().FindBinding(bindingName);
                adapter.Execute(string.Format(adapter.GetTableCreateString(binding), binding.Name));
                uint currentRecord = 0;
                uint count = Header.RecordCount;
                uint updateRate = count < 100 ? 100 : count / 100;
                uint index = 0;
                StringBuilder q = null;
                foreach (var recordMap in Body.RecordMaps)
                {
                    // This might be needed? Disabled unless bugs are reported around this
                    //if (r.record.ID == 0)
                    //  continue;
                    if (index == 0 || index % 250 == 0)
                    {
                        if (q != null)
                        {
                            q.Remove(q.Length - 2, 2);
                            adapter.Execute(q.ToString());
                        }
                        q = new StringBuilder();
                        q.Append(string.Format("INSERT INTO `{0}` VALUES ", bindingName));
                    }
                    if (++index % updateRate == 0)
                    {
                        // Visual studio says these casts are redundant but it does not work without them
                        double percent = (double)index / (double)count;
                        UpdateProgress(percent);
                    }
                    currentRecord = recordMap.ContainsKey(IdKey) ? (uint)recordMap[IdKey] : 0;
                    q.Append("(");
                    foreach (var field in binding.Fields)
                    {
                        switch (field.Type)
                        {
                        case BindingType.INT:
                        case BindingType.UINT:
                            {
                                q.Append(string.Format("'{0}', ", recordMap[field.Name]));
                                break;
                            }

                        case BindingType.FLOAT:
                        case BindingType.DOUBLE:
                            {
                                q.Append(string.Format("REPLACE('{0}', ',', '.'), ", recordMap[field.Name]));
                                break;
                            }

                        case BindingType.STRING_OFFSET:
                            {
                                var strOffset = (uint)recordMap[field.Name];
                                var lookupResult = Reader.LookupStringOffset(strOffset);
                                q.Append(string.Format("\'{0}\', ", adapter.EscapeString(lookupResult)));
                                break;
                            }

                        case BindingType.UNKNOWN:
                            break;

                        default:
                            throw new Exception($"ERROR: Record[{currentRecord}] Unhandled type: {field.Type} on field: {field.Name}");
                        }
                    }
                    q.Remove(q.Length - 2, 2);
                    q.Append("), ");
                }
                if (q.Length > 0)
                {
                    q.Remove(q.Length - 2, 2);
                    adapter.Execute(q.ToString());
                }
                // We have attempted to import the Spell.dbc so clean up unneeded data
                // This will be recreated if the import process is started again
                Reader.CleanStringsMap();
            }));
        }