forked from tomy137/mcc_recipies_convert_toSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonTOsql.js
More file actions
245 lines (202 loc) · 6.81 KB
/
Copy pathjsonTOsql.js
File metadata and controls
245 lines (202 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
'use strict';
const fs = require('fs');
//let rawdata = fs.readFileSync('recipe_test_01.json');
let rawdata = fs.readFileSync(process.argv[2]);
var recipe_json = JSON.parse(rawdata);
/**
**
** RECIPE TABLE
**
**/
var recipe_fields = []
// Champs intéressants
recipe_fields.push(["NAME",st(recipe_json.name)])
recipe_fields.push(["DURATION",recipe_json.duration])
recipe_fields.push(["DURATION_TOTAL",recipe_json.duration_total])
recipe_fields.push(["IMAGE_BASE",st(recipe_json.picture_path)])
recipe_fields.push(["IMAGE_NAME",st(recipe_json.picture_name)])
recipe_fields.push(["UNIT",st(recipe_json.yield.yield_unit)])
recipe_fields.push(["YIELD",recipe_json.yield.yield_value])
recipe_fields.push(["YIELD_UNIT",st(recipe_json.yield.yield_label)])
recipe_fields.push(["INSTRUCTIONS",st(recipe_json.instructions)])
recipe_fields.push(["LEVEL",recipe_json.level])
recipe_fields.push(["COMPLEXITY",st(recipe_json.complexity)])
// Autres champs nuls
recipe_fields.push(["PREPARATIONS",st("")])
recipe_fields.push(["RECIPE_TYPE",st("live")])
recipe_fields.push(["IS_FAVORITE",1])
recipe_fields.push(["LANGUAGE",st("fr")])
recipe_fields.push(["MACHINE_TYPE",st("MC2")])
recipe_fields.push(["MACHINE_VERSION",st("2.0")])
recipe_fields.push(["SCHEME_VERSION",1])
recipe_fields.push(["UPDATED",1585731600000])
recipe_fields.push(["VALID_FROM",null])
recipe_fields.push(["VALID_TO",null])
recipe_fields.push(["VERSION ",1])
console.log(arrayToSQL(recipe_fields,"RECIPE"))
const RECIPE_ID = "(SELECT MAX(_id) FROM RECIPE)"
const MEASUREMENT_ID = "(SELECT MAX(_id) FROM MEASUREMENT)"
const LED_ID = "(SELECT MAX(_id) FROM LED)"
const MACHINE_VALUES_ID = "(SELECT MAX(_id) FROM MACHINE_VALUES)"
const INGREDIENTS_BASE_ID = "(SELECT MAX(_id) FROM INGREDIENTS_BASE)"
/**
**
** NUTRIENT TABLE
**
**/
// D'abord les joules
var nutrient_fields_joules = []
nutrient_fields_joules.push(["RECIPE_ID",RECIPE_ID])
nutrient_fields_joules.push(["TYPE",st("joules")])
nutrient_fields_joules.push(["UNIT",st("kj")])
nutrient_fields_joules.push(["AMOUNT",recipe_json.nutrient_amount.joules])
console.log(arrayToSQL(nutrient_fields_joules,"NUTRIENT"))
// calories
var nutrient_fields_calories = []
nutrient_fields_calories.push(["RECIPE_ID",RECIPE_ID])
nutrient_fields_calories.push(["TYPE",st("calories")])
nutrient_fields_calories.push(["UNIT",st("kcal")])
nutrient_fields_calories.push(["AMOUNT",recipe_json.nutrient_amount.calories])
console.log(arrayToSQL(nutrient_fields_calories,"NUTRIENT"))
// protein
var nutrient_fields_protein = []
nutrient_fields_protein.push(["RECIPE_ID",RECIPE_ID])
nutrient_fields_protein.push(["TYPE",st("protein")])
nutrient_fields_protein.push(["UNIT",st("g")])
nutrient_fields_protein.push(["AMOUNT",recipe_json.nutrient_amount.protein])
console.log(arrayToSQL(nutrient_fields_protein,"NUTRIENT"))
// carbohydrate
var nutrient_fields_carbohydrate = []
nutrient_fields_carbohydrate.push(["RECIPE_ID",RECIPE_ID])
nutrient_fields_carbohydrate.push(["TYPE",st("carbohydrate")])
nutrient_fields_carbohydrate.push(["UNIT",st("g")])
nutrient_fields_carbohydrate.push(["AMOUNT",recipe_json.nutrient_amount.carbohydrate])
console.log(arrayToSQL(nutrient_fields_carbohydrate,"NUTRIENT"))
// fat
var nutrient_fields_fat = []
nutrient_fields_fat.push(["RECIPE_ID",RECIPE_ID])
nutrient_fields_fat.push(["TYPE",st("fat")])
nutrient_fields_fat.push(["UNIT",st("g")])
nutrient_fields_fat.push(["AMOUNT",recipe_json.nutrient_amount.fat])
console.log(arrayToSQL(nutrient_fields_fat,"NUTRIENT"))
/**
**
** TAGS
**
**/
recipe_json.tags.forEach(function(tag) {
var tag_fields = []
tag_fields.push(["CATEGORY",st("other")])
tag_fields.push(["RECIPE_ID",RECIPE_ID])
tag_fields.push(["NAME",st(tag.name)])
console.log(arrayToSQL(tag_fields,"TAG"))
});
/**
**
** INGREDIENTS_BASE
**
**/
recipe_json.ingredients_bases.forEach(function(_ingredients_base) {
var ib_fields = []
ib_fields.push(["RECIPE_ID",RECIPE_ID])
ib_fields.push(["NAME",st(_ingredients_base.name)])
console.log(arrayToSQL(ib_fields,"INGREDIENTS_BASE"))
addIngredients(_ingredients_base.ingredients)
});
/**
**
** INGREDIENTS
**
**/
function addIngredients(_ingredients) {
_ingredients.forEach(function(ingredient) {
var ing_fields = []
ing_fields.push(["INGREDIENTS_BASE_ID",INGREDIENTS_BASE_ID])
ing_fields.push(["NAME",st(ingredient.name)])
ing_fields.push(["AMOUNT",st("")])
ing_fields.push(["UNIT",st("")])
console.log(arrayToSQL(ing_fields,"INGREDIENT"))
});
}
/**
**
** STEP
**
**/
var step_number = 0
recipe_json.steps.forEach(function(_step) {
step_number++
var step_fields = []
step_fields.push(["RECIPE_ID",RECIPE_ID])
step_fields.push(["MODE",st(_step.mode)])
step_fields.push(["STEP",step_number])
step_fields.push(["TEXT",st(_step.txt)])
addMeasurement(_step.measurement)
step_fields.push(["MEASUREMENT_ID",MEASUREMENT_ID])
addMachine_values(_step.machine_values)
step_fields.push(["MACHINE_VALUES_ID",MACHINE_VALUES_ID])
addLED_values(_step.LED)
step_fields.push(["LED_ID",LED_ID])
console.log(arrayToSQL(step_fields,"STEP"))
});
/**
**
** MEASUREMENT
**
**/
function addMeasurement(_mesurement) {
var measurement_fields = []
measurement_fields.push(["SPEED",_mesurement.speed])
measurement_fields.push(["TEMP",_mesurement.temperature])
measurement_fields.push(["WEIGHT",_mesurement.weight])
measurement_fields.push(["LID",0])
console.log(arrayToSQL(measurement_fields,"MEASUREMENT"))
}
/**
**
** MACHINE_VALUES
**
**/
function addMachine_values(_machinev) {
var machinev_fields = []
machinev_fields.push(["SPEED",_machinev.speed])
machinev_fields.push(["TEMP",_machinev.temperature])
machinev_fields.push(["WEIGHT",_machinev.weight])
machinev_fields.push(["REVERSE",_machinev.reverse])
machinev_fields.push(["TIME",_machinev.time])
console.log(arrayToSQL(machinev_fields,"MACHINE_VALUES"))
}
/**
**
** LED
**
**/
function addLED_values(_led) {
var led_fields = []
led_fields.push(["ACTION",st("steady")])
led_fields.push(["COLOR",st("green")])
console.log(arrayToSQL(led_fields,"LED"))
}
/**
**
** Fonctions utiles pour gagner du temps
**
**/
function arrayToSQL(_array,_table) {
var SQL_FIELDS_LIST = "("
var SQL_FIELDS_VALUES = "("
_array.forEach(function(field) {
SQL_FIELDS_LIST += field[0]+", "
SQL_FIELDS_VALUES += field[1]+", "
});
// On supprime les virgules moches
SQL_FIELDS_LIST=SQL_FIELDS_LIST.substring(0, SQL_FIELDS_LIST.length - 2);
SQL_FIELDS_VALUES=SQL_FIELDS_VALUES.substring(0, SQL_FIELDS_VALUES.length - 2);
SQL_FIELDS_LIST += ")"
SQL_FIELDS_VALUES += ")"
var SQL_REQUEST_RECIPE = "INSERT INTO "+_table+" "+SQL_FIELDS_LIST+" VALUES "+SQL_FIELDS_VALUES+";"
return SQL_REQUEST_RECIPE
}
function st(_txt) {
return '"'+_txt+'"'
}