/// <summary> /// Parses the file properties. /// </summary> /// <param name="file">The file to analyze.</param> /// <returns>Instance of TuneToIndex containing file properties</returns> public TuneToIndex ParseProperties (string fileName) { TuneToIndex tune = new TuneToIndex(); tune.Path = Path.GetFullPath(fileName); //This is temporary code for Megalo, a sample of logic to parse filename //In release version, of course, we should read global and local preferences to know how to read filename string nom = Path.GetFileNameWithoutExtension(fileName); //Recherche de () Match match = Regex.Match(fileName, @"\(.*\)"); if (match.Success) { //On prend l'intérieur des () en commentaire tune.Comment = match.Value.Substring(1, match.Length - 2); //Et on l'efface du nom nom = nom.Replace(match.Value, "").Trim(); } //Le nom contient-il un tiret ? if (nom.Contains("-")) { string[] morceau = nom.Split(new char[1] { '-' }, 2, StringSplitOptions.RemoveEmptyEntries); if (morceau.Length == 1) { //Euh le tiret n'était précédé ou suivi de rien :/ tune.Title = morceau[0]; } else { //Et voici le cas normal :) tune.By = morceau[0]; tune.Title = morceau[1]; } } else { //Titre tune.Title = nom; } return tune; }
/// <summary> /// Gets the tune info. /// </summary> /// <param name="path">The path to the tune.</param> /// <returns>Tune info</returns> /// <exception cref="System.Exception">throw if path is not found in database</exception> public TuneToIndex GetTuneInfo(string path) { bool notFoundException = true; string sql = String.Format( "SELECT t.tune_by, t.tune_title, t.tune_comment FROM tunes t, files f WHERE t.tune_id = f.file_id AND f.file_path '{0}'", Utilities.SqlEscape(path) ); TuneToIndex tune = new TuneToIndex(); tune.Path = path; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { notFoundException = false; tune.By = reader.GetString(0); tune.Title = reader.GetString(1); tune.Comment = reader.GetString(2); } reader.Close(); cmd.Dispose(); if (notFoundException) { throw new Exception("Not found in database"); } return(tune); }
/// <summary> /// Ajoute un nouveau fichier /// </summary> /// <param name="file">fichier à ajouter</param> public void AddFile(string file) { //We ignore directories if (Directory.Exists(file)) { return; } TuneToIndex tune = ParseProperties(file); string sql; MySqlCommand cmd; int TuneID; //1 - add tune if needed and get its ID if (IsTuneExists(tune)) { TuneID = GetTuneID(tune); } else { sql = String.Format( @"INSERT INTO Tunes (tune_by, tune_title, tune_comment) VALUES ('{0}', '{1}', '{2}')", Utilities.SqlEscape(tune.By), Utilities.SqlEscape(tune.Title), Utilities.SqlEscape(tune.Comment) ); cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); cmd.Dispose(); TuneID = LastInsertID(); SqlLog(sql); Log(String.Format("Tune added: {0} [#{1}]", tune, TuneID)); } //2 - add file sql = String.Format( "INSERT INTO Files (file_path, tune_id) VALUES ('{0}', '{1}')", Utilities.SqlEscape(tune.Path), TuneID ); cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); cmd.Dispose(); SqlLog(sql); Log(String.Format("File added: {0}", file)); }
/// <summary> /// Parses the file properties. /// </summary> /// <param name="file">The file to analyze.</param> /// <returns>Instance of TuneToIndex containing file properties</returns> public TuneToIndex ParseProperties(string fileName) { TuneToIndex tune = new TuneToIndex(); tune.Path = Path.GetFullPath(fileName); //This is temporary code for Megalo, a sample of logic to parse filename //In release version, of course, we should read global and local preferences to know how to read filename string nom = Path.GetFileNameWithoutExtension(fileName); //Recherche de () Match match = Regex.Match(fileName, @"\(.*\)"); if (match.Success) { //On prend l'intérieur des () en commentaire tune.Comment = match.Value.Substring(1, match.Length - 2); //Et on l'efface du nom nom = nom.Replace(match.Value, "").Trim(); } //Le nom contient-il un tiret ? if (nom.Contains("-")) { string[] morceau = nom.Split(new char[1] { '-' }, 2, StringSplitOptions.RemoveEmptyEntries); if (morceau.Length == 1) { //Euh le tiret n'était précédé ou suivi de rien :/ tune.Title = morceau[0]; } else { //Et voici le cas normal :) tune.By = morceau[0]; tune.Title = morceau[1]; } } else { //Titre tune.Title = nom; } return(tune); }
/// <summary> /// Gets the tune ID. /// </summary> /// <param name="tune">The tune to find.</param> /// <returns>The tune ID</returns> private int GetTuneID(TuneToIndex tune) { string sql = String.Format( @"SELECT tune_id FROM Tunes WHERE tune_by = '{0}' AND tune_title = '{1}' AND tune_comment = '{2}'", Utilities.SqlEscape(tune.By), Utilities.SqlEscape(tune.Title), Utilities.SqlEscape(tune.Comment) ); MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); reader.Read(); int id = reader.GetInt32(0); reader.Close(); cmd.Dispose(); SqlLog(sql); return(id); }
/// <summary> /// Gets the tune info. /// </summary> /// <param name="path">The path to the tune.</param> /// <returns>Tune info</returns> /// <exception cref="System.Exception">throw if path is not found in database</exception> public TuneToIndex GetTuneInfo (string path) { bool notFoundException = true; string sql = String.Format( "SELECT t.tune_by, t.tune_title, t.tune_comment FROM tunes t, files f WHERE t.tune_id = f.file_id AND f.file_path '{0}'", Utilities.SqlEscape(path) ); TuneToIndex tune = new TuneToIndex(); tune.Path = path; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { notFoundException = false; tune.By = reader.GetString(0); tune.Title = reader.GetString(1); tune.Comment = reader.GetString(2); } reader.Close(); cmd.Dispose(); if (notFoundException) throw new Exception("Not found in database"); return tune; }
/// <summary> /// Determines whether the specified tune exists. /// </summary> /// <param name="tune">The tune.</param> /// <returns> /// <c>true</c> if [is tune exists] [the specified tune]; otherwise, <c>false</c>. /// </returns> private bool IsTuneExists(TuneToIndex tune) { string sql = String.Format( @"SELECT count(*) FROM Tunes WHERE tune_by = '{0}' AND tune_title = '{1}' AND tune_comment = '{2}'", Utilities.SqlEscape(tune.By), Utilities.SqlEscape(tune.Title), Utilities.SqlEscape(tune.Comment) ); MySqlCommand cmd = new MySqlCommand(sql, conn); cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); reader.Read(); int count = reader.GetInt32(0); reader.Close(); cmd.Dispose(); SqlLog(sql); //Si count(*) = 1, nous renvoyons true, sinon false return(count > 0); }
/// <summary> /// Gets the tune ID. /// </summary> /// <param name="tune">The tune to find.</param> /// <returns>The tune ID</returns> private int GetTuneID (TuneToIndex tune) { string sql = String.Format( @"SELECT tune_id FROM Tunes WHERE tune_by = '{0}' AND tune_title = '{1}' AND tune_comment = '{2}'", Utilities.SqlEscape(tune.By), Utilities.SqlEscape(tune.Title), Utilities.SqlEscape(tune.Comment) ); MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); reader.Read(); int id = reader.GetInt32(0); reader.Close(); cmd.Dispose(); SqlLog(sql); return id; }
/// <summary> /// Determines whether the specified tune exists. /// </summary> /// <param name="tune">The tune.</param> /// <returns> /// <c>true</c> if [is tune exists] [the specified tune]; otherwise, <c>false</c>. /// </returns> private bool IsTuneExists (TuneToIndex tune) { string sql = String.Format( @"SELECT count(*) FROM Tunes WHERE tune_by = '{0}' AND tune_title = '{1}' AND tune_comment = '{2}'", Utilities.SqlEscape(tune.By), Utilities.SqlEscape(tune.Title), Utilities.SqlEscape(tune.Comment) ); MySqlCommand cmd = new MySqlCommand(sql, conn); cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); reader.Read(); int count = reader.GetInt32(0); reader.Close(); cmd.Dispose(); SqlLog(sql); //Si count(*) = 1, nous renvoyons true, sinon false return (count > 0); }