Line data Source code
1 : // EnergyPlus, Copyright (c) 1996-2025, 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 799 : bool ParseSQLiteInput(EnergyPlusData &state, bool &writeOutputToSQLite, bool &writeTabularDataToSQLite)
91 : {
92 799 : auto &ip = state.dataInputProcessing->inputProcessor;
93 1598 : auto const instances = ip->epJSON.find("Output:SQLite");
94 799 : if (instances != ip->epJSON.end()) {
95 :
96 250 : auto find_input = [=, &state](nlohmann::json const &fields, std::string const &field_name) -> std::string {
97 250 : std::string input;
98 250 : auto found = fields.find(field_name);
99 250 : if (found != fields.end()) {
100 125 : input = found.value().get<std::string>();
101 : } else {
102 375 : state.dataInputProcessing->inputProcessor->getDefaultValue(state, "Output:SQLite", field_name, input);
103 : }
104 500 : return input;
105 250 : };
106 :
107 : // There can only be 1 "Output:SQLite"
108 125 : auto const instance = instances.value().begin();
109 125 : auto const &fields = instance.value();
110 375 : ip->markObjectAsUsed("Output:SQLite", instance.key());
111 :
112 : { // "option_type"
113 125 : std::string outputType = find_input(fields, "option_type");
114 125 : if ("SimpleAndTabular" == outputType) {
115 24 : writeTabularDataToSQLite = true;
116 24 : writeOutputToSQLite = true;
117 101 : } else if ("Simple" == outputType) {
118 101 : writeTabularDataToSQLite = false;
119 101 : writeOutputToSQLite = true;
120 : }
121 125 : }
122 : { // "unit_conversion_for_tabular_data"
123 125 : std::string tabularDataUnitConversion = find_input(fields, "unit_conversion_for_tabular_data");
124 125 : auto const &sql_ort = state.dataOutRptTab;
125 :
126 125 : 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 125 : } else if ("None" == tabularDataUnitConversion) {
132 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::None;
133 125 : } else if ("JtoKWH" == tabularDataUnitConversion) {
134 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::JtoKWH;
135 125 : } else if ("JtoMJ" == tabularDataUnitConversion) {
136 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::JtoMJ;
137 125 : } else if ("JtoGJ" == tabularDataUnitConversion) {
138 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::JtoGJ;
139 125 : } else if ("InchPound" == tabularDataUnitConversion) {
140 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::InchPound;
141 125 : } else if ("InchPoundExceptElectricity" == tabularDataUnitConversion) {
142 0 : sql_ort->unitsStyle_SQLite = OutputReportTabular::UnitsStyle::InchPoundExceptElectricity;
143 : }
144 125 : }
145 125 : return true;
146 125 : }
147 674 : return false;
148 799 : }
149 :
150 801 : std::unique_ptr<SQLite> CreateSQLiteDatabase(EnergyPlusData &state)
151 : {
152 801 : if (!state.files.outputControl.sqlite) {
153 2 : return nullptr;
154 : }
155 : try {
156 799 : bool writeOutputToSQLite = false;
157 799 : bool writeTabularDataToSQLite = false;
158 799 : bool parsedSQLite = ParseSQLiteInput(state, writeOutputToSQLite, writeTabularDataToSQLite);
159 799 : if (!parsedSQLite) {
160 674 : state.files.outputControl.sqlite = false;
161 674 : return nullptr;
162 : }
163 125 : auto errorStream = std::make_shared<std::ofstream>(state.dataStrGlobals->outputSqliteErrFilePath, std::ofstream::out | std::ofstream::trunc);
164 : return std::make_unique<SQLite>(errorStream,
165 250 : state.dataStrGlobals->outputSqlFilePath,
166 125 : state.dataStrGlobals->outputSqliteErrFilePath,
167 : writeOutputToSQLite,
168 125 : writeTabularDataToSQLite);
169 125 : } catch (const std::runtime_error &error) {
170 0 : ShowFatalError(state, error.what());
171 0 : return nullptr;
172 0 : }
173 : }
174 :
175 799 : void CreateSQLiteZoneExtendedOutput(EnergyPlusData &state)
176 : {
177 799 : if (state.dataSQLiteProcedures->sqlite && state.dataSQLiteProcedures->sqlite->writeOutputToSQLite()) {
178 1637 : for (int zoneNum = 1; zoneNum <= state.dataGlobal->NumOfZones; ++zoneNum) {
179 1512 : state.dataSQLiteProcedures->sqlite->addZoneData(zoneNum, state.dataHeatBal->Zone(zoneNum));
180 : }
181 137 : for (int listNum = 1; listNum <= state.dataHeatBal->NumOfZoneLists; ++listNum) {
182 12 : state.dataSQLiteProcedures->sqlite->addZoneListData(listNum, state.dataHeatBal->ZoneList(listNum));
183 : }
184 128 : for (int groupNum = 1; groupNum <= state.dataHeatBal->NumOfZoneGroups; ++groupNum) {
185 3 : state.dataSQLiteProcedures->sqlite->addZoneGroupData(groupNum, state.dataHeatBal->ZoneGroup(groupNum));
186 : }
187 3784 : for (auto *sched : state.dataSched->schedules) {
188 7318 : state.dataSQLiteProcedures->sqlite->addScheduleData(
189 : sched->Num,
190 : sched->Name,
191 7824 : (sched->schedTypeNum == -1) ? "" : state.dataSched->scheduleTypes[sched->schedTypeNum]->Name,
192 : sched->getMinVal(state),
193 : sched->getMaxVal(state));
194 125 : }
195 13802 : for (int surfaceNumber = 1; surfaceNumber <= state.dataSurface->TotSurfaces; ++surfaceNumber) {
196 13677 : auto const &surface = state.dataSurface->Surface(surfaceNumber);
197 13677 : state.dataSQLiteProcedures->sqlite->addSurfaceData(surfaceNumber, surface, DataSurfaces::cSurfaceClass(surface.Class));
198 : }
199 2064 : for (int materialNum = 1; materialNum <= state.dataMaterial->materials.isize(); ++materialNum) {
200 1939 : state.dataSQLiteProcedures->sqlite->addMaterialData(materialNum, state.dataMaterial->materials(materialNum));
201 : }
202 1044 : for (int constructNum = 1; constructNum <= state.dataHeatBal->TotConstructs; ++constructNum) {
203 919 : auto const &construction = state.dataConstruction->Construct(constructNum);
204 919 : if (construction.TotGlassLayers == 0) {
205 730 : state.dataSQLiteProcedures->sqlite->addConstructionData(constructNum, construction, construction.UValue);
206 : } else {
207 189 : state.dataSQLiteProcedures->sqlite->addConstructionData(constructNum, construction, state.dataHeatBal->NominalU(constructNum));
208 : }
209 : }
210 1529 : for (int lightNum = 1; lightNum <= state.dataHeatBal->TotLights; ++lightNum) {
211 1404 : state.dataSQLiteProcedures->sqlite->addNominalLightingData(lightNum, state.dataHeatBal->Lights(lightNum));
212 : }
213 1357 : for (int peopleNum = 1; peopleNum <= state.dataHeatBal->TotPeople; ++peopleNum) {
214 1232 : state.dataSQLiteProcedures->sqlite->addNominalPeopleData(peopleNum, state.dataHeatBal->People(peopleNum));
215 : }
216 1484 : for (int elecEquipNum = 1; elecEquipNum <= state.dataHeatBal->TotElecEquip; ++elecEquipNum) {
217 1359 : state.dataSQLiteProcedures->sqlite->addNominalElectricEquipmentData(elecEquipNum, state.dataHeatBal->ZoneElectric(elecEquipNum));
218 : }
219 161 : for (int gasEquipNum = 1; gasEquipNum <= state.dataHeatBal->TotGasEquip; ++gasEquipNum) {
220 36 : state.dataSQLiteProcedures->sqlite->addNominalGasEquipmentData(gasEquipNum, state.dataHeatBal->ZoneGas(gasEquipNum));
221 : }
222 125 : for (int steamEquipNum = 1; steamEquipNum <= state.dataHeatBal->TotStmEquip; ++steamEquipNum) {
223 0 : state.dataSQLiteProcedures->sqlite->addNominalSteamEquipmentData(steamEquipNum, state.dataHeatBal->ZoneSteamEq(steamEquipNum));
224 : }
225 134 : for (int hWEquipNum = 1; hWEquipNum <= state.dataHeatBal->TotHWEquip; ++hWEquipNum) {
226 9 : state.dataSQLiteProcedures->sqlite->addNominalHotWaterEquipmentData(hWEquipNum, state.dataHeatBal->ZoneHWEq(hWEquipNum));
227 : }
228 149 : for (int otherEquipNum = 1; otherEquipNum <= state.dataHeatBal->TotOthEquip; ++otherEquipNum) {
229 24 : state.dataSQLiteProcedures->sqlite->addNominalOtherEquipmentData(otherEquipNum, state.dataHeatBal->ZoneOtherEq(otherEquipNum));
230 : }
231 125 : for (int bBHeatNum = 1; bBHeatNum <= state.dataHeatBal->TotBBHeat; ++bBHeatNum) {
232 0 : state.dataSQLiteProcedures->sqlite->addNominalBaseboardData(bBHeatNum, state.dataHeatBal->ZoneBBHeat(bBHeatNum));
233 : }
234 1308 : for (int infilNum = 1; infilNum <= state.dataHeatBal->TotInfiltration; ++infilNum) {
235 1183 : state.dataSQLiteProcedures->sqlite->addInfiltrationData(infilNum, state.dataHeatBal->Infiltration(infilNum));
236 : }
237 133 : for (int ventNum = 1; ventNum <= state.dataHeatBal->TotVentilation; ++ventNum) {
238 8 : state.dataSQLiteProcedures->sqlite->addVentilationData(ventNum, state.dataHeatBal->Ventilation(ventNum));
239 : }
240 1637 : for (int zoneNum = 1; zoneNum <= state.dataGlobal->NumOfZones; ++zoneNum) {
241 1512 : state.dataSQLiteProcedures->sqlite->addRoomAirModelData(zoneNum, state.dataRoomAir->AirModel(zoneNum));
242 : }
243 :
244 125 : state.dataSQLiteProcedures->sqlite->createZoneExtendedOutput();
245 : }
246 799 : }
247 :
248 125 : SQLite::SQLite(std::shared_ptr<std::ostream> errorStream,
249 : fs::path const &dbName,
250 : fs::path const &errorFilePath,
251 : bool writeOutputToSQLite,
252 125 : bool writeTabularDataToSQLite)
253 125 : : SQLiteProcedures(errorStream, writeOutputToSQLite, dbName, errorFilePath), m_writeTabularDataToSQLite(writeTabularDataToSQLite),
254 250 : m_sqlDBTimeIndex(0), m_reportDataInsertStmt(nullptr), m_reportExtendedDataInsertStmt(nullptr), m_reportDictionaryInsertStmt(nullptr),
255 125 : m_timeIndexInsertStmt(nullptr), m_zoneInfoInsertStmt(nullptr), m_zoneInfoZoneListInsertStmt(nullptr), m_nominalLightingInsertStmt(nullptr),
256 125 : m_nominalElectricEquipmentInsertStmt(nullptr), m_nominalGasEquipmentInsertStmt(nullptr), m_nominalSteamEquipmentInsertStmt(nullptr),
257 125 : m_nominalHotWaterEquipmentInsertStmt(nullptr), m_nominalOtherEquipmentInsertStmt(nullptr), m_nominalBaseboardHeatInsertStmt(nullptr),
258 125 : m_surfaceInsertStmt(nullptr), m_constructionInsertStmt(nullptr), m_constructionLayerInsertStmt(nullptr), m_materialInsertStmt(nullptr),
259 125 : m_zoneListInsertStmt(nullptr), m_zoneGroupInsertStmt(nullptr), m_infiltrationInsertStmt(nullptr), m_ventilationInsertStmt(nullptr),
260 125 : m_nominalPeopleInsertStmt(nullptr), m_zoneSizingInsertStmt(nullptr), m_systemSizingInsertStmt(nullptr), m_componentSizingInsertStmt(nullptr),
261 125 : m_roomAirModelInsertStmt(nullptr), m_groundTemperatureInsertStmt(nullptr), m_weatherFileInsertStmt(nullptr), m_scheduleInsertStmt(nullptr),
262 125 : m_daylightMapTitleInsertStmt(nullptr), m_daylightMapHourlyTitleInsertStmt(nullptr), m_daylightMapHourlyDataInsertStmt(nullptr),
263 125 : m_environmentPeriodInsertStmt(nullptr), m_simulationsInsertStmt(nullptr), m_tabularDataInsertStmt(nullptr), m_stringsInsertStmt(nullptr),
264 125 : m_stringsLookUpStmt(nullptr), m_errorInsertStmt(nullptr), m_errorUpdateStmt(nullptr), m_simulationUpdateStmt(nullptr),
265 125 : m_simulationDataUpdateStmt(nullptr), m_rollbackToSavepointStmt(nullptr), m_createSavepointStmt(nullptr), m_releaseSavepointStmt(nullptr)
266 : {
267 125 : if (m_writeOutputToSQLite) {
268 125 : sqliteExecuteCommand("PRAGMA locking_mode = EXCLUSIVE;");
269 125 : sqliteExecuteCommand("PRAGMA journal_mode = OFF;");
270 125 : sqliteExecuteCommand("PRAGMA synchronous = OFF;");
271 125 : 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 125 : sqliteExecuteCommand("PRAGMA foreign_keys = OFF;");
278 :
279 125 : initializeSimulationsTable();
280 125 : initializeEnvironmentPeriodsTable();
281 125 : initializeErrorsTable();
282 125 : initializeTimeIndicesTable();
283 125 : initializeZoneInfoTable();
284 125 : initializeZoneListTable();
285 125 : initializeZoneGroupTable();
286 125 : initializeZoneInfoZoneListTable();
287 125 : initializeSchedulesTable();
288 125 : initializeMaterialsTable();
289 125 : initializeConstructionsTables();
290 125 : initializeSurfacesTable();
291 125 : initializeReportDataDictionaryTable();
292 125 : initializeReportDataTables();
293 125 : initializeNominalPeopleTable();
294 125 : initializeNominalLightingTable();
295 125 : initializeNominalElectricEquipmentTable();
296 125 : initializeNominalGasEquipmentTable();
297 125 : initializeNominalSteamEquipmentTable();
298 125 : initializeNominalHotWaterEquipmentTable();
299 125 : initializeNominalOtherEquipmentTable();
300 125 : initializeNominalBaseboardHeatTable();
301 125 : initializeNominalInfiltrationTable();
302 125 : initializeNominalVentilationTable();
303 125 : initializeZoneSizingTable();
304 125 : initializeSystemSizingTable();
305 125 : initializeComponentSizingTable();
306 125 : initializeRoomAirModelTable();
307 125 : initializeDaylightMapTables();
308 125 : initializeViews();
309 :
310 125 : if (m_writeTabularDataToSQLite) {
311 24 : initializeTabularDataTable();
312 24 : initializeTabularDataView();
313 : }
314 : }
315 125 : }
316 :
317 250 : SQLite::~SQLite()
318 : {
319 125 : sqlite3_finalize(m_reportDataInsertStmt);
320 125 : sqlite3_finalize(m_reportExtendedDataInsertStmt);
321 125 : sqlite3_finalize(m_reportDictionaryInsertStmt);
322 125 : sqlite3_finalize(m_timeIndexInsertStmt);
323 125 : sqlite3_finalize(m_zoneInfoInsertStmt);
324 125 : sqlite3_finalize(m_zoneListInsertStmt);
325 125 : sqlite3_finalize(m_zoneGroupInsertStmt);
326 125 : sqlite3_finalize(m_zoneInfoZoneListInsertStmt);
327 125 : sqlite3_finalize(m_nominalLightingInsertStmt);
328 125 : sqlite3_finalize(m_nominalElectricEquipmentInsertStmt);
329 125 : sqlite3_finalize(m_nominalGasEquipmentInsertStmt);
330 125 : sqlite3_finalize(m_nominalSteamEquipmentInsertStmt);
331 125 : sqlite3_finalize(m_nominalHotWaterEquipmentInsertStmt);
332 125 : sqlite3_finalize(m_nominalOtherEquipmentInsertStmt);
333 125 : sqlite3_finalize(m_nominalBaseboardHeatInsertStmt);
334 125 : sqlite3_finalize(m_surfaceInsertStmt);
335 125 : sqlite3_finalize(m_constructionInsertStmt);
336 125 : sqlite3_finalize(m_constructionLayerInsertStmt);
337 125 : sqlite3_finalize(m_materialInsertStmt);
338 125 : sqlite3_finalize(m_infiltrationInsertStmt);
339 125 : sqlite3_finalize(m_ventilationInsertStmt);
340 125 : sqlite3_finalize(m_nominalPeopleInsertStmt);
341 125 : sqlite3_finalize(m_zoneSizingInsertStmt);
342 125 : sqlite3_finalize(m_systemSizingInsertStmt);
343 125 : sqlite3_finalize(m_componentSizingInsertStmt);
344 125 : sqlite3_finalize(m_roomAirModelInsertStmt);
345 125 : sqlite3_finalize(m_groundTemperatureInsertStmt);
346 125 : sqlite3_finalize(m_weatherFileInsertStmt);
347 125 : sqlite3_finalize(m_scheduleInsertStmt);
348 125 : sqlite3_finalize(m_daylightMapTitleInsertStmt);
349 125 : sqlite3_finalize(m_daylightMapHourlyTitleInsertStmt);
350 125 : sqlite3_finalize(m_daylightMapHourlyDataInsertStmt);
351 125 : sqlite3_finalize(m_environmentPeriodInsertStmt);
352 125 : sqlite3_finalize(m_simulationsInsertStmt);
353 125 : sqlite3_finalize(m_tabularDataInsertStmt);
354 125 : sqlite3_finalize(m_stringsInsertStmt);
355 125 : sqlite3_finalize(m_stringsLookUpStmt);
356 125 : sqlite3_finalize(m_errorInsertStmt);
357 125 : sqlite3_finalize(m_errorUpdateStmt);
358 125 : sqlite3_finalize(m_simulationUpdateStmt);
359 125 : sqlite3_finalize(m_simulationDataUpdateStmt);
360 125 : sqlite3_finalize(m_rollbackToSavepointStmt);
361 125 : sqlite3_finalize(m_createSavepointStmt);
362 125 : sqlite3_finalize(m_releaseSavepointStmt);
363 250 : }
364 :
365 125 : bool SQLite::writeOutputToSQLite() const
366 : {
367 125 : return m_writeOutputToSQLite;
368 : }
369 :
370 0 : bool SQLite::writeTabularDataToSQLite() const
371 : {
372 0 : return m_writeTabularDataToSQLite;
373 : }
374 :
375 2619 : void SQLite::sqliteBegin()
376 : {
377 2619 : if (m_writeOutputToSQLite) {
378 2619 : sqliteExecuteCommand("BEGIN;");
379 : }
380 2619 : }
381 :
382 2437 : void SQLite::sqliteCommit()
383 : {
384 2437 : if (m_writeOutputToSQLite) {
385 2437 : sqliteExecuteCommand("COMMIT;");
386 : }
387 2437 : }
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 125 : void SQLite::initializeReportDataDictionaryTable()
448 : {
449 125 : 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 125 : sqliteExecuteCommand(newTableSQL);
462 :
463 125 : 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 125 : sqlitePrepareStatement(m_reportDictionaryInsertStmt, preparedSQL);
477 125 : }
478 :
479 125 : void SQLite::initializeReportDataTables()
480 : {
481 125 : 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 125 : sqliteExecuteCommand(reportDataTableSQL);
494 :
495 125 : constexpr std::string_view reportDataInsertSQL = "INSERT INTO ReportData ("
496 : "ReportDataIndex, "
497 : "TimeIndex, "
498 : "ReportDataDictionaryIndex, "
499 : "Value) "
500 : "VALUES(?,?,?,?);";
501 :
502 125 : sqlitePrepareStatement(m_reportDataInsertStmt, reportDataInsertSQL);
503 :
504 125 : 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 125 : sqliteExecuteCommand(reportExtendedDataTableSQL);
524 :
525 125 : 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 125 : sqlitePrepareStatement(m_reportExtendedDataInsertStmt, reportExtendedDataInsertSQL);
543 125 : }
544 :
545 125 : void SQLite::initializeTimeIndicesTable()
546 : {
547 125 : 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 125 : sqliteExecuteCommand(timeTableSQL);
563 :
564 125 : 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 125 : sqlitePrepareStatement(m_timeIndexInsertStmt, timeIndexInsertSQL);
581 125 : }
582 :
583 125 : void SQLite::initializeZoneInfoTable()
584 : {
585 125 : 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 125 : sqliteExecuteCommand(zonesTableSQL);
615 :
616 125 : 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 125 : sqlitePrepareStatement(m_zoneInfoInsertStmt, zoneInfoInsertSQL);
652 125 : }
653 :
654 125 : void SQLite::initializeZoneInfoZoneListTable()
655 : {
656 125 : 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 125 : sqliteExecuteCommand(zoneInfoZoneListTableSQL);
667 :
668 125 : constexpr std::string_view zoneInfoZoneListInsertSQL = "INSERT INTO ZoneInfoZoneLists ("
669 : "ZoneListIndex, "
670 : "ZoneIndex) "
671 : "VALUES (?,?);";
672 :
673 125 : sqlitePrepareStatement(m_zoneInfoZoneListInsertStmt, zoneInfoZoneListInsertSQL);
674 125 : }
675 :
676 125 : void SQLite::initializeNominalPeopleTable()
677 : {
678 125 : 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 125 : sqliteExecuteCommand(nominalPeopleTableSQL);
703 :
704 125 : constexpr std::string_view nominalPeopleInsertSQL = "INSERT INTO NominalPeople VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
705 :
706 125 : sqlitePrepareStatement(m_nominalPeopleInsertStmt, nominalPeopleInsertSQL);
707 125 : }
708 :
709 125 : void SQLite::initializeNominalLightingTable()
710 : {
711 125 : 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 125 : sqliteExecuteCommand(nominalLightingTableSQL);
723 :
724 125 : constexpr std::string_view nominalLightingInsertSQL = "INSERT INTO NominalLighting VALUES(?,?,?,?,?,?,?,?,?,?,?);";
725 :
726 125 : sqlitePrepareStatement(m_nominalLightingInsertStmt, nominalLightingInsertSQL);
727 125 : }
728 :
729 125 : void SQLite::initializeNominalElectricEquipmentTable()
730 : {
731 125 : 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 125 : sqliteExecuteCommand(nominalElectricEquipmentTableSQL);
744 :
745 125 : constexpr std::string_view nominalElectricEquipmentInsertSQL = "INSERT INTO NominalElectricEquipment VALUES(?,?,?,?,?,?,?,?,?,?);";
746 :
747 125 : sqlitePrepareStatement(m_nominalElectricEquipmentInsertStmt, nominalElectricEquipmentInsertSQL);
748 125 : }
749 :
750 125 : void SQLite::initializeNominalGasEquipmentTable()
751 : {
752 125 : 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 125 : sqliteExecuteCommand(nominalGasEquipmentTableSQL);
764 :
765 125 : constexpr std::string_view nominalGasEquipmentInsertSQL = "INSERT INTO NominalGasEquipment VALUES(?,?,?,?,?,?,?,?,?,?);";
766 :
767 125 : sqlitePrepareStatement(m_nominalGasEquipmentInsertStmt, nominalGasEquipmentInsertSQL);
768 125 : }
769 :
770 125 : void SQLite::initializeNominalSteamEquipmentTable()
771 : {
772 125 : 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 125 : sqliteExecuteCommand(nominalSteamEquipmentTableSQL);
784 :
785 125 : constexpr std::string_view nominalSteamEquipmentInsertSQL = "INSERT INTO NominalSteamEquipment VALUES(?,?,?,?,?,?,?,?,?,?);";
786 :
787 125 : sqlitePrepareStatement(m_nominalSteamEquipmentInsertStmt, nominalSteamEquipmentInsertSQL);
788 125 : }
789 :
790 125 : void SQLite::initializeNominalHotWaterEquipmentTable()
791 : {
792 125 : 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 125 : sqliteExecuteCommand(nominalHotWaterEquipmentTableSQL);
805 :
806 125 : constexpr std::string_view nominalHotWaterEquipmentInsertSQL = "INSERT INTO NominalHotWaterEquipment VALUES(?,?,?,?,?,?,?,?,?,?);";
807 :
808 125 : sqlitePrepareStatement(m_nominalHotWaterEquipmentInsertStmt, nominalHotWaterEquipmentInsertSQL);
809 125 : }
810 :
811 125 : void SQLite::initializeNominalOtherEquipmentTable()
812 : {
813 125 : 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 125 : sqliteExecuteCommand(nominalOtherEquipmentTableSQL);
825 :
826 125 : constexpr std::string_view nominalOtherEquipmentInsertSQL = "INSERT INTO NominalOtherEquipment VALUES(?,?,?,?,?,?,?,?,?,?);";
827 :
828 125 : sqlitePrepareStatement(m_nominalOtherEquipmentInsertStmt, nominalOtherEquipmentInsertSQL);
829 125 : }
830 :
831 125 : void SQLite::initializeNominalBaseboardHeatTable()
832 : {
833 125 : 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 125 : sqliteExecuteCommand(nominalBaseboardHeatersTableSQL);
845 :
846 125 : constexpr std::string_view nominalBaseboardHeatInsertSQL = "INSERT INTO NominalBaseboardHeaters VALUES(?,?,?,?,?,?,?,?,?,?,?);";
847 :
848 125 : sqlitePrepareStatement(m_nominalBaseboardHeatInsertStmt, nominalBaseboardHeatInsertSQL);
849 125 : }
850 :
851 125 : void SQLite::initializeSurfacesTable()
852 : {
853 125 : 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 125 : sqliteExecuteCommand(surfacesTableSQL);
869 :
870 125 : constexpr std::string_view surfaceInsertSQL = "INSERT INTO Surfaces VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
871 :
872 125 : sqlitePrepareStatement(m_surfaceInsertStmt, surfaceInsertSQL);
873 125 : }
874 :
875 125 : void SQLite::initializeConstructionsTables()
876 : {
877 125 : 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 125 : sqliteExecuteCommand(constructionsTableSQL);
886 :
887 125 : constexpr std::string_view constructionInsertSQL = "INSERT INTO Constructions VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
888 :
889 125 : sqlitePrepareStatement(m_constructionInsertStmt, constructionInsertSQL);
890 :
891 125 : 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 125 : sqliteExecuteCommand(constructionLayersTableSQL);
901 :
902 125 : constexpr std::string_view constructionLayerInsertSQL =
903 : "INSERT INTO ConstructionLayers(ConstructionIndex, LayerIndex, MaterialIndex) VALUES(?,?,?);";
904 :
905 125 : sqlitePrepareStatement(m_constructionLayerInsertStmt, constructionLayerInsertSQL);
906 125 : }
907 :
908 125 : void SQLite::initializeMaterialsTable()
909 : {
910 125 : 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 125 : sqliteExecuteCommand(materialsTableSQL);
918 :
919 125 : constexpr std::string_view materialInsertSQL = "INSERT INTO Materials VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
920 :
921 125 : sqlitePrepareStatement(m_materialInsertStmt, materialInsertSQL);
922 125 : }
923 :
924 125 : void SQLite::initializeZoneListTable()
925 : {
926 125 : constexpr std::string_view zoneListsTableSQL = "CREATE TABLE ZoneLists ( "
927 : "ZoneListIndex INTEGER PRIMARY KEY, Name TEXT);";
928 :
929 125 : sqliteExecuteCommand(zoneListsTableSQL);
930 :
931 125 : constexpr std::string_view zoneListInsertSQL = "INSERT INTO ZoneLists VALUES(?,?);";
932 :
933 125 : sqlitePrepareStatement(m_zoneListInsertStmt, zoneListInsertSQL);
934 125 : }
935 :
936 125 : void SQLite::initializeZoneGroupTable()
937 : {
938 125 : 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 125 : sqliteExecuteCommand(zoneGroupsTableSQL);
948 :
949 125 : constexpr std::string_view zoneGroupInsertSQL = "INSERT INTO ZoneGroups VALUES(?,?,?,?);";
950 :
951 125 : sqlitePrepareStatement(m_zoneGroupInsertStmt, zoneGroupInsertSQL);
952 125 : }
953 :
954 125 : void SQLite::initializeNominalInfiltrationTable()
955 : {
956 125 : 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 125 : sqliteExecuteCommand(nominalInfiltrationTableSQL);
967 :
968 125 : constexpr std::string_view infiltrationInsertSQL =
969 : "INSERT INTO NominalInfiltration (NominalInfiltrationIndex, ObjectName, ZoneIndex, ScheduleIndex, DesignLevel)"
970 : "VALUES (?,?,?,?,?);";
971 :
972 125 : sqlitePrepareStatement(m_infiltrationInsertStmt, infiltrationInsertSQL);
973 125 : }
974 :
975 125 : void SQLite::initializeNominalVentilationTable()
976 : {
977 125 : 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 125 : sqliteExecuteCommand(nominalVentilationTableSQL);
988 :
989 125 : constexpr std::string_view ventilationInsertSQL = "INSERT INTO NominalVentilation VALUES(?,?,?,?,?);";
990 :
991 125 : sqlitePrepareStatement(m_ventilationInsertStmt, ventilationInsertSQL);
992 125 : }
993 :
994 125 : void SQLite::initializeZoneSizingTable()
995 : {
996 125 : 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 125 : sqliteExecuteCommand(zoneSizesTableSQL);
1004 :
1005 125 : constexpr std::string_view zoneSizingInsertSQL = "INSERT INTO ZoneSizes VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?);";
1006 :
1007 125 : sqlitePrepareStatement(m_zoneSizingInsertStmt, zoneSizingInsertSQL);
1008 125 : }
1009 :
1010 125 : void SQLite::initializeSystemSizingTable()
1011 : {
1012 125 : 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 125 : sqliteExecuteCommand(systemSizesTableSQL);
1017 :
1018 125 : constexpr std::string_view systemSizingInsertSQL = "INSERT INTO SystemSizes VALUES(?,?,?,?,?,?,?,?,?);";
1019 :
1020 125 : sqlitePrepareStatement(m_systemSizingInsertStmt, systemSizingInsertSQL);
1021 125 : }
1022 :
1023 125 : void SQLite::initializeComponentSizingTable()
1024 : {
1025 125 : 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 125 : sqliteExecuteCommand(componentSizesTableSQL);
1029 :
1030 125 : constexpr std::string_view componentSizingInsertSQL = "INSERT INTO ComponentSizes VALUES (?,?,?,?,?,?);";
1031 :
1032 125 : sqlitePrepareStatement(m_componentSizingInsertStmt, componentSizingInsertSQL);
1033 125 : }
1034 :
1035 125 : void SQLite::initializeRoomAirModelTable()
1036 : {
1037 125 : constexpr std::string_view roomAirModelsTableSQL =
1038 : "CREATE TABLE RoomAirModels (ZoneIndex INTEGER PRIMARY KEY, AirModelName TEXT, AirModelType INTEGER, "
1039 : "TempCoupleScheme INTEGER, SimAirModel INTEGER);";
1040 :
1041 125 : sqliteExecuteCommand(roomAirModelsTableSQL);
1042 :
1043 125 : constexpr std::string_view roomAirModelInsertSQL = "INSERT INTO RoomAirModels VALUES(?,?,?,?,?);";
1044 :
1045 125 : sqlitePrepareStatement(m_roomAirModelInsertStmt, roomAirModelInsertSQL);
1046 125 : }
1047 :
1048 125 : void SQLite::initializeSchedulesTable()
1049 : {
1050 125 : constexpr std::string_view scheduleTableSQL = "CREATE TABLE Schedules (ScheduleIndex INTEGER PRIMARY KEY, ScheduleName TEXT, "
1051 : "ScheduleType TEXT, ScheduleMinimum REAL, ScheduleMaximum REAL);";
1052 :
1053 125 : sqliteExecuteCommand(scheduleTableSQL);
1054 :
1055 125 : constexpr std::string_view scheduleInsertSQL = "INSERT INTO Schedules VALUES(?,?,?,?,?);";
1056 :
1057 125 : sqlitePrepareStatement(m_scheduleInsertStmt, scheduleInsertSQL);
1058 125 : }
1059 :
1060 125 : void SQLite::initializeDaylightMapTables()
1061 : {
1062 125 : 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 125 : sqliteExecuteCommand(daylightMapsTableSQL);
1070 :
1071 125 : constexpr std::string_view daylightMapTitleInsertSQL = "INSERT INTO DaylightMaps VALUES(?,?,?,?,?,?);";
1072 :
1073 125 : sqlitePrepareStatement(m_daylightMapTitleInsertStmt, daylightMapTitleInsertSQL);
1074 :
1075 125 : 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 125 : sqliteExecuteCommand(daylightMapHourlyReportsTableSQL);
1083 :
1084 125 : constexpr std::string_view daylightMapHourlyTitleInsertSQL = "INSERT INTO DaylightMapHourlyReports VALUES(?,?,?,?,?,?);";
1085 :
1086 125 : sqlitePrepareStatement(m_daylightMapHourlyTitleInsertStmt, daylightMapHourlyTitleInsertSQL);
1087 :
1088 125 : 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 125 : sqliteExecuteCommand(daylightMapHourlyDataTableSQL);
1097 :
1098 125 : constexpr std::string_view daylightMapHourlyDataInsertSQL = "INSERT INTO DaylightMapHourlyData VALUES(?,?,?,?,?);";
1099 :
1100 125 : sqlitePrepareStatement(m_daylightMapHourlyDataInsertStmt, daylightMapHourlyDataInsertSQL);
1101 125 : }
1102 :
1103 125 : void SQLite::initializeViews()
1104 : {
1105 125 : 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 125 : sqliteExecuteCommand(reportVariableWithTimeViewSQL);
1121 :
1122 125 : 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 125 : sqliteExecuteCommand(reportVariableDataViewSQL);
1131 :
1132 125 : 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 125 : sqliteExecuteCommand(reportVariableDataDictionaryViewSQL);
1139 :
1140 125 : 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 125 : sqliteExecuteCommand(reportVariableExtendedDataViewSQL);
1147 :
1148 125 : 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 125 : sqliteExecuteCommand(reportMeterDataViewSQL);
1160 :
1161 125 : 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 125 : sqliteExecuteCommand(reportMeterDataDictionaryViewSQL);
1169 :
1170 125 : 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 125 : sqliteExecuteCommand(reportMeterExtendedDataViewSQL);
1182 125 : }
1183 :
1184 125 : void SQLite::initializeSimulationsTable()
1185 : {
1186 125 : 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 125 : sqliteExecuteCommand(simulationsTableSQL);
1191 :
1192 125 : constexpr std::string_view simulationsInsertSQL =
1193 : "INSERT INTO Simulations(SimulationIndex, EnergyPlusVersion, TimeStamp, Completed, CompletedSuccessfully) "
1194 : "VALUES(?,?,?,'FALSE','FALSE');";
1195 :
1196 125 : sqlitePrepareStatement(m_simulationsInsertStmt, simulationsInsertSQL);
1197 :
1198 125 : constexpr std::string_view simulationUpdateSQL = "UPDATE Simulations SET "
1199 : "Completed = ?, CompletedSuccessfully = ? "
1200 : "WHERE SimulationIndex = ?";
1201 :
1202 125 : sqlitePrepareStatement(m_simulationUpdateStmt, simulationUpdateSQL);
1203 :
1204 125 : constexpr std::string_view simulationDataUpdateSQL = "UPDATE Simulations SET "
1205 : "NumTimestepsPerHour = ? "
1206 : "WHERE SimulationIndex = ?";
1207 :
1208 125 : sqlitePrepareStatement(m_simulationDataUpdateStmt, simulationDataUpdateSQL);
1209 125 : }
1210 :
1211 125 : void SQLite::initializeErrorsTable()
1212 : {
1213 125 : 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 125 : sqliteExecuteCommand(errorsTableSQL);
1221 :
1222 125 : constexpr std::string_view errorInsertSQL = "INSERT INTO Errors VALUES(?,?,?,?,?);";
1223 :
1224 125 : sqlitePrepareStatement(m_errorInsertStmt, errorInsertSQL);
1225 :
1226 125 : 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 125 : sqlitePrepareStatement(m_errorUpdateStmt, errorUpdateSQL);
1231 125 : }
1232 :
1233 125 : void SQLite::initializeEnvironmentPeriodsTable()
1234 : {
1235 125 : 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 125 : sqliteExecuteCommand(environmentPeriodsTableSQL);
1243 :
1244 125 : constexpr std::string_view environmentPeriodInsertSQL = "INSERT INTO EnvironmentPeriods VALUES(?,?,?,?);";
1245 :
1246 125 : sqlitePrepareStatement(m_environmentPeriodInsertStmt, environmentPeriodInsertSQL);
1247 125 : }
1248 :
1249 24 : void SQLite::initializeTabularDataTable()
1250 : {
1251 24 : constexpr std::string_view sql = "CREATE TABLE StringTypes ( "
1252 : "StringTypeIndex INTEGER PRIMARY KEY, "
1253 : "Value TEXT"
1254 : ");";
1255 :
1256 24 : sqliteExecuteCommand(sql);
1257 :
1258 24 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'ReportName');", ReportNameId));
1259 24 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'ReportForString');", ReportForStringId));
1260 24 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'TableName');", TableNameId));
1261 24 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'RowName');", RowNameId));
1262 24 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'ColumnName');", ColumnNameId));
1263 24 : sqliteExecuteCommand(format("INSERT INTO StringTypes VALUES({},'Units');", UnitsId));
1264 :
1265 24 : 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 24 : sqliteExecuteCommand(sql2);
1275 :
1276 24 : constexpr std::string_view sql3 = "INSERT INTO Strings (StringIndex,StringTypeIndex,Value) VALUES(?,?,?);";
1277 :
1278 24 : sqlitePrepareStatement(m_stringsInsertStmt, sql3);
1279 :
1280 24 : constexpr std::string_view sql4 = "SELECT StringIndex FROM Strings WHERE StringTypeIndex=? AND Value=?;";
1281 :
1282 24 : sqlitePrepareStatement(m_stringsLookUpStmt, sql4);
1283 :
1284 24 : 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 24 : sqliteExecuteCommand(sql5);
1313 :
1314 24 : constexpr std::string_view sql6 = "INSERT INTO TabularData VALUES(?,?,?,?,?,?,?,?,?,?,?);";
1315 :
1316 24 : sqlitePrepareStatement(m_tabularDataInsertStmt, sql6);
1317 24 : }
1318 :
1319 24 : void SQLite::initializeTabularDataView()
1320 : {
1321 24 : 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 24 : sqliteExecuteCommand(sql);
1339 24 : }
1340 :
1341 125 : void SQLite::initializeIndexes()
1342 : {
1343 125 : if (m_writeOutputToSQLite) {
1344 125 : sqliteExecuteCommand("CREATE INDEX rddMTR ON ReportDataDictionary (IsMeter);");
1345 125 : 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 125 : }
1357 :
1358 29302 : void SQLite::adjustReportingHourAndMinutes(int &hour, int &minutes)
1359 : {
1360 29302 : switch (minutes) {
1361 4878 : case 60:
1362 4878 : minutes = 0;
1363 4878 : break;
1364 24424 : default:
1365 24424 : --hour;
1366 : }
1367 29302 : }
1368 :
1369 458462 : void SQLite::parseUnitsAndDescription(std::string_view combinedString, std::string &units, std::string &description)
1370 : {
1371 458462 : std::size_t leftPos = combinedString.find("[");
1372 458462 : std::size_t rightPos = combinedString.find("]");
1373 :
1374 458462 : if ((leftPos < rightPos) && (leftPos != std::string::npos) && (rightPos != std::string::npos)) {
1375 39062 : units = combinedString.substr(leftPos + 1, rightPos - leftPos - 1);
1376 39062 : description = combinedString.substr(0, leftPos - 1);
1377 : } else {
1378 419400 : units = "";
1379 419400 : description = combinedString;
1380 : }
1381 458462 : }
1382 :
1383 0 : int SQLite::logicalToInteger(const bool value)
1384 : {
1385 0 : return value ? 1 : 0;
1386 : }
1387 :
1388 16007 : 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 16007 : if (m_writeOutputToSQLite) {
1411 16007 : sqliteBindInteger(m_reportDictionaryInsertStmt, 1, reportVariableReportID);
1412 16007 : sqliteBindLogical(m_reportDictionaryInsertStmt, 2, isMeter);
1413 32014 : sqliteBindText(
1414 16007 : m_reportDictionaryInsertStmt, 3, (storeType == OutputProcessor::StoreType::Invalid) ? "Unknown!!!" : storeTypeStrings[(int)storeType]);
1415 16007 : sqliteBindText(m_reportDictionaryInsertStmt, 4, indexGroup);
1416 32014 : sqliteBindText(m_reportDictionaryInsertStmt,
1417 : 5,
1418 16007 : (timeStepType == OutputProcessor::TimeStepType::Invalid) ? "Unknown!!!" : timeStepTypeStrings[(int)timeStepType]);
1419 16007 : sqliteBindText(m_reportDictionaryInsertStmt, 6, keyedValueString);
1420 16007 : sqliteBindText(m_reportDictionaryInsertStmt, 7, variableName);
1421 32014 : sqliteBindText(m_reportDictionaryInsertStmt,
1422 : 8,
1423 16007 : (reportFreq == OutputProcessor::ReportFreq::Invalid) ? "Unknown!!!" : reportFreqStrings[(int)reportFreq]);
1424 :
1425 16007 : if (!scheduleName.empty()) {
1426 229 : sqliteBindText(m_reportDictionaryInsertStmt, 9, scheduleName);
1427 : } else {
1428 15778 : sqliteBindNULL(m_reportDictionaryInsertStmt, 9);
1429 : }
1430 :
1431 16007 : sqliteBindText(m_reportDictionaryInsertStmt, 10, units);
1432 :
1433 16007 : sqliteStepCommand(m_reportDictionaryInsertStmt);
1434 16007 : sqliteResetCommand(m_reportDictionaryInsertStmt);
1435 : }
1436 16007 : }
1437 :
1438 2166612 : 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 2166612 : if (!m_writeOutputToSQLite) {
1449 0 : return;
1450 : }
1451 :
1452 2166612 : ++m_dataIndex;
1453 :
1454 2166612 : sqliteBindInteger(m_reportDataInsertStmt, 1, m_dataIndex);
1455 2166612 : sqliteBindForeignKey(m_reportDataInsertStmt, 2, m_sqlDBTimeIndex);
1456 2166612 : sqliteBindForeignKey(m_reportDataInsertStmt, 3, recordIndex);
1457 2166612 : sqliteBindDouble(m_reportDataInsertStmt, 4, value);
1458 :
1459 2166612 : sqliteStepCommand(m_reportDataInsertStmt);
1460 2166612 : sqliteResetCommand(m_reportDataInsertStmt);
1461 :
1462 2166612 : 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 5576 : General::DecodeMonDayHrMin(minValueDate, minMonth, minDay, minHour, minMinute);
1473 5576 : General::DecodeMonDayHrMin(maxValueDate, maxMonth, maxDay, maxHour, maxMinute);
1474 :
1475 5576 : adjustReportingHourAndMinutes(minHour, minMinute);
1476 5576 : adjustReportingHourAndMinutes(maxHour, maxMinute);
1477 :
1478 5576 : ++m_extendedDataIndex;
1479 :
1480 5576 : if (minutesPerTimeStep != -1) { // This is for data created by a 'Report Meter' statement
1481 3872 : switch (reportFreq) {
1482 3872 : 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 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 1, m_extendedDataIndex);
1488 3872 : sqliteBindForeignKey(m_reportExtendedDataInsertStmt, 2, m_dataIndex);
1489 :
1490 3872 : sqliteBindDouble(m_reportExtendedDataInsertStmt, 3, maxValue);
1491 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 4, maxMonth);
1492 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 5, maxDay);
1493 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 6, maxHour);
1494 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 7, maxMinute - minutesPerTimeStep + 1);
1495 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 8, maxMinute);
1496 :
1497 3872 : sqliteBindDouble(m_reportExtendedDataInsertStmt, 9, minValue);
1498 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 10, minMonth);
1499 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 11, minDay);
1500 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 12, minHour);
1501 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 13, minMinute - minutesPerTimeStep + 1);
1502 3872 : sqliteBindInteger(m_reportExtendedDataInsertStmt, 14, minMinute);
1503 :
1504 3872 : sqliteStepCommand(m_reportExtendedDataInsertStmt);
1505 3872 : sqliteResetCommand(m_reportExtendedDataInsertStmt);
1506 3872 : } 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 23500 : 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 23500 : if (m_writeOutputToSQLite) {
1580 23500 : int intervalInMinutes = 60;
1581 :
1582 23748 : static std::vector<int> lastDayOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1583 23500 : if (curYearIsLeapYear) {
1584 0 : lastDayOfMonth[1] = 29;
1585 : }
1586 :
1587 23500 : switch (reportFreq) {
1588 18150 : case OutputProcessor::ReportFreq::EachCall:
1589 : case OutputProcessor::ReportFreq::TimeStep: {
1590 18150 : if (month == -1 || dayOfMonth == -1 || hour == -1 || endMinute == -1.0 || startMinute == -1.0 || dst == -1 || dayType == "") {
1591 0 : sqliteWriteMessage("Empty month, dayOfMonth, hour, endMinute, startMinute, dst, or dayType passed to CreateSQLiteTimeIndexRecord");
1592 0 : break;
1593 : }
1594 18150 : ++m_sqlDBTimeIndex;
1595 :
1596 18150 : int intEndMinute = static_cast<int>(endMinute + 0.5);
1597 18150 : int intStartMinute = static_cast<int>(startMinute + 0.5);
1598 18150 : int t_hour = hour;
1599 18150 : intervalInMinutes = intEndMinute - intStartMinute;
1600 18150 : adjustReportingHourAndMinutes(t_hour, intEndMinute);
1601 :
1602 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1603 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 2, simulationYear);
1604 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 3, month);
1605 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 4, dayOfMonth);
1606 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 5, t_hour);
1607 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 6, intEndMinute);
1608 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 7, dst);
1609 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 8, intervalInMinutes);
1610 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)reportFreq]);
1611 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 10, cumlativeSimulationDays);
1612 18150 : sqliteBindText(m_timeIndexInsertStmt, 11, dayType);
1613 18150 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1614 18150 : sqliteBindLogical(m_timeIndexInsertStmt, 13, warmupFlag);
1615 :
1616 18150 : sqliteStepCommand(m_timeIndexInsertStmt);
1617 18150 : sqliteResetCommand(m_timeIndexInsertStmt);
1618 :
1619 18150 : break;
1620 : }
1621 5208 : case OutputProcessor::ReportFreq::Hour: {
1622 5208 : if (month == -1 || dayOfMonth == -1 || hour == -1 || dst == -1 || dayType == "") {
1623 0 : sqliteWriteMessage("Empty month, dayOfMonth, hour, dst, or dayType passed to CreateSQLiteTimeIndexRecord");
1624 0 : break;
1625 : }
1626 5208 : ++m_sqlDBTimeIndex;
1627 :
1628 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1629 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 2, simulationYear);
1630 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 3, month);
1631 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 4, dayOfMonth);
1632 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 5, hour);
1633 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 6, 0);
1634 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 7, dst);
1635 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 8, intervalInMinutes);
1636 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)reportFreq]);
1637 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 10, cumlativeSimulationDays);
1638 5208 : sqliteBindText(m_timeIndexInsertStmt, 11, dayType);
1639 5208 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1640 :
1641 5208 : sqliteStepCommand(m_timeIndexInsertStmt);
1642 5208 : sqliteResetCommand(m_timeIndexInsertStmt);
1643 :
1644 5208 : break;
1645 : }
1646 17 : case OutputProcessor::ReportFreq::Day: {
1647 17 : if (month == -1 || dayOfMonth == -1 || dst == -1 || dayType == "") {
1648 0 : sqliteWriteMessage("Empty month, dayOfMonth, dst, or dayType passed to CreateSQLiteTimeIndexRecord");
1649 0 : break;
1650 : }
1651 17 : ++m_sqlDBTimeIndex;
1652 :
1653 17 : intervalInMinutes = 60 * 24;
1654 17 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1655 17 : sqliteBindInteger(m_timeIndexInsertStmt, 2, simulationYear);
1656 17 : sqliteBindInteger(m_timeIndexInsertStmt, 3, month);
1657 17 : sqliteBindInteger(m_timeIndexInsertStmt, 4, dayOfMonth);
1658 17 : sqliteBindInteger(m_timeIndexInsertStmt, 5, 24);
1659 17 : sqliteBindInteger(m_timeIndexInsertStmt, 6, 0);
1660 17 : sqliteBindInteger(m_timeIndexInsertStmt, 7, dst);
1661 17 : sqliteBindInteger(m_timeIndexInsertStmt, 8, intervalInMinutes);
1662 17 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)reportFreq]);
1663 17 : sqliteBindInteger(m_timeIndexInsertStmt, 10, cumlativeSimulationDays);
1664 17 : sqliteBindText(m_timeIndexInsertStmt, 11, dayType);
1665 17 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1666 :
1667 17 : sqliteStepCommand(m_timeIndexInsertStmt);
1668 17 : sqliteResetCommand(m_timeIndexInsertStmt);
1669 :
1670 17 : break;
1671 : }
1672 117 : case OutputProcessor::ReportFreq::Month: {
1673 117 : if (month == -1) {
1674 0 : sqliteWriteMessage("Empty month passed to CreateSQLiteTimeIndexRecord");
1675 0 : break;
1676 : }
1677 117 : ++m_sqlDBTimeIndex;
1678 :
1679 117 : intervalInMinutes = 60 * 24 * lastDayOfMonth[month - 1];
1680 117 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1681 117 : sqliteBindInteger(m_timeIndexInsertStmt, 2, simulationYear);
1682 117 : sqliteBindInteger(m_timeIndexInsertStmt, 3, month);
1683 117 : sqliteBindInteger(m_timeIndexInsertStmt, 4, lastDayOfMonth[month - 1]);
1684 117 : sqliteBindInteger(m_timeIndexInsertStmt, 5, 24);
1685 117 : sqliteBindInteger(m_timeIndexInsertStmt, 6, 0);
1686 117 : sqliteBindNULL(m_timeIndexInsertStmt, 7);
1687 117 : sqliteBindInteger(m_timeIndexInsertStmt, 8, intervalInMinutes);
1688 117 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)reportFreq]);
1689 117 : sqliteBindInteger(m_timeIndexInsertStmt, 10, cumlativeSimulationDays);
1690 117 : sqliteBindNULL(m_timeIndexInsertStmt, 11);
1691 117 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1692 :
1693 117 : sqliteStepCommand(m_timeIndexInsertStmt);
1694 117 : sqliteResetCommand(m_timeIndexInsertStmt);
1695 :
1696 117 : break;
1697 : }
1698 8 : case OutputProcessor::ReportFreq::Simulation: {
1699 8 : ++m_sqlDBTimeIndex;
1700 :
1701 8 : intervalInMinutes = 60 * 24 * cumlativeSimulationDays;
1702 8 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1703 8 : sqliteBindNULL(m_timeIndexInsertStmt, 2);
1704 8 : sqliteBindNULL(m_timeIndexInsertStmt, 3);
1705 8 : sqliteBindNULL(m_timeIndexInsertStmt, 4);
1706 8 : sqliteBindNULL(m_timeIndexInsertStmt, 5);
1707 8 : sqliteBindNULL(m_timeIndexInsertStmt, 6);
1708 8 : sqliteBindNULL(m_timeIndexInsertStmt, 7);
1709 8 : sqliteBindInteger(m_timeIndexInsertStmt, 8, intervalInMinutes);
1710 8 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)reportFreq]);
1711 8 : sqliteBindInteger(m_timeIndexInsertStmt, 10, cumlativeSimulationDays);
1712 8 : sqliteBindNULL(m_timeIndexInsertStmt, 11);
1713 8 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1714 :
1715 8 : sqliteStepCommand(m_timeIndexInsertStmt);
1716 8 : sqliteResetCommand(m_timeIndexInsertStmt);
1717 :
1718 8 : break;
1719 : }
1720 0 : default: {
1721 0 : std::stringstream ss;
1722 0 : ss << "Illegal reportingInterval passed to CreateSQLiteTimeIndexRecord: " << (int)reportFreq;
1723 0 : sqliteWriteMessage(ss.str());
1724 0 : }
1725 : }
1726 : }
1727 23500 : } // SQLite::createSQLiteTimeIndexRecord()
1728 :
1729 0 : void SQLite::createYearlyTimeIndexRecord(int const simulationYear, int const curEnvirNum)
1730 : {
1731 0 : if (m_writeOutputToSQLite) {
1732 :
1733 0 : ++m_sqlDBTimeIndex;
1734 :
1735 0 : sqliteBindInteger(m_timeIndexInsertStmt, 1, m_sqlDBTimeIndex);
1736 0 : sqliteBindInteger(m_timeIndexInsertStmt, 2, simulationYear);
1737 0 : sqliteBindNULL(m_timeIndexInsertStmt, 3);
1738 0 : sqliteBindNULL(m_timeIndexInsertStmt, 4);
1739 0 : sqliteBindNULL(m_timeIndexInsertStmt, 5);
1740 0 : sqliteBindNULL(m_timeIndexInsertStmt, 6);
1741 0 : sqliteBindNULL(m_timeIndexInsertStmt, 7);
1742 0 : sqliteBindNULL(m_timeIndexInsertStmt, 8);
1743 0 : sqliteBindInteger(m_timeIndexInsertStmt, 9, reportFreqInts[(int)OutputProcessor::ReportFreq::Year]);
1744 0 : sqliteBindNULL(m_timeIndexInsertStmt, 10);
1745 0 : sqliteBindNULL(m_timeIndexInsertStmt, 11);
1746 0 : sqliteBindInteger(m_timeIndexInsertStmt, 12, curEnvirNum);
1747 :
1748 0 : sqliteStepCommand(m_timeIndexInsertStmt);
1749 0 : sqliteResetCommand(m_timeIndexInsertStmt);
1750 : }
1751 0 : }
1752 :
1753 2504 : void SQLite::addSQLiteZoneSizingRecord(std::string_view zoneName, // the name of the zone
1754 : std::string_view loadType, // the description of the input variable
1755 : Real64 const calcDesLoad, // the value from the sizing calculation [W]
1756 : Real64 const userDesLoad, // the value from the sizing calculation modified by user input [W]
1757 : Real64 const calcDesFlow, // calculated design air flow rate [m3/s]
1758 : Real64 const userDesFlow, // user input or modified design air flow rate [m3/s]
1759 : std::string_view desDayName, // the name of the design day that produced the peak
1760 : std::string_view peakHrMin, // time stamp of the peak
1761 : Real64 const peakTemp, // temperature at peak [C]
1762 : Real64 const peakHumRat, // humidity ratio at peak [kg water/kg dry air]
1763 : Real64 const minOAVolFlow, // zone design minimum outside air flow rate [m3/s]
1764 : Real64 const DOASHeatAddRate // zone design heat addition rate from the DOAS [W]
1765 : )
1766 : {
1767 2504 : if (m_writeOutputToSQLite) {
1768 2504 : ++m_zoneSizingIndex;
1769 2504 : sqliteBindInteger(m_zoneSizingInsertStmt, 1, m_zoneSizingIndex);
1770 2504 : sqliteBindText(m_zoneSizingInsertStmt, 2, zoneName);
1771 2504 : sqliteBindText(m_zoneSizingInsertStmt, 3, loadType);
1772 :
1773 2504 : sqliteBindDouble(m_zoneSizingInsertStmt, 4, calcDesLoad);
1774 2504 : sqliteBindDouble(m_zoneSizingInsertStmt, 5, userDesLoad);
1775 2504 : sqliteBindDouble(m_zoneSizingInsertStmt, 6, calcDesFlow);
1776 2504 : sqliteBindDouble(m_zoneSizingInsertStmt, 7, userDesFlow);
1777 :
1778 2504 : sqliteBindText(m_zoneSizingInsertStmt, 8, desDayName);
1779 2504 : sqliteBindText(m_zoneSizingInsertStmt, 9, peakHrMin);
1780 :
1781 2504 : sqliteBindDouble(m_zoneSizingInsertStmt, 10, peakTemp);
1782 2504 : sqliteBindDouble(m_zoneSizingInsertStmt, 11, peakHumRat);
1783 2504 : sqliteBindDouble(m_zoneSizingInsertStmt, 12, minOAVolFlow);
1784 2504 : sqliteBindDouble(m_zoneSizingInsertStmt, 13, DOASHeatAddRate);
1785 :
1786 2504 : sqliteStepCommand(m_zoneSizingInsertStmt);
1787 2504 : sqliteResetCommand(m_zoneSizingInsertStmt);
1788 : }
1789 2504 : }
1790 :
1791 542 : void SQLite::addSQLiteSystemSizingRecord(std::string_view SysName, // the name of the system
1792 : std::string_view LoadType, // either "Cooling" or "Heating"
1793 : std::string_view PeakLoadType, // either "Sensible" or "Total"
1794 : Real64 const UserDesCap, // User Design Capacity
1795 : Real64 const CalcDesVolFlow, // Calculated Cooling Design Air Flow Rate
1796 : Real64 const UserDesVolFlow, // User Cooling Design Air Flow Rate
1797 : std::string_view DesDayName, // the name of the design day that produced the peak
1798 : std::string_view PeakHrMin // time stamp of the peak
1799 : )
1800 : {
1801 542 : if (m_writeOutputToSQLite) {
1802 542 : ++m_systemSizingIndex;
1803 542 : sqliteBindInteger(m_systemSizingInsertStmt, 1, m_systemSizingIndex);
1804 542 : sqliteBindText(m_systemSizingInsertStmt, 2, SysName);
1805 542 : sqliteBindText(m_systemSizingInsertStmt, 3, LoadType);
1806 542 : sqliteBindText(m_systemSizingInsertStmt, 4, PeakLoadType);
1807 :
1808 542 : sqliteBindDouble(m_systemSizingInsertStmt, 5, UserDesCap);
1809 542 : sqliteBindDouble(m_systemSizingInsertStmt, 6, CalcDesVolFlow);
1810 542 : sqliteBindDouble(m_systemSizingInsertStmt, 7, UserDesVolFlow);
1811 542 : sqliteBindText(m_systemSizingInsertStmt, 8, DesDayName);
1812 542 : sqliteBindText(m_systemSizingInsertStmt, 9, PeakHrMin);
1813 :
1814 542 : sqliteStepCommand(m_systemSizingInsertStmt);
1815 542 : sqliteResetCommand(m_systemSizingInsertStmt);
1816 : }
1817 542 : }
1818 :
1819 21820 : void SQLite::addSQLiteComponentSizingRecord(std::string_view compType, // the type of the component
1820 : std::string_view compName, // the name of the component
1821 : std::string_view varDesc, // the description of the input variable
1822 : Real64 const varValue // the value from the sizing calculation
1823 : )
1824 : {
1825 21820 : if (m_writeOutputToSQLite) {
1826 21820 : ++m_componentSizingIndex;
1827 :
1828 21820 : std::string description;
1829 21820 : std::string units;
1830 :
1831 21820 : parseUnitsAndDescription(varDesc, units, description);
1832 :
1833 21820 : sqliteBindInteger(m_componentSizingInsertStmt, 1, m_componentSizingIndex);
1834 21820 : sqliteBindText(m_componentSizingInsertStmt, 2, compType);
1835 21820 : sqliteBindText(m_componentSizingInsertStmt, 3, compName);
1836 21820 : sqliteBindText(m_componentSizingInsertStmt, 4, description);
1837 21820 : sqliteBindDouble(m_componentSizingInsertStmt, 5, varValue);
1838 21820 : sqliteBindText(m_componentSizingInsertStmt, 6, units);
1839 :
1840 21820 : sqliteStepCommand(m_componentSizingInsertStmt);
1841 21820 : sqliteResetCommand(m_componentSizingInsertStmt);
1842 21820 : }
1843 21820 : }
1844 :
1845 1 : void SQLite::createSQLiteDaylightMapTitle(
1846 : int const mapNum, std::string_view mapName, std::string_view environmentName, int const zone, std::string_view refPts, Real64 const zCoord)
1847 : {
1848 1 : if (m_writeOutputToSQLite) {
1849 : // for some reason it is adding extra mapNumbers that are getting UNIQUE constraint ignored.
1850 : // Might need to look into it, basically I think something is getting double inserted (12/06/14)
1851 1 : sqliteBindInteger(m_daylightMapTitleInsertStmt, 1, mapNum);
1852 1 : sqliteBindText(m_daylightMapTitleInsertStmt, 2, mapName);
1853 1 : sqliteBindText(m_daylightMapTitleInsertStmt, 3, environmentName);
1854 1 : sqliteBindForeignKey(m_daylightMapTitleInsertStmt, 4, zone);
1855 1 : sqliteBindText(m_daylightMapTitleInsertStmt, 5, refPts);
1856 1 : sqliteBindDouble(m_daylightMapTitleInsertStmt, 6, zCoord);
1857 :
1858 1 : sqliteStepCommand(m_daylightMapTitleInsertStmt);
1859 1 : sqliteResetCommand(m_daylightMapTitleInsertStmt);
1860 : }
1861 1 : }
1862 :
1863 16 : void SQLite::createSQLiteDaylightMap(int const mapNum,
1864 : int const year,
1865 : int const month,
1866 : int const dayOfMonth,
1867 : int const hourOfDay,
1868 : int const nX,
1869 : Array1D<Real64> const &x,
1870 : int const nY,
1871 : Array1D<Real64> const &y,
1872 : Array2<Real64> const &illuminance)
1873 : {
1874 16 : if (m_writeOutputToSQLite) {
1875 16 : ++m_hourlyReportIndex;
1876 16 : int b = 0;
1877 16 : sqliteBindInteger(m_daylightMapHourlyTitleInsertStmt, ++b, m_hourlyReportIndex);
1878 16 : sqliteBindForeignKey(m_daylightMapHourlyTitleInsertStmt, ++b, mapNum);
1879 16 : sqliteBindForeignKey(m_daylightMapHourlyTitleInsertStmt, ++b, year);
1880 16 : sqliteBindInteger(m_daylightMapHourlyTitleInsertStmt, ++b, month);
1881 16 : sqliteBindInteger(m_daylightMapHourlyTitleInsertStmt, ++b, dayOfMonth);
1882 16 : sqliteBindInteger(m_daylightMapHourlyTitleInsertStmt, ++b, hourOfDay);
1883 :
1884 16 : sqliteStepCommand(m_daylightMapHourlyTitleInsertStmt);
1885 16 : sqliteResetCommand(m_daylightMapHourlyTitleInsertStmt);
1886 :
1887 176 : for (int yIndex = 1; yIndex <= nY; ++yIndex) {
1888 1760 : for (int xIndex = 1; xIndex <= nX; ++xIndex) {
1889 1600 : ++m_hourlyDataIndex;
1890 1600 : sqliteBindInteger(m_daylightMapHourlyDataInsertStmt, 1, m_hourlyDataIndex);
1891 1600 : sqliteBindForeignKey(m_daylightMapHourlyDataInsertStmt, 2, m_hourlyReportIndex);
1892 1600 : sqliteBindDouble(m_daylightMapHourlyDataInsertStmt, 3, x(xIndex));
1893 1600 : sqliteBindDouble(m_daylightMapHourlyDataInsertStmt, 4, y(yIndex));
1894 1600 : sqliteBindDouble(m_daylightMapHourlyDataInsertStmt, 5, illuminance(xIndex, yIndex));
1895 :
1896 1600 : sqliteStepCommand(m_daylightMapHourlyDataInsertStmt);
1897 1600 : sqliteResetCommand(m_daylightMapHourlyDataInsertStmt);
1898 : }
1899 : }
1900 : }
1901 16 : }
1902 :
1903 18919 : void SQLite::createSQLiteTabularDataRecords(Array2D_string const &body, // html table row, html table column
1904 : Array1D_string const &rowLabels,
1905 : Array1D_string const &columnLabels,
1906 : std::string_view reportName,
1907 : std::string_view reportForString,
1908 : std::string_view tableName)
1909 : {
1910 18919 : if (m_writeTabularDataToSQLite) {
1911 4049 : size_t sizeColumnLabels = columnLabels.size();
1912 4049 : size_t sizeRowLabels = rowLabels.size();
1913 :
1914 4049 : int const reportNameIndex = createSQLiteStringTableRecord(reportName, ReportNameId);
1915 4049 : int const reportForStringIndex = createSQLiteStringTableRecord(reportForString, ReportForStringId);
1916 4049 : int const tableNameIndex = createSQLiteStringTableRecord(tableName, TableNameId);
1917 : int unitsIndex;
1918 :
1919 34780 : for (size_t iCol = 0, k = body.index(1, 1); iCol < sizeColumnLabels; ++iCol) {
1920 30731 : std::string colUnits;
1921 30731 : std::string colDescription;
1922 30731 : parseUnitsAndDescription(columnLabels[iCol], colUnits, colDescription);
1923 :
1924 30731 : int const columnLabelIndex = createSQLiteStringTableRecord(colDescription, ColumnNameId);
1925 :
1926 30731 : if (!colUnits.empty()) {
1927 15606 : unitsIndex = createSQLiteStringTableRecord(colUnits, UnitsId);
1928 : }
1929 :
1930 436642 : for (size_t iRow = 0; iRow < sizeRowLabels; ++iRow) {
1931 405911 : ++m_tabularDataIndex;
1932 405911 : std::string rowUnits;
1933 405911 : std::string rowDescription;
1934 405911 : parseUnitsAndDescription(rowLabels[iRow], rowUnits, rowDescription);
1935 :
1936 405911 : int const rowLabelIndex = createSQLiteStringTableRecord(rowDescription, RowNameId);
1937 :
1938 405911 : if (colUnits.empty()) {
1939 208839 : unitsIndex = createSQLiteStringTableRecord(rowUnits, UnitsId);
1940 : }
1941 :
1942 405911 : sqliteBindInteger(m_tabularDataInsertStmt, 1, m_tabularDataIndex);
1943 405911 : sqliteBindForeignKey(m_tabularDataInsertStmt, 2, reportNameIndex);
1944 405911 : sqliteBindForeignKey(m_tabularDataInsertStmt, 3, reportForStringIndex);
1945 405911 : sqliteBindForeignKey(m_tabularDataInsertStmt, 4, tableNameIndex);
1946 405911 : sqliteBindForeignKey(m_tabularDataInsertStmt, 5, rowLabelIndex);
1947 405911 : sqliteBindForeignKey(m_tabularDataInsertStmt, 6, columnLabelIndex);
1948 405911 : sqliteBindForeignKey(m_tabularDataInsertStmt, 7, unitsIndex);
1949 405911 : sqliteBindForeignKey(m_tabularDataInsertStmt, 8, 1);
1950 405911 : sqliteBindInteger(m_tabularDataInsertStmt, 9, iRow);
1951 405911 : sqliteBindInteger(m_tabularDataInsertStmt, 10, iCol);
1952 405911 : sqliteBindText(m_tabularDataInsertStmt, 11, body[k]);
1953 :
1954 405911 : sqliteStepCommand(m_tabularDataInsertStmt);
1955 405911 : sqliteResetCommand(m_tabularDataInsertStmt);
1956 :
1957 405911 : ++k;
1958 405911 : }
1959 30731 : }
1960 : }
1961 18919 : }
1962 :
1963 673234 : int SQLite::createSQLiteStringTableRecord(std::string_view stringValue, int const stringType)
1964 : {
1965 673234 : int rowId = -1;
1966 673234 : if (m_writeOutputToSQLite) {
1967 :
1968 673234 : auto ret = m_tabularStrings.emplace(make_pair(stringValue, stringType), 0);
1969 :
1970 673234 : if (!ret.second) {
1971 632848 : rowId = ret.first->second;
1972 : } else {
1973 40386 : sqliteBindInteger(m_stringsInsertStmt, 1, m_stringIndex);
1974 40386 : sqliteBindForeignKey(m_stringsInsertStmt, 2, stringType);
1975 40386 : sqliteBindText(m_stringsInsertStmt, 3, stringValue);
1976 :
1977 40386 : int errorcode = sqliteStepCommand(m_stringsInsertStmt);
1978 40386 : sqliteResetCommand(m_stringsInsertStmt);
1979 :
1980 40386 : if (errorcode != SQLITE_CONSTRAINT) {
1981 40386 : rowId = m_stringIndex++;
1982 : } else {
1983 0 : sqliteBindInteger(m_stringsLookUpStmt, 1, stringType);
1984 0 : sqliteBindText(m_stringsLookUpStmt, 2, stringValue);
1985 0 : sqliteStepCommand(m_stringsLookUpStmt);
1986 0 : rowId = sqlite3_column_int(m_stringsLookUpStmt, 0);
1987 0 : sqliteResetCommand(m_stringsLookUpStmt);
1988 : }
1989 40386 : ret.first->second = rowId;
1990 : }
1991 673234 : }
1992 673234 : return rowId;
1993 : }
1994 :
1995 125 : void SQLite::createSQLiteSimulationsRecord(int const id, std::string_view verString, std::string_view currentDateTime)
1996 : {
1997 125 : if (m_writeOutputToSQLite) {
1998 125 : sqliteBindInteger(m_simulationsInsertStmt, 1, id);
1999 125 : sqliteBindText(m_simulationsInsertStmt, 2, verString);
2000 125 : sqliteBindText(m_simulationsInsertStmt, 3, currentDateTime);
2001 :
2002 125 : sqliteStepCommand(m_simulationsInsertStmt);
2003 125 : sqliteResetCommand(m_simulationsInsertStmt);
2004 : }
2005 125 : }
2006 :
2007 2161 : void SQLite::createSQLiteErrorRecord(int const simulationIndex, int const errorType, std::string_view errorMessage, int const cnt)
2008 : {
2009 2161 : if (m_writeOutputToSQLite) {
2010 2161 : ++m_errorIndex;
2011 :
2012 2161 : sqliteBindInteger(m_errorInsertStmt, 1, m_errorIndex);
2013 2161 : sqliteBindForeignKey(m_errorInsertStmt, 2, simulationIndex);
2014 2161 : sqliteBindInteger(m_errorInsertStmt, 3, errorType);
2015 2161 : sqliteBindText(m_errorInsertStmt, 4, errorMessage);
2016 2161 : sqliteBindInteger(m_errorInsertStmt, 5, cnt);
2017 :
2018 2161 : sqliteStepCommand(m_errorInsertStmt);
2019 2161 : sqliteResetCommand(m_errorInsertStmt);
2020 : }
2021 2161 : }
2022 :
2023 940 : void SQLite::updateSQLiteErrorRecord(std::string const &errorMessage)
2024 : {
2025 940 : if (m_writeOutputToSQLite) {
2026 940 : sqliteBindText(m_errorUpdateStmt, 1, " " + errorMessage);
2027 :
2028 940 : sqliteStepCommand(m_errorUpdateStmt);
2029 940 : sqliteResetCommand(m_errorUpdateStmt);
2030 : }
2031 940 : }
2032 :
2033 125 : void SQLite::updateSQLiteSimulationRecord(int const id, int const numOfTimeStepInHour)
2034 : {
2035 125 : if (m_writeOutputToSQLite) {
2036 125 : sqliteBindInteger(m_simulationDataUpdateStmt, 1, numOfTimeStepInHour);
2037 125 : sqliteBindForeignKey(m_simulationDataUpdateStmt, 2, id);
2038 :
2039 125 : sqliteStepCommand(m_simulationDataUpdateStmt);
2040 125 : sqliteResetCommand(m_simulationDataUpdateStmt);
2041 : }
2042 125 : }
2043 :
2044 0 : void SQLite::updateSQLiteSimulationRecord(bool const completed, bool const completedSuccessfully, int const id)
2045 : {
2046 0 : if (m_writeOutputToSQLite) {
2047 0 : sqliteBindLogical(m_simulationUpdateStmt, 1, completed);
2048 0 : sqliteBindLogical(m_simulationUpdateStmt, 2, completedSuccessfully);
2049 0 : sqliteBindForeignKey(m_simulationUpdateStmt, 3, id); // seems to always be 1, SimulationManager::ManageSimulation()
2050 :
2051 0 : sqliteStepCommand(m_simulationUpdateStmt);
2052 0 : sqliteResetCommand(m_simulationUpdateStmt);
2053 : }
2054 0 : }
2055 :
2056 125 : void SQLite::createZoneExtendedOutput()
2057 : {
2058 125 : if (m_writeOutputToSQLite) {
2059 1637 : for (auto const &zone : zones) {
2060 1512 : zone->insertIntoSQLite(m_zoneInfoInsertStmt);
2061 125 : }
2062 137 : for (auto const &zoneList : zoneLists) {
2063 12 : zoneList->insertIntoSQLite(m_zoneListInsertStmt, m_zoneInfoZoneListInsertStmt);
2064 125 : }
2065 128 : for (auto const &zoneGroup : zoneGroups) {
2066 3 : zoneGroup->insertIntoSQLite(m_zoneGroupInsertStmt);
2067 125 : }
2068 3784 : for (auto const &schedule : schedules) {
2069 3659 : schedule->insertIntoSQLite(m_scheduleInsertStmt);
2070 125 : }
2071 2064 : for (auto const &material : materials) {
2072 1939 : material->insertIntoSQLite(m_materialInsertStmt);
2073 125 : }
2074 1044 : for (auto const &construction : constructions) {
2075 919 : construction->insertIntoSQLite(m_constructionInsertStmt, m_constructionLayerInsertStmt);
2076 125 : }
2077 13802 : for (auto const &surface : surfaces) {
2078 13677 : surface->insertIntoSQLite(m_surfaceInsertStmt);
2079 125 : }
2080 1529 : for (auto const &nominalLighting : nominalLightings) {
2081 1404 : nominalLighting->insertIntoSQLite(m_nominalLightingInsertStmt);
2082 125 : }
2083 1357 : for (auto const &nominalPeople : nominalPeoples) {
2084 1232 : nominalPeople->insertIntoSQLite(m_nominalPeopleInsertStmt);
2085 125 : }
2086 1484 : for (auto const &nominalElectricEquipment : nominalElectricEquipments) {
2087 1359 : nominalElectricEquipment->insertIntoSQLite(m_nominalElectricEquipmentInsertStmt);
2088 125 : }
2089 161 : for (auto const &nominalGasEquipment : nominalGasEquipments) {
2090 36 : nominalGasEquipment->insertIntoSQLite(m_nominalGasEquipmentInsertStmt);
2091 125 : }
2092 125 : for (auto const &nominalSteamEquipment : nominalSteamEquipments) {
2093 0 : nominalSteamEquipment->insertIntoSQLite(m_nominalSteamEquipmentInsertStmt);
2094 125 : }
2095 134 : for (auto const &nominalHotWaterEquipment : nominalHotWaterEquipments) {
2096 9 : nominalHotWaterEquipment->insertIntoSQLite(m_nominalHotWaterEquipmentInsertStmt);
2097 125 : }
2098 149 : for (auto const &nominalOtherEquipment : nominalOtherEquipments) {
2099 24 : nominalOtherEquipment->insertIntoSQLite(m_nominalOtherEquipmentInsertStmt);
2100 125 : }
2101 125 : for (auto const &nominalBaseboardHeat : nominalBaseboardHeats) {
2102 0 : nominalBaseboardHeat->insertIntoSQLite(m_nominalBaseboardHeatInsertStmt);
2103 125 : }
2104 1308 : for (auto const &infiltration : infiltrations) {
2105 1183 : infiltration->insertIntoSQLite(m_infiltrationInsertStmt);
2106 125 : }
2107 133 : for (auto const &ventilation : ventilations) {
2108 8 : ventilation->insertIntoSQLite(m_ventilationInsertStmt);
2109 125 : }
2110 1637 : for (auto const &roomAirModel : roomAirModels) {
2111 1512 : roomAirModel->insertIntoSQLite(m_roomAirModelInsertStmt);
2112 125 : }
2113 : }
2114 125 : }
2115 :
2116 255 : void SQLite::createSQLiteEnvironmentPeriodRecord(const int curEnvirNum,
2117 : std::string_view environmentName,
2118 : const Constant::KindOfSim kindOfSim,
2119 : const int simulationIndex)
2120 : {
2121 255 : if (m_writeOutputToSQLite) {
2122 255 : sqliteBindInteger(m_environmentPeriodInsertStmt, 1, curEnvirNum);
2123 255 : sqliteBindForeignKey(m_environmentPeriodInsertStmt, 2, simulationIndex);
2124 255 : sqliteBindText(m_environmentPeriodInsertStmt, 3, environmentName);
2125 255 : sqliteBindInteger(m_environmentPeriodInsertStmt, 4, static_cast<int>(kindOfSim));
2126 :
2127 255 : sqliteStepCommand(m_environmentPeriodInsertStmt);
2128 255 : sqliteResetCommand(m_environmentPeriodInsertStmt);
2129 : }
2130 255 : }
2131 :
2132 3659 : void SQLite::addScheduleData(int const number, std::string_view name, std::string_view type, double const minValue, double const maxValue)
2133 : {
2134 3659 : schedules.push_back(std::make_unique<Schedule>(m_errorStream, m_db, number, name, type, minValue, maxValue));
2135 3659 : }
2136 :
2137 1512 : void SQLite::addZoneData(int const number, DataHeatBalance::ZoneData const &zoneData)
2138 : {
2139 1512 : zones.push_back(std::make_unique<Zone>(m_errorStream, m_db, number, zoneData));
2140 1512 : }
2141 :
2142 12 : void SQLite::addZoneListData(int const number, DataHeatBalance::ZoneListData const &zoneListData)
2143 : {
2144 12 : zoneLists.push_back(std::make_unique<ZoneList>(m_errorStream, m_db, number, zoneListData));
2145 12 : }
2146 :
2147 13677 : void SQLite::addSurfaceData(int const number, DataSurfaces::SurfaceData const &surfaceData, std::string_view surfaceClass)
2148 : {
2149 13677 : surfaces.push_back(std::make_unique<Surface>(m_errorStream, m_db, number, surfaceData, surfaceClass));
2150 13677 : }
2151 :
2152 3 : void SQLite::addZoneGroupData(int const number, DataHeatBalance::ZoneGroupData const &zoneGroupData)
2153 : {
2154 3 : zoneGroups.push_back(std::make_unique<ZoneGroup>(m_errorStream, m_db, number, zoneGroupData));
2155 3 : }
2156 :
2157 1939 : void SQLite::addMaterialData(int const number, EnergyPlus::Material::MaterialBase const *materialData)
2158 : {
2159 1939 : materials.push_back(std::make_unique<Material>(m_errorStream, m_db, number, materialData));
2160 1939 : }
2161 919 : void SQLite::addConstructionData(int const number,
2162 : EnergyPlus::Construction::ConstructionProps const &constructionData,
2163 : double const &constructionUValue)
2164 : {
2165 919 : constructions.push_back(std::make_unique<Construction>(m_errorStream, m_db, number, constructionData, constructionUValue));
2166 919 : }
2167 1404 : void SQLite::addNominalLightingData(int const number, DataHeatBalance::LightsData const &nominalLightingData)
2168 : {
2169 1404 : nominalLightings.push_back(std::make_unique<NominalLighting>(m_errorStream, m_db, number, nominalLightingData));
2170 1404 : }
2171 1232 : void SQLite::addNominalPeopleData(int const number, DataHeatBalance::PeopleData const &nominalPeopleData)
2172 : {
2173 1232 : nominalPeoples.push_back(std::make_unique<NominalPeople>(m_errorStream, m_db, number, nominalPeopleData));
2174 1232 : }
2175 1359 : void SQLite::addNominalElectricEquipmentData(int const number, DataHeatBalance::ZoneEquipData const &nominalElectricEquipmentData)
2176 : {
2177 1359 : nominalElectricEquipments.push_back(std::make_unique<NominalElectricEquipment>(m_errorStream, m_db, number, nominalElectricEquipmentData));
2178 1359 : }
2179 36 : void SQLite::addNominalGasEquipmentData(int const number, DataHeatBalance::ZoneEquipData const &nominalGasEquipmentData)
2180 : {
2181 36 : nominalGasEquipments.push_back(std::make_unique<NominalGasEquipment>(m_errorStream, m_db, number, nominalGasEquipmentData));
2182 36 : }
2183 0 : void SQLite::addNominalSteamEquipmentData(int const number, DataHeatBalance::ZoneEquipData const &nominalSteamEquipmentData)
2184 : {
2185 0 : nominalSteamEquipments.push_back(std::make_unique<NominalSteamEquipment>(m_errorStream, m_db, number, nominalSteamEquipmentData));
2186 0 : }
2187 9 : void SQLite::addNominalHotWaterEquipmentData(int const number, DataHeatBalance::ZoneEquipData const &nominalHotWaterEquipmentData)
2188 : {
2189 9 : nominalHotWaterEquipments.push_back(std::make_unique<NominalHotWaterEquipment>(m_errorStream, m_db, number, nominalHotWaterEquipmentData));
2190 9 : }
2191 24 : void SQLite::addNominalOtherEquipmentData(int const number, DataHeatBalance::ZoneEquipData const &nominalOtherEquipmentData)
2192 : {
2193 24 : nominalOtherEquipments.push_back(std::make_unique<NominalOtherEquipment>(m_errorStream, m_db, number, nominalOtherEquipmentData));
2194 24 : }
2195 0 : void SQLite::addNominalBaseboardData(int const number, DataHeatBalance::BBHeatData const &nominalBaseboardData)
2196 : {
2197 0 : nominalBaseboardHeats.push_back(std::make_unique<NominalBaseboardHeat>(m_errorStream, m_db, number, nominalBaseboardData));
2198 0 : }
2199 1183 : void SQLite::addInfiltrationData(int const number, DataHeatBalance::InfiltrationData const &infiltrationData)
2200 : {
2201 1183 : infiltrations.push_back(std::make_unique<Infiltration>(m_errorStream, m_db, number, infiltrationData));
2202 1183 : }
2203 8 : void SQLite::addVentilationData(int const number, DataHeatBalance::VentilationData const &ventilationData)
2204 : {
2205 8 : ventilations.push_back(std::make_unique<Ventilation>(m_errorStream, m_db, number, ventilationData));
2206 8 : }
2207 1512 : void SQLite::addRoomAirModelData(int const number, RoomAir::AirModelData const &roomAirModelData)
2208 : {
2209 1512 : roomAirModels.push_back(std::make_unique<RoomAirModel>(m_errorStream, m_db, number, roomAirModelData));
2210 1512 : }
2211 :
2212 3 : bool SQLite::ZoneGroup::insertIntoSQLite(sqlite3_stmt *insertStmt)
2213 : {
2214 3 : sqliteBindInteger(insertStmt, 1, number);
2215 3 : sqliteBindText(insertStmt, 2, name);
2216 3 : sqliteBindForeignKey(insertStmt, 3, zoneList);
2217 3 : sqliteBindInteger(insertStmt, 4, multiplier);
2218 :
2219 3 : int rc = sqliteStepCommand(insertStmt);
2220 3 : bool validInsert = sqliteStepValidity(rc);
2221 3 : sqliteResetCommand(insertStmt);
2222 3 : return validInsert;
2223 : }
2224 1939 : bool SQLite::Material::insertIntoSQLite(sqlite3_stmt *insertStmt)
2225 : {
2226 1939 : double isoMoistCap = 0.0;
2227 1939 : double thermGradCoef = 0.0;
2228 1939 : sqliteBindInteger(insertStmt, 1, number);
2229 1939 : sqliteBindText(insertStmt, 2, name);
2230 1939 : sqliteBindInteger(insertStmt, 3, static_cast<int>(group));
2231 1939 : sqliteBindInteger(insertStmt, 4, static_cast<int>(roughness));
2232 1939 : sqliteBindDouble(insertStmt, 5, conductivity);
2233 1939 : sqliteBindDouble(insertStmt, 6, density);
2234 1939 : sqliteBindDouble(insertStmt, 7, isoMoistCap);
2235 1939 : sqliteBindDouble(insertStmt, 8, porosity);
2236 1939 : sqliteBindDouble(insertStmt, 9, resistance);
2237 1939 : sqliteBindLogical(insertStmt, 10, rOnly);
2238 1939 : sqliteBindDouble(insertStmt, 11, specHeat);
2239 1939 : sqliteBindDouble(insertStmt, 12, thermGradCoef);
2240 1939 : sqliteBindDouble(insertStmt, 13, thickness);
2241 1939 : sqliteBindDouble(insertStmt, 14, vaporDiffus);
2242 :
2243 1939 : int rc = sqliteStepCommand(insertStmt);
2244 1939 : bool validInsert = sqliteStepValidity(rc);
2245 1939 : sqliteResetCommand(insertStmt);
2246 1939 : return validInsert;
2247 : }
2248 919 : bool SQLite::Construction::insertIntoSQLite(sqlite3_stmt *insertStmt)
2249 : {
2250 919 : sqliteBindInteger(insertStmt, 1, number);
2251 919 : sqliteBindText(insertStmt, 2, name);
2252 919 : sqliteBindInteger(insertStmt, 3, totLayers);
2253 919 : sqliteBindInteger(insertStmt, 4, totSolidLayers);
2254 919 : sqliteBindInteger(insertStmt, 5, totGlassLayers);
2255 919 : sqliteBindDouble(insertStmt, 6, insideAbsorpVis);
2256 919 : sqliteBindDouble(insertStmt, 7, outsideAbsorpVis);
2257 919 : sqliteBindDouble(insertStmt, 8, insideAbsorpSolar);
2258 919 : sqliteBindDouble(insertStmt, 9, outsideAbsorpSolar);
2259 919 : sqliteBindDouble(insertStmt, 10, insideAbsorpThermal);
2260 919 : sqliteBindDouble(insertStmt, 11, outsideAbsorpThermal);
2261 919 : sqliteBindInteger(insertStmt, 12, static_cast<int>(outsideRoughness));
2262 919 : sqliteBindLogical(insertStmt, 13, typeIsWindow);
2263 919 : sqliteBindDouble(insertStmt, 14, uValue);
2264 :
2265 919 : int rc = sqliteStepCommand(insertStmt);
2266 919 : bool validInsert = sqliteStepValidity(rc);
2267 919 : sqliteResetCommand(insertStmt);
2268 919 : return validInsert;
2269 : }
2270 919 : bool SQLite::Construction::insertIntoSQLite(sqlite3_stmt *insertStmt, sqlite3_stmt *subInsertStmt)
2271 : {
2272 919 : bool constructionInsertValid = insertIntoSQLite(insertStmt);
2273 919 : if (!constructionInsertValid) {
2274 0 : return false;
2275 : }
2276 :
2277 919 : bool valid = true;
2278 2905 : for (auto const &constructionLayer : constructionLayers) {
2279 1986 : bool validInsert = constructionLayer->insertIntoSQLite(subInsertStmt);
2280 1986 : if (valid && !validInsert) {
2281 0 : valid = false;
2282 : }
2283 919 : }
2284 919 : return valid;
2285 : }
2286 1986 : bool SQLite::Construction::ConstructionLayer::insertIntoSQLite(sqlite3_stmt *insertStmt)
2287 : {
2288 1986 : sqliteBindForeignKey(insertStmt, 1, constructNumber);
2289 1986 : sqliteBindInteger(insertStmt, 2, layerNumber);
2290 1986 : sqliteBindForeignKey(insertStmt, 3, layerPoint);
2291 :
2292 1986 : int rc = sqliteStepCommand(insertStmt);
2293 1986 : bool validInsert = sqliteStepValidity(rc);
2294 1986 : sqliteResetCommand(insertStmt);
2295 1986 : return validInsert;
2296 : }
2297 1404 : bool SQLite::NominalLighting::insertIntoSQLite(sqlite3_stmt *insertStmt)
2298 : {
2299 1404 : sqliteBindInteger(insertStmt, 1, number);
2300 1404 : sqliteBindText(insertStmt, 2, name);
2301 1404 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2302 1404 : sqliteBindForeignKey(insertStmt, 4, sched->Num);
2303 1404 : sqliteBindDouble(insertStmt, 5, designLevel);
2304 1404 : sqliteBindDouble(insertStmt, 6, fractionReturnAir);
2305 1404 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2306 1404 : sqliteBindDouble(insertStmt, 8, fractionShortWave);
2307 1404 : sqliteBindDouble(insertStmt, 9, fractionReplaceable);
2308 1404 : sqliteBindDouble(insertStmt, 10, fractionConvected);
2309 1404 : sqliteBindText(insertStmt, 11, endUseSubcategory);
2310 :
2311 1404 : int rc = sqliteStepCommand(insertStmt);
2312 1404 : bool validInsert = sqliteStepValidity(rc);
2313 1404 : sqliteResetCommand(insertStmt);
2314 1404 : return validInsert;
2315 : }
2316 1232 : bool SQLite::NominalPeople::insertIntoSQLite(sqlite3_stmt *insertStmt)
2317 : {
2318 1232 : sqliteBindInteger(insertStmt, 1, number);
2319 1232 : sqliteBindText(insertStmt, 2, name);
2320 1232 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2321 1232 : sqliteBindDouble(insertStmt, 4, numberOfPeople);
2322 1232 : sqliteBindForeignKey(insertStmt, 5, numberOfPeopleSched ? numberOfPeopleSched->Num : -1);
2323 1232 : sqliteBindForeignKey(insertStmt, 6, activityLevelSched ? activityLevelSched->Num : -1);
2324 1232 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2325 1232 : sqliteBindDouble(insertStmt, 8, fractionConvected);
2326 1232 : sqliteBindForeignKey(insertStmt, 9, workEffSched ? workEffSched->Num : -1);
2327 1232 : sqliteBindForeignKey(insertStmt, 10, clothingSched ? clothingSched->Num : -1);
2328 1232 : sqliteBindForeignKey(insertStmt, 11, airVelocitySched ? airVelocitySched->Num : -1);
2329 1232 : sqliteBindLogical(insertStmt, 12, fanger);
2330 1232 : sqliteBindLogical(insertStmt, 13, pierce);
2331 1232 : sqliteBindLogical(insertStmt, 14, ksu);
2332 1232 : sqliteBindInteger(insertStmt, 15, static_cast<int>(mrtCalcType));
2333 1232 : sqliteBindForeignKey(insertStmt, 16, surfacePtr);
2334 1232 : sqliteBindText(insertStmt, 17, angleFactorListName);
2335 1232 : sqliteBindInteger(insertStmt, 18, angleFactorListPtr);
2336 1232 : sqliteBindDouble(insertStmt, 19, userSpecSensFrac);
2337 1232 : sqliteBindLogical(insertStmt, 20, show55Warning);
2338 :
2339 1232 : int rc = sqliteStepCommand(insertStmt);
2340 1232 : bool validInsert = sqliteStepValidity(rc);
2341 1232 : sqliteResetCommand(insertStmt);
2342 1232 : return validInsert;
2343 : }
2344 1359 : bool SQLite::NominalElectricEquipment::insertIntoSQLite(sqlite3_stmt *insertStmt)
2345 : {
2346 1359 : sqliteBindInteger(insertStmt, 1, number);
2347 1359 : sqliteBindText(insertStmt, 2, name);
2348 1359 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2349 1359 : sqliteBindForeignKey(insertStmt, 4, sched->Num);
2350 1359 : sqliteBindDouble(insertStmt, 5, designLevel);
2351 1359 : sqliteBindDouble(insertStmt, 6, fractionLatent);
2352 1359 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2353 1359 : sqliteBindDouble(insertStmt, 8, fractionLost);
2354 1359 : sqliteBindDouble(insertStmt, 9, fractionConvected);
2355 1359 : sqliteBindText(insertStmt, 10, endUseSubcategory);
2356 :
2357 1359 : int rc = sqliteStepCommand(insertStmt);
2358 1359 : bool validInsert = sqliteStepValidity(rc);
2359 1359 : sqliteResetCommand(insertStmt);
2360 1359 : return validInsert;
2361 : }
2362 36 : bool SQLite::NominalGasEquipment::insertIntoSQLite(sqlite3_stmt *insertStmt)
2363 : {
2364 36 : sqliteBindInteger(insertStmt, 1, number);
2365 36 : sqliteBindText(insertStmt, 2, name);
2366 36 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2367 36 : sqliteBindForeignKey(insertStmt, 4, sched->Num);
2368 36 : sqliteBindDouble(insertStmt, 5, designLevel);
2369 36 : sqliteBindDouble(insertStmt, 6, fractionLatent);
2370 36 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2371 36 : sqliteBindDouble(insertStmt, 8, fractionLost);
2372 36 : sqliteBindDouble(insertStmt, 9, fractionConvected);
2373 36 : sqliteBindText(insertStmt, 10, endUseSubcategory);
2374 :
2375 36 : int rc = sqliteStepCommand(insertStmt);
2376 36 : bool validInsert = sqliteStepValidity(rc);
2377 36 : sqliteResetCommand(insertStmt);
2378 36 : return validInsert;
2379 : }
2380 0 : bool SQLite::NominalSteamEquipment::insertIntoSQLite(sqlite3_stmt *insertStmt)
2381 : {
2382 0 : sqliteBindInteger(insertStmt, 1, number);
2383 0 : sqliteBindText(insertStmt, 2, name);
2384 0 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2385 0 : sqliteBindForeignKey(insertStmt, 4, sched->Num);
2386 0 : sqliteBindDouble(insertStmt, 5, designLevel);
2387 0 : sqliteBindDouble(insertStmt, 6, fractionLatent);
2388 0 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2389 0 : sqliteBindDouble(insertStmt, 8, fractionLost);
2390 0 : sqliteBindDouble(insertStmt, 9, fractionConvected);
2391 0 : sqliteBindText(insertStmt, 10, endUseSubcategory);
2392 :
2393 0 : int rc = sqliteStepCommand(insertStmt);
2394 0 : bool validInsert = sqliteStepValidity(rc);
2395 0 : sqliteResetCommand(insertStmt);
2396 0 : return validInsert;
2397 : }
2398 9 : bool SQLite::NominalHotWaterEquipment::insertIntoSQLite(sqlite3_stmt *insertStmt)
2399 : {
2400 9 : sqliteBindInteger(insertStmt, 1, number);
2401 9 : sqliteBindText(insertStmt, 2, name);
2402 9 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2403 9 : sqliteBindForeignKey(insertStmt, 4, sched->Num);
2404 9 : sqliteBindDouble(insertStmt, 5, designLevel);
2405 9 : sqliteBindDouble(insertStmt, 6, fractionLatent);
2406 9 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2407 9 : sqliteBindDouble(insertStmt, 8, fractionLost);
2408 9 : sqliteBindDouble(insertStmt, 9, fractionConvected);
2409 9 : sqliteBindText(insertStmt, 10, endUseSubcategory);
2410 :
2411 9 : int rc = sqliteStepCommand(insertStmt);
2412 9 : bool validInsert = sqliteStepValidity(rc);
2413 9 : sqliteResetCommand(insertStmt);
2414 9 : return validInsert;
2415 : }
2416 24 : bool SQLite::NominalOtherEquipment::insertIntoSQLite(sqlite3_stmt *insertStmt)
2417 : {
2418 24 : sqliteBindInteger(insertStmt, 1, number);
2419 24 : sqliteBindText(insertStmt, 2, name);
2420 24 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2421 24 : sqliteBindForeignKey(insertStmt, 4, sched->Num);
2422 24 : sqliteBindDouble(insertStmt, 5, designLevel);
2423 24 : sqliteBindDouble(insertStmt, 6, fractionLatent);
2424 24 : sqliteBindDouble(insertStmt, 7, fractionRadiant);
2425 24 : sqliteBindDouble(insertStmt, 8, fractionLost);
2426 24 : sqliteBindDouble(insertStmt, 9, fractionConvected);
2427 24 : sqliteBindText(insertStmt, 10, endUseSubcategory);
2428 :
2429 24 : int rc = sqliteStepCommand(insertStmt);
2430 24 : bool validInsert = sqliteStepValidity(rc);
2431 24 : sqliteResetCommand(insertStmt);
2432 24 : return validInsert;
2433 : }
2434 0 : bool SQLite::NominalBaseboardHeat::insertIntoSQLite(sqlite3_stmt *insertStmt)
2435 : {
2436 0 : sqliteBindInteger(insertStmt, 1, number);
2437 0 : sqliteBindText(insertStmt, 2, name);
2438 0 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2439 0 : sqliteBindForeignKey(insertStmt, 4, sched->Num);
2440 0 : sqliteBindDouble(insertStmt, 5, capatLowTemperature);
2441 0 : sqliteBindDouble(insertStmt, 6, lowTemperature);
2442 0 : sqliteBindDouble(insertStmt, 7, capatHighTemperature);
2443 0 : sqliteBindDouble(insertStmt, 8, highTemperature);
2444 0 : sqliteBindDouble(insertStmt, 9, fractionRadiant);
2445 0 : sqliteBindDouble(insertStmt, 10, fractionConvected);
2446 0 : sqliteBindText(insertStmt, 11, endUseSubcategory);
2447 :
2448 0 : int rc = sqliteStepCommand(insertStmt);
2449 0 : bool validInsert = sqliteStepValidity(rc);
2450 0 : sqliteResetCommand(insertStmt);
2451 0 : return validInsert;
2452 : }
2453 1183 : bool SQLite::Infiltration::insertIntoSQLite(sqlite3_stmt *insertStmt)
2454 : {
2455 1183 : sqliteBindInteger(insertStmt, 1, number);
2456 1183 : sqliteBindText(insertStmt, 2, name);
2457 1183 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2458 1183 : sqliteBindForeignKey(insertStmt, 4, sched->Num);
2459 1183 : sqliteBindDouble(insertStmt, 5, designLevel);
2460 :
2461 1183 : int rc = sqliteStepCommand(insertStmt);
2462 1183 : bool validInsert = sqliteStepValidity(rc);
2463 1183 : sqliteResetCommand(insertStmt);
2464 1183 : return validInsert;
2465 : }
2466 8 : bool SQLite::Ventilation::insertIntoSQLite(sqlite3_stmt *insertStmt)
2467 : {
2468 8 : sqliteBindInteger(insertStmt, 1, number);
2469 8 : sqliteBindText(insertStmt, 2, name);
2470 8 : sqliteBindForeignKey(insertStmt, 3, zonePtr);
2471 8 : sqliteBindForeignKey(insertStmt, 4, sched->Num);
2472 8 : sqliteBindDouble(insertStmt, 5, designLevel);
2473 :
2474 8 : int rc = sqliteStepCommand(insertStmt);
2475 8 : bool validInsert = sqliteStepValidity(rc);
2476 8 : sqliteResetCommand(insertStmt);
2477 8 : return validInsert;
2478 : }
2479 1512 : bool SQLite::RoomAirModel::insertIntoSQLite(sqlite3_stmt *insertStmt)
2480 : {
2481 1512 : sqliteBindInteger(insertStmt, 1, number);
2482 1512 : sqliteBindText(insertStmt, 2, airModelName);
2483 1512 : sqliteBindInteger(insertStmt, 3, static_cast<int>(airModel));
2484 1512 : sqliteBindInteger(insertStmt, 4, static_cast<int>(tempCoupleScheme));
2485 1512 : sqliteBindLogical(insertStmt, 5, simAirModel);
2486 :
2487 1512 : int rc = sqliteStepCommand(insertStmt);
2488 1512 : bool validInsert = sqliteStepValidity(rc);
2489 1512 : sqliteResetCommand(insertStmt);
2490 1512 : return validInsert;
2491 : }
2492 :
2493 13677 : bool SQLite::Surface::insertIntoSQLite(sqlite3_stmt *insertStmt)
2494 : {
2495 13677 : sqliteBindInteger(insertStmt, 1, number);
2496 13677 : sqliteBindText(insertStmt, 2, name);
2497 13677 : sqliteBindForeignKey(insertStmt, 3, construction);
2498 13677 : sqliteBindText(insertStmt, 4, surfaceClass);
2499 13677 : sqliteBindDouble(insertStmt, 5, area);
2500 13677 : sqliteBindDouble(insertStmt, 6, grossArea);
2501 13677 : sqliteBindDouble(insertStmt, 7, perimeter);
2502 13677 : sqliteBindDouble(insertStmt, 8, azimuth);
2503 13677 : sqliteBindDouble(insertStmt, 9, height);
2504 13677 : sqliteBindDouble(insertStmt, 10, reveal);
2505 13677 : sqliteBindInteger(insertStmt, 11, static_cast<int>(shape));
2506 13677 : sqliteBindInteger(insertStmt, 12, sides);
2507 13677 : sqliteBindDouble(insertStmt, 13, tilt);
2508 13677 : sqliteBindDouble(insertStmt, 14, width);
2509 13677 : sqliteBindLogical(insertStmt, 15, heatTransSurf);
2510 13677 : sqliteBindForeignKey(insertStmt, 16, baseSurf);
2511 13677 : sqliteBindForeignKey(insertStmt, 17, zone);
2512 13677 : sqliteBindInteger(insertStmt, 18, extBoundCond);
2513 13677 : sqliteBindLogical(insertStmt, 19, extSolar);
2514 13677 : sqliteBindLogical(insertStmt, 20, extWind);
2515 :
2516 13677 : int rc = sqliteStepCommand(insertStmt);
2517 13677 : bool validInsert = sqliteStepValidity(rc);
2518 13677 : sqliteResetCommand(insertStmt);
2519 13677 : return validInsert;
2520 : }
2521 :
2522 12 : bool SQLite::ZoneList::insertIntoSQLite(sqlite3_stmt *insertStmt)
2523 : {
2524 12 : sqliteBindInteger(insertStmt, 1, number);
2525 12 : sqliteBindText(insertStmt, 2, name);
2526 :
2527 12 : int rc = sqliteStepCommand(insertStmt);
2528 12 : bool validInsert = sqliteStepValidity(rc);
2529 12 : sqliteResetCommand(insertStmt);
2530 12 : return validInsert;
2531 : }
2532 :
2533 12 : bool SQLite::ZoneList::insertIntoSQLite(sqlite3_stmt *insertStmt, sqlite3_stmt *subInsertStmt)
2534 : {
2535 12 : bool zoneListInsertValid = insertIntoSQLite(insertStmt);
2536 12 : if (!zoneListInsertValid) {
2537 0 : return false;
2538 : }
2539 12 : bool valid = true;
2540 176 : for (size_t i = 1; i <= zones.size(); ++i) {
2541 164 : sqliteBindForeignKey(subInsertStmt, 1, number);
2542 164 : sqliteBindForeignKey(subInsertStmt, 2, zones(i));
2543 164 : int rc = sqliteStepCommand(subInsertStmt);
2544 164 : bool validInsert = sqliteStepValidity(rc);
2545 164 : sqliteResetCommand(subInsertStmt);
2546 164 : if (valid && !validInsert) {
2547 0 : valid = false;
2548 : }
2549 : }
2550 12 : return valid;
2551 : }
2552 :
2553 3659 : bool SQLite::Schedule::insertIntoSQLite(sqlite3_stmt *insertStmt)
2554 : {
2555 3659 : sqliteBindInteger(insertStmt, 1, number);
2556 3659 : sqliteBindText(insertStmt, 2, name);
2557 3659 : sqliteBindText(insertStmt, 3, type);
2558 3659 : sqliteBindDouble(insertStmt, 4, minValue);
2559 3659 : sqliteBindDouble(insertStmt, 5, maxValue);
2560 :
2561 3659 : int rc = sqliteStepCommand(insertStmt);
2562 3659 : bool validInsert = sqliteStepValidity(rc);
2563 3659 : sqliteResetCommand(insertStmt);
2564 3659 : return validInsert;
2565 : }
2566 :
2567 1512 : bool SQLite::Zone::insertIntoSQLite(sqlite3_stmt *insertStmt)
2568 : {
2569 1512 : sqliteBindInteger(insertStmt, 1, number);
2570 1512 : sqliteBindText(insertStmt, 2, name);
2571 1512 : sqliteBindDouble(insertStmt, 3, relNorth);
2572 1512 : sqliteBindDouble(insertStmt, 4, originX);
2573 1512 : sqliteBindDouble(insertStmt, 5, originY);
2574 1512 : sqliteBindDouble(insertStmt, 6, originZ);
2575 1512 : sqliteBindDouble(insertStmt, 7, centroidX);
2576 1512 : sqliteBindDouble(insertStmt, 8, centroidY);
2577 1512 : sqliteBindDouble(insertStmt, 9, centroidZ);
2578 1512 : sqliteBindInteger(insertStmt, 10, ofType);
2579 1512 : sqliteBindInteger(insertStmt, 11, multiplier);
2580 1512 : sqliteBindInteger(insertStmt, 12, listMultiplier);
2581 1512 : sqliteBindDouble(insertStmt, 13, minimumX);
2582 1512 : sqliteBindDouble(insertStmt, 14, maximumX);
2583 1512 : sqliteBindDouble(insertStmt, 15, minimumY);
2584 1512 : sqliteBindDouble(insertStmt, 16, maximumY);
2585 1512 : sqliteBindDouble(insertStmt, 17, minimumZ);
2586 1512 : sqliteBindDouble(insertStmt, 18, maximumZ);
2587 1512 : sqliteBindDouble(insertStmt, 19, ceilingHeight);
2588 1512 : sqliteBindDouble(insertStmt, 20, volume);
2589 1512 : sqliteBindInteger(insertStmt, 21, Convect::HcIntReportVals[static_cast<int>(insideConvectionAlgo)]);
2590 1512 : sqliteBindInteger(insertStmt, 22, Convect::HcExtReportVals[static_cast<int>(outsideConvectionAlgo)]);
2591 1512 : sqliteBindDouble(insertStmt, 23, floorArea);
2592 1512 : sqliteBindDouble(insertStmt, 24, extGrossWallArea);
2593 1512 : sqliteBindDouble(insertStmt, 25, extNetWallArea);
2594 1512 : sqliteBindDouble(insertStmt, 26, extWindowArea);
2595 1512 : sqliteBindLogical(insertStmt, 27, isPartOfTotalArea);
2596 :
2597 1512 : int rc = sqliteStepCommand(insertStmt);
2598 1512 : bool validInsert = sqliteStepValidity(rc);
2599 1512 : sqliteResetCommand(insertStmt);
2600 1512 : return validInsert;
2601 : }
2602 :
2603 30474 : SQLite::SQLiteData::SQLiteData(std::shared_ptr<std::ostream> const &errorStream, std::shared_ptr<sqlite3> const &db)
2604 30474 : : SQLiteProcedures(errorStream, db)
2605 : {
2606 30474 : }
2607 :
2608 30474 : SQLiteProcedures::SQLiteProcedures(std::shared_ptr<std::ostream> const &errorStream, std::shared_ptr<sqlite3> const &db)
2609 30474 : : m_writeOutputToSQLite(true), m_errorStream(errorStream), m_db(db)
2610 : {
2611 30474 : }
2612 :
2613 125 : SQLiteProcedures::SQLiteProcedures(std::shared_ptr<std::ostream> const &errorStream,
2614 : bool writeOutputToSQLite,
2615 : fs::path const &dbName,
2616 125 : fs::path const &errorFilePath)
2617 125 : : m_writeOutputToSQLite(writeOutputToSQLite), m_errorStream(errorStream)
2618 : {
2619 125 : sqlite3 *m_connection = nullptr;
2620 125 : if (m_writeOutputToSQLite) {
2621 : int rc;
2622 125 : bool ok = true;
2623 :
2624 125 : std::string const dbName_utf8 = FileSystem::toGenericString(dbName);
2625 :
2626 : // Test if we can write to the sqlite error file
2627 : // Does there need to be a separate sqlite.err file at all? Consider using eplusout.err
2628 125 : if (m_errorStream) {
2629 125 : *m_errorStream << "SQLite3 message, " << FileSystem::toGenericString(errorFilePath) << " open for processing!" << std::endl;
2630 : } else {
2631 0 : ok = false;
2632 : }
2633 :
2634 : // Test if we can create a new file named dbName
2635 125 : if (ok && dbName != ":memory:") {
2636 125 : std::ofstream test(dbName, std::ofstream::out | std::ofstream::trunc);
2637 125 : if (test.is_open()) {
2638 125 : test.close();
2639 : } else {
2640 0 : ok = false;
2641 : }
2642 125 : }
2643 :
2644 : // Test if we can write to the database
2645 : // If we can't then there are probably locks on the database
2646 125 : if (ok) {
2647 : // sqlite3_open_v2 could return SQLITE_BUSY at this point. If so, do not proceed to sqlite3_exec.
2648 125 : rc = sqlite3_open_v2(dbName_utf8.c_str(), &m_connection, SQLITE_OPEN_READWRITE, nullptr);
2649 125 : if (rc) {
2650 0 : *m_errorStream << "SQLite3 message, can't get exclusive lock to open database: " << sqlite3_errmsg(m_connection) << std::endl;
2651 0 : ok = false;
2652 : }
2653 : }
2654 :
2655 125 : if (ok) {
2656 125 : char *zErrMsg = nullptr;
2657 : // Set journal_mode OFF to avoid creating the file dbName + "-journal" (when dbName is a regular file)
2658 125 : rc = sqlite3_exec(m_connection, "PRAGMA journal_mode = OFF;", nullptr, 0, &zErrMsg);
2659 125 : if (!rc) {
2660 125 : rc = sqlite3_exec(m_connection, "CREATE TABLE Test(x INTEGER PRIMARY KEY)", nullptr, 0, &zErrMsg);
2661 : }
2662 125 : sqlite3_close(m_connection);
2663 125 : if (rc) {
2664 0 : *m_errorStream << "SQLite3 message, can't get exclusive lock to edit database: " << zErrMsg << std::endl;
2665 0 : ok = false;
2666 : } else {
2667 125 : if (dbName != ":memory:") {
2668 : // Remove test db
2669 : // rc = remove(dbName_utf8.c_str());
2670 125 : if (fs::is_regular_file(dbName)) {
2671 125 : std::error_code ec;
2672 125 : if (!fs::remove(dbName, ec)) {
2673 : // File operation failed. SQLite connection is not in an error state.
2674 0 : *m_errorStream << "SQLite3 message, can't remove old database. code=" << ec.value() << ", error: " << ec.message()
2675 0 : << std::endl;
2676 0 : ok = false;
2677 : }
2678 : }
2679 : }
2680 : }
2681 125 : sqlite3_free(zErrMsg);
2682 : }
2683 :
2684 125 : if (ok) {
2685 : // Now open the output db for the duration of the simulation
2686 125 : rc = sqlite3_open_v2(dbName_utf8.c_str(), &m_connection, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);
2687 125 : m_db = std::shared_ptr<sqlite3>(m_connection, sqlite3_close);
2688 125 : if (rc) {
2689 0 : *m_errorStream << "SQLite3 message, can't open new database: " << sqlite3_errmsg(m_connection) << std::endl;
2690 0 : ok = false;
2691 : }
2692 : }
2693 :
2694 125 : if (!ok) {
2695 0 : throw std::runtime_error("The SQLite database failed to open.");
2696 : }
2697 125 : }
2698 125 : }
2699 :
2700 11353 : int SQLiteProcedures::sqliteExecuteCommand(std::string_view commandBuffer)
2701 : {
2702 11353 : char *zErrMsg = 0;
2703 :
2704 11353 : int rc = sqlite3_exec(m_db.get(), commandBuffer.data(), NULL, 0, &zErrMsg);
2705 11353 : if (rc != SQLITE_OK) {
2706 0 : *m_errorStream << zErrMsg;
2707 : }
2708 11353 : sqlite3_free(zErrMsg);
2709 :
2710 11353 : return rc;
2711 : }
2712 :
2713 4572 : int SQLiteProcedures::sqlitePrepareStatement(sqlite3_stmt *&stmt, std::string_view stmtBuffer)
2714 : {
2715 4572 : int rc = sqlite3_prepare_v2(m_db.get(), stmtBuffer.data(), stmtBuffer.size(), &stmt, nullptr);
2716 4572 : if (rc != SQLITE_OK) {
2717 0 : *m_errorStream << "SQLite3 message, sqlite3_prepare_v2 message:\n"
2718 : << stmtBuffer << "\n"
2719 : << sqlite3_errstr(rc) << "\n"
2720 0 : << sqlite3_errmsg(m_db.get()) << std::endl;
2721 : }
2722 :
2723 4572 : return rc;
2724 : }
2725 :
2726 735453 : int SQLiteProcedures::sqliteBindText(sqlite3_stmt *stmt, const int stmtInsertLocationIndex, std::string_view textBuffer)
2727 : {
2728 735453 : int rc = sqlite3_bind_text(stmt, stmtInsertLocationIndex, textBuffer.data(), textBuffer.size(), SQLITE_TRANSIENT);
2729 735453 : if (rc != SQLITE_OK) {
2730 0 : *m_errorStream << "SQLite3 message, sqlite3_bind_text failed:\n"
2731 : << textBuffer << "\n"
2732 : << sqlite3_errstr(rc) << "\n"
2733 0 : << sqlite3_errmsg(m_db.get()) << std::endl;
2734 : }
2735 :
2736 735453 : return rc;
2737 : }
2738 :
2739 3968883 : int SQLiteProcedures::sqliteBindInteger(sqlite3_stmt *stmt, const int stmtInsertLocationIndex, const int intToInsert)
2740 : {
2741 3968883 : int rc = sqlite3_bind_int(stmt, stmtInsertLocationIndex, intToInsert);
2742 3968883 : if (rc != SQLITE_OK) {
2743 0 : *m_errorStream << "SQLite3 message, sqlite3_bind_int failed: " << intToInsert << std::endl;
2744 : }
2745 :
2746 3968883 : return rc;
2747 : }
2748 :
2749 2417072 : int SQLiteProcedures::sqliteBindDouble(sqlite3_stmt *stmt, const int stmtInsertLocationIndex, const double doubleToInsert)
2750 : {
2751 2417072 : int rc = sqlite3_bind_double(stmt, stmtInsertLocationIndex, doubleToInsert);
2752 2417072 : if (rc != SQLITE_OK) {
2753 0 : *m_errorStream << "SQLite3 message, sqlite3_bind_double failed: " << doubleToInsert << std::endl;
2754 : }
2755 :
2756 2417072 : return rc;
2757 : }
2758 :
2759 19476 : int SQLiteProcedures::sqliteBindNULL(sqlite3_stmt *stmt, const int stmtInsertLocationIndex)
2760 : {
2761 19476 : int rc = sqlite3_bind_null(stmt, stmtInsertLocationIndex);
2762 19476 : if (rc != SQLITE_OK) {
2763 0 : *m_errorStream << "SQLite3 message, sqlite3_bind_null failed" << std::endl;
2764 : }
2765 :
2766 19476 : return rc;
2767 : }
2768 :
2769 7286741 : int SQLiteProcedures::sqliteBindForeignKey(sqlite3_stmt *stmt, const int stmtInsertLocationIndex, const int intToInsert)
2770 : {
2771 7286741 : int rc = -1;
2772 7286741 : if (intToInsert > 0) {
2773 7284431 : rc = sqlite3_bind_int(stmt, stmtInsertLocationIndex, intToInsert);
2774 : } else {
2775 2310 : rc = sqlite3_bind_null(stmt, stmtInsertLocationIndex);
2776 : }
2777 7286741 : if (rc != SQLITE_OK) {
2778 0 : *m_errorStream << "SQLite3 message, sqliteBindForeignKey failed: " << intToInsert << std::endl;
2779 : }
2780 :
2781 7286741 : return rc;
2782 : }
2783 :
2784 85998 : int SQLiteProcedures::sqliteBindLogical(sqlite3_stmt *stmt, const int stmtInsertLocationIndex, const bool valueToInsert)
2785 : {
2786 85998 : return sqliteBindInteger(stmt, stmtInsertLocationIndex, valueToInsert ? 1 : 0);
2787 : }
2788 :
2789 30638 : bool SQLiteProcedures::sqliteStepValidity(int const rc)
2790 : {
2791 30638 : bool isValid = false;
2792 30638 : switch (rc) {
2793 30638 : case SQLITE_DONE:
2794 : case SQLITE_OK:
2795 : case SQLITE_ROW:
2796 30638 : isValid = true;
2797 30638 : break;
2798 0 : default:
2799 0 : break;
2800 : }
2801 30638 : return isValid;
2802 : }
2803 :
2804 2718719 : int SQLiteProcedures::sqliteStepCommand(sqlite3_stmt *stmt)
2805 : {
2806 2718719 : int rc = sqlite3_step(stmt);
2807 2718719 : switch (rc) {
2808 2718719 : case SQLITE_DONE:
2809 : case SQLITE_OK:
2810 : case SQLITE_ROW:
2811 2718719 : break;
2812 0 : default:
2813 0 : *m_errorStream << "SQLite3 message, sqlite3_step message: " << sqlite3_errmsg(m_db.get()) << std::endl;
2814 0 : break;
2815 : }
2816 :
2817 2718719 : return rc;
2818 : }
2819 :
2820 2718719 : int SQLiteProcedures::sqliteResetCommand(sqlite3_stmt *stmt)
2821 : {
2822 2718719 : return sqlite3_reset(stmt);
2823 : }
2824 :
2825 0 : bool SQLiteProcedures::sqliteWithinTransaction()
2826 : {
2827 0 : return (sqlite3_get_autocommit(m_db.get()) == 0);
2828 : }
2829 :
2830 : // int SQLiteProcedures::sqliteClearBindings(sqlite3_stmt * stmt)
2831 : // {
2832 : // return sqlite3_clear_bindings(stmt);
2833 : // }
2834 :
2835 : // int SQLiteProcedures::sqliteFinalizeCommand(sqlite3_stmt * stmt)
2836 : // {
2837 : // return sqlite3_finalize(stmt);
2838 : // }
2839 :
2840 : } // namespace EnergyPlus
|