private void InsertInto(string x) { Match m = Regex.Match(x, "insert[ ]+into[ ]+([A-z0-9]*)[ ]*\\.[ ]*([A-z0-9]+)[ ]*(\\(([^\\)]+)\\)|[^ ]*)[ ]*values[ ]*(\\(.+\\))"); string tableName; string databaseName; List <string[]> foot = new List <string[]>(); string[][] Names = new string[1][] { new string[] { "" } }; if (m.Success) { tableName = m.Groups[2].ToString(); databaseName = m.Groups[1].ToString(); if (m.Groups[4].ToString() != "") { Names[0] = m.Groups[3].ToString().Replace("(", "").Replace(")", "").Trim().Split(","); } if (m.Groups[5].ToString() != "") { string temp = m.Groups[5].ToString(); foreach (string s in Regex.Split(temp.Substring(temp.IndexOf("(") + 1, temp.LastIndexOf(")") - temp.IndexOf("(") - 1), "[ ]*\\)[ ]*,[ ]*\\([ ]*")) { foot.Add(s.Replace("(", "").Replace(")", "").Split(",")); } } newStruct = new SQLStruct.Builder("insert into") .setSQLHead(new string[] { databaseName, tableName }) .setSQLBody(Names) .setSQLFoot(foot.ToArray()) .Build(); } }
private void SelectParse(string x) { Match m; bool hasWhere = x.IndexOf("where") > -1; if (hasWhere) { m = Regex.Match(x, "select *(.*) *where *(.*) *"); } else { m = Regex.Match(x, "select *(.*)( *)"); } if (m.Success) { List <string> head; List <string[]> body = new List <string[]>(); head = (from s in m.Groups[1].ToString().Split(",") select s).ToList(); if (hasWhere) { string whereSub = m.Groups[2].ToString().Trim(); foreach (string s in whereSub.Split(" or ")) { string[] temp = s.Trim().Split(" and "); body.Add(temp); } } newStruct = new SQLStruct.Builder("select") .setSQLHead(head.ToArray()) .setSQLBody(body.ToArray()) .Build(); } }
public void Work(string sql) { curSQLStruct = Parser.Parse(sql); Console.WriteLine(curSQLStruct.ToString()); try { sa.Handle(curSQLStruct); }catch (Exception e) { Console.WriteLine(e.Message); } //Console.WriteLine(sa.ToString()); }
private void DropParse(string x) { Match m = Regex.Match(x, "drop[ ]+(table|database)[ ]+([A-z0-9]+)( +from[ ]+([A-z0-9]+)|)"); string dropName; string databaseName; if (m.Success) { dropName = m.Groups[2].ToString().Trim(); databaseName = m.Groups[4].ToString().Trim(); newStruct = new SQLStruct.Builder("drop " + m.Groups[1].ToString()) .setSQLHead(new string[] { databaseName, dropName }) .Build(); } }
private void UpdateParse(string x) { Match m = Regex.Match(x, "update[ ]+([A-z0-9]*)[ ]*\\.[ ]*([A-z0-9]+)[ ]+set *([ ]+where(.+)|)"); string tableName; string databaseName; if (m.Success) { databaseName = m.Groups[1].ToString().Trim(); tableName = m.Groups[2].ToString().Trim(); List <string[]> body = new List <string[]>(); List <string[]> foot = new List <string[]>(); if (x.IndexOf(" where ") > -1) { m = Regex.Match(x, "set.*where(.+)"); string whereSub = m.Groups[1].ToString().Trim(); foreach (string s in whereSub.Split(" or ")) { string[] temp = s.Trim().Split(" and "); foot.Add(temp); } } if (x.IndexOf(" where ") > -1) { m = Regex.Match(x, "set *(.*) *where"); } else { m = Regex.Match(x, "set *(.*)"); } string setSub = m.Groups[1].ToString(); Console.WriteLine(setSub); foreach (string s in setSub.Trim().Split(",")) { string[] temp = s.Trim().Split("="); body.Add(temp); } newStruct = new SQLStruct.Builder("update") .setSQLHead(new string[] { databaseName, tableName }) .setSQLBody(body.ToArray()) .setSQLFoot(foot.ToArray()) .Build(); } }
private void CreateParse(string x) { Match m = Regex.Match(x, "create[ ]+(table|database)[ ]+([A-z0-9]+)([ ]*\\((.+)\\)|)([ ]*from[ ]+([A-z0-9]+)|)"); string tableName; string databaseName; if (m.Success) { tableName = m.Groups[2].ToString().Trim(); databaseName = m.Groups[m.Groups.Count - 1].ToString().Trim(); List <string[]> body = new List <string[]>(); foreach (string s in m.Groups[3].ToString().Replace("(", "").Replace(")", "").Trim().Split(",")) { string[] temp = s.Split(" "); body.Add(temp); } newStruct = new SQLStruct.Builder("create " + m.Groups[1].ToString()) .setSQLHead(new string[] { databaseName, tableName }) .setSQLBody(body.ToArray()) .Build(); } }
private void DeleteParse(string x) { Match m = Regex.Match(x, "delete[ ]+from[ ]+(([A-z0-9]+)\\.([A-z0-9]+))([ ]+where(.+)|)"); string tableName; string databaseName; if (m.Success) { databaseName = m.Groups[2].ToString().Trim(); tableName = m.Groups[3].ToString().Trim(); List <string[]> body = new List <string[]>(); string whereSub = m.Groups[5].ToString().Trim(); foreach (string s in whereSub.Split(" or ")) { string[] temp = s.Split(" and "); body.Add(temp); } newStruct = new SQLStruct.Builder(whereSub == "" ? "delete" : "delete where") .setSQLHead(new string[] { databaseName, tableName }) .setSQLBody(body.ToArray()) .Build(); } }
public void Handle(SQLStruct s) { switch (s.SQLMode) { case "create database": { if (s.SQLHead.Length > 1) { dbs.Add(s.SQLHead[1]); } else { throw new Exception("SQLAction -> Handle(): create database error: length <= 1"); } break; } case "create table": { if (s.SQLHead.Length > 1) { Database db = dbs.Get(s.SQLHead[0]); db.Add(s.SQLHead[1]); DatabaseTable dt = db.Get(s.SQLHead[1]); foreach (string[] i in s.SQLBody) { dt.Add(i[0], i[1]); } } else { throw new Exception("SQLAction -> Handle(): create table error: length <= 1"); } break; } case "drop database": { if (s.SQLHead.Length > 1) { dbs.Delete(s.SQLHead[1]); } else { throw new Exception("SQLAction -> Handle(): drop database error: length <= 1"); } break; } case "drop table": { if (s.SQLHead.Length > 1) { Database db = dbs.Get(s.SQLHead[0]); db.Delete(s.SQLHead[1]); } else { throw new Exception("SQLAction -> Handle(): drop table error: length <= 1"); } break; } case "insert into": { if (!(s.SQLHead.Length > 1)) { throw new Exception("SQLAction -> Handle(): insert into: SQLHead Length Error.\nDo you miss table/database name?"); } if (!(s.SQLBody.Length > 0)) { throw new Exception("SQLAction -> Handle(): insert into: SQLBody Length Error.\nDo you miss field(s)?"); } if (!(s.SQLFoot.Length > 0)) { throw new Exception("SQLAction -> Handle(): insert into: SQLFoot Length Error.\nDo you miss value(s)?"); } Database db = dbs.Get(s.SQLHead[0]); db.Add(s.SQLHead[1]); DatabaseTable dt = db.Get(s.SQLHead[1]); List <DatabaseTableField> dtfs = new List <DatabaseTableField>(); foreach (string i in s.SQLBody[0]) { dtfs.Add(dt.Get(i)); } foreach (string[] i in s.SQLFoot) { for (int j = 0; j < i.Length; j++) { dtfs[j].Add(i[j], dtfs[j].dataType); } } break; } case "delete": { if (s.SQLHead.Length > 1) { Database db = dbs.Get(s.SQLHead[0]); DatabaseTable dt = db.Get(s.SQLHead[1]); dt.ToEmpty(); } else { throw new Exception("SQLAction -> Handle(): Delete error: length <= 1"); } break; } default: throw new Exception("SQLManager -> Work(): An unexcepted SQL action type."); } }