Changeset 2221 for branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.Database.SQLServerCompact
- Timestamp:
- 08/03/09 11:33:15 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs
r2217 r2221 17 17 public DatabaseService(string connection) { 18 18 this.connection = connection; 19 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 20 if (!ctx.DatabaseExists()) 21 ctx.CreateDatabase(); 22 } 19 Connect(); 20 if (!ctx.DatabaseExists()) 21 ctx.CreateDatabase(); 23 22 } 24 23 25 24 private void EmptyDatabase() { 26 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 27 ctx.DeleteDatabase(); 28 ctx.CreateDatabase(); 29 } 25 ctx.DeleteDatabase(); 26 ctx.CreateDatabase(); 27 } 28 29 private ModelingDataContext ctx; 30 public void Connect() { 31 if (ctx != null) 32 return; 33 34 ctx = new ModelingDataContext(connection); 35 DataLoadOptions dlo = new DataLoadOptions(); 36 dlo.LoadWith<ModelResult>(mr => mr.Result); 37 dlo.LoadWith<InputVariableResult>(ir => ir.Variable); 38 dlo.LoadWith<InputVariableResult>(ir => ir.Result); 39 dlo.LoadWith<Model>(m => m.TargetVariable); 40 ctx.LoadOptions = dlo; 41 } 42 43 public void Disconnect() { 44 if (ctx == null) 45 return; 46 ctx.Dispose(); 47 ctx = null; 30 48 } 31 49 … … 103 121 104 122 public Dataset GetDataset() { 105 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 106 if (ctx.Problems.Count() != 1) 107 throw new InvalidOperationException("Could not get dataset. No or more than one problems are persisted in the database."); 108 109 Problem problem = ctx.Problems.Single(); 110 return problem.Dataset; 111 } 123 if (ctx.Problems.Count() != 1) 124 throw new InvalidOperationException("Could not get dataset. No or more than one problems are persisted in the database."); 125 126 Problem problem = ctx.Problems.Single(); 127 return problem.Dataset; 128 112 129 } 113 130 114 131 public Problem GetOrCreateProblem(Dataset dataset) { 115 132 Problem problem; 116 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 117 if (ctx.Problems.Count() == 0) 118 problem = PersistProblem(dataset); 119 else 120 problem = ctx.Problems.Single(); 121 if (problem.Dataset.ToString() != dataset.ToString()) 122 throw new InvalidOperationException("Could not persist dataset. The database already contains a different dataset."); 123 } 133 if (ctx.Problems.Count() == 0) 134 problem = PersistProblem(dataset); 135 else 136 problem = ctx.Problems.Single(); 137 if (problem.Dataset.ToString() != dataset.ToString()) 138 throw new InvalidOperationException("Could not persist dataset. The database already contains a different dataset."); 124 139 return problem; 125 140 } … … 165 180 public Dictionary<string, Variable> GetAllVariables() { 166 181 Dictionary<string, Variable> dict = new Dictionary<string, Variable>(); 167 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 168 dict = ctx.Variables.ToDictionary<Variable, string>(delegate(Variable v) { return v.Name; }); 169 } 182 dict = ctx.Variables.ToDictionary<Variable, string>(delegate(Variable v) { return v.Name; }); 170 183 return dict; 171 184 } … … 192 205 193 206 public IEnumerable<IResult> GetAllResults() { 194 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 195 return ctx.Results.ToList().Cast<IResult>(); 196 } 207 return ctx.Results.ToList().Cast<IResult>(); 197 208 } 198 209 199 210 public IEnumerable<IResult> GetAllResultsForInputVariables() { 200 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 201 return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>(); 202 } 211 return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>(); 203 212 } 204 213 … … 207 216 #region ModelResult 208 217 public IEnumerable<IModelResult> GetModelResults(IModel model) { 209 ModelingDataContext ctx = new ModelingDataContext(connection);210 DataLoadOptions dlo = new DataLoadOptions();211 dlo.LoadWith<ModelResult>(mr => mr.Result);212 ctx.LoadOptions = dlo;213 214 218 var results = from result in ctx.ModelResults 215 219 where result.Model == model … … 217 221 return results.ToList().Cast<IModelResult>(); 218 222 } 219 220 public void GetAllModelResults() {221 ModelingDataContext ctx = new ModelingDataContext(connection);222 DataLoadOptions dlo = new DataLoadOptions();223 dlo.LoadWith<ModelResult>(mr => mr.Result);224 dlo.LoadWith<ModelResult>(mr => mr.Model);225 ctx.LoadOptions = dlo;226 227 var results = from result in ctx.ModelResults228 select result;229 results.ToList();230 231 }232 223 #endregion 233 224 234 225 #region InputVariableResults 235 226 public IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model) { 236 ModelingDataContext ctx = new ModelingDataContext(connection);237 DataLoadOptions dlo = new DataLoadOptions();238 dlo.LoadWith<InputVariableResult>(ir => ir.Variable);239 dlo.LoadWith<InputVariableResult>(ir => ir.Result);240 ctx.LoadOptions = dlo;241 242 227 var inputResults = from ir in ctx.InputVariableResults 243 228 where ir.Model == model … … 250 235 #region Model 251 236 public IEnumerable<IModel> GetAllModels() { 252 ModelingDataContext ctx = new ModelingDataContext(connection);253 DataLoadOptions dlo = new DataLoadOptions();254 dlo.LoadWith<Model>(m => m.TargetVariable);255 ctx.LoadOptions = dlo;256 237 return ctx.Models.ToList().Cast<IModel>(); 257 238 } 258 239 259 240 public byte[] GetModelData(IModel model) { 260 using (ModelingDataContext ctx = new ModelingDataContext(connection)) { 261 var data = (from md in ctx.ModelData 262 where md.Model == model 263 select md); 264 if (data.Count() == 0) 265 return null; 266 return data.Single().Data; 267 } 241 var data = (from md in ctx.ModelData 242 where md.Model == model 243 select md); 244 if (data.Count() == 0) 245 return null; 246 return data.Single().Data; 268 247 } 269 248 #endregion
Note: See TracChangeset
for help on using the changeset viewer.