Skip to content

Instantly share code, notes, and snippets.

@SIRprise
Last active January 17, 2020 14:37
Show Gist options
  • Save SIRprise/0b2998a9248439215acbe7939cda8a78 to your computer and use it in GitHub Desktop.
Save SIRprise/0b2998a9248439215acbe7939cda8a78 to your computer and use it in GitHub Desktop.
MySQL dump parser (incomplete)
using System;
using System.IO;
using System.Linq;
namespace SQLdumpParser
{
class Program
{
//see https://www.mysqltutorial.org/mysql-sample-database.aspx
static string filePath = @"..\..\mysqlsampledatabase.sql";
static void Main(string[] args)
{
//string line = "";
//System.IO.StreamReader file = new System.IO.StreamReader(filePath);
char[] delimiters = new char[] { '-', ';' };
/*
while ((line = file.ReadLine()) != null)
{
}
*/
string newPath = Path.GetFullPath(filePath);
string DB = File.ReadAllText(newPath);
//extract db name
int DBnameStart = DB.IndexOf("`", DB.IndexOf("CREATE DATABASE"))+1;
int DBnameEnd = DB.IndexOf("`",DBnameStart);
string DBname = DB.Substring(DBnameStart, DBnameEnd - DBnameStart);
Console.WriteLine(DBname);
int DBtableStart = DBnameEnd;
int DBtableValuesLineStart;
//--loop start
while (true)
{
//extract table
//extract table name
int DBtableNameStart = DB.IndexOf("`", DB.IndexOf("insert into")) + 1;
if (DBtableNameStart == -1)
break;
int DBtableNameEnd = DB.IndexOf("`", DBtableNameStart);
string DBtableName = DB.Substring(DBtableNameStart, DBtableNameEnd - DBtableNameStart);
Console.WriteLine(DBtableName);
//extract table definition
string idCountStr = DB.Substring(DBtableNameEnd, DB.IndexOf(")", DBtableNameEnd + 1) - DBtableNameEnd);
idCountStr = idCountStr.Replace("(", "");
string[] idCountStrNames = idCountStr.Replace("`", "").Split(',');
idCountStrNames = idCountStrNames.Select(x => x.Trim()).ToArray();
//Array.ForEach(idCountStrNames, Console.Write);
Console.WriteLine("[{0}]", string.Join(", ", idCountStrNames));
//TBD: primary key could be parsed here, too
DBtableValuesLineStart = DBtableNameEnd + idCountStr.Length;
DBtableValuesLineStart = DB.IndexOf("(", DBtableValuesLineStart) + 1;
//string[] DBvalueLines = DBvaluesStr.Split(','); // <-- comma and brackets could also be part of text :/
// ->we have to parse every single field with its specific limiter
string temp;
//--inner loop start
while (true)
{
//parse values
for (int i = 0; i < idCountStrNames.Length; i++)
{
//check, if next char is "\'"
char separator = '\'';
temp = "";
//string debuggertemp = DB.Substring(DBtableValuesLineStart, 2);
if (DB[DBtableValuesLineStart] == separator)
{
//we could decide between string and ID or int here
temp = DB.Substring(DBtableValuesLineStart + 1, DB.IndexOf("\'", DBtableValuesLineStart + 1) - (DBtableValuesLineStart + 1));
DBtableValuesLineStart = DBtableValuesLineStart + 1 + temp.Length + 2;
}
else
{
//this value is not a string (could be a number, id or NULL)
temp = DB.Substring(DBtableValuesLineStart, DB.IndexOf(",", DBtableValuesLineStart + 1) - (DBtableValuesLineStart));
DBtableValuesLineStart = DBtableValuesLineStart + temp.Length + 1;
}
}
//check here if next char is a ',' or a ';' (end of command)
temp = DB.Substring(DBtableValuesLineStart, 6);
if (temp.Contains(';'))
{
DBtableValuesLineStart = DB.IndexOf(';', DBtableValuesLineStart) + 1;
break;
}
else
{
DBtableValuesLineStart = DB.IndexOf('(', DBtableValuesLineStart) + 1;
}
}
//--inner loop end
DBtableStart = DBtableValuesLineStart;
}
//--loop end
//We need some kind of dynamically created dictionary here, with dynamical name. something like:
//var dict = new Dictionary<string, List<string>>();
//dict["x1"] = new List<string>();
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment