public bool SaveFuzzyDatabase(FdbEntity fdb) { //To do here... try { String query = String.Empty; SqliteConnection connection = new SqliteConnection(fdb.ConnString); ///Save Name////////////////////////////////////////////////////////////////////////////////// ///Save Schemes/////////////////////////////////////////////////////////////////////////////// int schemeID = 0; foreach (FzSchemeEntity s in fdb.Schemes) { /// Update <Scheme> to System Scheme Table ////////////////////////////////////////////// String schemeName = s.SchemeName; schemeID++; query = ""; query += "INSERT INTO SystemScheme VALUES ("; query += schemeID + ","; query += "'" + schemeName + "'"; query += " );"; if (!connection.Update(query)) { throw new Exception(connection.ErrorMessage); } ///Save attributes of the scheme to the System Attribute Table ///////////////////////// if (s.Attributes.Count > 0) { int attributeID = 0; foreach (FzAttributeEntity attr in s.Attributes) { attributeID++; query = ""; query += "INSERT INTO SystemAttribute VALUES ( "; query += attributeID + ","; query += "'" + attr.PrimaryKey + "'" + ","; query += "'" + attr.AttributeName + "'" + ","; query += "'" + attr.DataType.TypeName + "'" + ","; query += "'" + attr.DataType.DomainString + "'" + ","; query += "'" + attr.Description + "'" + ","; query += schemeID; query += " );"; if (!connection.Update(query)) { throw new Exception(connection.ErrorMessage); } } } } //System.Windows.Forms.MessageBox.Show("Save scheme OK"); ///Save Relations ////////////////////////////////////////////////////////////////////////// int relationID = 0; foreach (FzRelationEntity relation in fdb.Relations) { //int schemeID = 0; relationID++; String relationName = relation.RelationName; schemeID = connection.GetSchemeID(relation.Scheme.SchemeName); query = ""; query += "INSERT INTO SystemRelation VALUES ( "; query += relationID + ","; query += "'" + relationName + "'" + ","; query += schemeID; query += " );"; if (!connection.Update(query)) { throw new Exception(connection.ErrorMessage); } //System.Windows.Forms.MessageBox.Show("insert to system relaton ok"); ///Create Table <Relation> //////////////////////////////////////////////// if (relation.Scheme.Attributes.Count > 0) { query = ""; query += "CREATE TABLE " + relationName + " ( "; foreach (FzAttributeEntity attribute in relation.Scheme.Attributes) { query += attribute.AttributeName + " " + "TEXT" + ", "; } query = query.Remove(query.LastIndexOf(','), 1); query += " ); "; if (!connection.CreateTable(query)) { throw new Exception(connection.ErrorMessage); } } //System.Windows.Forms.MessageBox.Show("create table relation ok!"); ///Insert tuples into Talbe <Relation> //////////////////////////////////// if (relation.Tuples.Count > 0) { foreach (FzTupleEntity tuple in relation.Tuples) { query = ""; query += "INSERT INTO " + relationName + " VALUES ("; foreach (var value in tuple.ValuesOnPerRow) { query += "'" + value + "'" + ","; } query = query.Remove(query.LastIndexOf(','), 1); query += " ); "; if (!connection.Update(query)) { throw new Exception(connection.ErrorMessage); } } } } //System.Windows.Forms.MessageBox.Show("Insert to tuple ok"); ///Save queries//////////////////////////////////////////////////////////////// int queryID = 0; foreach (FzQueryEntity q in fdb.Queries) { queryID++; query = ""; query += "INSERT INTO SystemQuery VALUES ("; query += queryID + ","; query += "'" + q.QueryName + "'" + ","; query += "'" + q.QueryString + "'"; query += " );"; if (!connection.Update(query)) { throw new Exception(connection.ErrorMessage); } } connection.CloseConnect(); } catch (SQLiteException ex) { throw new Exception("ERROR:\n" + ex.Message); } return true; }
public void DropFuzzyDatabase(FdbEntity fdb) { try { SqliteConnection connection = new SqliteConnection(fdb.ConnString); DataSet ds = new DataSet(); ds.Tables.Add(connection.GetDataTable("SELECT * FROM SystemScheme", "system_scheme")); ds.Tables.Add(connection.GetDataTable("SELECT * FROM SystemRelation", "system_relation")); ds.Tables.Add(connection.GetDataTable("SELECT * FROM SystemAttribute", "system_attribute")); ds.Tables.Add(connection.GetDataTable("SELECT * FROM SystemQuery", "system_query")); foreach (DataRow row in ds.Tables["system_relation"].Rows) { String relationname = row[1].ToString(); if (!connection.DropTable(relationname)) { throw new Exception(connection.ErrorMessage); } } if (!connection.Update("DELETE FROM SystemScheme")) { throw new Exception(connection.ErrorMessage); } if (!connection.Update("DELETE FROM SystemRelation")) { throw new Exception(connection.ErrorMessage); } if (!connection.Update("DELETE FROM SystemAttribute")) { throw new Exception(connection.ErrorMessage); } if (!connection.Update("DELETE FROM SystemQuery")) { throw new Exception(connection.ErrorMessage); } connection.CloseConnect(); } catch (Exception ex) { throw new Exception("ERROR:\n" + ex.Message); } }