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