using System; using lliurex; namespace lliurex { namespace controlcenter { public class SqlManager { //private Mono.Data.SqliteClient.SqliteConnection connection; private Mono.Data.Sqlite.SqliteConnection connection; private string sql_url; public bool sql_ready; public SqlManager () { Console.WriteLine("[SqlManager] Connecting to database..."); sql_url="URI=file:/usr/share/zero-sqlmanager/status.db"; try { connection=new Mono.Data.Sqlite.SqliteConnection(sql_url); connection.Open(); sql_ready=true; } catch { Console.WriteLine("[SqlManager] Database is not ready. States won't be saved"); sql_ready=false; } } public void select_query(string query, out System.Collections.Generic.Dictionary data) { System.Collections.Generic.Dictionary tmp_data = new System.Collections.Generic.Dictionary(); if(sql_ready) { Console.WriteLine("[SqlManager] Executing {0}...",query); Mono.Data.Sqlite.SqliteCommand command=connection.CreateCommand(); command.CommandText=query; Mono.Data.Sqlite.SqliteDataReader reader=command.ExecuteReader(); while(reader.Read()) { // 0 ID, 1 NAME, 2 STATE try { tmp_data[reader.GetString(1)]=reader.GetInt32(2); } catch { try { tmp_data[reader.GetString(1)]=int.Parse(reader.GetString(2)); } catch { Console.WriteLine("[SqlManager] Error processing {0} state",reader.GetString(1)); } } } reader.Close(); reader=null; command.Dispose(); command=null; } data=tmp_data; } public void insert_query(string query) { if (sql_ready) { Console.WriteLine("[SqlManager] Executing {0}...",query); Mono.Data.Sqlite.SqliteCommand command = connection.CreateCommand(); command.CommandText=query; command.ExecuteNonQuery(); command.Dispose(); command=null; } } } } }