Line data Source code
1 : // EnergyPlus, Copyright (c) 1996-2024, The Board of Trustees of the University of Illinois,
2 : // The Regents of the University of California, through Lawrence Berkeley National Laboratory
3 : // (subject to receipt of any required approvals from the U.S. Dept. of Energy), Oak Ridge
4 : // National Laboratory, managed by UT-Battelle, Alliance for Sustainable Energy, LLC, and other
5 : // contributors. All rights reserved.
6 : //
7 : // NOTICE: This Software was developed under funding from the U.S. Department of Energy and the
8 : // U.S. Government consequently retains certain rights. As such, the U.S. Government has been
9 : // granted for itself and others acting on its behalf a paid-up, nonexclusive, irrevocable,
10 : // worldwide license in the Software to reproduce, distribute copies to the public, prepare
11 : // derivative works, and perform publicly and display publicly, and to permit others to do so.
12 : //
13 : // Redistribution and use in source and binary forms, with or without modification, are permitted
14 : // provided that the following conditions are met:
15 : //
16 : // (1) Redistributions of source code must retain the above copyright notice, this list of
17 : // conditions and the following disclaimer.
18 : //
19 : // (2) Redistributions in binary form must reproduce the above copyright notice, this list of
20 : // conditions and the following disclaimer in the documentation and/or other materials
21 : // provided with the distribution.
22 : //
23 : // (3) Neither the name of the University of California, Lawrence Berkeley National Laboratory,
24 : // the University of Illinois, U.S. Dept. of Energy nor the names of its contributors may be
25 : // used to endorse or promote products derived from this software without specific prior
26 : // written permission.
27 : //
28 : // (4) Use of EnergyPlus(TM) Name. If Licensee (i) distributes the software in stand-alone form
29 : // without changes from the version obtained under this License, or (ii) Licensee makes a
30 : // reference solely to the software portion of its product, Licensee must refer to the
31 : // software as "EnergyPlus version X" software, where "X" is the version number Licensee
32 : // obtained under this License and may not use a different name for the software. Except as
33 : // specifically required in this Section (4), Licensee shall not use in a company name, a
34 : // product name, in advertising, publicity, or other promotional activities any name, trade
35 : // name, trademark, logo, or other designation of "EnergyPlus", "E+", "e+" or confusingly
36 : // similar designation, without the U.S. Department of Energy's prior written consent.
37 : //
38 : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
39 : // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
40 : // AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
41 : // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 : // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43 : // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
45 : // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46 : // POSSIBILITY OF SUCH DAMAGE.
47 :
48 : // C++ headers
49 : #include <ios>
50 : #include <memory>
51 : #include <sstream>
52 : #include <stdexcept>
53 :
54 : // EnergyPlus Headers
55 : #include <EnergyPlus/Construction.hh>
56 : #include <EnergyPlus/ConvectionConstants.hh>
57 : #include <EnergyPlus/Data/EnergyPlusData.hh>
58 : #include <EnergyPlus/DataEnvironment.hh>
59 : #include <EnergyPlus/DataHeatBalance.hh>
60 : #include <EnergyPlus/DataRoomAirModel.hh>
61 : #include <EnergyPlus/DataStringGlobals.hh>
62 : #include <EnergyPlus/FileSystem.hh>
63 : #include <EnergyPlus/General.hh>
64 : #include <EnergyPlus/InputProcessing/InputProcessor.hh>
65 : #include <EnergyPlus/Material.hh>
66 : #include <EnergyPlus/OutputReportTabular.hh>
67 : #include <EnergyPlus/SQLiteProcedures.hh>
68 : #include <EnergyPlus/ScheduleManager.hh>
69 : #include <EnergyPlus/UtilityRoutines.hh>
70 :
71 : namespace EnergyPlus {
72 :
73 : constexpr std::array<int, (int)OutputProcessor::ReportFreq::Num> reportFreqInts = {
74 : -1, // EachCall
75 : 0, // TimeStep
76 : 1, // Hour
77 : 2, // Day
78 : 3, // Month
79 : 4, // Simulation
80 : 5 // Year
81 : };
82 :
83 : const int SQLite::ReportNameId = 1;
84 : const int SQLite::ReportForStringId = 2;
85 : const int SQLite::TableNameId = 3;
86 : const int SQLite::RowNameId = 4;
87 : const int SQLite::ColumnNameId = 5;
88 : const int SQLite::UnitsId = 6;
89 :
90 795 : bool ParseSQLiteInput(EnergyPlusData &state, bool &writeOutputToSQLite, bool &writeTabularDataToSQLite)
91 : {
92 795 : auto &ip = state.dataInputProcessing->inputProcessor;
93 795 : auto const instances = ip->epJSON.find("Output:SQLite");
94 795 : if (instances != ip->epJSON.end()) {
95 :
96 492 : auto find_input = [=, &state](nlohmann::json const &fields, std::string const &field_name) -> std::string {
97 246 : std::string input;
98 246 : auto found = fields.find(field_name);
99 246 : if (found != fields.end()) {
100 123 : input = found.value().get<std::string>();
101 : } else {
102 123 : state.dataInputProcessing->inputProcessor->getDefaultValue(state, "Output:SQLite", field_name, input);
103 : }
104 492 : return input;
105 246 : };
106 :
107 123 : auto &sql_ort = state.dataOutRptTab;
108 :
109 : // There can only be 1 "Output:SQLite"
110 123 : auto const instance = instances.value().begin();
111 123 : auto const &fields = instance.value();
112 123 : ip->markObjectAsUsed("Output:SQLite", instance.key());
113 :
114 : { // "option_type"
115 246 : std::string outputType = find_input(fields, "option_type");
116 123 : if ("SimpleAndTabular" == outputType) {
117 23 : writeTabularDataToSQLite = true;
118 23 : writeOutputToSQLite = true;
119 100 : } else if ("Simple" == outputType) {
120 100 : writeTabularDataToSQLite = false;
121 100 : writeOutputToSQLite = true;
122 : }
123 123 : }
124 : { // "unit_conversion_for_tabular_data"
125 246 : std::string tabularDataUnitConversion = find_input(fields, "unit_conversion_for_tabular_data");
126 123 : if ("UseOutputControlTableStyles" == tabularDataUnitConversion) {
127 : // Jan 2021 Note: Since here we do not know weather sql_ort->unitsStyle has been processed or not,
128 : // the value "NotFound" is used for the option "UseOutputControlTableStyles" at this point;
129 : // This will be updated again and got concretely assigned first thing in OutputReportTabular::WriteTabularReports().
130 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::NotFound;
131 123 : } else if ("None" == tabularDataUnitConversion) {
132 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::None;
133 123 : } else if ("JtoKWH" == tabularDataUnitConversion) {
134 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::JtoKWH;
135 123 : } else if ("JtoMJ" == tabularDataUnitConversion) {
136 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::JtoMJ;
137 123 : } else if ("JtoGJ" == tabularDataUnitConversion) {
138 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::JtoGJ;
139 123 : } else if ("InchPound" == tabularDataUnitConversion) {
140 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::InchPound;
141 123 : } else if ("InchPoundExceptElectricity" == tabularDataUnitConversion) {
142 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::InchPoundExceptElectricity;
143 : }
144 123 : }
145 123 : return true;
146 123 : }
147 672 : return false;
148 795 : }
149 :
150 796 : std::unique_ptr<SQLite> CreateSQLiteDatabase(EnergyPlusData &state)
151 : {
152 796 : if (!state.files.outputControl.sqlite) {
153 1 : return nullptr;
154 : }
155 : try {
156 795 : bool writeOutputToSQLite = false;
157 795 : bool writeTabularDataToSQLite = false;
158 795 : bool parsedSQLite = ParseSQLiteInput(state, writeOutputToSQLite, writeTabularDataToSQLite);
159 795 : if (!parsedSQLite) {
160 672 : state.files.outputControl.sqlite = false;
161 672 : return nullptr;
162 : }
163 123 : auto errorStream = std::make_shared<std::ofstream>(state.dataStrGlobals->outputSqliteErrFilePath, std::ofstream::out | std::ofstream::trunc);
164 : return std::make_unique<SQLite>(errorStream,
165 246 : state.dataStrGlobals->outputSqlFilePath,
166 123 : state.dataStrGlobals->outputSqliteErrFilePath,
167 : writeOutputToSQLite,
168 123 : writeTabularDataToSQLite);
169 123 : } catch (const std::runtime_error &error) {
170 0 : ShowFatalError(state, error.what());
171 0 : return nullptr;
172 0 : }
173 : }
174 :
175 794 : void CreateSQLiteZoneExtendedOutput(EnergyPlusData &state)
176 : {
177 794 : if (state.dataSQLiteProcedures->sqlite && state.dataSQLiteProcedures->sqlite->writeOutputToSQLite()) {
178 1629 : for (int zoneNum = 1; zoneNum <= state.dataGlobal->NumOfZones; ++zoneNum) {
179 1506 : state.dataSQLiteProcedures->sqlite->addZoneData(zoneNum, state.dataHeatBal->Zone(zoneNum));
180 : }
181 135 : for (int listNum = 1; listNum <= state.dataHeatBal->NumOfZoneLists; ++listNum) {
182 12 : state.dataSQLiteProcedures->sqlite->addZoneListData(listNum, state.dataHeatBal->ZoneList(listNum));
183 : }
184 126 : for (int groupNum = 1; groupNum <= state.dataHeatBal->NumOfZoneGroups; ++groupNum) {
185 3 : state.dataSQLiteProcedures->sqlite->addZoneGroupData(groupNum, state.dataHeatBal->ZoneGroup(groupNum));
186 : }
187 3459 : for (int scheduleNumber = 1, numberOfSchedules = ScheduleManager::GetNumberOfSchedules(state); scheduleNumber <= numberOfSchedules;
188 : ++scheduleNumber) {
189 10008 : state.dataSQLiteProcedures->sqlite->addScheduleData(scheduleNumber,
190 6672 : ScheduleManager::GetScheduleName(state, scheduleNumber),
191 6672 : ScheduleManager::GetScheduleType(state, scheduleNumber),
192 : ScheduleManager::GetScheduleMinValue(state, scheduleNumber),
193 : ScheduleManager::GetScheduleMaxValue(state, scheduleNumber));
194 : }
195 13744 : for (int surfaceNumber = 1; surfaceNumber <= state.dataSurface->TotSurfaces; ++surfaceNumber) {
196 13621 : auto const &surface = state.dataSurface->Surface(surfaceNumber);
197 13621 : state.dataSQLiteProcedures->sqlite->addSurfaceData(surfaceNumber, surface, DataSurfaces::cSurfaceClass(surface.Class));
198 : }
199 2015 : for (int materialNum = 1; materialNum <= state.dataMaterial->TotMaterials; ++materialNum) {
200 1892 : state.dataSQLiteProcedures->sqlite->addMaterialData(materialNum, state.dataMaterial->Material(materialNum));
201 : }
202 1021 : for (int constructNum = 1; constructNum <= state.dataHeatBal->TotConstructs; ++constructNum) {
203 898 : auto const &construction = state.dataConstruction->Construct(constructNum);
204 898 : if (construction.TotGlassLayers == 0) {
205 716 : state.dataSQLiteProcedures->sqlite->addConstructionData(constructNum, construction, construction.UValue);
206 : } else {
207 182 : state.dataSQLiteProcedures->sqlite->addConstructionData(constructNum, construction, state.dataHeatBal->NominalU(constructNum));
208 : }
209 : }
210 1521 : for (int lightNum = 1; lightNum <= state.dataHeatBal->TotLights; ++lightNum) {
211 1398 : state.dataSQLiteProcedures->sqlite->addNominalLightingData(lightNum, state.dataHeatBal->Lights(lightNum));
212 : }
213 1349 : for (int peopleNum = 1; peopleNum <= state.dataHeatBal->TotPeople; ++peopleNum) {
214 1226 : state.dataSQLiteProcedures->sqlite->addNominalPeopleData(peopleNum, state.dataHeatBal->People(peopleNum));
215 : }
216 1467 : for (int elecEquipNum = 1; elecEquipNum <= state.dataHeatBal->TotElecEquip; ++elecEquipNum) {
217 1344 : state.dataSQLiteProcedures->sqlite->addNominalElectricEquipmentData(elecEquipNum, state.dataHeatBal->ZoneElectric(elecEquipNum));
218 : }
219 154 : for (int gasEquipNum = 1; gasEquipNum <= state.dataHeatBal->TotGasEquip; ++gasEquipNum) {
220 31 : state.dataSQLiteProcedures->sqlite->addNominalGasEquipmentData(gasEquipNum, state.dataHeatBal->ZoneGas(gasEquipNum));
221 : }
222 123 : for (int steamEquipNum = 1; steamEquipNum <= state.dataHeatBal->TotStmEquip; ++steamEquipNum) {
223 0 : state.dataSQLiteProcedures->sqlite->addNominalSteamEquipmentData(steamEquipNum, state.dataHeatBal->ZoneSteamEq(steamEquipNum));
224 : }
225 132 : for (int hWEquipNum = 1; hWEquipNum <= state.dataHeatBal->TotHWEquip; ++hWEquipNum) {
226 9 : state.dataSQLiteProcedures->sqlite->addNominalHotWaterEquipmentData(hWEquipNum, state.dataHeatBal->ZoneHWEq(hWEquipNum));
227 : }
228 142 : for (int otherEquipNum = 1; otherEquipNum <= state.dataHeatBal->TotOthEquip; ++otherEquipNum) {
229 19 : state.dataSQLiteProcedures->sqlite->addNominalOtherEquipmentData(otherEquipNum, state.dataHeatBal->ZoneOtherEq(otherEquipNum));
230 : }
231 123 : for (int bBHeatNum = 1; bBHeatNum <= state.dataHeatBal->TotBBHeat; ++bBHeatNum) {
232 0 : state.dataSQLiteProcedures->sqlite->addNominalBaseboardData(bBHeatNum, state.dataHeatBal->ZoneBBHeat(bBHeatNum));
233 : }
234 1298 : for (int infilNum = 1; infilNum <= state.dataHeatBal->TotInfiltration; ++infilNum) {
235 1175 : state.dataSQLiteProcedures->sqlite->addInfiltrationData(infilNum, state.dataHeatBal->Infiltration(infilNum));
236 : }
237 131 : for (int ventNum = 1; ventNum <= state.dataHeatBal->TotVentilation; ++ventNum) {
238 8 : state.dataSQLiteProcedures->sqlite->addVentilationData(ventNum, state.dataHeatBal->Ventilation(ventNum));
239 : }
240 1629 : for (int zoneNum = 1; zoneNum <= state.dataGlobal->NumOfZones; ++zoneNum) {
241 1506 : state.dataSQLiteProcedures->sqlite->addRoomAirModelData(zoneNum, state.dataRoomAir->AirModel(zoneNum));
242 : }
243 :
244 123 : state.dataSQLiteProcedures->sqlite->createZoneExtendedOutput();
245 : }
246 794 : }
247 :
248 123 : SQLite::SQLite(std::shared_ptr<std::ostream> errorStream,
249 : fs::path const &dbName,
250 : fs::path const &errorFilePath,
251 : bool writeOutputToSQLite,
252 123 : bool writeTabularDataToSQLite)
253 123 : : SQLiteProcedures(errorStream, writeOutputToSQLite, dbName, errorFilePath), m_writeTabularDataToSQLite(writeTabularDataToSQLite),
254 246 : m_sqlDBTimeIndex(0), m_reportDataInsertStmt(nullptr), m_reportExtendedDataInsertStmt(nullptr), m_reportDictionaryInsertStmt(nullptr),
255 123 : m_timeIndexInsertStmt(nullptr), m_zoneInfoInsertStmt(nullptr), m_zoneInfoZoneListInsertStmt(nullptr), m_nominalLightingInsertStmt(nullptr),
256 123 : m_nominalElectricEquipmentInsertStmt(nullptr), m_nominalGasEquipmentInsertStmt(nullptr), m_nominalSteamEquipmentInsertStmt(nullptr),
257 123 : m_nominalHotWaterEquipmentInsertStmt(nullptr), m_nominalOtherEquipmentInsertStmt(nullptr), m_nominalBaseboardHeatInsertStmt(nullptr),
258 123 : m_surfaceInsertStmt(nullptr), m_constructionInsertStmt(nullptr), m_constructionLayerInsertStmt(nullptr), m_materialInsertStmt(nullptr),
259 123 : m_zoneListInsertStmt(nullptr), m_zoneGroupInsertStmt(nullptr), m_infiltrationInsertStmt(nullptr), m_ventilationInsertStmt(nullptr),
260 123 : m_nominalPeopleInsertStmt(nullptr), m_zoneSizingInsertStmt(nullptr), m_systemSizingInsertStmt(nullptr), m_componentSizingInsertStmt(nullptr),
261 123 : m_roomAirModelInsertStmt(nullptr), m_groundTemperatureInsertStmt(nullptr), m_weatherFileInsertStmt(nullptr), m_scheduleInsertStmt(nullptr),
262 123 : m_daylightMapTitleInsertStmt(nullptr), m_daylightMapHourlyTitleInsertStmt(nullptr), m_daylightMapHourlyDataInsertStmt(nullptr),
263 123 : m_environmentPeriodInsertStmt(nullptr), m_simulationsInsertStmt(nullptr), m_tabularDataInsertStmt(nullptr), m_stringsInsertStmt(nullptr),
264 123 : m_stringsLookUpStmt(nullptr), m_errorInsertStmt(nullptr), m_errorUpdateStmt(nullptr), m_simulationUpdateStmt(nullptr),
265 123 : m_simulationDataUpdateStmt(nullptr), m_rollbackToSavepointStmt(nullptr), m_createSavepointStmt(nullptr), m_releaseSavepointStmt(nullptr)
266 : {
267 123 : if (m_writeOutputToSQLite) {
268 123 : sqliteExecuteCommand("PRAGMA locking_mode = EXCLUSIVE;");
269 123 : sqliteExecuteCommand("PRAGMA journal_mode = OFF;");
270 123 : sqliteExecuteCommand("PRAGMA synchronous = OFF;");
271 123 : sqliteExecuteCommand("PRAGMA encoding=\"UTF-8\";");
272 :
273 : // Turn this to ON for Foreign Key constraints.
274 : // This must be turned ON for every connection
275 : // Currently, inserting into daylighting tables does not work with this ON. The ZoneIndex referenced by DaylightMaps does not exist in
276 : // the database at the time data is inserted.
277 123 : sqliteExecuteCommand("PRAGMA foreign_keys = OFF;");
278 :
279 123 : initializeSimulationsTable();
280 123 : initializeEnvironmentPeriodsTable();
281 123 : initializeErrorsTable();
282 123 : initializeTimeIndicesTable();
283 123 : initializeZoneInfoTable();
284 123 : initializeZoneListTable();
285 123 : initializeZoneGroupTable();
286 123 : initializeZoneInfoZoneListTable();
287 123 : initializeSchedulesTable();
288 123 : initializeMaterialsTable();
289 123 : initializeConstructionsTables();
290 123 : initializeSurfacesTable();
291 123 : initializeReportDataDictionaryTable();
292 123 : initializeReportDataTables();
293 123 : initializeNominalPeopleTable();
294 123 : initializeNominalLightingTable();
295 123 : initializeNominalElectricEquipmentTable();
296 123 : initializeNominalGasEquipmentTable();
297 123 : initializeNominalSteamEquipmentTable();
298 123 : initializeNominalHotWaterEquipmentTable();
299 123 : initializeNominalOtherEquipmentTable();
300 123 : initializeNominalBaseboardHeatTable();
301 123 : initializeNominalInfiltrationTable();
302 123 : initializeNominalVentilationTable();
303 123 : initializeZoneSizingTable();
304 123 : initializeSystemSizingTable();
305 123 : initializeComponentSizingTable();
306 123 : initializeRoomAirModelTable();
307 123 : initializeDaylightMapTables();
308 123 : initializeViews();
309 :
310 123 : if (m_writeTabularDataToSQLite) {
311 23 : initializeTabularDataTable();
312 23 : initializeTabularDataView();
313 : }
314 : }
315 123 : }
316 :
317 246 : SQLite::~SQLite()
318 : {
319 123 : sqlite3_finalize(m_reportDataInsertStmt);
320 123 : sqlite3_finalize(m_reportExtendedDataInsertStmt);
321 123 : sqlite3_finalize(m_reportDictionaryInsertStmt);
322 123 : sqlite3_finalize(m_timeIndexInsertStmt);
323 123 : sqlite3_finalize(m_zoneInfoInsertStmt);
324 123 : sqlite3_finalize(m_zoneListInsertStmt);
325 123 : sqlite3_finalize(m_zoneGroupInsertStmt);
326 123 : sqlite3_finalize(m_zoneInfoZoneListInsertStmt);
327 123 : sqlite3_finalize(m_nominalLightingInsertStmt);
328 123 : sqlite3_finalize(m_nominalElectricEquipmentInsertStmt);
329 123 : sqlite3_finalize(m_nominalGasEquipmentInsertStmt);
330 123 : sqlite3_finalize(m_nominalSteamEquipmentInsertStmt);
331 123 : sqlite3_finalize(m_nominalHotWaterEquipmentInsertStmt);
332 123 : sqlite3_finalize(m_nominalOtherEquipmentInsertStmt);
333 123 : sqlite3_finalize(m_nominalBaseboardHeatInsertStmt);
334 123 : sqlite3_finalize(m_surfaceInsertStmt);
335 123 : sqlite3_finalize(m_constructionInsertStmt);
336 123 : sqlite3_finalize(m_constructionLayerInsertStmt);
337 123 : sqlite3_finalize(m_materialInsertStmt);
338 123 : sqlite3_finalize(m_infiltrationInsertStmt);
339 123 : sqlite3_finalize(m_ventilationInsertStmt);
340 123 : sqlite3_finalize(m_nominalPeopleInsertStmt);
341 123 : sqlite3_finalize(m_zoneSizingInsertStmt);
342 123 : sqlite3_finalize(m_systemSizingInsertStmt);
343 123 : sqlite3_finalize(m_componentSizingInsertStmt);
344 123 : sqlite3_finalize(m_roomAirModelInsertStmt);
345 123 : sqlite3_finalize(m_groundTemperatureInsertStmt);
346 123 : sqlite3_finalize(m_weatherFileInsertStmt);
347 123 : sqlite3_finalize(m_scheduleInsertStmt);
348 123 : sqlite3_finalize(m_daylightMapTitleInsertStmt);
349 123 : sqlite3_finalize(m_daylightMapHourlyTitleInsertStmt);
350 123 : sqlite3_finalize(m_daylightMapHourlyDataInsertStmt);
351 123 : sqlite3_finalize(m_environmentPeriodInsertStmt);
352 123 : sqlite3_finalize(m_simulationsInsertStmt);
353 123 : sqlite3_finalize(m_tabularDataInsertStmt);
354 123 : sqlite3_finalize(m_stringsInsertStmt);
355 123 : sqlite3_finalize(m_stringsLookUpStmt);
356 123 : sqlite3_finalize(m_errorInsertStmt);
357 123 : sqlite3_finalize(m_errorUpdateStmt);
358 123 : sqlite3_finalize(m_simulationUpdateStmt);
359 123 : sqlite3_finalize(m_simulationDataUpdateStmt);
360 123 : sqlite3_finalize(m_rollbackToSavepointStmt);
361 123 : sqlite3_finalize(m_createSavepointStmt);
362 123 : sqlite3_finalize(m_releaseSavepointStmt);
363 246 : }
364 :
365 123 : bool SQLite::writeOutputToSQLite() const
366 : {
367 123 : return m_writeOutputToSQLite;
368 : }
369 :
370 0 : bool SQLite::writeTabularDataToSQLite() const
371 : {
372 0 : return m_writeTabularDataToSQLite;
373 : }
374 :
375 2586 : void SQLite::sqliteBegin()
376 : {
377 2586 : if (m_writeOutputToSQLite) {
378 2586 : sqliteExecuteCommand("BEGIN;");
379 : }
380 2586 : }
381 :
382 2404 : void SQLite::sqliteCommit()
383 : {
384 2404 : if (m_writeOutputToSQLite) {
385 2404 : sqliteExecuteCommand("COMMIT;");
386 : }
387 2404 : }
388 :
389 182 : void SQLite::sqliteRollback()
390 : {
391 182 : if (m_writeOutputToSQLite) {
392 182 : sqliteExecuteCommand("ROLLBACK;");
393 : }
394 182 : }
395 :
396 0 : void SQLite::sqliteRollbackToSavepoint(std::string_view savepoint_name)
397 : {
398 0 : if (m_writeOutputToSQLite) {
399 : static constexpr std::string_view rollbackToSavepointSQL("ROLLBACK TO SAVEPOINT ?;");
400 :
401 0 : sqlitePrepareStatement(m_rollbackToSavepointStmt, rollbackToSavepointSQL);
402 0 : sqliteBindText(m_rollbackToSavepointStmt, 1, savepoint_name);
403 0 : sqliteStepCommand(m_rollbackToSavepointStmt);
404 0 : sqliteResetCommand(m_rollbackToSavepointStmt);
405 : }
406 0 : }
407 :
408 0 : void SQLite::sqliteReleaseSavepoint(std::string_view savepoint_name)
409 : {
410 0 : if (m_writeOutputToSQLite) {
411 : static constexpr std::string_view releaseSavepointSQL("RELEASE SAVEPOINT ?;");
412 :
413 0 : sqlitePrepareStatement(m_releaseSavepointStmt, releaseSavepointSQL);
414 0 : sqliteBindText(m_releaseSavepointStmt, 1, savepoint_name);
415 0 : sqliteStepCommand(m_releaseSavepointStmt);
416 0 : sqliteResetCommand(m_releaseSavepointStmt);
417 : }
418 0 : }
419 :
420 0 : void SQLite::sqliteCreateSavepoint(std::string_view savepoint_name)
421 : {
422 0 : if (m_writeOutputToSQLite) {
423 : static constexpr std::string_view createSavepointSQL("SAVEPOINT ?;");
424 :
425 0 : sqlitePrepareStatement(m_createSavepointStmt, createSavepointSQL);
426 0 : sqliteBindText(m_createSavepointStmt, 1, savepoint_name);
427 0 : sqliteStepCommand(m_createSavepointStmt);
428 0 : sqliteResetCommand(m_createSavepointStmt);
429 : }
430 0 : }
431 :
432 0 : bool SQLite::sqliteWithinTransaction()
433 : {
434 0 : if (m_writeOutputToSQLite) {
435 0 : return SQLiteProcedures::sqliteWithinTransaction();
436 : }
437 0 : return false;
438 : }
439 :
440 0 : void SQLite::sqliteWriteMessage(std::string_view message)
441 : {
442 0 : if (m_writeOutputToSQLite) {
443 0 : *m_errorStream << "SQLite3 message, " << message << std::endl;
444 : }
445 0 : }
446 :
447 123 : void SQLite::initializeReportDataDictionaryTable()
448 : {
449 123 : constexpr std::string_view newTableSQL = "CREATE TABLE ReportDataDictionary("
450 : "ReportDataDictionaryIndex INTEGER PRIMARY KEY, "
451 : "IsMeter INTEGER, "
452 : "Type TEXT, "
453 : "IndexGroup TEXT, "
454 : "TimestepType TEXT, "
455 : "KeyValue TEXT, "
456 : "Name TEXT, "
457 : "ReportingFrequency TEXT, "
458 : "ScheduleName TEXT, "
459 : "Units TEXT);";
460 :
461 123 : sqliteExecuteCommand(newTableSQL);
462 :
463 123 : constexpr std::string_view preparedSQL = "INSERT INTO ReportDataDictionary ("
464 : "ReportDataDictionaryIndex, "
465 : "IsMeter, "
466 : "Type, "
467 : "IndexGroup, "
468 : "TimestepType, "
469 : "KeyValue, "
470 : "Name, "
471 : "ReportingFrequency, "
472 : "ScheduleName, "
473 : "Units) "
474 : "VALUES(?,?,?,?,?,?,?,?,?,?);";
475 :
476 123 : sqlitePrepareStatement(m_reportDictionaryInsertStmt, preparedSQL);
477 123 : }
478 :
479 123 : void SQLite::initializeReportDataTables()
480 : {
481 123 : constexpr std::string_view reportDataTableSQL =
482 : "CREATE TABLE ReportData ("
483 : "ReportDataIndex INTEGER PRIMARY KEY, "
484 : "TimeIndex INTEGER, "
485 : "ReportDataDictionaryIndex INTEGER, "
486 : "Value REAL, "
487 : "FOREIGN KEY(TimeIndex) REFERENCES Time(TimeIndex) "
488 : "ON DELETE CASCADE ON UPDATE CASCADE "
489 : "FOREIGN KEY(ReportDataDictionaryIndex) REFERENCES ReportDataDictionary(ReportDataDictionaryIndex) "
490 : "ON DELETE CASCADE ON UPDATE CASCADE "
491 : ");";
492 :
493 123 : sqliteExecuteCommand(reportDataTableSQL);
494 :
495 123 : constexpr std::string_view reportDataInsertSQL = "INSERT INTO ReportData ("
496 : "ReportDataIndex, "
497 : "TimeIndex, "
498 : "ReportDataDictionaryIndex, "
499 : "Value) "
500 : "VALUES(?,?,?,?);";
501 :
502 123 : sqlitePrepareStatement(m_reportDataInsertStmt, reportDataInsertSQL);
503 :
504 123 : constexpr std::string_view reportExtendedDataTableSQL = "CREATE TABLE ReportExtendedData ("
505 : "ReportExtendedDataIndex INTEGER PRIMARY KEY, "
506 : "ReportDataIndex INTEGER, "
507 : "MaxValue REAL, "
508 : "MaxMonth INTEGER, "
509 : "MaxDay INTEGER, "
510 : "MaxHour INTEGER, "
511 : "MaxStartMinute INTEGER, "
512 : "MaxMinute INTEGER, "
513 : "MinValue REAL, "
514 : "MinMonth INTEGER, "
515 : "MinDay INTEGER, "
516 : "MinHour INTEGER, "
517 : "MinStartMinute INTEGER, "
518 : "MinMinute INTEGER, "
519 : "FOREIGN KEY(ReportDataIndex) REFERENCES ReportData(ReportDataIndex) "
520 : "ON DELETE CASCADE ON UPDATE CASCADE "
521 : ");";
522 :
523 123 : sqliteExecuteCommand(reportExtendedDataTableSQL);
524 :
525 123 : constexpr std::string_view reportExtendedDataInsertSQL = "INSERT INTO ReportExtendedData ("
526 : "ReportExtendedDataIndex, "
527 : "ReportDataIndex, "
528 : "MaxValue, "
529 : "MaxMonth, "
530 : "MaxDay, "
531 : "MaxHour, "
532 : "MaxStartMinute, "
533 : "MaxMinute, "
534 : "MinValue, "
535 : "MinMonth, "
536 : "MinDay, "
537 : "MinHour, "
538 : "MinStartMinute, "
539 : "MinMinute) "
540 : "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
541 :
542 123 : sqlitePrepareStatement(m_reportExtendedDataInsertStmt, reportExtendedDataInsertSQL);
543 123 : }
544 :
545 123 : void SQLite::initializeTimeIndicesTable()
546 : {
547 123 : constexpr std::string_view timeTableSQL = "CREATE TABLE Time ("
548 : "TimeIndex INTEGER PRIMARY KEY, "
549 : "Year INTEGER, "
550 : "Month INTEGER, "
551 : "Day INTEGER, "
552 : "Hour INTEGER, "
553 : "Minute INTEGER, "
554 : "Dst INTEGER, "
555 : "Interval INTEGER, "
556 : "IntervalType INTEGER, "
557 : "SimulationDays INTEGER, "
558 : "DayType TEXT, "
559 : "EnvironmentPeriodIndex INTEGER, "
560 : "WarmupFlag INTEGER);";
561 :
562 123 : sqliteExecuteCommand(timeTableSQL);
563 :
564 123 : constexpr std::string_view timeIndexInsertSQL = "INSERT INTO Time ("
565 : "TimeIndex, "
566 : "Year, "
567 : "Month, "
568 : "Day, "
569 : "Hour, "
570 : "Minute, "
571 : "DST, "
572 : "Interval, "
573 : "IntervalType, "
574 : "SimulationDays, "
575 : "DayType, "
576 : "EnvironmentPeriodIndex, "
577 : "WarmupFlag) "
578 : "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?);";
579 :
580 123 : sqlitePrepareStatement(m_timeIndexInsertStmt, timeIndexInsertSQL);
581 123 : }
582 :
583 123 : void SQLite::initializeZoneInfoTable()
584 : {
585 123 : constexpr std::string_view zonesTableSQL = "CREATE TABLE Zones ("
586 : "ZoneIndex INTEGER PRIMARY KEY, "
587 : "ZoneName TEXT, "
588 : "RelNorth REAL, "
589 : "OriginX REAL, "
590 : "OriginY REAL, "
591 : "OriginZ REAL, "
592 : "CentroidX REAL, "
593 : "CentroidY REAL, "
594 : "CentroidZ REAL, "
595 : "OfType INTEGER, "
596 : "Multiplier REAL, "
597 : "ListMultiplier REAL, "
598 : "MinimumX REAL, "
599 : "MaximumX REAL, "
600 : "MinimumY REAL, "
601 : "MaximumY REAL, "
602 : "MinimumZ REAL, "
603 : "MaximumZ REAL, "
604 : "CeilingHeight REAL, "
605 : "Volume REAL, "
606 : "InsideConvectionAlgo INTEGER, "
607 : "OutsideConvectionAlgo INTEGER, "
608 : "FloorArea REAL, "
609 : "ExtGrossWallArea REAL, "
610 : "ExtNetWallArea REAL, "
611 : "ExtWindowArea REAL, "
612 : "IsPartOfTotalArea INTEGER);";
613 :
614 123 : sqliteExecuteCommand(zonesTableSQL);
615 :
616 123 : constexpr std::string_view zoneInfoInsertSQL = "INSERT INTO Zones ("
617 : "ZoneIndex, "
618 : "ZoneName, "
619 : "RelNorth, "
620 : "OriginX, "
621 : "OriginY, "
622 :
623 : "OriginZ, "
624 : "CentroidX, "
625 : "CentroidY, "
626 : "CentroidZ, "
627 : "OfType, "
628 :
629 : "Multiplier, "
630 : "ListMultiplier, "
631 : "MinimumX, "
632 : "MaximumX, "
633 : "MinimumY, "
634 :
635 : "MaximumY, "
636 : "MinimumZ, "
637 : "MaximumZ, "
638 : "CeilingHeight, "
639 : "Volume, "
640 :
641 : "InsideConvectionAlgo, "
642 : "OutsideConvectionAlgo, "
643 : "FloorArea, "
644 : "ExtGrossWallArea, "
645 : "ExtNetWallArea, "
646 :
647 : "ExtWindowArea, "
648 : "IsPartOfTotalArea) "
649 : "VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?);";
650 :
651 123 : sqlitePrepareStatement(m_zoneInfoInsertStmt, zoneInfoInsertSQL);
652 123 : }
653 :
654 123 : void SQLite::initializeZoneInfoZoneListTable()
655 : {
656 123 : constexpr std::string_view zoneInfoZoneListTableSQL = "CREATE TABLE ZoneInfoZoneLists ("
657 : "ZoneListIndex INTEGER NOT NULL, "
658 : "ZoneIndex INTEGER NOT NULL, "
659 : "PRIMARY KEY(ZoneListIndex, ZoneIndex), "
660 : "FOREIGN KEY(ZoneListIndex) REFERENCES ZoneLists(ZoneListIndex) "
661 : "ON DELETE CASCADE ON UPDATE CASCADE, "
662 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
663 : "ON DELETE CASCADE ON UPDATE CASCADE "
664 : ");";
665 :
666 123 : sqliteExecuteCommand(zoneInfoZoneListTableSQL);
667 :
668 123 : constexpr std::string_view zoneInfoZoneListInsertSQL = "INSERT INTO ZoneInfoZoneLists ("
669 : "ZoneListIndex, "
670 : "ZoneIndex) "
671 : "VALUES (?,?);";
672 :
673 123 : sqlitePrepareStatement(m_zoneInfoZoneListInsertStmt, zoneInfoZoneListInsertSQL);
674 123 : }
675 :
676 123 : void SQLite::initializeNominalPeopleTable()
677 : {
678 123 : constexpr std::string_view nominalPeopleTableSQL =
679 : "CREATE TABLE NominalPeople ( "
680 : "NominalPeopleIndex INTEGER PRIMARY KEY, ObjectName TEXT, ZoneIndex INTEGER,"
681 : "NumberOfPeople INTEGER, NumberOfPeopleScheduleIndex INTEGER, ActivityScheduleIndex INTEGER, FractionRadiant REAL, "
682 : "FractionConvected REAL, WorkEfficiencyScheduleIndex INTEGER, ClothingEfficiencyScheduleIndex INTEGER, "
683 : "AirVelocityScheduleIndex INTEGER, Fanger INTEGER, Pierce INTEGER, KSU INTEGER, "
684 : "MRTCalcType INTEGER, SurfaceIndex INTEGER, "
685 : "AngleFactorListName TEXT, AngleFactorList INTEGER, UserSpecifeidSensibleFraction REAL, Show55Warning INTEGER, "
686 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
687 : "ON DELETE CASCADE ON UPDATE CASCADE, "
688 : "FOREIGN KEY(NumberOfPeopleScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
689 : "ON UPDATE CASCADE, "
690 : "FOREIGN KEY(ActivityScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
691 : "ON UPDATE CASCADE, "
692 : "FOREIGN KEY(WorkEfficiencyScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
693 : "ON UPDATE CASCADE, "
694 : "FOREIGN KEY(ClothingEfficiencyScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
695 : "ON UPDATE CASCADE, "
696 : "FOREIGN KEY(AirVelocityScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
697 : "ON UPDATE CASCADE, "
698 : "FOREIGN KEY(SurfaceIndex) REFERENCES Surfaces(SurfaceIndex) "
699 : "ON UPDATE CASCADE "
700 : ");";
701 :
702 123 : sqliteExecuteCommand(nominalPeopleTableSQL);
703 :
704 123 : constexpr std::string_view nominalPeopleInsertSQL = "INSERT INTO NominalPeople VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
705 :
706 123 : sqlitePrepareStatement(m_nominalPeopleInsertStmt, nominalPeopleInsertSQL);
707 123 : }
708 :
709 123 : void SQLite::initializeNominalLightingTable()
710 : {
711 123 : constexpr std::string_view nominalLightingTableSQL =
712 : "CREATE TABLE NominalLighting ( "
713 : "NominalLightingIndex INTEGER PRIMARY KEY, ObjectName TEXT, "
714 : "ZoneIndex INTEGER, ScheduleIndex INTEGER, DesignLevel REAL, FractionReturnAir REAL, FractionRadiant REAL, "
715 : "FractionShortWave REAL, FractionReplaceable REAL, FractionConvected REAL, EndUseSubcategory TEXT, "
716 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
717 : "ON DELETE CASCADE ON UPDATE CASCADE, "
718 : "FOREIGN KEY(ScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
719 : "ON UPDATE CASCADE "
720 : ");";
721 :
722 123 : sqliteExecuteCommand(nominalLightingTableSQL);
723 :
724 123 : constexpr std::string_view nominalLightingInsertSQL = "INSERT INTO NominalLighting VALUES(?,?,?,?,?,?,?,?,?,?,?);";
725 :
726 123 : sqlitePrepareStatement(m_nominalLightingInsertStmt, nominalLightingInsertSQL);
727 123 : }
728 :
729 123 : void SQLite::initializeNominalElectricEquipmentTable()
730 : {
731 123 : constexpr std::string_view nominalElectricEquipmentTableSQL = "CREATE TABLE NominalElectricEquipment ("
732 : "NominalElectricEquipmentIndex INTEGER PRIMARY KEY, "
733 : "ObjectName TEXT, "
734 : "ZoneIndex INTEGER, ScheduleIndex INTEGER, DesignLevel REAL, "
735 : "FractionLatent REAL, FractionRadiant REAL, FractionLost REAL, "
736 : "FractionConvected REAL, EndUseSubcategory TEXT, "
737 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
738 : "ON DELETE CASCADE ON UPDATE CASCADE, "
739 : "FOREIGN KEY(ScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
740 : "ON UPDATE CASCADE "
741 : ");";
742 :
743 123 : sqliteExecuteCommand(nominalElectricEquipmentTableSQL);
744 :
745 123 : constexpr std::string_view nominalElectricEquipmentInsertSQL = "INSERT INTO NominalElectricEquipment VALUES(?,?,?,?,?,?,?,?,?,?);";
746 :
747 123 : sqlitePrepareStatement(m_nominalElectricEquipmentInsertStmt, nominalElectricEquipmentInsertSQL);
748 123 : }
749 :
750 123 : void SQLite::initializeNominalGasEquipmentTable()
751 : {
752 123 : constexpr std::string_view nominalGasEquipmentTableSQL = "CREATE TABLE NominalGasEquipment( "
753 : "NominalGasEquipmentIndex INTEGER PRIMARY KEY, ObjectName TEXT, "
754 : "ZoneIndex INTEGER, ScheduleIndex INTEGER, "
755 : "DesignLevel REAL, FractionLatent REAL, FractionRadiant REAL, FractionLost REAL, "
756 : "FractionConvected REAL, EndUseSubcategory TEXT, "
757 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
758 : "ON DELETE CASCADE ON UPDATE CASCADE, "
759 : "FOREIGN KEY(ScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
760 : "ON UPDATE CASCADE "
761 : ");";
762 :
763 123 : sqliteExecuteCommand(nominalGasEquipmentTableSQL);
764 :
765 123 : constexpr std::string_view nominalGasEquipmentInsertSQL = "INSERT INTO NominalGasEquipment VALUES(?,?,?,?,?,?,?,?,?,?);";
766 :
767 123 : sqlitePrepareStatement(m_nominalGasEquipmentInsertStmt, nominalGasEquipmentInsertSQL);
768 123 : }
769 :
770 123 : void SQLite::initializeNominalSteamEquipmentTable()
771 : {
772 123 : constexpr std::string_view nominalSteamEquipmentTableSQL = "CREATE TABLE NominalSteamEquipment( "
773 : "NominalSteamEquipmentIndex INTEGER PRIMARY KEY, ObjectName TEXT, "
774 : "ZoneIndex INTEGER, ScheduleIndex INTEGER, DesignLevel REAL, "
775 : "FractionLatent REAL, FractionRadiant REAL, FractionLost REAL, "
776 : "FractionConvected REAL, EndUseSubcategory TEXT, "
777 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
778 : "ON DELETE CASCADE ON UPDATE CASCADE, "
779 : "FOREIGN KEY(ScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
780 : "ON UPDATE CASCADE "
781 : ");";
782 :
783 123 : sqliteExecuteCommand(nominalSteamEquipmentTableSQL);
784 :
785 123 : constexpr std::string_view nominalSteamEquipmentInsertSQL = "INSERT INTO NominalSteamEquipment VALUES(?,?,?,?,?,?,?,?,?,?);";
786 :
787 123 : sqlitePrepareStatement(m_nominalSteamEquipmentInsertStmt, nominalSteamEquipmentInsertSQL);
788 123 : }
789 :
790 123 : void SQLite::initializeNominalHotWaterEquipmentTable()
791 : {
792 123 : constexpr std::string_view nominalHotWaterEquipmentTableSQL =
793 : "CREATE TABLE NominalHotWaterEquipment("
794 : "NominalHotWaterEquipmentIndex INTEGER PRIMARY KEY, "
795 : "ObjectName TEXT, "
796 : "ZoneIndex INTEGER, SchedNo INTEGER, DesignLevel REAL, FractionLatent REAL, FractionRadiant REAL, FractionLost REAL, "
797 : "FractionConvected REAL, EndUseSubcategory TEXT, "
798 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
799 : "ON DELETE CASCADE ON UPDATE CASCADE, "
800 : "FOREIGN KEY(SchedNo) REFERENCES Schedules(ScheduleIndex) "
801 : "ON UPDATE CASCADE "
802 : ");";
803 :
804 123 : sqliteExecuteCommand(nominalHotWaterEquipmentTableSQL);
805 :
806 123 : constexpr std::string_view nominalHotWaterEquipmentInsertSQL = "INSERT INTO NominalHotWaterEquipment VALUES(?,?,?,?,?,?,?,?,?,?);";
807 :
808 123 : sqlitePrepareStatement(m_nominalHotWaterEquipmentInsertStmt, nominalHotWaterEquipmentInsertSQL);
809 123 : }
810 :
811 123 : void SQLite::initializeNominalOtherEquipmentTable()
812 : {
813 123 : constexpr std::string_view nominalOtherEquipmentTableSQL = "CREATE TABLE NominalOtherEquipment( "
814 : "NominalOtherEquipmentIndex INTEGER PRIMARY KEY, ObjectName TEXT, "
815 : "ZoneIndex INTEGER, ScheduleIndex INTEGER, DesignLevel REAL, FractionLatent REAL, "
816 : "FractionRadiant REAL, FractionLost REAL, "
817 : "FractionConvected REAL, EndUseSubcategory TEXT, "
818 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
819 : "ON DELETE CASCADE ON UPDATE CASCADE, "
820 : "FOREIGN KEY(ScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
821 : "ON UPDATE CASCADE "
822 : ");";
823 :
824 123 : sqliteExecuteCommand(nominalOtherEquipmentTableSQL);
825 :
826 123 : constexpr std::string_view nominalOtherEquipmentInsertSQL = "INSERT INTO NominalOtherEquipment VALUES(?,?,?,?,?,?,?,?,?,?);";
827 :
828 123 : sqlitePrepareStatement(m_nominalOtherEquipmentInsertStmt, nominalOtherEquipmentInsertSQL);
829 123 : }
830 :
831 123 : void SQLite::initializeNominalBaseboardHeatTable()
832 : {
833 123 : constexpr std::string_view nominalBaseboardHeatersTableSQL =
834 : "CREATE TABLE NominalBaseboardHeaters ( "
835 : "NominalBaseboardHeaterIndex INTEGER PRIMARY KEY, ObjectName TEXT, "
836 : "ZoneIndex INTEGER, ScheduleIndex INTEGER, CapatLowTemperature REAL, LowTemperature REAL, CapatHighTemperature REAL, "
837 : "HighTemperature REAL, FractionRadiant REAL, FractionConvected REAL, EndUseSubcategory TEXT, "
838 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
839 : "ON DELETE CASCADE ON UPDATE CASCADE, "
840 : "FOREIGN KEY(ScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
841 : "ON UPDATE CASCADE "
842 : ");";
843 :
844 123 : sqliteExecuteCommand(nominalBaseboardHeatersTableSQL);
845 :
846 123 : constexpr std::string_view nominalBaseboardHeatInsertSQL = "INSERT INTO NominalBaseboardHeaters VALUES(?,?,?,?,?,?,?,?,?,?,?);";
847 :
848 123 : sqlitePrepareStatement(m_nominalBaseboardHeatInsertStmt, nominalBaseboardHeatInsertSQL);
849 123 : }
850 :
851 123 : void SQLite::initializeSurfacesTable()
852 : {
853 123 : constexpr std::string_view surfacesTableSQL = "CREATE TABLE Surfaces ( "
854 : "SurfaceIndex INTEGER PRIMARY KEY, SurfaceName TEXT, ConstructionIndex INTEGER, "
855 : "ClassName TEXT, Area REAL, GrossArea REAL, Perimeter REAL, "
856 : "Azimuth REAL, Height REAL, Reveal REAL, "
857 : "Shape INTEGER, Sides INTEGER, Tilt REAL, Width REAL, HeatTransferSurf INTEGER, "
858 : "BaseSurfaceIndex INTEGER, ZoneIndex INTEGER, ExtBoundCond INTEGER, "
859 : "ExtSolar INTEGER, ExtWind INTEGER, "
860 : "FOREIGN KEY(ConstructionIndex) REFERENCES Constructions(ConstructionIndex) "
861 : "ON UPDATE CASCADE, "
862 : "FOREIGN KEY(BaseSurfaceIndex) REFERENCES Surfaces(SurfaceIndex) "
863 : "ON UPDATE CASCADE, "
864 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
865 : "ON DELETE CASCADE ON UPDATE CASCADE "
866 : ");";
867 :
868 123 : sqliteExecuteCommand(surfacesTableSQL);
869 :
870 123 : constexpr std::string_view surfaceInsertSQL = "INSERT INTO Surfaces VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
871 :
872 123 : sqlitePrepareStatement(m_surfaceInsertStmt, surfaceInsertSQL);
873 123 : }
874 :
875 123 : void SQLite::initializeConstructionsTables()
876 : {
877 123 : constexpr std::string_view constructionsTableSQL =
878 : "CREATE TABLE Constructions ( "
879 : "ConstructionIndex INTEGER PRIMARY KEY, Name TEXT, TotalLayers INTEGER, "
880 : "TotalSolidLayers INTEGER, TotalGlassLayers INTEGER, InsideAbsorpVis REAL, OutsideAbsorpVis REAL, "
881 : "InsideAbsorpSolar REAL, OutsideAbsorpSolar REAL, InsideAbsorpThermal REAL, OutsideAbsorpThermal REAL, "
882 : "OutsideRoughness INTEGER, TypeIsWindow INTEGER, Uvalue REAL"
883 : ");";
884 :
885 123 : sqliteExecuteCommand(constructionsTableSQL);
886 :
887 123 : constexpr std::string_view constructionInsertSQL = "INSERT INTO Constructions VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
888 :
889 123 : sqlitePrepareStatement(m_constructionInsertStmt, constructionInsertSQL);
890 :
891 123 : constexpr std::string_view constructionLayersTableSQL = "CREATE TABLE ConstructionLayers ( "
892 : "ConstructionLayersIndex INTEGER PRIMARY KEY, "
893 : "ConstructionIndex INTEGER, LayerIndex INTEGER, MaterialIndex INTEGER, "
894 : "FOREIGN KEY(ConstructionIndex) REFERENCES Constructions(ConstructionIndex) "
895 : "ON DELETE CASCADE ON UPDATE CASCADE, "
896 : "FOREIGN KEY(MaterialIndex) REFERENCES Materials(MaterialIndex) "
897 : "ON UPDATE CASCADE "
898 : ");";
899 :
900 123 : sqliteExecuteCommand(constructionLayersTableSQL);
901 :
902 123 : constexpr std::string_view constructionLayerInsertSQL =
903 : "INSERT INTO ConstructionLayers(ConstructionIndex, LayerIndex, MaterialIndex) VALUES(?,?,?);";
904 :
905 123 : sqlitePrepareStatement(m_constructionLayerInsertStmt, constructionLayerInsertSQL);
906 123 : }
907 :
908 123 : void SQLite::initializeMaterialsTable()
909 : {
910 123 : constexpr std::string_view materialsTableSQL = "CREATE TABLE Materials ( "
911 : "MaterialIndex INTEGER PRIMARY KEY, "
912 : "Name TEXT, MaterialType INTEGER, Roughness INTEGER, "
913 : "Conductivity REAL, Density REAL, IsoMoistCap REAL, Porosity REAL, Resistance REAL, "
914 : "ROnly INTEGER, SpecHeat REAL, ThermGradCoef REAL, Thickness REAL, VaporDiffus REAL "
915 : ");";
916 :
917 123 : sqliteExecuteCommand(materialsTableSQL);
918 :
919 123 : constexpr std::string_view materialInsertSQL = "INSERT INTO Materials VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
920 :
921 123 : sqlitePrepareStatement(m_materialInsertStmt, materialInsertSQL);
922 123 : }
923 :
924 123 : void SQLite::initializeZoneListTable()
925 : {
926 123 : constexpr std::string_view zoneListsTableSQL = "CREATE TABLE ZoneLists ( "
927 : "ZoneListIndex INTEGER PRIMARY KEY, Name TEXT);";
928 :
929 123 : sqliteExecuteCommand(zoneListsTableSQL);
930 :
931 123 : constexpr std::string_view zoneListInsertSQL = "INSERT INTO ZoneLists VALUES(?,?);";
932 :
933 123 : sqlitePrepareStatement(m_zoneListInsertStmt, zoneListInsertSQL);
934 123 : }
935 :
936 123 : void SQLite::initializeZoneGroupTable()
937 : {
938 123 : constexpr std::string_view zoneGroupsTableSQL = "CREATE TABLE ZoneGroups ( "
939 : "ZoneGroupIndex INTEGER PRIMARY KEY, "
940 : "ZoneGroupName TEXT, "
941 : "ZoneListIndex INTEGER, "
942 : "ZoneListMultiplier INTEGER, "
943 : "FOREIGN KEY(ZoneListIndex) REFERENCES ZoneLists(ZoneListIndex) "
944 : "ON UPDATE CASCADE "
945 : ");";
946 :
947 123 : sqliteExecuteCommand(zoneGroupsTableSQL);
948 :
949 123 : constexpr std::string_view zoneGroupInsertSQL = "INSERT INTO ZoneGroups VALUES(?,?,?,?);";
950 :
951 123 : sqlitePrepareStatement(m_zoneGroupInsertStmt, zoneGroupInsertSQL);
952 123 : }
953 :
954 123 : void SQLite::initializeNominalInfiltrationTable()
955 : {
956 123 : constexpr std::string_view nominalInfiltrationTableSQL = "CREATE TABLE NominalInfiltration ( "
957 : "NominalInfiltrationIndex INTEGER PRIMARY KEY, "
958 : "ObjectName TEXT, "
959 : "ZoneIndex INTEGER, ScheduleIndex INTEGER, DesignLevel REAL, "
960 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
961 : "ON DELETE CASCADE ON UPDATE CASCADE, "
962 : "FOREIGN KEY(ScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
963 : "ON UPDATE CASCADE "
964 : ");";
965 :
966 123 : sqliteExecuteCommand(nominalInfiltrationTableSQL);
967 :
968 123 : constexpr std::string_view infiltrationInsertSQL =
969 : "INSERT INTO NominalInfiltration (NominalInfiltrationIndex, ObjectName, ZoneIndex, ScheduleIndex, DesignLevel)"
970 : "VALUES (?,?,?,?,?);";
971 :
972 123 : sqlitePrepareStatement(m_infiltrationInsertStmt, infiltrationInsertSQL);
973 123 : }
974 :
975 123 : void SQLite::initializeNominalVentilationTable()
976 : {
977 123 : constexpr std::string_view nominalVentilationTableSQL = "CREATE TABLE NominalVentilation ( "
978 : "NominalVentilationIndex INTEGER PRIMARY KEY, "
979 : "ObjectName TEXT, "
980 : "ZoneIndex INTEGER, ScheduleIndex INTEGER, DesignLevel REAL, "
981 : "FOREIGN KEY(ZoneIndex) REFERENCES Zones(ZoneIndex) "
982 : "ON DELETE CASCADE ON UPDATE CASCADE, "
983 : "FOREIGN KEY(ScheduleIndex) REFERENCES Schedules(ScheduleIndex) "
984 : "ON UPDATE CASCADE "
985 : ");";
986 :
987 123 : sqliteExecuteCommand(nominalVentilationTableSQL);
988 :
989 123 : constexpr std::string_view ventilationInsertSQL = "INSERT INTO NominalVentilation VALUES(?,?,?,?,?);";
990 :
991 123 : sqlitePrepareStatement(m_ventilationInsertStmt, ventilationInsertSQL);
992 123 : }
993 :
994 123 : void SQLite::initializeZoneSizingTable()
995 : {
996 123 : constexpr std::string_view zoneSizesTableSQL =
997 : "CREATE TABLE ZoneSizes ( "
998 : "ZoneSizesIndex INTEGER PRIMARY KEY, ZoneName TEXT, LoadType TEXT, "
999 : "CalcDesLoad REAL, UserDesLoad REAL, CalcDesFlow REAL, UserDesFlow REAL, DesDayName TEXT, PeakHrMin TEXT, "
1000 : "PeakTemp REAL, PeakHumRat REAL, CalcOutsideAirFlow REAL, DOASHeatAddRate REAL"
1001 : ");";
1002 :
1003 123 : sqliteExecuteCommand(zoneSizesTableSQL);
1004 :
1005 123 : constexpr std::string_view zoneSizingInsertSQL = "INSERT INTO ZoneSizes VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?);";
1006 :
1007 123 : sqlitePrepareStatement(m_zoneSizingInsertStmt, zoneSizingInsertSQL);
1008 123 : }
1009 :
1010 123 : void SQLite::initializeSystemSizingTable()
1011 : {
1012 123 : constexpr std::string_view systemSizesTableSQL =
1013 : "CREATE TABLE SystemSizes (SystemSizesIndex INTEGER PRIMARY KEY, SystemName TEXT, LoadType TEXT, PeakLoadType TEXT, "
1014 : "UserDesCap REAL, CalcDesVolFlow REAL, UserDesVolFlow REAL, DesDayName TEXT, PeakHrMin TEXT);";
1015 :
1016 123 : sqliteExecuteCommand(systemSizesTableSQL);
1017 :
1018 123 : constexpr std::string_view systemSizingInsertSQL = "INSERT INTO SystemSizes VALUES(?,?,?,?,?,?,?,?,?);";
1019 :
1020 123 : sqlitePrepareStatement(m_systemSizingInsertStmt, systemSizingInsertSQL);
1021 123 : }
1022 :
1023 123 : void SQLite::initializeComponentSizingTable()
1024 : {
1025 123 : constexpr std::string_view componentSizesTableSQL = "CREATE TABLE ComponentSizes (ComponentSizesIndex INTEGER PRIMARY KEY, "
1026 : "CompType TEXT, CompName TEXT, Description TEXT, Value REAL, Units TEXT);";
1027 :
1028 123 : sqliteExecuteCommand(componentSizesTableSQL);
1029 :
1030 123 : constexpr std::string_view componentSizingInsertSQL = "INSERT INTO ComponentSizes VALUES (?,?,?,?,?,?);";
1031 :
1032 123 : sqlitePrepareStatement(m_componentSizingInsertStmt, componentSizingInsertSQL);
1033 123 : }
1034 :
1035 123 : void SQLite::initializeRoomAirModelTable()
1036 : {
1037 123 : constexpr std::string_view roomAirModelsTableSQL =
1038 : "CREATE TABLE RoomAirModels (ZoneIndex INTEGER PRIMARY KEY, AirModelName TEXT, AirModelType INTEGER, "
1039 : "TempCoupleScheme INTEGER, SimAirModel INTEGER);";
1040 :
1041 123 : sqliteExecuteCommand(roomAirModelsTableSQL);
1042 :
1043 123 : constexpr std::string_view roomAirModelInsertSQL = "INSERT INTO RoomAirModels VALUES(?,?,?,?,?);";
1044 :
1045 123 : sqlitePrepareStatement(m_roomAirModelInsertStmt, roomAirModelInsertSQL);
1046 123 : }
1047 :
1048 123 : void SQLite::initializeSchedulesTable()
1049 : {
1050 123 : constexpr std::string_view scheduleTableSQL = "CREATE TABLE Schedules (ScheduleIndex INTEGER PRIMARY KEY, ScheduleName TEXT, "
1051 : "ScheduleType TEXT, ScheduleMinimum REAL, ScheduleMaximum REAL);";
1052 :
1053 123 : sqliteExecuteCommand(scheduleTableSQL);
1054 :
1055 123 : constexpr std::string_view scheduleInsertSQL = "INSERT INTO Schedules VALUES(?,?,?,?,?);";
1056 :
1057 123 : sqlitePrepareStatement(m_scheduleInsertStmt, scheduleInsertSQL);
1058 123 : }
1059 :
1060 123 : void SQLite::initializeDaylightMapTables()
1061 : {
1062 123 : constexpr std::string_view daylightMapsTableSQL = "CREATE TABLE DaylightMaps ( "
1063 : "MapNumber INTEGER PRIMARY KEY, MapName TEXT, "
1064 : "Environment TEXT, Zone INTEGER, ReferencePts TEXT, Z REAL, "
1065 : "FOREIGN KEY(Zone) REFERENCES Zones(ZoneIndex) "
1066 : "ON DELETE CASCADE ON UPDATE CASCADE "
1067 : ");";
1068 :
1069 123 : sqliteExecuteCommand(daylightMapsTableSQL);
1070 :
1071 123 : constexpr std::string_view daylightMapTitleInsertSQL = "INSERT INTO DaylightMaps VALUES(?,?,?,?,?,?);";
1072 :
1073 123 : sqlitePrepareStatement(m_daylightMapTitleInsertStmt, daylightMapTitleInsertSQL);
1074 :
1075 123 : constexpr std::string_view daylightMapHourlyReportsTableSQL = "CREATE TABLE DaylightMapHourlyReports ( "
1076 : "HourlyReportIndex INTEGER PRIMARY KEY, "
1077 : "MapNumber INTEGER, Year INTEGER, Month INTEGER, DayOfMonth INTEGER, Hour INTEGER, "
1078 : "FOREIGN KEY(MapNumber) REFERENCES DaylightMaps(MapNumber) "
1079 : "ON DELETE CASCADE ON UPDATE CASCADE "
1080 : ");";
1081 :
1082 123 : sqliteExecuteCommand(daylightMapHourlyReportsTableSQL);
1083 :
1084 123 : constexpr std::string_view daylightMapHourlyTitleInsertSQL = "INSERT INTO DaylightMapHourlyReports VALUES(?,?,?,?,?,?);";
1085 :
1086 123 : sqlitePrepareStatement(m_daylightMapHourlyTitleInsertStmt, daylightMapHourlyTitleInsertSQL);
1087 :
1088 123 : constexpr std::string_view daylightMapHourlyDataTableSQL =
1089 : "CREATE TABLE DaylightMapHourlyData ( "
1090 : "HourlyDataIndex INTEGER PRIMARY KEY, HourlyReportIndex INTEGER, "
1091 : "X REAL, Y REAL, Illuminance REAL, "
1092 : "FOREIGN KEY(HourlyReportIndex) REFERENCES DaylightMapHourlyReports(HourlyReportIndex) "
1093 : "ON DELETE CASCADE ON UPDATE CASCADE "
1094 : ");";
1095 :
1096 123 : sqliteExecuteCommand(daylightMapHourlyDataTableSQL);
1097 :
1098 123 : constexpr std::string_view daylightMapHourlyDataInsertSQL = "INSERT INTO DaylightMapHourlyData VALUES(?,?,?,?,?);";
1099 :
1100 123 : sqlitePrepareStatement(m_daylightMapHourlyDataInsertStmt, daylightMapHourlyDataInsertSQL);
1101 123 : }
1102 :
1103 123 : void SQLite::initializeViews()
1104 : {
1105 123 : constexpr std::string_view reportVariableWithTimeViewSQL =
1106 : "CREATE VIEW ReportVariableWithTime AS "
1107 : "SELECT rd.ReportDataIndex, rd.TimeIndex, rd.ReportDataDictionaryIndex, red.ReportExtendedDataIndex, rd.Value, "
1108 : "t.Month, t.Day, t.Hour, t.Minute, t.Dst, t.Interval, t.IntervalType, t.SimulationDays, t.DayType, t.EnvironmentPeriodIndex, t.WarmupFlag, "
1109 : "rdd.IsMeter, rdd.Type, rdd.IndexGroup, rdd.TimestepType, rdd.KeyValue, rdd.Name, rdd.ReportingFrequency, rdd.ScheduleName, rdd.Units, "
1110 : "red.MaxValue, red.MaxMonth, red.MaxDay, red.MaxStartMinute, red.MaxMinute, red.MinValue, red.MinMonth, red.MinDay, red.MinStartMinute, "
1111 : "red.MinMinute "
1112 : "FROM ReportData As rd "
1113 : "INNER JOIN ReportDataDictionary As rdd "
1114 : "ON rd.ReportDataDictionaryIndex = rdd.ReportDataDictionaryIndex "
1115 : "LEFT OUTER JOIN ReportExtendedData As red "
1116 : "ON rd.ReportDataIndex = red.ReportDataIndex "
1117 : "INNER JOIN Time As t "
1118 : "ON rd.TimeIndex = t.TimeIndex;";
1119 :
1120 123 : sqliteExecuteCommand(reportVariableWithTimeViewSQL);
1121 :
1122 123 : constexpr std::string_view reportVariableDataViewSQL =
1123 : "CREATE VIEW ReportVariableData AS "
1124 : "SELECT rd.ReportDataIndex As rowid, rd.TimeIndex, rd.ReportDataDictionaryIndex As ReportVariableDataDictionaryIndex, "
1125 : "rd.Value As VariableValue, red.ReportExtendedDataIndex As ReportVariableExtendedDataIndex "
1126 : "FROM ReportData As rd "
1127 : "LEFT OUTER JOIN ReportExtendedData As red "
1128 : "ON rd.ReportDataIndex = red.ReportDataIndex;";
1129 :
1130 123 : sqliteExecuteCommand(reportVariableDataViewSQL);
1131 :
1132 123 : constexpr std::string_view reportVariableDataDictionaryViewSQL =
1133 : "CREATE VIEW ReportVariableDataDictionary AS "
1134 : "SELECT rdd.ReportDataDictionaryIndex As ReportVariableDataDictionaryIndex, rdd.Type As VariableType, rdd.IndexGroup, rdd.TimestepType, "
1135 : "rdd.KeyValue, rdd.Name As VariableName, rdd.ReportingFrequency, rdd.ScheduleName, rdd.Units As VariableUnits "
1136 : "FROM ReportDataDictionary As rdd;";
1137 :
1138 123 : sqliteExecuteCommand(reportVariableDataDictionaryViewSQL);
1139 :
1140 123 : constexpr std::string_view reportVariableExtendedDataViewSQL =
1141 : "CREATE VIEW ReportVariableExtendedData AS "
1142 : "SELECT red.ReportExtendedDataIndex As ReportVariableExtendedDataIndex, red.MaxValue, red.MaxMonth, red.MaxDay, "
1143 : "red.MaxStartMinute, red.MaxMinute, red.MinValue, red.MinMonth, red.MinDay, red.MinStartMinute, red.MinMinute "
1144 : "FROM ReportExtendedData As red;";
1145 :
1146 123 : sqliteExecuteCommand(reportVariableExtendedDataViewSQL);
1147 :
1148 123 : constexpr std::string_view reportMeterDataViewSQL =
1149 : "CREATE VIEW ReportMeterData AS "
1150 : "SELECT rd.ReportDataIndex As rowid, rd.TimeIndex, rd.ReportDataDictionaryIndex As ReportMeterDataDictionaryIndex, "
1151 : "rd.Value As VariableValue, red.ReportExtendedDataIndex As ReportVariableExtendedDataIndex "
1152 : "FROM ReportData As rd "
1153 : "LEFT OUTER JOIN ReportExtendedData As red "
1154 : "ON rd.ReportDataIndex = red.ReportDataIndex "
1155 : "INNER JOIN ReportDataDictionary As rdd "
1156 : "ON rd.ReportDataDictionaryIndex = rdd.ReportDataDictionaryIndex "
1157 : "WHERE rdd.IsMeter = 1;";
1158 :
1159 123 : sqliteExecuteCommand(reportMeterDataViewSQL);
1160 :
1161 123 : constexpr std::string_view reportMeterDataDictionaryViewSQL =
1162 : "CREATE VIEW ReportMeterDataDictionary AS "
1163 : "SELECT rdd.ReportDataDictionaryIndex As ReportMeterDataDictionaryIndex, rdd.Type As VariableType, rdd.IndexGroup, rdd.TimestepType, "
1164 : "rdd.KeyValue, rdd.Name As VariableName, rdd.ReportingFrequency, rdd.ScheduleName, rdd.Units As VariableUnits "
1165 : "FROM ReportDataDictionary As rdd "
1166 : "WHERE rdd.IsMeter = 1;";
1167 :
1168 123 : sqliteExecuteCommand(reportMeterDataDictionaryViewSQL);
1169 :
1170 123 : constexpr std::string_view reportMeterExtendedDataViewSQL =
1171 : "CREATE VIEW ReportMeterExtendedData AS "
1172 : "SELECT red.ReportExtendedDataIndex As ReportMeterExtendedDataIndex, red.MaxValue, red.MaxMonth, red.MaxDay, "
1173 : "red.MaxStartMinute, red.MaxMinute, red.MinValue, red.MinMonth, red.MinDay, red.MinStartMinute, red.MinMinute "
1174 : "FROM ReportExtendedData As red "
1175 : "LEFT OUTER JOIN ReportData As rd "
1176 : "ON rd.ReportDataIndex = red.ReportDataIndex "
1177 : "INNER JOIN ReportDataDictionary As rdd "
1178 : "ON rd.ReportDataDictionaryIndex = rdd.ReportDataDictionaryIndex "
1179 : "WHERE rdd.IsMeter = 1;";
1180 :
1181 123 : sqliteExecuteCommand(reportMeterExtendedDataViewSQL);
1182 123 : }
1183 :
1184 123 : void SQLite::initializeSimulationsTable()
1185 : {
1186 123 : constexpr std::string_view simulationsTableSQL = "CREATE TABLE Simulations (SimulationIndex INTEGER PRIMARY KEY, "
1187 : "EnergyPlusVersion TEXT, TimeStamp TEXT, NumTimestepsPerHour INTEGER, Completed BOOL, "
1188 : "CompletedSuccessfully BOOL);";
1189 :
1190 123 : sqliteExecuteCommand(simulationsTableSQL);
1191 :
1192 123 : constexpr std::string_view simulationsInsertSQL =
1193 : "INSERT INTO Simulations(SimulationIndex, EnergyPlusVersion, TimeStamp, Completed, CompletedSuccessfully) "
1194 : "VALUES(?,?,?,'FALSE','FALSE');";
1195 :
1196 123 : sqlitePrepareStatement(m_simulationsInsertStmt, simulationsInsertSQL);
1197 :
1198 123 : constexpr std::string_view simulationUpdateSQL = "UPDATE Simulations SET "
1199 : "Completed = ?, CompletedSuccessfully = ? "
1200 : "WHERE SimulationIndex = ?";
1201 :
1202 123 : sqlitePrepareStatement(m_simulationUpdateStmt, simulationUpdateSQL);
1203 :
1204 123 : constexpr std::string_view simulationDataUpdateSQL = "UPDATE Simulations SET "
1205 : "NumTimestepsPerHour = ? "
1206 : "WHERE SimulationIndex = ?";
1207 :
1208 123 : sqlitePrepareStatement(m_simulationDataUpdateStmt, simulationDataUpdateSQL);
1209 123 : }
1210 :
1211 123 : void SQLite::initializeErrorsTable()
1212 : {
1213 123 : constexpr std::string_view errorsTableSQL = "CREATE TABLE Errors ( "
1214 : "ErrorIndex INTEGER PRIMARY KEY, SimulationIndex INTEGER, "
1215 : "ErrorType INTEGER, ErrorMessage TEXT, Count INTEGER, "
1216 : "FOREIGN KEY(SimulationIndex) REFERENCES Simulations(SimulationIndex) "
1217 : "ON DELETE CASCADE ON UPDATE CASCADE "
1218 : ");";
1219 :
1220 123 : sqliteExecuteCommand(errorsTableSQL);
1221 :
1222 123 : constexpr std::string_view errorInsertSQL = "INSERT INTO Errors VALUES(?,?,?,?,?);";
1223 :
1224 123 : sqlitePrepareStatement(m_errorInsertStmt, errorInsertSQL);
1225 :
1226 123 : constexpr std::string_view errorUpdateSQL =
1227 : "UPDATE Errors SET "
1228 : "ErrorMessage = ErrorMessage || ? WHERE ErrorIndex = (SELECT ErrorIndex FROM Errors ORDER BY ErrorIndex DESC LIMIT 1)";
1229 :
1230 123 : sqlitePrepareStatement(m_errorUpdateStmt, errorUpdateSQL);
1231 123 : }
1232 :
1233 123 : void SQLite::initializeEnvironmentPeriodsTable()
1234 : {
1235 123 : constexpr std::string_view environmentPeriodsTableSQL = "CREATE TABLE EnvironmentPeriods ( "
1236 : "EnvironmentPeriodIndex INTEGER PRIMARY KEY, "
1237 : "SimulationIndex INTEGER, EnvironmentName TEXT, EnvironmentType INTEGER, "
1238 : "FOREIGN KEY(SimulationIndex) REFERENCES Simulations(SimulationIndex) "
1239 : "ON DELETE CASCADE ON UPDATE CASCADE "
1240 : ");";
1241 :
1242 123 : sqliteExecuteCommand(environmentPeriodsTableSQL);
1243 :
1244 123 : constexpr std::string_view environmentPeriodInsertSQL = "INSERT INTO EnvironmentPeriods VALUES(?,?,?,?);";
1245 :
1246 123 : sqlitePrepareStatement(m_environmentPeriodInsertStmt, environmentPeriodInsertSQL);
1247 123 : }
1248 :
1249 23 : void SQLite::initializeTabularDataTable()
1250 : {
1251 23 : constexpr std::string_view sql = "CREATE TABLE StringTypes ( "
1252 : "StringTypeIndex INTEGER PRIMARY KEY, "
1253 : "Value TEXT"
1254 : ");";
1255 :
1256 23 : sqliteExecuteCommand(sql);
1257 :
1258 23 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'ReportName');", ReportNameId));
1259 23 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'ReportForString');", ReportForStringId));
1260 23 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'TableName');", TableNameId));
1261 23 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'RowName');", RowNameId));
1262 23 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'ColumnName');", ColumnNameId));
1263 23 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'Units');", UnitsId));
1264 :
1265 23 : constexpr std::string_view sql2 = "CREATE TABLE Strings ( "
1266 : "StringIndex INTEGER PRIMARY KEY, "
1267 : "StringTypeIndex INTEGER, "
1268 : "Value TEXT, "
1269 : "UNIQUE(StringTypeIndex, Value), "
1270 : "FOREIGN KEY(StringTypeIndex) REFERENCES StringTypes(StringTypeIndex) "
1271 : "ON UPDATE CASCADE "
1272 : ");";
1273 :
1274 23 : sqliteExecuteCommand(sql2);
1275 :
1276 23 : constexpr std::string_view sql3 = "INSERT INTO Strings (StringIndex,StringTypeIndex,Value) VALUES(?,?,?);";
1277 :
1278 23 : sqlitePrepareStatement(m_stringsInsertStmt, sql3);
1279 :
1280 23 : constexpr std::string_view sql4 = "SELECT StringIndex FROM Strings WHERE StringTypeIndex=? AND Value=?;";
1281 :
1282 23 : sqlitePrepareStatement(m_stringsLookUpStmt, sql4);
1283 :
1284 23 : constexpr std::string_view sql5 = "CREATE TABLE TabularData ( "
1285 : "TabularDataIndex INTEGER PRIMARY KEY, "
1286 : "ReportNameIndex INTEGER, "
1287 : "ReportForStringIndex INTEGER, "
1288 : "TableNameIndex INTEGER, "
1289 : "RowNameIndex INTEGER, "
1290 : "ColumnNameIndex INTEGER, "
1291 : "UnitsIndex INTEGER, "
1292 : "SimulationIndex INTEGER, "
1293 : "RowId INTEGER, "
1294 : "ColumnId INTEGER, "
1295 : "Value TEXT, "
1296 : "FOREIGN KEY(ReportNameIndex) REFERENCES Strings(StringIndex) "
1297 : "ON UPDATE CASCADE "
1298 : "FOREIGN KEY(ReportForStringIndex) REFERENCES Strings(StringIndex) "
1299 : "ON UPDATE CASCADE "
1300 : "FOREIGN KEY(TableNameIndex) REFERENCES Strings(StringIndex) "
1301 : "ON UPDATE CASCADE "
1302 : "FOREIGN KEY(RowNameIndex) REFERENCES Strings(StringIndex) "
1303 : "ON UPDATE CASCADE "
1304 : "FOREIGN KEY(ColumnNameIndex) REFERENCES Strings(StringIndex) "
1305 : "ON UPDATE CASCADE "
1306 : "FOREIGN KEY(UnitsIndex) REFERENCES Strings(StringIndex) "
1307 : "ON UPDATE CASCADE "
1308 : "FOREIGN KEY(SimulationIndex) REFERENCES Simulations(SimulationIndex) "
1309 : "ON DELETE CASCADE ON UPDATE CASCADE "
1310 : ");";
1311 :
1312 23 : sqliteExecuteCommand(sql5);
1313 :
1314 23 : constexpr std::string_view sql6 = "INSERT INTO TabularData VALUES(?,?,?,?,?,?,?,?,?,?,?);";
1315 :
1316 23 : sqlitePrepareStatement(m_tabularDataInsertStmt, sql6);
1317 23 : }
1318 :
1319 23 : void SQLite::initializeTabularDataView()
1320 : {
1321 23 : constexpr std::string_view sql = "CREATE VIEW TabularDataWithStrings AS SELECT "
1322 : "td.TabularDataIndex, "
1323 : "td.Value As Value, "
1324 : "reportn.Value As ReportName, "
1325 : "fs.Value As ReportForString, "
1326 : "tn.Value As TableName, "
1327 : "rn.Value As RowName, "
1328 : "cn.Value As ColumnName, "
1329 : "u.Value As Units "
1330 : "FROM TabularData As td "
1331 : "INNER JOIN Strings As reportn ON reportn.StringIndex=td.ReportNameIndex "
1332 : "INNER JOIN Strings As fs ON fs.StringIndex=td.ReportForStringIndex "
1333 : "INNER JOIN Strings As tn ON tn.StringIndex=td.TableNameIndex "
1334 : "INNER JOIN Strings As rn ON rn.StringIndex=td.RowNameIndex "
1335 : "INNER JOIN Strings As cn ON cn.StringIndex=td.ColumnNameIndex "
1336 : "INNER JOIN Strings As u ON u.StringIndex=td.UnitsIndex;";
1337 :
1338 23 : sqliteExecuteCommand(sql);
1339 23 : }
1340 :
1341 123 : void SQLite::initializeIndexes()
1342 : {
1343 123 : if (m_writeOutputToSQLite) {
1344 123 : sqliteExecuteCommand("CREATE INDEX rddMTR ON ReportDataDictionary (IsMeter);");
1345 123 : sqliteExecuteCommand("CREATE INDEX redRD ON ReportExtendedData (ReportDataIndex);");
1346 :
1347 : // These following indexes could potentially be used by sqlite, but for a narrow range of queries
1348 : // There are no built in views that use these indexes in their queries.
1349 : // sqliteExecuteCommand("CREATE INDEX dmhdHRI ON DaylightMapHourlyData (HourlyReportIndex);");
1350 : // sqliteExecuteCommand("CREATE INDEX dmhrMNI ON DaylightMapHourlyReports (MapNumber);");
1351 :
1352 : // This following index is used by sqlite, but doesn't seem to increase performance in my testing
1353 : // sqliteExecuteCommand("CREATE INDEX tdI ON TabularData (ReportNameIndex, ReportForStringIndex, TableNameIndex, RowNameIndex,
1354 : // ColumnNameIndex, UnitsIndex, Value);");
1355 : }
1356 123 : }
1357 :
1358 29006 : void SQLite::adjustReportingHourAndMinutes(int &hour, int &minutes)
1359 : {
1360 29006 : switch (minutes) {
1361 4745 : case 60:
1362 4745 : minutes = 0;
1363 4745 : break;
1364 24261 : default:
1365 24261 : --hour;
1366 : }
1367 29006 : }
1368 :
1369 446175 : void SQLite::parseUnitsAndDescription(std::string_view combinedString, std::string &units, std::string &description)
1370 : {
1371 446175 : std::size_t leftPos = combinedString.find("[");
1372 446175 : std::size_t rightPos = combinedString.find("]");
1373 :
1374 446175 : if ((leftPos < rightPos) && (leftPos != std::string::npos) && (rightPos != std::string::npos)) {
1375 38138 : units = combinedString.substr(leftPos + 1, rightPos - leftPos - 1);
1376 38138 : description = combinedString.substr(0, leftPos - 1);
1377 : } else {
1378 408037 : units = "";
1379 408037 : description = combinedString;
1380 : }
1381 446175 : }
1382 :
1383 0 : int SQLite::logicalToInteger(const bool value)
1384 : {
1385 0 : return value ? 1 : 0;
1386 : }
1387 :
1388 15902 : void SQLite::createSQLiteReportDictionaryRecord(int const reportVariableReportID,
1389 : OutputProcessor::StoreType const storeType,
1390 : std::string_view indexGroup,
1391 : std::string_view keyedValueString,
1392 : std::string_view const variableName,
1393 : OutputProcessor::TimeStepType timeStepType,
1394 : std::string_view units,
1395 : OutputProcessor::ReportFreq const reportFreq,
1396 : bool isMeter,
1397 : std::string_view const scheduleName)
1398 : {
1399 : static constexpr std::array<std::string_view, (int)OutputProcessor::ReportFreq::Num> reportFreqStrings = {
1400 : "HVAC System Timestep", "Zone Timestep", "Hourly", "Daily", "Monthly", "Run Period", "Annual"};
1401 :
1402 : static constexpr std::array<std::string_view, (int)OutputProcessor::StoreType::Num> storeTypeStrings = {// "Dummy",
1403 : "Avg",
1404 : "Sum"};
1405 :
1406 : static constexpr std::array<std::string_view, (int)OutputProcessor::TimeStepType::Num> timeStepTypeStrings = {// "Dummy",
1407 : "Zone",
1408 : "HVAC System"};
1409 :
1410 15902 : if (m_writeOutputToSQLite) {
1411 15902 : sqliteBindInteger(m_reportDictionaryInsertStmt, 1, reportVariableReportID);
1412 15902 : sqliteBindLogical(m_reportDictionaryInsertStmt, 2, isMeter);
1413 31804 : sqliteBindText(
1414 15902 : m_reportDictionaryInsertStmt, 3, (storeType == OutputProcessor::StoreType::Invalid) ? "Unknown!!!" : storeTypeStrings[(int)storeType]);
1415 15902 : sqliteBindText(m_reportDictionaryInsertStmt, 4, indexGroup);
1416 31804 : sqliteBindText(m_reportDictionaryInsertStmt,
1417 : 5,
1418 15902 : (timeStepType == OutputProcessor::TimeStepType::Invalid) ? "Unknown!!!" : timeStepTypeStrings[(int)timeStepType]);
1419 15902 : sqliteBindText(m_reportDictionaryInsertStmt, 6, keyedValueString);
1420 15902 : sqliteBindText(m_reportDictionaryInsertStmt, 7, variableName);
1421 31804 : sqliteBindText(m_reportDictionaryInsertStmt,
1422 : 8,
1423 15902 : (reportFreq == OutputProcessor::ReportFreq::Invalid) ? "Unknown!!!" : reportFreqStrings[(int)reportFreq]);
1424 :
1425 15902 : if (!scheduleName.empty()) {
1426 229 : sqliteBindText(m_reportDictionaryInsertStmt, 9, scheduleName);
1427 : } else {
1428 15673 : sqliteBindNULL(m_reportDictionaryInsertStmt, 9);
1429 : }
1430 :
1431 15902 : sqliteBindText(m_reportDictionaryInsertStmt, 10, units);
1432 :
1433 15902 : sqliteStepCommand(m_reportDictionaryInsertStmt);
1434 15902 : sqliteResetCommand(m_reportDictionaryInsertStmt);
1435 : }
1436 15902 : }
1437 :
1438 2148224 : void SQLite::createSQLiteReportDataRecord(int const recordIndex,
1439 : Real64 const value,
1440 : OutputProcessor::ReportFreq const reportFreq,
1441 : Real64 const minValue,
1442 : int const minValueDate,
1443 : Real64 const maxValue,
1444 : int const maxValueDate,
1445 : int const minutesPerTimeStep)
1446 : {
1447 :
1448 2148224 : if (!m_writeOutputToSQLite) {
1449 0 : return;
1450 : }
1451 :
1452 2148224 : ++m_dataIndex;
1453 :
1454 2148224 : sqliteBindInteger(m_reportDataInsertStmt, 1, m_dataIndex);
1455 2148224 : sqliteBindForeignKey(m_reportDataInsertStmt, 2, m_sqlDBTimeIndex);
1456 2148224 : sqliteBindForeignKey(m_reportDataInsertStmt, 3, recordIndex);
1457 2148224 : sqliteBindDouble(m_reportDataInsertStmt, 4, value);
1458 :
1459 2148224 : sqliteStepCommand(m_reportDataInsertStmt);
1460 2148224 : sqliteResetCommand(m_reportDataInsertStmt);
1461 :
1462 2148224 : if (minValueDate != -1 && maxValueDate != -1) {
1463 : int minMonth;
1464 : int minDay;
1465 : int minHour;
1466 : int minMinute;
1467 : int maxMonth;
1468 : int maxDay;
1469 : int maxHour;
1470 : int maxMinute;
1471 :
1472 5572 : General::DecodeMonDayHrMin(minValueDate, minMonth, minDay, minHour, minMinute);
1473 5572 : General::DecodeMonDayHrMin(maxValueDate, maxMonth, maxDay, maxHour, maxMinute);
1474 :
1475 5572 : adjustReportingHourAndMinutes(minHour, minMinute);
1476 5572 : adjustReportingHourAndMinutes(maxHour, maxMinute);
1477 :
1478 5572 : ++m_extendedDataIndex;
1479 :
1480 5572 : if (minutesPerTimeStep != -1) { // This is for data created by a 'Report Meter' statement
1481 3868 : switch (reportFreq) {
1482 3868 : case OutputProcessor::ReportFreq::Hour:
1483 : case OutputProcessor::ReportFreq::Day:
1484 : case OutputProcessor::ReportFreq::Month:
1485 : case OutputProcessor::ReportFreq::Simulation:
1486 : case OutputProcessor::ReportFreq::Year: {
1487 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 1, m_extendedDataIndex);
1488 3868 : sqliteBindForeignKey(m_reportExtendedDataInsertStmt, 2, m_dataIndex);
1489 :
1490 3868 : sqliteBindDouble(m_reportExtendedDataInsertStmt, 3, maxValue);
1491 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 4, maxMonth);
1492 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 5, maxDay);
1493 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 6, maxHour);
1494 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 7, maxMinute - minutesPerTimeStep + 1);
1495 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 8, maxMinute);
1496 :
1497 3868 : sqliteBindDouble(m_reportExtendedDataInsertStmt, 9, minValue);
1498 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 10, minMonth);
1499 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 11, minDay);
1500 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 12, minHour);
1501 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 13, minMinute - minutesPerTimeStep + 1);
1502 3868 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 14, minMinute);
1503 :
1504 3868 : sqliteStepCommand(m_reportExtendedDataInsertStmt);
1505 3868 : sqliteResetCommand(m_reportExtendedDataInsertStmt);
1506 3868 : } break;
1507 :
1508 0 : case OutputProcessor::ReportFreq::TimeStep:
1509 : case OutputProcessor::ReportFreq::EachCall: {
1510 0 : --m_extendedDataIndex; // Reset the data index to account for the error
1511 0 : } break;
1512 :
1513 0 : default: {
1514 0 : --m_extendedDataIndex; // Reset the data index to account for the error
1515 0 : std::stringstream ss;
1516 0 : ss << "Illegal reportingInterval passed to CreateSQLiteMeterRecord: " << (int)reportFreq;
1517 0 : sqliteWriteMessage(ss.str());
1518 0 : } break;
1519 : } // switch (reportFreq)
1520 :
1521 : } else { // This is for data created by a 'Report Variable' statement
1522 1704 : switch (reportFreq) {
1523 1704 : case OutputProcessor::ReportFreq::Day:
1524 : case OutputProcessor::ReportFreq::Month:
1525 : case OutputProcessor::ReportFreq::Simulation:
1526 : case OutputProcessor::ReportFreq::Year: {
1527 1704 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 1, m_extendedDataIndex);
1528 1704 : sqliteBindForeignKey(m_reportExtendedDataInsertStmt, 2, m_dataIndex);
1529 :
1530 1704 : sqliteBindDouble(m_reportExtendedDataInsertStmt, 3, maxValue);
1531 1704 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 4, maxMonth);
1532 1704 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 5, maxDay);
1533 1704 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 6, maxHour);
1534 1704 : sqliteBindNULL(m_reportExtendedDataInsertStmt, 7);
1535 1704 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 8, maxMinute);
1536 :
1537 1704 : sqliteBindDouble(m_reportExtendedDataInsertStmt, 9, minValue);
1538 1704 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 10, minMonth);
1539 1704 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 11, minDay);
1540 1704 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 12, minHour);
1541 1704 : sqliteBindNULL(m_reportExtendedDataInsertStmt, 13);
1542 1704 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 14, minMinute);
1543 :
1544 1704 : sqliteStepCommand(m_reportExtendedDataInsertStmt);
1545 1704 : sqliteResetCommand(m_reportExtendedDataInsertStmt);
1546 1704 : } break;
1547 :
1548 0 : case OutputProcessor::ReportFreq::TimeStep:
1549 : case OutputProcessor::ReportFreq::EachCall:
1550 : case OutputProcessor::ReportFreq::Hour: {
1551 0 : --m_extendedDataIndex; // Reset the data index to account for the error
1552 0 : } break;
1553 0 : default: {
1554 0 : --m_extendedDataIndex; // Reset the data index to account for the error
1555 0 : std::stringstream ss;
1556 0 : ss << "Illegal reportingInterval passed to CreateSQLiteMeterRecord: " << (int)reportFreq;
1557 0 : sqliteWriteMessage(ss.str());
1558 0 : } break;
1559 : } // switch (reportFreq)
1560 : } // if (minutesPerTimeStep != -1)
1561 : } // if (minDataValue != 0)
1562 : } // SQLite::createSQLiteReportDataRecord()
1563 :
1564 23162 : void SQLite::createSQLiteTimeIndexRecord(OutputProcessor::ReportFreq const reportFreq,
1565 : [[maybe_unused]] int const recordIndex,
1566 : int const cumlativeSimulationDays,
1567 : int const curEnvirNum,
1568 : int const simulationYear,
1569 : bool const curYearIsLeapYear,
1570 : int const month,
1571 : int const dayOfMonth,
1572 : int const hour,
1573 : Real64 const endMinute,
1574 : Real64 const startMinute,
1575 : int const dst,
1576 : std::string_view const dayType,
1577 : bool const warmupFlag)
1578 : {
1579 23162 : if (m_writeOutputToSQLite) {
1580 23162 : int intStartMinute = 0;
1581 23162 : int intervalInMinutes = 60;
1582 :
1583 23162 : static std::vector<int> lastDayOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1584 23162 : if (curYearIsLeapYear) {
1585 0 : lastDayOfMonth[1] = 29;
1586 : }
1587 :
1588 23162 : switch (reportFreq) {
1589 17862 : case OutputProcessor::ReportFreq::EachCall:
1590 : case OutputProcessor::ReportFreq::TimeStep: {
1591 17862 : if (month == -1 || dayOfMonth == -1 || hour == -1 || endMinute == -1.0 || startMinute == -1.0 || dst == -1 || dayType == "") {
1592 0 : sqliteWriteMessage("Empty month, dayOfMonth, hour, endMinute, startMinute, dst, or dayType passed to CreateSQLiteTimeIndexRecord");
1593 0 : break;
1594 : }
1595 17862 : ++m_sqlDBTimeIndex;
1596 :
1597 17862 : int intEndMinute = static_cast<int>(endMinute + 0.5);
1598 17862 : intStartMinute = static_cast<int>(startMinute + 0.5);
1599 17862 : int t_hour = hour;
1600 17862 : intervalInMinutes = intEndMinute - intStartMinute;
1601 17862 : adjustReportingHourAndMinutes(t_hour, intEndMinute);
1602 :
1603 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1604 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 2, simulationYear);
1605 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 3, month);
1606 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 4, dayOfMonth);
1607 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 5, t_hour);
1608 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 6, intEndMinute);
1609 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 7, dst);
1610 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 8, intervalInMinutes);
1611 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)reportFreq]);
1612 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 10, cumlativeSimulationDays);
1613 17862 : sqliteBindText(m_timeIndexInsertStmt, 11, dayType);
1614 17862 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1615 17862 : sqliteBindLogical(m_timeIndexInsertStmt, 13, warmupFlag);
1616 :
1617 17862 : sqliteStepCommand(m_timeIndexInsertStmt);
1618 17862 : sqliteResetCommand(m_timeIndexInsertStmt);
1619 :
1620 17862 : break;
1621 : }
1622 5160 : case OutputProcessor::ReportFreq::Hour: {
1623 5160 : if (month == -1 || dayOfMonth == -1 || hour == -1 || dst == -1 || dayType == "") {
1624 0 : sqliteWriteMessage("Empty month, dayOfMonth, hour, dst, or dayType passed to CreateSQLiteTimeIndexRecord");
1625 0 : break;
1626 : }
1627 5160 : ++m_sqlDBTimeIndex;
1628 :
1629 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1630 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 2, simulationYear);
1631 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 3, month);
1632 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 4, dayOfMonth);
1633 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 5, hour);
1634 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 6, 0);
1635 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 7, dst);
1636 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 8, intervalInMinutes);
1637 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)reportFreq]);
1638 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 10, cumlativeSimulationDays);
1639 5160 : sqliteBindText(m_timeIndexInsertStmt, 11, dayType);
1640 5160 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1641 :
1642 5160 : sqliteStepCommand(m_timeIndexInsertStmt);
1643 5160 : sqliteResetCommand(m_timeIndexInsertStmt);
1644 :
1645 5160 : break;
1646 : }
1647 17 : case OutputProcessor::ReportFreq::Day: {
1648 17 : if (month == -1 || dayOfMonth == -1 || dst == -1 || dayType == "") {
1649 0 : sqliteWriteMessage("Empty month, dayOfMonth, dst, or dayType passed to CreateSQLiteTimeIndexRecord");
1650 0 : break;
1651 : }
1652 17 : ++m_sqlDBTimeIndex;
1653 :
1654 17 : intervalInMinutes = 60 * 24;
1655 17 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1656 17 : sqliteBindInteger(m_timeIndexInsertStmt, 2, simulationYear);
1657 17 : sqliteBindInteger(m_timeIndexInsertStmt, 3, month);
1658 17 : sqliteBindInteger(m_timeIndexInsertStmt, 4, dayOfMonth);
1659 17 : sqliteBindInteger(m_timeIndexInsertStmt, 5, 24);
1660 17 : sqliteBindInteger(m_timeIndexInsertStmt, 6, 0);
1661 17 : sqliteBindInteger(m_timeIndexInsertStmt, 7, dst);
1662 17 : sqliteBindInteger(m_timeIndexInsertStmt, 8, intervalInMinutes);
1663 17 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)reportFreq]);
1664 17 : sqliteBindInteger(m_timeIndexInsertStmt, 10, cumlativeSimulationDays);
1665 17 : sqliteBindText(m_timeIndexInsertStmt, 11, dayType);
1666 17 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1667 :
1668 17 : sqliteStepCommand(m_timeIndexInsertStmt);
1669 17 : sqliteResetCommand(m_timeIndexInsertStmt);
1670 :
1671 17 : break;
1672 : }
1673 115 : case OutputProcessor::ReportFreq::Month: {
1674 115 : if (month == -1) {
1675 0 : sqliteWriteMessage("Empty month passed to CreateSQLiteTimeIndexRecord");
1676 0 : break;
1677 : }
1678 115 : ++m_sqlDBTimeIndex;
1679 :
1680 115 : intervalInMinutes = 60 * 24 * lastDayOfMonth[month - 1];
1681 115 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1682 115 : sqliteBindInteger(m_timeIndexInsertStmt, 2, simulationYear);
1683 115 : sqliteBindInteger(m_timeIndexInsertStmt, 3, month);
1684 115 : sqliteBindInteger(m_timeIndexInsertStmt, 4, lastDayOfMonth[month - 1]);
1685 115 : sqliteBindInteger(m_timeIndexInsertStmt, 5, 24);
1686 115 : sqliteBindInteger(m_timeIndexInsertStmt, 6, 0);
1687 115 : sqliteBindNULL(m_timeIndexInsertStmt, 7);
1688 115 : sqliteBindInteger(m_timeIndexInsertStmt, 8, intervalInMinutes);
1689 115 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)reportFreq]);
1690 115 : sqliteBindInteger(m_timeIndexInsertStmt, 10, cumlativeSimulationDays);
1691 115 : sqliteBindNULL(m_timeIndexInsertStmt, 11);
1692 115 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1693 :
1694 115 : sqliteStepCommand(m_timeIndexInsertStmt);
1695 115 : sqliteResetCommand(m_timeIndexInsertStmt);
1696 :
1697 115 : break;
1698 : }
1699 8 : case OutputProcessor::ReportFreq::Simulation: {
1700 8 : ++m_sqlDBTimeIndex;
1701 :
1702 8 : intervalInMinutes = 60 * 24 * cumlativeSimulationDays;
1703 8 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1704 8 : sqliteBindNULL(m_timeIndexInsertStmt, 2);
1705 8 : sqliteBindNULL(m_timeIndexInsertStmt, 3);
1706 8 : sqliteBindNULL(m_timeIndexInsertStmt, 4);
1707 8 : sqliteBindNULL(m_timeIndexInsertStmt, 5);
1708 8 : sqliteBindNULL(m_timeIndexInsertStmt, 6);
1709 8 : sqliteBindNULL(m_timeIndexInsertStmt, 7);
1710 8 : sqliteBindInteger(m_timeIndexInsertStmt, 8, intervalInMinutes);
1711 8 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)reportFreq]);
1712 8 : sqliteBindInteger(m_timeIndexInsertStmt, 10, cumlativeSimulationDays);
1713 8 : sqliteBindNULL(m_timeIndexInsertStmt, 11);
1714 8 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1715 :
1716 8 : sqliteStepCommand(m_timeIndexInsertStmt);
1717 8 : sqliteResetCommand(m_timeIndexInsertStmt);
1718 :
1719 8 : break;
1720 : }
1721 0 : default: {
1722 0 : std::stringstream ss;
1723 0 : ss << "Illegal reportingInterval passed to CreateSQLiteTimeIndexRecord: " << (int)reportFreq;
1724 0 : sqliteWriteMessage(ss.str());
1725 0 : }
1726 : }
1727 : }
1728 23162 : } // SQLite::createSQLiteTimeIndexRecord()
1729 :
1730 0 : void SQLite::createYearlyTimeIndexRecord(int const simulationYear, int const curEnvirNum)
1731 : {
1732 0 : if (m_writeOutputToSQLite) {
1733 :
1734 0 : ++m_sqlDBTimeIndex;
1735 :
1736 0 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1737 0 : sqliteBindInteger(m_timeIndexInsertStmt, 2, simulationYear);
1738 0 : sqliteBindNULL(m_timeIndexInsertStmt, 3);
1739 0 : sqliteBindNULL(m_timeIndexInsertStmt, 4);
1740 0 : sqliteBindNULL(m_timeIndexInsertStmt, 5);
1741 0 : sqliteBindNULL(m_timeIndexInsertStmt, 6);
1742 0 : sqliteBindNULL(m_timeIndexInsertStmt, 7);
1743 0 : sqliteBindNULL(m_timeIndexInsertStmt, 8);
1744 0 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)OutputProcessor::ReportFreq::Year]);
1745 0 : sqliteBindNULL(m_timeIndexInsertStmt, 10);
1746 0 : sqliteBindNULL(m_timeIndexInsertStmt, 11);
1747 0 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1748 :
1749 0 : sqliteStepCommand(m_timeIndexInsertStmt);
1750 0 : sqliteResetCommand(m_timeIndexInsertStmt);
1751 : }
1752 0 : }
1753 :
1754 2494 : void SQLite::addSQLiteZoneSizingRecord(std::string_view zoneName, // the name of the zone
1755 : std::string_view loadType, // the description of the input variable
1756 : Real64 const calcDesLoad, // the value from the sizing calculation [W]
1757 : Real64 const userDesLoad, // the value from the sizing calculation modified by user input [W]
1758 : Real64 const calcDesFlow, // calculated design air flow rate [m3/s]
1759 : Real64 const userDesFlow, // user input or modified design air flow rate [m3/s]
1760 : std::string_view desDayName, // the name of the design day that produced the peak
1761 : std::string_view peakHrMin, // time stamp of the peak
1762 : Real64 const peakTemp, // temperature at peak [C]
1763 : Real64 const peakHumRat, // humidity ratio at peak [kg water/kg dry air]
1764 : Real64 const minOAVolFlow, // zone design minimum outside air flow rate [m3/s]
1765 : Real64 const DOASHeatAddRate // zone design heat addition rate from the DOAS [W]
1766 : )
1767 : {
1768 2494 : if (m_writeOutputToSQLite) {
1769 2494 : ++m_zoneSizingIndex;
1770 2494 : sqliteBindInteger(m_zoneSizingInsertStmt, 1, m_zoneSizingIndex);
1771 2494 : sqliteBindText(m_zoneSizingInsertStmt, 2, zoneName);
1772 2494 : sqliteBindText(m_zoneSizingInsertStmt, 3, loadType);
1773 :
1774 2494 : sqliteBindDouble(m_zoneSizingInsertStmt, 4, calcDesLoad);
1775 2494 : sqliteBindDouble(m_zoneSizingInsertStmt, 5, userDesLoad);
1776 2494 : sqliteBindDouble(m_zoneSizingInsertStmt, 6, calcDesFlow);
1777 2494 : sqliteBindDouble(m_zoneSizingInsertStmt, 7, userDesFlow);
1778 :
1779 2494 : sqliteBindText(m_zoneSizingInsertStmt, 8, desDayName);
1780 2494 : sqliteBindText(m_zoneSizingInsertStmt, 9, peakHrMin);
1781 :
1782 2494 : sqliteBindDouble(m_zoneSizingInsertStmt, 10, peakTemp);
1783 2494 : sqliteBindDouble(m_zoneSizingInsertStmt, 11, peakHumRat);
1784 2494 : sqliteBindDouble(m_zoneSizingInsertStmt, 12, minOAVolFlow);
1785 2494 : sqliteBindDouble(m_zoneSizingInsertStmt, 13, DOASHeatAddRate);
1786 :
1787 2494 : sqliteStepCommand(m_zoneSizingInsertStmt);
1788 2494 : sqliteResetCommand(m_zoneSizingInsertStmt);
1789 : }
1790 2494 : }
1791 :
1792 532 : void SQLite::addSQLiteSystemSizingRecord(std::string_view SysName, // the name of the system
1793 : std::string_view LoadType, // either "Cooling" or "Heating"
1794 : std::string_view PeakLoadType, // either "Sensible" or "Total"
1795 : Real64 const UserDesCap, // User Design Capacity
1796 : Real64 const CalcDesVolFlow, // Calculated Cooling Design Air Flow Rate
1797 : Real64 const UserDesVolFlow, // User Cooling Design Air Flow Rate
1798 : std::string_view DesDayName, // the name of the design day that produced the peak
1799 : std::string_view PeakHrMin // time stamp of the peak
1800 : )
1801 : {
1802 532 : if (m_writeOutputToSQLite) {
1803 532 : ++m_systemSizingIndex;
1804 532 : sqliteBindInteger(m_systemSizingInsertStmt, 1, m_systemSizingIndex);
1805 532 : sqliteBindText(m_systemSizingInsertStmt, 2, SysName);
1806 532 : sqliteBindText(m_systemSizingInsertStmt, 3, LoadType);
1807 532 : sqliteBindText(m_systemSizingInsertStmt, 4, PeakLoadType);
1808 :
1809 532 : sqliteBindDouble(m_systemSizingInsertStmt, 5, UserDesCap);
1810 532 : sqliteBindDouble(m_systemSizingInsertStmt, 6, CalcDesVolFlow);
1811 532 : sqliteBindDouble(m_systemSizingInsertStmt, 7, UserDesVolFlow);
1812 532 : sqliteBindText(m_systemSizingInsertStmt, 8, DesDayName);
1813 532 : sqliteBindText(m_systemSizingInsertStmt, 9, PeakHrMin);
1814 :
1815 532 : sqliteStepCommand(m_systemSizingInsertStmt);
1816 532 : sqliteResetCommand(m_systemSizingInsertStmt);
1817 : }
1818 532 : }
1819 :
1820 21689 : void SQLite::addSQLiteComponentSizingRecord(std::string_view compType, // the type of the component
1821 : std::string_view compName, // the name of the component
1822 : std::string_view varDesc, // the description of the input variable
1823 : Real64 const varValue // the value from the sizing calculation
1824 : )
1825 : {
1826 21689 : if (m_writeOutputToSQLite) {
1827 21689 : ++m_componentSizingIndex;
1828 :
1829 21689 : std::string description;
1830 21689 : std::string units;
1831 :
1832 21689 : parseUnitsAndDescription(varDesc, units, description);
1833 :
1834 21689 : sqliteBindInteger(m_componentSizingInsertStmt, 1, m_componentSizingIndex);
1835 21689 : sqliteBindText(m_componentSizingInsertStmt, 2, compType);
1836 21689 : sqliteBindText(m_componentSizingInsertStmt, 3, compName);
1837 21689 : sqliteBindText(m_componentSizingInsertStmt, 4, description);
1838 21689 : sqliteBindDouble(m_componentSizingInsertStmt, 5, varValue);
1839 21689 : sqliteBindText(m_componentSizingInsertStmt, 6, units);
1840 :
1841 21689 : sqliteStepCommand(m_componentSizingInsertStmt);
1842 21689 : sqliteResetCommand(m_componentSizingInsertStmt);
1843 21689 : }
1844 21689 : }
1845 :
1846 1 : void SQLite::createSQLiteDaylightMapTitle(
1847 : int const mapNum, std::string_view mapName, std::string_view environmentName, int const zone, std::string_view refPts, Real64 const zCoord)
1848 : {
1849 1 : if (m_writeOutputToSQLite) {
1850 : // for some reason it is adding extra mapNumbers that are getting UNIQUE constraint ignored.
1851 : // Might need to look into it, basically I think something is getting double inserted (12/06/14)
1852 1 : sqliteBindInteger(m_daylightMapTitleInsertStmt, 1, mapNum);
1853 1 : sqliteBindText(m_daylightMapTitleInsertStmt, 2, mapName);
1854 1 : sqliteBindText(m_daylightMapTitleInsertStmt, 3, environmentName);
1855 1 : sqliteBindForeignKey(m_daylightMapTitleInsertStmt, 4, zone);
1856 1 : sqliteBindText(m_daylightMapTitleInsertStmt, 5, refPts);
1857 1 : sqliteBindDouble(m_daylightMapTitleInsertStmt, 6, zCoord);
1858 :
1859 1 : sqliteStepCommand(m_daylightMapTitleInsertStmt);
1860 1 : sqliteResetCommand(m_daylightMapTitleInsertStmt);
1861 : }
1862 1 : }
1863 :
1864 16 : void SQLite::createSQLiteDaylightMap(int const mapNum,
1865 : int const year,
1866 : int const month,
1867 : int const dayOfMonth,
1868 : int const hourOfDay,
1869 : int const nX,
1870 : Array1D<Real64> const &x,
1871 : int const nY,
1872 : Array1D<Real64> const &y,
1873 : Array2<Real64> const &illuminance)
1874 : {
1875 16 : if (m_writeOutputToSQLite) {
1876 16 : ++m_hourlyReportIndex;
1877 16 : int b = 0;
1878 16 : sqliteBindInteger(m_daylightMapHourlyTitleInsertStmt, ++b, m_hourlyReportIndex);
1879 16 : sqliteBindForeignKey(m_daylightMapHourlyTitleInsertStmt, ++b, mapNum);
1880 16 : sqliteBindForeignKey(m_daylightMapHourlyTitleInsertStmt, ++b, year);
1881 16 : sqliteBindInteger(m_daylightMapHourlyTitleInsertStmt, ++b, month);
1882 16 : sqliteBindInteger(m_daylightMapHourlyTitleInsertStmt, ++b, dayOfMonth);
1883 16 : sqliteBindInteger(m_daylightMapHourlyTitleInsertStmt, ++b, hourOfDay);
1884 :
1885 16 : sqliteStepCommand(m_daylightMapHourlyTitleInsertStmt);
1886 16 : sqliteResetCommand(m_daylightMapHourlyTitleInsertStmt);
1887 :
1888 176 : for (int yIndex = 1; yIndex <= nY; ++yIndex) {
1889 1760 : for (int xIndex = 1; xIndex <= nX; ++xIndex) {
1890 1600 : ++m_hourlyDataIndex;
1891 1600 : sqliteBindInteger(m_daylightMapHourlyDataInsertStmt, 1, m_hourlyDataIndex);
1892 1600 : sqliteBindForeignKey(m_daylightMapHourlyDataInsertStmt, 2, m_hourlyReportIndex);
1893 1600 : sqliteBindDouble(m_daylightMapHourlyDataInsertStmt, 3, x(xIndex));
1894 1600 : sqliteBindDouble(m_daylightMapHourlyDataInsertStmt, 4, y(yIndex));
1895 1600 : sqliteBindDouble(m_daylightMapHourlyDataInsertStmt, 5, illuminance(xIndex, yIndex));
1896 :
1897 1600 : sqliteStepCommand(m_daylightMapHourlyDataInsertStmt);
1898 1600 : sqliteResetCommand(m_daylightMapHourlyDataInsertStmt);
1899 : }
1900 : }
1901 : }
1902 16 : }
1903 :
1904 18476 : void SQLite::createSQLiteTabularDataRecords(Array2D_string const &body, // html table row, html table column
1905 : Array1D_string const &rowLabels,
1906 : Array1D_string const &columnLabels,
1907 : std::string_view reportName,
1908 : std::string_view reportForString,
1909 : std::string_view tableName)
1910 : {
1911 18476 : if (m_writeTabularDataToSQLite) {
1912 3852 : size_t sizeColumnLabels = columnLabels.size();
1913 3852 : size_t sizeRowLabels = rowLabels.size();
1914 :
1915 3852 : int const reportNameIndex = createSQLiteStringTableRecord(reportName, ReportNameId);
1916 3852 : int const reportForStringIndex = createSQLiteStringTableRecord(reportForString, ReportForStringId);
1917 3852 : int const tableNameIndex = createSQLiteStringTableRecord(tableName, TableNameId);
1918 : int unitsIndex;
1919 :
1920 32854 : for (size_t iCol = 0, k = body.index(1, 1); iCol < sizeColumnLabels; ++iCol) {
1921 29002 : std::string colUnits;
1922 29002 : std::string colDescription;
1923 29002 : parseUnitsAndDescription(columnLabels[iCol], colUnits, colDescription);
1924 :
1925 29002 : int const columnLabelIndex = createSQLiteStringTableRecord(colDescription, ColumnNameId);
1926 :
1927 29002 : if (!colUnits.empty()) {
1928 14873 : unitsIndex = createSQLiteStringTableRecord(colUnits, UnitsId);
1929 : }
1930 :
1931 424486 : for (size_t iRow = 0; iRow < sizeRowLabels; ++iRow) {
1932 395484 : ++m_tabularDataIndex;
1933 395484 : std::string rowUnits;
1934 395484 : std::string rowDescription;
1935 395484 : parseUnitsAndDescription(rowLabels[iRow], rowUnits, rowDescription);
1936 :
1937 395484 : int const rowLabelIndex = createSQLiteStringTableRecord(rowDescription, RowNameId);
1938 :
1939 395484 : if (colUnits.empty()) {
1940 202832 : unitsIndex = createSQLiteStringTableRecord(rowUnits, UnitsId);
1941 : }
1942 :
1943 395484 : sqliteBindInteger(m_tabularDataInsertStmt, 1, m_tabularDataIndex);
1944 395484 : sqliteBindForeignKey(m_tabularDataInsertStmt, 2, reportNameIndex);
1945 395484 : sqliteBindForeignKey(m_tabularDataInsertStmt, 3, reportForStringIndex);
1946 395484 : sqliteBindForeignKey(m_tabularDataInsertStmt, 4, tableNameIndex);
1947 395484 : sqliteBindForeignKey(m_tabularDataInsertStmt, 5, rowLabelIndex);
1948 395484 : sqliteBindForeignKey(m_tabularDataInsertStmt, 6, columnLabelIndex);
1949 395484 : sqliteBindForeignKey(m_tabularDataInsertStmt, 7, unitsIndex);
1950 395484 : sqliteBindForeignKey(m_tabularDataInsertStmt, 8, 1);
1951 395484 : sqliteBindInteger(m_tabularDataInsertStmt, 9, iRow);
1952 395484 : sqliteBindInteger(m_tabularDataInsertStmt, 10, iCol);
1953 395484 : sqliteBindText(m_tabularDataInsertStmt, 11, body[k]);
1954 :
1955 395484 : sqliteStepCommand(m_tabularDataInsertStmt);
1956 395484 : sqliteResetCommand(m_tabularDataInsertStmt);
1957 :
1958 395484 : ++k;
1959 395484 : }
1960 29002 : }
1961 : }
1962 18476 : }
1963 :
1964 653747 : int SQLite::createSQLiteStringTableRecord(std::string_view stringValue, int const stringType)
1965 : {
1966 653747 : int rowId = -1;
1967 653747 : if (m_writeOutputToSQLite) {
1968 :
1969 653747 : auto ret = m_tabularStrings.emplace(make_pair(stringValue, stringType), 0);
1970 :
1971 653747 : if (!ret.second) {
1972 614985 : rowId = ret.first->second;
1973 : } else {
1974 38762 : sqliteBindInteger(m_stringsInsertStmt, 1, m_stringIndex);
1975 38762 : sqliteBindForeignKey(m_stringsInsertStmt, 2, stringType);
1976 38762 : sqliteBindText(m_stringsInsertStmt, 3, stringValue);
1977 :
1978 38762 : int errorcode = sqliteStepCommand(m_stringsInsertStmt);
1979 38762 : sqliteResetCommand(m_stringsInsertStmt);
1980 :
1981 38762 : if (errorcode != SQLITE_CONSTRAINT) {
1982 38762 : rowId = m_stringIndex++;
1983 : } else {
1984 0 : sqliteBindInteger(m_stringsLookUpStmt, 1, stringType);
1985 0 : sqliteBindText(m_stringsLookUpStmt, 2, stringValue);
1986 0 : sqliteStepCommand(m_stringsLookUpStmt);
1987 0 : rowId = sqlite3_column_int(m_stringsLookUpStmt, 0);
1988 0 : sqliteResetCommand(m_stringsLookUpStmt);
1989 : }
1990 38762 : ret.first->second = rowId;
1991 : }
1992 653747 : }
1993 653747 : return rowId;
1994 : }
1995 :
1996 123 : void SQLite::createSQLiteSimulationsRecord(int const id, std::string_view verString, std::string_view currentDateTime)
1997 : {
1998 123 : if (m_writeOutputToSQLite) {
1999 123 : sqliteBindInteger(m_simulationsInsertStmt, 1, id);
2000 123 : sqliteBindText(m_simulationsInsertStmt, 2, verString);
2001 123 : sqliteBindText(m_simulationsInsertStmt, 3, currentDateTime);
2002 :
2003 123 : sqliteStepCommand(m_simulationsInsertStmt);
2004 123 : sqliteResetCommand(m_simulationsInsertStmt);
2005 : }
2006 123 : }
2007 :
2008 11384 : void SQLite::createSQLiteErrorRecord(int const simulationIndex, int const errorType, std::string_view errorMessage, int const cnt)
2009 : {
2010 11384 : if (m_writeOutputToSQLite) {
2011 11384 : ++m_errorIndex;
2012 :
2013 11384 : sqliteBindInteger(m_errorInsertStmt, 1, m_errorIndex);
2014 11384 : sqliteBindForeignKey(m_errorInsertStmt, 2, simulationIndex);
2015 11384 : sqliteBindInteger(m_errorInsertStmt, 3, errorType);
2016 11384 : sqliteBindText(m_errorInsertStmt, 4, errorMessage);
2017 11384 : sqliteBindInteger(m_errorInsertStmt, 5, cnt);
2018 :
2019 11384 : sqliteStepCommand(m_errorInsertStmt);
2020 11384 : sqliteResetCommand(m_errorInsertStmt);
2021 : }
2022 11384 : }
2023 :
2024 19427 : void SQLite::updateSQLiteErrorRecord(std::string const &errorMessage)
2025 : {
2026 19427 : if (m_writeOutputToSQLite) {
2027 19427 : sqliteBindText(m_errorUpdateStmt, 1, " " + errorMessage);
2028 :
2029 19427 : sqliteStepCommand(m_errorUpdateStmt);
2030 19427 : sqliteResetCommand(m_errorUpdateStmt);
2031 : }
2032 19427 : }
2033 :
2034 123 : void SQLite::updateSQLiteSimulationRecord(int const id, int const numOfTimeStepInHour)
2035 : {
2036 123 : if (m_writeOutputToSQLite) {
2037 123 : sqliteBindInteger(m_simulationDataUpdateStmt, 1, numOfTimeStepInHour);
2038 123 : sqliteBindForeignKey(m_simulationDataUpdateStmt, 2, id);
2039 :
2040 123 : sqliteStepCommand(m_simulationDataUpdateStmt);
2041 123 : sqliteResetCommand(m_simulationDataUpdateStmt);
2042 : }
2043 123 : }
2044 :
2045 0 : void SQLite::updateSQLiteSimulationRecord(bool const completed, bool const completedSuccessfully, int const id)
2046 : {
2047 0 : if (m_writeOutputToSQLite) {
2048 0 : sqliteBindLogical(m_simulationUpdateStmt, 1, completed);
2049 0 : sqliteBindLogical(m_simulationUpdateStmt, 2, completedSuccessfully);
2050 0 : sqliteBindForeignKey(m_simulationUpdateStmt, 3, id); // seems to always be 1, SimulationManager::ManageSimulation()
2051 :
2052 0 : sqliteStepCommand(m_simulationUpdateStmt);
2053 0 : sqliteResetCommand(m_simulationUpdateStmt);
2054 : }
2055 0 : }
2056 :
2057 123 : void SQLite::createZoneExtendedOutput()
2058 : {
2059 123 : if (m_writeOutputToSQLite) {
2060 1629 : for (auto const &zone : zones) {
2061 1506 : zone->insertIntoSQLite(m_zoneInfoInsertStmt);
2062 123 : }
2063 135 : for (auto const &zoneList : zoneLists) {
2064 12 : zoneList->insertIntoSQLite(m_zoneListInsertStmt, m_zoneInfoZoneListInsertStmt);
2065 123 : }
2066 126 : for (auto const &zoneGroup : zoneGroups) {
2067 3 : zoneGroup->insertIntoSQLite(m_zoneGroupInsertStmt);
2068 123 : }
2069 3459 : for (auto const &schedule : schedules) {
2070 3336 : schedule->insertIntoSQLite(m_scheduleInsertStmt);
2071 123 : }
2072 2015 : for (auto const &material : materials) {
2073 1892 : material->insertIntoSQLite(m_materialInsertStmt);
2074 123 : }
2075 1021 : for (auto const &construction : constructions) {
2076 898 : construction->insertIntoSQLite(m_constructionInsertStmt, m_constructionLayerInsertStmt);
2077 123 : }
2078 13744 : for (auto const &surface : surfaces) {
2079 13621 : surface->insertIntoSQLite(m_surfaceInsertStmt);
2080 123 : }
2081 1521 : for (auto const &nominalLighting : nominalLightings) {
2082 1398 : nominalLighting->insertIntoSQLite(m_nominalLightingInsertStmt);
2083 123 : }
2084 1349 : for (auto const &nominalPeople : nominalPeoples) {
2085 1226 : nominalPeople->insertIntoSQLite(m_nominalPeopleInsertStmt);
2086 123 : }
2087 1467 : for (auto const &nominalElectricEquipment : nominalElectricEquipments) {
2088 1344 : nominalElectricEquipment->insertIntoSQLite(m_nominalElectricEquipmentInsertStmt);
2089 123 : }
2090 154 : for (auto const &nominalGasEquipment : nominalGasEquipments) {
2091 31 : nominalGasEquipment->insertIntoSQLite(m_nominalGasEquipmentInsertStmt);
2092 123 : }
2093 123 : for (auto const &nominalSteamEquipment : nominalSteamEquipments) {
2094 0 : nominalSteamEquipment->insertIntoSQLite(m_nominalSteamEquipmentInsertStmt);
2095 123 : }
2096 132 : for (auto const &nominalHotWaterEquipment : nominalHotWaterEquipments) {
2097 9 : nominalHotWaterEquipment->insertIntoSQLite(m_nominalHotWaterEquipmentInsertStmt);
2098 123 : }
2099 142 : for (auto const &nominalOtherEquipment : nominalOtherEquipments) {
2100 19 : nominalOtherEquipment->insertIntoSQLite(m_nominalOtherEquipmentInsertStmt);
2101 123 : }
2102 123 : for (auto const &nominalBaseboardHeat : nominalBaseboardHeats) {
2103 0 : nominalBaseboardHeat->insertIntoSQLite(m_nominalBaseboardHeatInsertStmt);
2104 123 : }
2105 1298 : for (auto const &infiltration : infiltrations) {
2106 1175 : infiltration->insertIntoSQLite(m_infiltrationInsertStmt);
2107 123 : }
2108 131 : for (auto const &ventilation : ventilations) {
2109 8 : ventilation->insertIntoSQLite(m_ventilationInsertStmt);
2110 123 : }
2111 1629 : for (auto const &roomAirModel : roomAirModels) {
2112 1506 : roomAirModel->insertIntoSQLite(m_roomAirModelInsertStmt);
2113 123 : }
2114 : }
2115 123 : }
2116 :
2117 251 : void SQLite::createSQLiteEnvironmentPeriodRecord(const int curEnvirNum,
2118 : std::string_view environmentName,
2119 : const Constant::KindOfSim kindOfSim,
2120 : const int simulationIndex)
2121 : {
2122 251 : if (m_writeOutputToSQLite) {
2123 251 : sqliteBindInteger(m_environmentPeriodInsertStmt, 1, curEnvirNum);
2124 251 : sqliteBindForeignKey(m_environmentPeriodInsertStmt, 2, simulationIndex);
2125 251 : sqliteBindText(m_environmentPeriodInsertStmt, 3, environmentName);
2126 251 : sqliteBindInteger(m_environmentPeriodInsertStmt, 4, static_cast<int>(kindOfSim));
2127 :
2128 251 : sqliteStepCommand(m_environmentPeriodInsertStmt);
2129 251 : sqliteResetCommand(m_environmentPeriodInsertStmt);
2130 : }
2131 251 : }
2132 :
2133 3336 : void SQLite::addScheduleData(int const number, std::string_view name, std::string_view type, double const minValue, double const maxValue)
2134 : {
2135 3336 : schedules.push_back(std::make_unique<Schedule>(m_errorStream, m_db, number, name, type, minValue, maxValue));
2136 3336 : }
2137 :
2138 1506 : void SQLite::addZoneData(int const number, DataHeatBalance::ZoneData const &zoneData)
2139 : {
2140 1506 : zones.push_back(std::make_unique<Zone>(m_errorStream, m_db, number, zoneData));
2141 1506 : }
2142 :
2143 12 : void SQLite::addZoneListData(int const number, DataHeatBalance::ZoneListData const &zoneListData)
2144 : {
2145 12 : zoneLists.push_back(std::make_unique<ZoneList>(m_errorStream, m_db, number, zoneListData));
2146 12 : }
2147 :
2148 13621 : void SQLite::addSurfaceData(int const number, DataSurfaces::SurfaceData const &surfaceData, std::string_view surfaceClass)
2149 : {
2150 13621 : surfaces.push_back(std::make_unique<Surface>(m_errorStream, m_db, number, surfaceData, surfaceClass));
2151 13621 : }
2152 :
2153 3 : void SQLite::addZoneGroupData(int const number, DataHeatBalance::ZoneGroupData const &zoneGroupData)
2154 : {
2155 3 : zoneGroups.push_back(std::make_unique<ZoneGroup>(m_errorStream, m_db, number, zoneGroupData));
2156 3 : }
2157 :
2158 1892 : void SQLite::addMaterialData(int const number, EnergyPlus::Material::MaterialBase const *materialData)
2159 : {
2160 1892 : materials.push_back(std::make_unique<Material>(m_errorStream, m_db, number, materialData));
2161 1892 : }
2162 898 : void SQLite::addConstructionData(int const number,
2163 : EnergyPlus::Construction::ConstructionProps const &constructionData,
2164 : double const &constructionUValue)
2165 : {
2166 898 : constructions.push_back(std::make_unique<Construction>(m_errorStream, m_db, number, constructionData, constructionUValue));
2167 898 : }
2168 1398 : void SQLite::addNominalLightingData(int const number, DataHeatBalance::LightsData const &nominalLightingData)
2169 : {
2170 1398 : nominalLightings.push_back(std::make_unique<NominalLighting>(m_errorStream, m_db, number, nominalLightingData));
2171 1398 : }
2172 1226 : void SQLite::addNominalPeopleData(int const number, DataHeatBalance::PeopleData const &nominalPeopleData)
2173 : {
2174 1226 : nominalPeoples.push_back(std::make_unique<NominalPeople>(m_errorStream, m_db, number, nominalPeopleData));
2175 1226 : }
2176 1344 : void SQLite::addNominalElectricEquipmentData(int const number, DataHeatBalance::ZoneEquipData const &nominalElectricEquipmentData)
2177 : {
2178 1344 : nominalElectricEquipments.push_back(std::make_unique<NominalElectricEquipment>(m_errorStream, m_db, number, nominalElectricEquipmentData));
2179 1344 : }
2180 31 : void SQLite::addNominalGasEquipmentData(int const number, DataHeatBalance::ZoneEquipData const &nominalGasEquipmentData)
2181 : {
2182 31 : nominalGasEquipments.push_back(std::make_unique<NominalGasEquipment>(m_errorStream, m_db, number, nominalGasEquipmentData));
2183 31 : }
2184 0 : void SQLite::addNominalSteamEquipmentData(int const number, DataHeatBalance::ZoneEquipData const &nominalSteamEquipmentData)
2185 : {
2186 0 : nominalSteamEquipments.push_back(std::make_unique<NominalSteamEquipment>(m_errorStream, m_db, number, nominalSteamEquipmentData));
2187 0 : }
2188 9 : void SQLite::addNominalHotWaterEquipmentData(int const number, DataHeatBalance::ZoneEquipData const &nominalHotWaterEquipmentData)
2189 : {
2190 9 : nominalHotWaterEquipments.push_back(std::make_unique<NominalHotWaterEquipment>(m_errorStream, m_db, number, nominalHotWaterEquipmentData));
2191 9 : }
2192 19 : void SQLite::addNominalOtherEquipmentData(int const number, DataHeatBalance::ZoneEquipData const &nominalOtherEquipmentData)
2193 : {
2194 19 : nominalOtherEquipments.push_back(std::make_unique<NominalOtherEquipment>(m_errorStream, m_db, number, nominalOtherEquipmentData));
2195 19 : }
2196 0 : void SQLite::addNominalBaseboardData(int const number, DataHeatBalance::BBHeatData const &nominalBaseboardData)
2197 : {
2198 0 : nominalBaseboardHeats.push_back(std::make_unique<NominalBaseboardHeat>(m_errorStream, m_db, number, nominalBaseboardData));
2199 0 : }
2200 1175 : void SQLite::addInfiltrationData(int const number, DataHeatBalance::InfiltrationData const &infiltrationData)
2201 : {
2202 1175 : infiltrations.push_back(std::make_unique<Infiltration>(m_errorStream, m_db, number, infiltrationData));
2203 1175 : }
2204 8 : void SQLite::addVentilationData(int const number, DataHeatBalance::VentilationData const &ventilationData)
2205 : {
2206 8 : ventilations.push_back(std::make_unique<Ventilation>(m_errorStream, m_db, number, ventilationData));
2207 8 : }
2208 1506 : void SQLite::addRoomAirModelData(int const number, RoomAir::AirModelData const &roomAirModelData)
2209 : {
2210 1506 : roomAirModels.push_back(std::make_unique<RoomAirModel>(m_errorStream, m_db, number, roomAirModelData));
2211 1506 : }
2212 :
2213 3 : bool SQLite::ZoneGroup::insertIntoSQLite(sqlite3_stmt *insertStmt)
2214 : {
2215 3 : sqliteBindInteger(insertStmt, 1, number);
2216 3 : sqliteBindText(insertStmt, 2, name);
2217 3 : sqliteBindForeignKey(insertStmt, 3, zoneList);
2218 3 : sqliteBindInteger(insertStmt, 4, multiplier);
2219 :
2220 3 : int rc = sqliteStepCommand(insertStmt);
2221 3 : bool validInsert = sqliteStepValidity(rc);
2222 3 : sqliteResetCommand(insertStmt);
2223 3 : return validInsert;
2224 : }
2225 1892 : bool SQLite::Material::insertIntoSQLite(sqlite3_stmt *insertStmt)
2226 : {
2227 1892 : sqliteBindInteger(insertStmt, 1, number);
2228 1892 : sqliteBindText(insertStmt, 2, name);
2229 1892 : sqliteBindInteger(insertStmt, 3, static_cast<int>(group));
2230 1892 : sqliteBindInteger(insertStmt, 4, static_cast<int>(roughness));
2231 1892 : sqliteBindDouble(insertStmt, 5, conductivity);
2232 1892 : sqliteBindDouble(insertStmt, 6, density);
2233 1892 : sqliteBindDouble(insertStmt, 7, isoMoistCap);
2234 1892 : sqliteBindDouble(insertStmt, 8, porosity);
2235 1892 : sqliteBindDouble(insertStmt, 9, resistance);
2236 1892 : sqliteBindLogical(insertStmt, 10, rOnly);
2237 1892 : sqliteBindDouble(insertStmt, 11, specHeat);
2238 1892 : sqliteBindDouble(insertStmt, 12, thermGradCoef);
2239 1892 : sqliteBindDouble(insertStmt, 13, thickness);
2240 1892 : sqliteBindDouble(insertStmt, 14, vaporDiffus);
2241 :
2242 1892 : int rc = sqliteStepCommand(insertStmt);
2243 1892 : bool validInsert = sqliteStepValidity(rc);
2244 1892 : sqliteResetCommand(insertStmt);
2245 1892 : return validInsert;
2246 : }
2247 898 : bool SQLite::Construction::insertIntoSQLite(sqlite3_stmt *insertStmt)
2248 : {
2249 898 : sqliteBindInteger(insertStmt, 1, number);
2250 898 : sqliteBindText(insertStmt, 2, name);
2251 898 : sqliteBindInteger(insertStmt, 3, totLayers);
2252 898 : sqliteBindInteger(insertStmt, 4, totSolidLayers);
2253 898 : sqliteBindInteger(insertStmt, 5, totGlassLayers);
2254 898 : sqliteBindDouble(insertStmt, 6, insideAbsorpVis);
2255 898 : sqliteBindDouble(insertStmt, 7, outsideAbsorpVis);
2256 898 : sqliteBindDouble(insertStmt, 8, insideAbsorpSolar);
2257 898 : sqliteBindDouble(insertStmt, 9, outsideAbsorpSolar);
2258 898 : sqliteBindDouble(insertStmt, 10, insideAbsorpThermal);
2259 898 : sqliteBindDouble(insertStmt, 11, outsideAbsorpThermal);
2260 898 : sqliteBindInteger(insertStmt, 12, static_cast<int>(outsideRoughness));
2261 898 : sqliteBindLogical(insertStmt, 13, typeIsWindow);
2262 898 : sqliteBindDouble(insertStmt, 14, uValue);
2263 :
2264 898 : int rc = sqliteStepCommand(insertStmt);
2265 898 : bool validInsert = sqliteStepValidity(rc);
2266 898 : sqliteResetCommand(insertStmt);
2267 898 : return validInsert;
2268 : }
2269 898 : bool SQLite::Construction::insertIntoSQLite(sqlite3_stmt *insertStmt, sqlite3_stmt *subInsertStmt)
2270 : {
2271 898 : bool constructionInsertValid = insertIntoSQLite(insertStmt);
2272 898 : if (!constructionInsertValid) return false;
2273 :
2274 898 : bool valid = true;
2275 2833 : for (auto const &constructionLayer : constructionLayers) {
2276 1935 : bool validInsert = constructionLayer->insertIntoSQLite(subInsertStmt);
2277 1935 : if (valid && !validInsert) valid = false;
2278 898 : }
2279 898 : return valid;
2280 : }
2281 1935 : bool SQLite::Construction::ConstructionLayer::insertIntoSQLite(sqlite3_stmt *insertStmt)
2282 : {
2283 1935 : sqliteBindForeignKey(insertStmt, 1, constructNumber);
2284 1935 : sqliteBindInteger(insertStmt, 2, layerNumber);
2285 1935 : sqliteBindForeignKey(insertStmt, 3, layerPoint);
2286 :
2287 1935 : int rc = sqliteStepCommand(insertStmt);
2288 1935 : bool validInsert = sqliteStepValidity(rc);
2289 1935 : sqliteResetCommand(insertStmt);
2290 1935 : return validInsert;
2291 : }
2292 1398 : bool SQLite::NominalLighting::insertIntoSQLite(sqlite3_stmt *insertStmt)
2293 : {
2294 1398 : sqliteBindInteger(insertStmt, 1, number);
2295 1398 : sqliteBindText(insertStmt, 2, name);
2296 1398 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2297 1398 : sqliteBindForeignKey(insertStmt, 4, schedulePtr);
2298 1398 : sqliteBindDouble(insertStmt, 5, designLevel);
2299 1398 : sqliteBindDouble(insertStmt, 6, fractionReturnAir);
2300 1398 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2301 1398 : sqliteBindDouble(insertStmt, 8, fractionShortWave);
2302 1398 : sqliteBindDouble(insertStmt, 9, fractionReplaceable);
2303 1398 : sqliteBindDouble(insertStmt, 10, fractionConvected);
2304 1398 : sqliteBindText(insertStmt, 11, endUseSubcategory);
2305 :
2306 1398 : int rc = sqliteStepCommand(insertStmt);
2307 1398 : bool validInsert = sqliteStepValidity(rc);
2308 1398 : sqliteResetCommand(insertStmt);
2309 1398 : return validInsert;
2310 : }
2311 1226 : bool SQLite::NominalPeople::insertIntoSQLite(sqlite3_stmt *insertStmt)
2312 : {
2313 1226 : sqliteBindInteger(insertStmt, 1, number);
2314 1226 : sqliteBindText(insertStmt, 2, name);
2315 1226 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2316 1226 : sqliteBindDouble(insertStmt, 4, numberOfPeople);
2317 1226 : sqliteBindForeignKey(insertStmt, 5, numberOfPeoplePtr);
2318 1226 : sqliteBindForeignKey(insertStmt, 6, activityLevelPtr);
2319 1226 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2320 1226 : sqliteBindDouble(insertStmt, 8, fractionConvected);
2321 1226 : sqliteBindForeignKey(insertStmt, 9, workEffPtr);
2322 1226 : sqliteBindForeignKey(insertStmt, 10, clothingPtr);
2323 1226 : sqliteBindForeignKey(insertStmt, 11, airVelocityPtr);
2324 1226 : sqliteBindLogical(insertStmt, 12, fanger);
2325 1226 : sqliteBindLogical(insertStmt, 13, pierce);
2326 1226 : sqliteBindLogical(insertStmt, 14, ksu);
2327 1226 : sqliteBindInteger(insertStmt, 15, static_cast<int>(mrtCalcType));
2328 1226 : sqliteBindForeignKey(insertStmt, 16, surfacePtr);
2329 1226 : sqliteBindText(insertStmt, 17, angleFactorListName);
2330 1226 : sqliteBindInteger(insertStmt, 18, angleFactorListPtr);
2331 1226 : sqliteBindDouble(insertStmt, 19, userSpecSensFrac);
2332 1226 : sqliteBindLogical(insertStmt, 20, show55Warning);
2333 :
2334 1226 : int rc = sqliteStepCommand(insertStmt);
2335 1226 : bool validInsert = sqliteStepValidity(rc);
2336 1226 : sqliteResetCommand(insertStmt);
2337 1226 : return validInsert;
2338 : }
2339 1344 : bool SQLite::NominalElectricEquipment::insertIntoSQLite(sqlite3_stmt *insertStmt)
2340 : {
2341 1344 : sqliteBindInteger(insertStmt, 1, number);
2342 1344 : sqliteBindText(insertStmt, 2, name);
2343 1344 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2344 1344 : sqliteBindForeignKey(insertStmt, 4, schedulePtr);
2345 1344 : sqliteBindDouble(insertStmt, 5, designLevel);
2346 1344 : sqliteBindDouble(insertStmt, 6, fractionLatent);
2347 1344 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2348 1344 : sqliteBindDouble(insertStmt, 8, fractionLost);
2349 1344 : sqliteBindDouble(insertStmt, 9, fractionConvected);
2350 1344 : sqliteBindText(insertStmt, 10, endUseSubcategory);
2351 :
2352 1344 : int rc = sqliteStepCommand(insertStmt);
2353 1344 : bool validInsert = sqliteStepValidity(rc);
2354 1344 : sqliteResetCommand(insertStmt);
2355 1344 : return validInsert;
2356 : }
2357 31 : bool SQLite::NominalGasEquipment::insertIntoSQLite(sqlite3_stmt *insertStmt)
2358 : {
2359 31 : sqliteBindInteger(insertStmt, 1, number);
2360 31 : sqliteBindText(insertStmt, 2, name);
2361 31 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2362 31 : sqliteBindForeignKey(insertStmt, 4, schedulePtr);
2363 31 : sqliteBindDouble(insertStmt, 5, designLevel);
2364 31 : sqliteBindDouble(insertStmt, 6, fractionLatent);
2365 31 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2366 31 : sqliteBindDouble(insertStmt, 8, fractionLost);
2367 31 : sqliteBindDouble(insertStmt, 9, fractionConvected);
2368 31 : sqliteBindText(insertStmt, 10, endUseSubcategory);
2369 :
2370 31 : int rc = sqliteStepCommand(insertStmt);
2371 31 : bool validInsert = sqliteStepValidity(rc);
2372 31 : sqliteResetCommand(insertStmt);
2373 31 : return validInsert;
2374 : }
2375 0 : bool SQLite::NominalSteamEquipment::insertIntoSQLite(sqlite3_stmt *insertStmt)
2376 : {
2377 0 : sqliteBindInteger(insertStmt, 1, number);
2378 0 : sqliteBindText(insertStmt, 2, name);
2379 0 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2380 0 : sqliteBindForeignKey(insertStmt, 4, schedulePtr);
2381 0 : sqliteBindDouble(insertStmt, 5, designLevel);
2382 0 : sqliteBindDouble(insertStmt, 6, fractionLatent);
2383 0 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2384 0 : sqliteBindDouble(insertStmt, 8, fractionLost);
2385 0 : sqliteBindDouble(insertStmt, 9, fractionConvected);
2386 0 : sqliteBindText(insertStmt, 10, endUseSubcategory);
2387 :
2388 0 : int rc = sqliteStepCommand(insertStmt);
2389 0 : bool validInsert = sqliteStepValidity(rc);
2390 0 : sqliteResetCommand(insertStmt);
2391 0 : return validInsert;
2392 : }
2393 9 : bool SQLite::NominalHotWaterEquipment::insertIntoSQLite(sqlite3_stmt *insertStmt)
2394 : {
2395 9 : sqliteBindInteger(insertStmt, 1, number);
2396 9 : sqliteBindText(insertStmt, 2, name);
2397 9 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2398 9 : sqliteBindForeignKey(insertStmt, 4, schedulePtr);
2399 9 : sqliteBindDouble(insertStmt, 5, designLevel);
2400 9 : sqliteBindDouble(insertStmt, 6, fractionLatent);
2401 9 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2402 9 : sqliteBindDouble(insertStmt, 8, fractionLost);
2403 9 : sqliteBindDouble(insertStmt, 9, fractionConvected);
2404 9 : sqliteBindText(insertStmt, 10, endUseSubcategory);
2405 :
2406 9 : int rc = sqliteStepCommand(insertStmt);
2407 9 : bool validInsert = sqliteStepValidity(rc);
2408 9 : sqliteResetCommand(insertStmt);
2409 9 : return validInsert;
2410 : }
2411 19 : bool SQLite::NominalOtherEquipment::insertIntoSQLite(sqlite3_stmt *insertStmt)
2412 : {
2413 19 : sqliteBindInteger(insertStmt, 1, number);
2414 19 : sqliteBindText(insertStmt, 2, name);
2415 19 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2416 19 : sqliteBindForeignKey(insertStmt, 4, schedulePtr);
2417 19 : sqliteBindDouble(insertStmt, 5, designLevel);
2418 19 : sqliteBindDouble(insertStmt, 6, fractionLatent);
2419 19 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2420 19 : sqliteBindDouble(insertStmt, 8, fractionLost);
2421 19 : sqliteBindDouble(insertStmt, 9, fractionConvected);
2422 19 : sqliteBindText(insertStmt, 10, endUseSubcategory);
2423 :
2424 19 : int rc = sqliteStepCommand(insertStmt);
2425 19 : bool validInsert = sqliteStepValidity(rc);
2426 19 : sqliteResetCommand(insertStmt);
2427 19 : return validInsert;
2428 : }
2429 0 : bool SQLite::NominalBaseboardHeat::insertIntoSQLite(sqlite3_stmt *insertStmt)
2430 : {
2431 0 : sqliteBindInteger(insertStmt, 1, number);
2432 0 : sqliteBindText(insertStmt, 2, name);
2433 0 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2434 0 : sqliteBindForeignKey(insertStmt, 4, schedPtr);
2435 0 : sqliteBindDouble(insertStmt, 5, capatLowTemperature);
2436 0 : sqliteBindDouble(insertStmt, 6, lowTemperature);
2437 0 : sqliteBindDouble(insertStmt, 7, capatHighTemperature);
2438 0 : sqliteBindDouble(insertStmt, 8, highTemperature);
2439 0 : sqliteBindDouble(insertStmt, 9, fractionRadiant);
2440 0 : sqliteBindDouble(insertStmt, 10, fractionConvected);
2441 0 : sqliteBindText(insertStmt, 11, endUseSubcategory);
2442 :
2443 0 : int rc = sqliteStepCommand(insertStmt);
2444 0 : bool validInsert = sqliteStepValidity(rc);
2445 0 : sqliteResetCommand(insertStmt);
2446 0 : return validInsert;
2447 : }
2448 1175 : bool SQLite::Infiltration::insertIntoSQLite(sqlite3_stmt *insertStmt)
2449 : {
2450 1175 : sqliteBindInteger(insertStmt, 1, number);
2451 1175 : sqliteBindText(insertStmt, 2, name);
2452 1175 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2453 1175 : sqliteBindForeignKey(insertStmt, 4, schedPtr);
2454 1175 : sqliteBindDouble(insertStmt, 5, designLevel);
2455 :
2456 1175 : int rc = sqliteStepCommand(insertStmt);
2457 1175 : bool validInsert = sqliteStepValidity(rc);
2458 1175 : sqliteResetCommand(insertStmt);
2459 1175 : return validInsert;
2460 : }
2461 8 : bool SQLite::Ventilation::insertIntoSQLite(sqlite3_stmt *insertStmt)
2462 : {
2463 8 : sqliteBindInteger(insertStmt, 1, number);
2464 8 : sqliteBindText(insertStmt, 2, name);
2465 8 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2466 8 : sqliteBindForeignKey(insertStmt, 4, schedPtr);
2467 8 : sqliteBindDouble(insertStmt, 5, designLevel);
2468 :
2469 8 : int rc = sqliteStepCommand(insertStmt);
2470 8 : bool validInsert = sqliteStepValidity(rc);
2471 8 : sqliteResetCommand(insertStmt);
2472 8 : return validInsert;
2473 : }
2474 1506 : bool SQLite::RoomAirModel::insertIntoSQLite(sqlite3_stmt *insertStmt)
2475 : {
2476 1506 : sqliteBindInteger(insertStmt, 1, number);
2477 1506 : sqliteBindText(insertStmt, 2, airModelName);
2478 1506 : sqliteBindInteger(insertStmt, 3, static_cast<int>(airModel));
2479 1506 : sqliteBindInteger(insertStmt, 4, static_cast<int>(tempCoupleScheme));
2480 1506 : sqliteBindLogical(insertStmt, 5, simAirModel);
2481 :
2482 1506 : int rc = sqliteStepCommand(insertStmt);
2483 1506 : bool validInsert = sqliteStepValidity(rc);
2484 1506 : sqliteResetCommand(insertStmt);
2485 1506 : return validInsert;
2486 : }
2487 :
2488 13621 : bool SQLite::Surface::insertIntoSQLite(sqlite3_stmt *insertStmt)
2489 : {
2490 13621 : sqliteBindInteger(insertStmt, 1, number);
2491 13621 : sqliteBindText(insertStmt, 2, name);
2492 13621 : sqliteBindForeignKey(insertStmt, 3, construction);
2493 13621 : sqliteBindText(insertStmt, 4, surfaceClass);
2494 13621 : sqliteBindDouble(insertStmt, 5, area);
2495 13621 : sqliteBindDouble(insertStmt, 6, grossArea);
2496 13621 : sqliteBindDouble(insertStmt, 7, perimeter);
2497 13621 : sqliteBindDouble(insertStmt, 8, azimuth);
2498 13621 : sqliteBindDouble(insertStmt, 9, height);
2499 13621 : sqliteBindDouble(insertStmt, 10, reveal);
2500 13621 : sqliteBindInteger(insertStmt, 11, static_cast<int>(shape));
2501 13621 : sqliteBindInteger(insertStmt, 12, sides);
2502 13621 : sqliteBindDouble(insertStmt, 13, tilt);
2503 13621 : sqliteBindDouble(insertStmt, 14, width);
2504 13621 : sqliteBindLogical(insertStmt, 15, heatTransSurf);
2505 13621 : sqliteBindForeignKey(insertStmt, 16, baseSurf);
2506 13621 : sqliteBindForeignKey(insertStmt, 17, zone);
2507 13621 : sqliteBindInteger(insertStmt, 18, extBoundCond);
2508 13621 : sqliteBindLogical(insertStmt, 19, extSolar);
2509 13621 : sqliteBindLogical(insertStmt, 20, extWind);
2510 :
2511 13621 : int rc = sqliteStepCommand(insertStmt);
2512 13621 : bool validInsert = sqliteStepValidity(rc);
2513 13621 : sqliteResetCommand(insertStmt);
2514 13621 : return validInsert;
2515 : }
2516 :
2517 12 : bool SQLite::ZoneList::insertIntoSQLite(sqlite3_stmt *insertStmt)
2518 : {
2519 12 : sqliteBindInteger(insertStmt, 1, number);
2520 12 : sqliteBindText(insertStmt, 2, name);
2521 :
2522 12 : int rc = sqliteStepCommand(insertStmt);
2523 12 : bool validInsert = sqliteStepValidity(rc);
2524 12 : sqliteResetCommand(insertStmt);
2525 12 : return validInsert;
2526 : }
2527 :
2528 12 : bool SQLite::ZoneList::insertIntoSQLite(sqlite3_stmt *insertStmt, sqlite3_stmt *subInsertStmt)
2529 : {
2530 12 : bool zoneListInsertValid = insertIntoSQLite(insertStmt);
2531 12 : if (!zoneListInsertValid) return false;
2532 12 : bool valid = true;
2533 176 : for (size_t i = 1; i <= zones.size(); ++i) {
2534 164 : sqliteBindForeignKey(subInsertStmt, 1, number);
2535 164 : sqliteBindForeignKey(subInsertStmt, 2, zones(i));
2536 164 : int rc = sqliteStepCommand(subInsertStmt);
2537 164 : bool validInsert = sqliteStepValidity(rc);
2538 164 : sqliteResetCommand(subInsertStmt);
2539 164 : if (valid && !validInsert) valid = false;
2540 : }
2541 12 : return valid;
2542 : }
2543 :
2544 3336 : bool SQLite::Schedule::insertIntoSQLite(sqlite3_stmt *insertStmt)
2545 : {
2546 3336 : sqliteBindInteger(insertStmt, 1, number);
2547 3336 : sqliteBindText(insertStmt, 2, name);
2548 3336 : sqliteBindText(insertStmt, 3, type);
2549 3336 : sqliteBindDouble(insertStmt, 4, minValue);
2550 3336 : sqliteBindDouble(insertStmt, 5, maxValue);
2551 :
2552 3336 : int rc = sqliteStepCommand(insertStmt);
2553 3336 : bool validInsert = sqliteStepValidity(rc);
2554 3336 : sqliteResetCommand(insertStmt);
2555 3336 : return validInsert;
2556 : }
2557 :
2558 1506 : bool SQLite::Zone::insertIntoSQLite(sqlite3_stmt *insertStmt)
2559 : {
2560 1506 : sqliteBindInteger(insertStmt, 1, number);
2561 1506 : sqliteBindText(insertStmt, 2, name);
2562 1506 : sqliteBindDouble(insertStmt, 3, relNorth);
2563 1506 : sqliteBindDouble(insertStmt, 4, originX);
2564 1506 : sqliteBindDouble(insertStmt, 5, originY);
2565 1506 : sqliteBindDouble(insertStmt, 6, originZ);
2566 1506 : sqliteBindDouble(insertStmt, 7, centroidX);
2567 1506 : sqliteBindDouble(insertStmt, 8, centroidY);
2568 1506 : sqliteBindDouble(insertStmt, 9, centroidZ);
2569 1506 : sqliteBindInteger(insertStmt, 10, ofType);
2570 1506 : sqliteBindInteger(insertStmt, 11, multiplier);
2571 1506 : sqliteBindInteger(insertStmt, 12, listMultiplier);
2572 1506 : sqliteBindDouble(insertStmt, 13, minimumX);
2573 1506 : sqliteBindDouble(insertStmt, 14, maximumX);
2574 1506 : sqliteBindDouble(insertStmt, 15, minimumY);
2575 1506 : sqliteBindDouble(insertStmt, 16, maximumY);
2576 1506 : sqliteBindDouble(insertStmt, 17, minimumZ);
2577 1506 : sqliteBindDouble(insertStmt, 18, maximumZ);
2578 1506 : sqliteBindDouble(insertStmt, 19, ceilingHeight);
2579 1506 : sqliteBindDouble(insertStmt, 20, volume);
2580 1506 : sqliteBindInteger(insertStmt, 21, Convect::HcIntReportVals[static_cast<int>(insideConvectionAlgo)]);
2581 1506 : sqliteBindInteger(insertStmt, 22, Convect::HcExtReportVals[static_cast<int>(outsideConvectionAlgo)]);
2582 1506 : sqliteBindDouble(insertStmt, 23, floorArea);
2583 1506 : sqliteBindDouble(insertStmt, 24, extGrossWallArea);
2584 1506 : sqliteBindDouble(insertStmt, 25, extNetWallArea);
2585 1506 : sqliteBindDouble(insertStmt, 26, extWindowArea);
2586 1506 : sqliteBindLogical(insertStmt, 27, isPartOfTotalArea);
2587 :
2588 1506 : int rc = sqliteStepCommand(insertStmt);
2589 1506 : bool validInsert = sqliteStepValidity(rc);
2590 1506 : sqliteResetCommand(insertStmt);
2591 1506 : return validInsert;
2592 : }
2593 :
2594 29919 : SQLite::SQLiteData::SQLiteData(std::shared_ptr<std::ostream> const &errorStream, std::shared_ptr<sqlite3> const &db)
2595 29919 : : SQLiteProcedures(errorStream, db)
2596 : {
2597 29919 : }
2598 :
2599 29919 : SQLiteProcedures::SQLiteProcedures(std::shared_ptr<std::ostream> const &errorStream, std::shared_ptr<sqlite3> const &db)
2600 29919 : : m_writeOutputToSQLite(true), m_errorStream(errorStream), m_db(db)
2601 : {
2602 29919 : }
2603 :
2604 123 : SQLiteProcedures::SQLiteProcedures(std::shared_ptr<std::ostream> const &errorStream,
2605 : bool writeOutputToSQLite,
2606 : fs::path const &dbName,
2607 123 : fs::path const &errorFilePath)
2608 123 : : m_writeOutputToSQLite(writeOutputToSQLite), m_errorStream(errorStream)
2609 : {
2610 123 : sqlite3 *m_connection = nullptr;
2611 123 : if (m_writeOutputToSQLite) {
2612 123 : int rc = -1;
2613 123 : bool ok = true;
2614 :
2615 123 : std::string const dbName_utf8 = FileSystem::toGenericString(dbName);
2616 :
2617 : // Test if we can write to the sqlite error file
2618 : // Does there need to be a separate sqlite.err file at all? Consider using eplusout.err
2619 123 : if (m_errorStream) {
2620 123 : *m_errorStream << "SQLite3 message, " << FileSystem::toGenericString(errorFilePath) << " open for processing!" << std::endl;
2621 : } else {
2622 0 : ok = false;
2623 : }
2624 :
2625 : // Test if we can create a new file named dbName
2626 123 : if (ok && dbName != ":memory:") {
2627 123 : std::ofstream test(dbName, std::ofstream::out | std::ofstream::trunc);
2628 123 : if (test.is_open()) {
2629 123 : test.close();
2630 : } else {
2631 0 : ok = false;
2632 : }
2633 123 : }
2634 :
2635 : // Test if we can write to the database
2636 : // If we can't then there are probably locks on the database
2637 123 : if (ok) {
2638 : // sqlite3_open_v2 could return SQLITE_BUSY at this point. If so, do not proceed to sqlite3_exec.
2639 123 : rc = sqlite3_open_v2(dbName_utf8.c_str(), &m_connection, SQLITE_OPEN_READWRITE, nullptr);
2640 123 : if (rc) {
2641 0 : *m_errorStream << "SQLite3 message, can't get exclusive lock to open database: " << sqlite3_errmsg(m_connection) << std::endl;
2642 0 : ok = false;
2643 : }
2644 : }
2645 :
2646 123 : if (ok) {
2647 123 : char *zErrMsg = nullptr;
2648 : // Set journal_mode OFF to avoid creating the file dbName + "-journal" (when dbName is a regular file)
2649 123 : rc = sqlite3_exec(m_connection, "PRAGMA journal_mode = OFF;", nullptr, 0, &zErrMsg);
2650 123 : if (!rc) {
2651 123 : rc = sqlite3_exec(m_connection, "CREATE TABLE Test(x INTEGER PRIMARY KEY)", nullptr, 0, &zErrMsg);
2652 : }
2653 123 : sqlite3_close(m_connection);
2654 123 : if (rc) {
2655 0 : *m_errorStream << "SQLite3 message, can't get exclusive lock to edit database: " << zErrMsg << std::endl;
2656 0 : ok = false;
2657 : } else {
2658 123 : if (dbName != ":memory:") {
2659 : // Remove test db
2660 : // rc = remove(dbName_utf8.c_str());
2661 123 : if (fs::is_regular_file(dbName)) {
2662 123 : std::error_code ec;
2663 123 : if (!fs::remove(dbName, ec)) {
2664 : // File operation failed. SQLite connection is not in an error state.
2665 0 : *m_errorStream << "SQLite3 message, can't remove old database. code=" << ec.value() << ", error: " << ec.message()
2666 0 : << std::endl;
2667 0 : ok = false;
2668 : }
2669 : }
2670 : }
2671 : }
2672 123 : sqlite3_free(zErrMsg);
2673 : }
2674 :
2675 123 : if (ok) {
2676 : // Now open the output db for the duration of the simulation
2677 123 : rc = sqlite3_open_v2(dbName_utf8.c_str(), &m_connection, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);
2678 123 : m_db = std::shared_ptr<sqlite3>(m_connection, sqlite3_close);
2679 123 : if (rc) {
2680 0 : *m_errorStream << "SQLite3 message, can't open new database: " << sqlite3_errmsg(m_connection) << std::endl;
2681 0 : ok = false;
2682 : }
2683 : }
2684 :
2685 123 : if (!ok) {
2686 0 : throw std::runtime_error("The SQLite database failed to open.");
2687 : }
2688 123 : }
2689 123 : }
2690 :
2691 11183 : int SQLiteProcedures::sqliteExecuteCommand(std::string_view commandBuffer)
2692 : {
2693 11183 : char *zErrMsg = 0;
2694 :
2695 11183 : int rc = sqlite3_exec(m_db.get(), commandBuffer.data(), NULL, 0, &zErrMsg);
2696 11183 : if (rc != SQLITE_OK) {
2697 0 : *m_errorStream << zErrMsg;
2698 : }
2699 11183 : sqlite3_free(zErrMsg);
2700 :
2701 11183 : return rc;
2702 : }
2703 :
2704 4497 : int SQLiteProcedures::sqlitePrepareStatement(sqlite3_stmt *&stmt, std::string_view stmtBuffer)
2705 : {
2706 4497 : int rc = sqlite3_prepare_v2(m_db.get(), stmtBuffer.data(), stmtBuffer.size(), &stmt, nullptr);
2707 4497 : if (rc != SQLITE_OK) {
2708 0 : *m_errorStream << "SQLite3 message, sqlite3_prepare_v2 message:\n"
2709 : << stmtBuffer << "\n"
2710 : << sqlite3_errstr(rc) << "\n"
2711 0 : << sqlite3_errmsg(m_db.get()) << std::endl;
2712 : }
2713 :
2714 4497 : return rc;
2715 : }
2716 :
2717 748499 : int SQLiteProcedures::sqliteBindText(sqlite3_stmt *stmt, const int stmtInsertLocationIndex, std::string_view textBuffer)
2718 : {
2719 748499 : int rc = sqlite3_bind_text(stmt, stmtInsertLocationIndex, textBuffer.data(), textBuffer.size(), SQLITE_TRANSIENT);
2720 748499 : if (rc != SQLITE_OK) {
2721 0 : *m_errorStream << "SQLite3 message, sqlite3_bind_text failed:\n"
2722 : << textBuffer << "\n"
2723 : << sqlite3_errstr(rc) << "\n"
2724 0 : << sqlite3_errmsg(m_db.get()) << std::endl;
2725 : }
2726 :
2727 748499 : return rc;
2728 : }
2729 :
2730 3939611 : int SQLiteProcedures::sqliteBindInteger(sqlite3_stmt *stmt, const int stmtInsertLocationIndex, const int intToInsert)
2731 : {
2732 3939611 : int rc = sqlite3_bind_int(stmt, stmtInsertLocationIndex, intToInsert);
2733 3939611 : if (rc != SQLITE_OK) {
2734 0 : *m_errorStream << "SQLite3 message, sqlite3_bind_int failed: " << intToInsert << std::endl;
2735 : }
2736 :
2737 3939611 : return rc;
2738 : }
2739 :
2740 2396464 : int SQLiteProcedures::sqliteBindDouble(sqlite3_stmt *stmt, const int stmtInsertLocationIndex, const double doubleToInsert)
2741 : {
2742 2396464 : int rc = sqlite3_bind_double(stmt, stmtInsertLocationIndex, doubleToInsert);
2743 2396464 : if (rc != SQLITE_OK) {
2744 0 : *m_errorStream << "SQLite3 message, sqlite3_bind_double failed: " << doubleToInsert << std::endl;
2745 : }
2746 :
2747 2396464 : return rc;
2748 : }
2749 :
2750 19367 : int SQLiteProcedures::sqliteBindNULL(sqlite3_stmt *stmt, const int stmtInsertLocationIndex)
2751 : {
2752 19367 : int rc = sqlite3_bind_null(stmt, stmtInsertLocationIndex);
2753 19367 : if (rc != SQLITE_OK) {
2754 0 : *m_errorStream << "SQLite3 message, sqlite3_bind_null failed" << std::endl;
2755 : }
2756 :
2757 19367 : return rc;
2758 : }
2759 :
2760 7184175 : int SQLiteProcedures::sqliteBindForeignKey(sqlite3_stmt *stmt, const int stmtInsertLocationIndex, const int intToInsert)
2761 : {
2762 7184175 : int rc = -1;
2763 7184175 : if (intToInsert > 0) {
2764 7181873 : rc = sqlite3_bind_int(stmt, stmtInsertLocationIndex, intToInsert);
2765 : } else {
2766 2302 : rc = sqlite3_bind_null(stmt, stmtInsertLocationIndex);
2767 : }
2768 7184175 : if (rc != SQLITE_OK) {
2769 0 : *m_errorStream << "SQLite3 message, sqliteBindForeignKey failed: " << intToInsert << std::endl;
2770 : }
2771 :
2772 7184175 : return rc;
2773 : }
2774 :
2775 85333 : int SQLiteProcedures::sqliteBindLogical(sqlite3_stmt *stmt, const int stmtInsertLocationIndex, const bool valueToInsert)
2776 : {
2777 85333 : return sqliteBindInteger(stmt, stmtInsertLocationIndex, valueToInsert ? 1 : 0);
2778 : }
2779 :
2780 30083 : bool SQLiteProcedures::sqliteStepValidity(int const rc)
2781 : {
2782 30083 : bool isValid = false;
2783 30083 : switch (rc) {
2784 30083 : case SQLITE_DONE:
2785 : case SQLITE_OK:
2786 : case SQLITE_ROW:
2787 30083 : isValid = true;
2788 30083 : break;
2789 0 : default:
2790 0 : break;
2791 : }
2792 30083 : return isValid;
2793 : }
2794 :
2795 2714829 : int SQLiteProcedures::sqliteStepCommand(sqlite3_stmt *stmt)
2796 : {
2797 2714829 : int rc = sqlite3_step(stmt);
2798 2714829 : switch (rc) {
2799 2714829 : case SQLITE_DONE:
2800 : case SQLITE_OK:
2801 : case SQLITE_ROW:
2802 2714829 : break;
2803 0 : default:
2804 0 : *m_errorStream << "SQLite3 message, sqlite3_step message: " << sqlite3_errmsg(m_db.get()) << std::endl;
2805 0 : break;
2806 : }
2807 :
2808 2714829 : return rc;
2809 : }
2810 :
2811 2714829 : int SQLiteProcedures::sqliteResetCommand(sqlite3_stmt *stmt)
2812 : {
2813 2714829 : return sqlite3_reset(stmt);
2814 : }
2815 :
2816 0 : bool SQLiteProcedures::sqliteWithinTransaction()
2817 : {
2818 0 : return (sqlite3_get_autocommit(m_db.get()) == 0);
2819 : }
2820 :
2821 : // int SQLiteProcedures::sqliteClearBindings(sqlite3_stmt * stmt)
2822 : // {
2823 : // return sqlite3_clear_bindings(stmt);
2824 : // }
2825 :
2826 : // int SQLiteProcedures::sqliteFinalizeCommand(sqlite3_stmt * stmt)
2827 : // {
2828 : // return sqlite3_finalize(stmt);
2829 : // }
2830 :
2831 : } // namespace EnergyPlus
|