public List <ResultTableDefinition> LoadTables([NotNull] HouseholdKey dbKey)
        {
            List <ResultTableDefinition> td = new List <ResultTableDefinition>();
            const string sql = "SELECT * FROM TableDescription";

            string constr = "Data Source=" + FilenameByHouseholdKey[dbKey].Filename + ";Version=3";

            using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(constr)) {
                //;Synchronous=OFF;Journal Mode=WAL;
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand()) {
                    cmd.Connection  = conn;
                    cmd.CommandText = sql;
                    var reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        string                tableName      = reader["TableName"].ToString();
                        string                description    = reader["Description"].ToString();
                        int                   resultTableid  = (int)(long)reader["ResultTableID"];
                        CalcOption            enablingOption = (CalcOption)(long)reader["EnablingOption"];
                        ResultTableDefinition fe             = new ResultTableDefinition(tableName, (ResultTableID)resultTableid, description, enablingOption);
                        td.Add(fe);
                    }
                }

                conn.Close();
            }

            return(td);
        }
 protected DataSaverBase([NotNull] Type savingType, [NotNull] ResultTableDefinition resultTableDefinition,
                         [CanBeNull] SqlResultLoggingService srls)
 {
     SavingType = savingType;
     //check if no readonly properties
     CheckType(savingType);
     ResultTableDefinition = resultTableDefinition;
     _srls = srls;
 }
        public List <T> ReadFromJson <T>([NotNull] ResultTableDefinition rtd, [NotNull] HouseholdKey key,
                                         ExpectedResultCount expectedResult)
        {
            if (!_isFileNameDictLoaded)
            {
                LoadFileNameDict();
            }

            string sql = "SELECT json FROM " + rtd.TableName;

            if (!FilenameByHouseholdKey.ContainsKey(key))
            {
                throw new LPGException("Missing sql file for household key " + key);
            }

            string constr = "Data Source=" + FilenameByHouseholdKey[key].Filename + ";Version=3";

            using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(constr)) {
                //;Synchronous=OFF;Journal Mode=WAL;
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand()) {
                    cmd.Connection = conn;

                    /*
                     * List<string> tables = new List<string>();
                     * cmd.CommandText = "SELECT name FROM sqlite_master WHERE type='table';";
                     * using (var reader2 = cmd.ExecuteReader()) {
                     *  while (reader2.Read())
                     *  {
                     *      tables.Add(reader2[0].ToString());
                     *  }
                     * }*/

                    cmd.CommandText = sql;
                    List <string> results = new List <string>();
                    var           reader  = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        results.Add(reader[0].ToString());
                    }

                    switch (expectedResult)
                    {
                    case ExpectedResultCount.One:
                        if (results.Count != 1)
                        {
                            throw new DataIntegrityException("Not exactly one result");
                        }

                        break;

                    case ExpectedResultCount.Many:
                        if (results.Count < 2)
                        {
                            throw new DataIntegrityException("Not many results");
                        }

                        break;

                    case ExpectedResultCount.OneOrMore:
                        if (results.Count < 1)
                        {
                            throw new DataIntegrityException("Not one or more results");
                        }

                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(expectedResult), expectedResult, null);
                    }

                    List <T> resultsObjects = new List <T>();
                    foreach (string s in results)
                    {
                        T re = JsonConvert.DeserializeObject <T>(s);
                        resultsObjects.Add(re);
                    }

                    conn.Close();
                    return(resultsObjects);
                }
            }
        }