public static void RemoveGridSystems(int[] gridids, Action <string> report = null)
        {
            using (SQLiteConnectionSystem cn = new SQLiteConnectionSystem(mode: SQLLiteExtensions.SQLExtConnection.AccessMode.Writer))
            {
                report?.Invoke("Delete System Information from sector:");
                for (int i = 0; i < gridids.Length; i += 32)
                {
                    int   left = Math.Min(gridids.Length - i, 32);   // could do it all at once, but this way, some visual feedback
                    int[] todo = new int[left];
                    Array.Copy(gridids, i, todo, 0, left);

                    report?.Invoke(" " + string.Join(" ", todo));

                    using (DbCommand cmd = cn.CreateDelete("Systems", "sectorid IN (Select id FROM Sectors WHERE gridid IN (" + string.Join(",", todo) + "))"))
                    {
                        cmd.ExecuteNonQuery();
                    }

                    using (DbCommand cmd = cn.CreateDelete("Sectors", "gridid IN (" + string.Join(",", todo) + ")"))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }

                report?.Invoke(Environment.NewLine);
            }
        }
Exemple #2
0
        public static long ParseAlias(JsonTextReader jr)
        {
            long updates = 0;

            System.Diagnostics.Debug.WriteLine("Update aliases");

            using (SQLiteConnectionSystem cn = new SQLiteConnectionSystem(mode: SQLLiteExtensions.SQLExtConnection.AccessMode.Writer))  // open the db
            {
                using (DbTransaction txn = cn.BeginTransaction())
                {
                    DbCommand selectCmd       = cn.CreateSelect("Aliases", "edsmid", "edsmid = @edsmid", inparas: new string[] { "edsmid:int64" }, limit: "1", tx: txn); // 1 return matching ID
                    DbCommand deletesystemcmd = cn.CreateDelete("Systems", "edsmid=@edsmid", paras: new string[] { "edsmid:int64" }, tx: txn);
                    DbCommand insertCmd       = cn.CreateReplace("Aliases", paras: new string[] { "edsmid:int64", "edsmid_mergedto:int64", "name:string" }, tx: txn);

                    try
                    {       // protect against json exceptions
                        while (true)
                        {
                            if (!jr.Read())
                            {
                                break;
                            }

                            if (jr.TokenType == JsonToken.StartObject)
                            {
                                JObject jo = JObject.Load(jr);

                                long   edsmid   = (long)jo["id"];
                                string name     = (string)jo["system"];
                                string action   = (string)jo["action"];
                                long   mergedto = 0;

                                if (jo["mergedTo"] != null)
                                {
                                    mergedto = (long)jo["mergedTo"];
                                }

                                if (action.Contains("delete system", System.StringComparison.InvariantCultureIgnoreCase))
                                {
                                    deletesystemcmd.Parameters[0].Value = edsmid;
                                    deletesystemcmd.ExecuteNonQuery();
                                }

                                if (mergedto > 0)
                                {
                                    selectCmd.Parameters[0].Value = edsmid;
                                    long foundedsmid = selectCmd.ExecuteScalar <long>(-1);

                                    if (foundedsmid == -1)
                                    {
                                        insertCmd.Parameters[0].Value = edsmid;
                                        insertCmd.Parameters[1].Value = mergedto;
                                        insertCmd.Parameters[2].Value = name;
                                        insertCmd.ExecuteNonQuery();
                                        //System.Diagnostics.Debug.WriteLine("Alias " + edsmid + " -> " + mergedto + " " + name);
                                        updates++;
                                    }
                                }
                            }
                        }
                    }
                    catch (System.Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine("JSON format error in aliases " + ex);
                    }

                    txn.Commit();
                    selectCmd.Dispose();
                    deletesystemcmd.Dispose();
                    insertCmd.Dispose();
                }
            }

            return(updates);
        }