■ Le schéma SQL serait :
- Une table contenant les familles d'ingrédients. Ce que tu appelles les rubriques je pense.
Exemple : Fruits, légumes, Céréales et féculents, etc ...
- Une table ingrédient ou chaque ingrédient est tagué avec une famille.
. Chaque famille doit être référencée dans la table familles.
Fruits [Orange, Pomme, Banane, Poire, ....]
Légumes [Carottes, poireau, épinard, .....]
Féculents [Pâtes, riz, pommes de terres, lentilles, ....]
■ A la question
est-il possible des créer des bases de données avec JSON avec des tables tout comme avec SQLite ?
Et bien oui c'est possible.
Un exemple de code qui permet de choisir une famille d'ingrédients.
La liste des ingrédients associée à la famille sélectionnée se met automatiquement à jour.
La sauvegarde de la base de données se fait au format JSON.
C'est un début qui ne comprend pas la création manuel des ingrédients ni la prise en charge de la liste des ingrédients choisis.
Code : Tout sélectionner
Enumeration File
#JSON
EndEnumeration
Enumeration Window
#mf
EndEnumeration
Enumeration Gadgets
#mfFamille
#mfIngredient
#mfRecette
EndEnumeration
;Référenciel familles
Structure Famille
familleID.i
nom.s
EndStructure
Global NewList Familles.Famille()
;Référenciel ingrédients (Chaque ingrédient est associé à une famille)
Structure Ingredient
IngredientID.i
familleID.i
nom.s
EndStructure
Global NewList Ingredients.Ingredient()
;Base de données
Structure sDatabase
valeurs.s ;Contenu de chacune des tables au format JSON
EndStructure
Global NewMap Database.sDatabase()
;Plan de l'application
Declare Start()
Declare ListeIngredient()
Declare SelectIngredient()
Declare DatabaseCreate()
Declare DataBaseLoad()
Declare DataBaseSave()
Start()
Procedure Start()
If Not DataBaseLoad()
DatabaseCreate()
EndIf
OpenWindow(#mf, 0, 0, 800, 600, "Recettes", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(-1, 10, 20, 100, 22, "Famille")
ComboBoxGadget(#mfFamille, 110, 20, 200, 24)
TextGadget(-1, 350, 20, 100, 22, "Ingrédient")
ComboBoxGadget(#mfIngredient, 460, 20, 200, 24)
ForEach(familles())
AddGadgetItem(#mfFamille, -1, Familles()\nom)
SetGadgetItemData(#mfFamille, ListIndex(familles()), Familles()\familleID)
Next
SetGadgetState(#mfFamille, 0)
ListeIngredient()
;Triggers
BindEvent(#PB_Event_CloseWindow, @DataBaseSave())
BindGadgetEvent(#mfFamille, @ListeIngredient())
BindGadgetEvent(#mfIngredient, @SelectIngredient())
Repeat : WaitWindowEvent() : ForEver
EndProcedure
Procedure ListeIngredient()
Protected FamilleID = GetGadgetState(#mfFamille)
Protected Index
ClearGadgetItems(#mfIngredient)
ForEach(Ingredients())
With Ingredients()
If \familleID = FamilleID
AddGadgetItem(#mfIngredient, -1, \nom)
SetGadgetItemData(#mfIngredient, Index, Ingredients()\IngredientID)
Index + 1
EndIf
EndWith
Next
SetGadgetState(#mfIngredient, 0)
EndProcedure
Procedure SelectIngredient()
SelectElement(Ingredients(), GetGadgetItemData(#mfIngredient, GetGadgetState(#mfIngredient)))
With Ingredients()
Debug "FamilleID : " + \familleID + " IngredientID : " + \IngredientID + " Nom : " + \nom
EndWith
EndProcedure
Procedure DatabaseCreate()
;Famille
AddElement(Familles())
With Familles()
\familleID = 0
\nom = "Fruits"
EndWith
AddElement(Familles())
With Familles()
\familleID = 1
\nom = "Légumes"
EndWith
AddElement(Familles())
With Familles()
\familleID = 2
\nom = "Féculents"
EndWith
;Ingrédients
AddElement(Ingredients())
With Ingredients()
\IngredientID = 0
\familleID = 0
\nom = "Pommes"
EndWith
AddElement(Ingredients())
With Ingredients()
\IngredientID = 1
\familleID = 0
\nom = "Poires"
EndWith
AddElement(Ingredients())
With Ingredients()
\IngredientID = 2
\familleID = 0
\nom = "Abricots"
EndWith
AddElement(Ingredients())
With Ingredients()
\IngredientID = 3
\familleID = 1
\nom = "Carottes"
EndWith
AddElement(Ingredients())
With Ingredients()
\IngredientID = 4
\familleID = 1
\nom = "Poireaux"
EndWith
AddElement(Ingredients())
With Ingredients()
\IngredientID = 5
\familleID = 1
\nom = "Epinard"
EndWith
AddElement(Ingredients())
With Ingredients()
\IngredientID = 6
\familleID = 2
\nom = "Pates"
EndWith
AddElement(Ingredients())
With Ingredients()
\IngredientID = 7
\familleID = 2
\nom = "Riz"
EndWith
AddElement(Ingredients())
With Ingredients()
\IngredientID = 8
\familleID = 2
\nom = "Lentilles"
EndWith
EndProcedure
Procedure DataBaseLoad()
If LoadJSON(#JSON, "recettes.json", #PB_JSON_NoCase)
ExtractJSONMap(JSONValue(#JSON), Database())
FindMapElement(Database(), "familles")
ParseJSON(#JSON, database()\valeurs)
ExtractJSONList(JSONValue(#JSON), Familles())
FindMapElement(Database(), "ingredients")
ParseJSON(#JSON, database()\valeurs)
ExtractJSONList(JSONValue(#JSON), Ingredients())
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure DataBaseSave()
CreateJSON(#JSON)
;Sauvegarde des tables
InsertJSONList(JSONValue(#JSON), Familles())
AddMapElement(Database(), "familles")
Database()\valeurs = ComposeJSON(#JSON)
InsertJSONList(JSONValue(#JSON), Ingredients())
AddMapElement(Database(), "ingredients")
Database()\valeurs = ComposeJSON(#JSON)
;Sauvegarde de la base de données au format JSON
InsertJSONMap(JSONValue(#JSON), Database())
SaveJSON(#JSON, "recettes.json", #PB_JSON_PrettyPrint)
End
EndProcedure