Exemplo n.º 1
0
        /// <summary>
        /// Simples the migration.
        /// </summary>
        /// <param name="force">If set to <c>true</c> force.</param>
        public void SimpleMigration(bool force = true)
        {
            // StreamingAssets folder
            string path = Path.Combine(Application.streamingAssetsPath, Path.GetFileName(pathDB));

            if (path.Contains("://"))    // android
            {
                WWW www = new WWW(path);
                while (!www.isDone)
                {
                    ;
                }

                if (www.error == null)
                {
                    path = string.Format("{0}_copy{1}", Path.GetFileNameWithoutExtension(path), Path.GetExtension(path));
                    path = Path.Combine(Application.persistentDataPath, path);
                    File.WriteAllBytes(path, www.bytes);
                }
                else
                {
                    HDebug.LogWarning(www.error);
                    return;
                }

                path = Path.GetFileName(path);
            }

            Query query = new Query(path);

            SqliteMastser[]      resources = query.SELECT <SqliteMastser> ();
            List <SqliteMastser> list      = new List <SqliteMastser> (resources);

            query = new Query(pathDB);
            SqliteMastser[] masters = query.SELECT <SqliteMastser> ();

            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < masters.Length; i++)
            {
                SqliteMastser master = list.Find(x => x.Name == masters [i].Name);
                if (master != null)
                {
                    list.Remove(master);
                }

                if (force)
                {
                    if (master != null)
                    {
                        if (masters [i].Sql != master.Sql)
                        {
                            stringBuilder.Append(query.GenerateDropTableSQL(master.Name));
                            stringBuilder.AppendLine();
                            stringBuilder.AppendFormat("{0};", master.Sql);
                            stringBuilder.AppendLine();
                        }
                    }
                    else
                    {
                        stringBuilder.Append(query.GenerateDeleteSQL(masters [i].Name));
                        stringBuilder.AppendLine();
                    }
                }
            }

            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    stringBuilder.AppendFormat("{0};", list [i].Sql);
                    stringBuilder.AppendLine();
                }
            }

            if (stringBuilder.ToString() != "")
            {
                query.ExecuteNonQuery(stringBuilder.ToString());
            }

            if (path.Contains("://"))    // android
            {
                File.Delete(path);
            }
        }