using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
[System.Serializable]
public class SqliteItem
{
public string Name;
public string Path;
public string Data;
public string[] querys;
public SqliteItem(string name, string path, string data)
{
Name = name;
Path = path;
Data = data;
}
}
public class Converter : MonoBehaviour
{
public SqliteItem[] Items;
public void Btn_Convert()
{
StartCoroutine(ReadFile());
}
public void Btn_Exit()
{
Application.Quit();
}
//불러온 .sql 파일들을 sqlite 데이터베이스 파일로 변환하여 생성하기
void Write()
{
foreach (var item in Items)
{
SqliteDB db = new SqliteDB(item.Name);
db.connection();
for (int i = 0; i < item.querys.Length; i++)
{
db.ApplyQuery(item.querys[i]);
}
db.Disconnection();
}
}
//.sql 파일들을 불러와 저장하기
IEnumerator ReadFile()
{
DirectoryInfo di = new DirectoryInfo(Application.streamingAssetsPath);
List<SqliteItem> tmp = new List<SqliteItem>();
foreach (var item in di.GetFiles("*.sql"))
{
WWW www = new WWW(Application.streamingAssetsPath + "/" + item.Name);
while (!www.isDone)
{
Debug.Log("대기중");
yield return new WaitForFixedUpdate();
}
tmp.Add(new SqliteItem(item.Name.Split('.')[0], item.DirectoryName, www.text));
}
if (tmp.Count == 0)
{
Debug.Log("파일이 없습니다");
}
Items = tmp.ToArray();
StartCoroutine(Convert());
}
IEnumerator Convert()
{
foreach (var item in Items)
{
StringReader sr = new StringReader(item.Data);
List<string> querys = new List<string>();
string line = sr.ReadLine();
string query = "";
if (line.Contains(" "))
{
line = line.Replace(" ", " ");
}
while (line != null)
{
if (line.Contains("--") || line.Contains("/*") || line.Contains("*/") || line.Contains("USE") ||
line.Contains("COMMENT") || line.Contains("UNIQUE KEY") ||
line.Contains("ENGINE") || line.Contains("PRIMARY KEY") || line.Contains("KEY `"))
{
if(line.Contains("DATABASE"))
{
line = sr.ReadLine();
continue;
}
if(line.Contains("CREATE")||line.Contains("INSERT INTO"))
{
query += line;
line = sr.ReadLine();
continue;
}
line = sr.ReadLine();
continue;
}
if (line.Equals(""))
{
if (!query.Equals(""))
{
query = SqlToSqlite(query);
querys.Add(query);
query = "";
}
}
else
{
if(line.Contains("INSERT IGNORE INTO"))
{
line = line.Replace("INSERT IGNORE INTO", "INSERT OR IGNORE INTO");
}
query += line;
}
line = sr.ReadLine();
}
//M용 유저 테이블 추가
querys.Add();
item.querys = querys.ToArray();
yield return null;
}
Write();
}
string SqlToSqlite(string query)
{
string[] tmp = query.Split(' ');
query = "";
for (int i = 0; i < tmp.Length; i++)
{
if (tmp[i].Contains("int("))
tmp[i] = "INTEGER";
if (tmp[i].Contains("varchar("))
{
tmp[i] = "TEXT";
}
if (tmp[i].Contains("AUTO_INCREMENT"))
tmp[i] = tmp[i].Replace("AUTO_INCREMENT", "PRIMARY KEY AUTOINCREMENT");
if (tmp[i].Contains("float"))
tmp[i] = "NUMERIC";
query += tmp[i] + " ";
}
query = query.TrimEnd();
query += ")";
query = query.Replace("))", ")");
query = query.Replace(";)", ";");
query = query.Replace(",)", ")");
query = query.Replace(" ", " ");
return query;
}
}