16h00 - Fil rouge « SGBD »

Navigation rapide : Lundi / Mardi / Mercredi / Jeudi / Vendredi Mémos : Perl / Python / Ruby

SGBD

1

Se connecter à une base sqlite3
cf http://sequel.rubyforge.org/rdoc/files/README_rdoc.html

correction

1: %w(rubygems sequel).each do |lib| require lib end
2:
3: db = Sequel.sqlite('labintel.sqlite3')


2

Créer une table nommée « pers » ayant deux colonnes « p_numero_sel et name » de type Chaine de caractères

correction

db.create_table? :pers do
  primary_key :id
  String      :p_numero_sel
  String      :name
end


Créer une table nommée « pers_attr » ayant trois colonnes « pers_id, attribute et value » de type Chaine de caractères

correction

db.create_table? :pers_attr do
  primary_key :id
  String      :pers_id
  String      :attribute
  String      :value
end


3

Peupler la table « pers » à partir du fichier « pers.yaml ».
cf http://sequel.rubyforge.org/rdoc/classes/Sequel/Model/ClassMethods.html

pers.yaml

---
- id: 1
  p_numero_sel: '1234567890'
  name: 'davinci'
- id: 2
  p_numero_sel: '3456789012'
  name: 'watson'

correction

 1: require 'yaml'
 2:
 3: class Person < Sequel::Model
 4:   set_dataset :pers      # permet de choisir une table ne portant pas le même nom que le modèle
 5:   unrestrict_primary_key # permet de modifier la clef primaire qui est par défaut vérouillée en écriture
 6: end
 7:
 8: persons_yaml = YAML.load_file('pers.yaml')
 9: persons_yaml.each do |yaml_person|
10:   person              = Person.new
11:   person.id           = yaml_person["id"]
12:   person.p_numero_sel = yaml_person["p_numero_sel"]
13:   person.name         = yaml_person["name"]
14:   person.save
15: end


4

Peupler la table « pers_attr » à partir du fichier « pers_attr.json ».
cf http://sequel.rubyforge.org/rdoc-plugins/classes/Sequel/Plugins/JsonSerializer.html

pers_attr.json

[{
    "id":        1,
    "pers_id":    1,
    "attribute":    "email",
    "value":    "leonardo@vinci-closluce.com"
},{
    "id":        2,
    "pers_id":    1,
    "attribute":    "labo",
    "value":    "umrxyz"
}]

correction

 1: require 'json'
 2:
 3: class Attributes < Sequel::Model
 4:   set_dataset :pers_attr    # permet de choisir une table ne portant pas le même nom que le modèle
 5:   unrestricted_primary_key  # permet de modifier la clef primaire qui est par défaut vérouillée en écriture
 6: end
 7:
 8: Attributes.plugin :json_serializer
 9: pers_attr_json = JSON(File.read('pers_attr.json')) # tranforme un objet JSON en un objet Ruby
10: pers_attr_json.each do |json_attribute|
11:   attribute = Attributes.new
12:   attribute.from_json(    # modifie l'instance 'attribute' du modèle 'Attributes' en fonction de l'objet JSON
13:     JSON[json_attribute]  # transforme un objet Ruby en objet JSON
14:   )
15:   attribute.save
16: end


5

Lister toutes les entrées de la table pers

correction

1: Person.each do |person| pp person end

6

Lister toutes les entrées de la table pers_attrs

correction

1: Attribute.each do |attribute| pp attribute end

7

Pour chaque entrée de la table « pers » lister toutes les entrées associées de la table « pers_attr »

correction

1: class Person < Sequel::Model
2:   ...
3:   one_to_many :attributes, :class => Attribute, :key => :pers_id
4: end
5:
6: Person.each do |person|
7:   pp person.attributes
8: end

8

ajouter un attribut « statut=chercheur » à la personne ayant l'id 1

correction

1: person    = Person["1"]
2: attribute = Attribute.new( "attribute" => "statut", "value" => "chercheur" )
3: person.add_attribute(attribute)
4: person.save
5:
6: Person.each do |person|
7:   pp person.attributes
8: end

9

ajouter une contrainte d'unicité sur l'attribut « statut »
cf http://sequel.rubyforge.org/rdoc/files/doc/validations_rdoc.html

correction

1: class Attribute < Sequel::Model
2:   plugin :validation_helpers
3:   def validate
4:     super
5:     validates_unique [ :pers_id, :attribute ] if values[:attribute] == 'statut'
6:   end
7: end

10

lister toutes les personnes ayant le statut de chercheurs
cf http://sequel.heroku.com/2011/05/16/filtering-by-associations/

correction

1: pp Person.filter(
2:   :attributes => Attribute.filter(
3:      :attribute => "statut",
4:      :value     => "chercheur"
5:   )
6: ).all

Version

$Id: mardi_corrige_ruby_1.txt 637 2012-05-23 06:19:00Z delavennat $

mardi_corrige_ruby_1.txt · Last modified: 2012/05/23 08:19 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki