An Introduction to DBIx::Class. Tom Hukins

Similar documents
FUNCTIONAL RELATIONAL MAPPING WITH SLICK

Barista Document Output Object

Table Reservations Quick Reference Guide

Yelp Chanllenge. Tianshu Fan Xinhang Shao University of Washington. June 7, 2013

Database documentation for the Hayton Roman Pottery Database

kampaicocktails The Specialist Mobile Cocktail Bar Company

Using Relational Databases With ScalaQuery. Stefan Zeiger

NVIVO 10 WORKSHOP. Hui Bian Office for Faculty Excellence BY HUI BIAN

Annis on MonetDB. Viktor Rosenfeld 14. January Advisors: Prof. Dr. Ulf Leser and Dr.

Mapping and Tracking (Invasive) Plants with Calflora s Weed Manager

Dum Ka Biryani, Make for each other

Barista at a Glance BASIS International Ltd.

4/10/17. Announcements. Database Systems CSE 414. Examples of Complex Queries. Recap from last lecture. Example 1. Example 1

Cafeteria Ordering System, Release 1.0

Brewculator Final Report

Manchester s got everything, except a beach. IAN BROWN, THE STONE ROSES

50 Great Bar Drink Ideas

Sparkling Wine. Rose' White Wines

PRODUCTION SOFTWARE FOR WINEMAKERS. Wine Operations and Laboratory Analyses

Tamanend Wine Consulting

GLUTEN FREE PRODUCTS

Texas Tech University

Package cdltools. August 1, 2016

GLUTEN FREE PRODUCTS

Structures of Life. Investigation 1: Origin of Seeds. Big Question: 3 rd Science Notebook. Name:

Sparkling and Champagne

ANCIENT THE CHINESE STATE

Introduction to Management Science Midterm Exam October 29, 2002

GLOSSARY Last Updated: 10/17/ KL. Terms and Definitions

Team Ross Favourites

If you are searching for a book On the Chocolate Trail: A Delicious Adventure Connecting Jews, Religions, History, Travel, Rituals and Recipes to the

OALCF Task Cover Sheet. Goal Path: Employment Apprenticeship Secondary School Post Secondary Independence

THE STEEL DETAILER SolidWorks 2016 INSTALLATION PROCEDURE

PLEASE TAKE THE TIME TO READ THROUGH THIS FOR IMPORTANT POINTERS ON HOW TO MAXIMIZE YOUR TIME AND SEARCHES ON THE DATABASE.

Depth to Water Table Macomb County, Michigan, and Oakland County, Michigan (River Bends Park, West Side, Shelby Twp.)

Database Systems CSE 414. Lecture 7: SQL Wrap-up

Statewide Monthly Participation for Asian Non-Hispanic by Cultural Identities, Months of Prenatal Participation & Breastfeeding Amount

TOUCH IOT WITH SAP LEONARDO

The best time for new beginnings is now with our Zero Calorie Natural Flavorings! Fill With Club Soda

AWRI Refrigeration Demand Calculator

UV21078 Principles of beverage product knowledge

Michael Bankier, Jean-Marc Fillion, Manchi Luc and Christian Nadeau Manchi Luc, 15A R.H. Coats Bldg., Statistics Canada, Ottawa K1A 0T6

Join the Conversation on Twitter: #FreshConnections PRODUCE MARKETING ASSOCIATION

Stocktaking in. memsec EPoS. Categories

Easy Microwave Desserts in a Mug. For Kids. Gloria Hander Lyons. Blue Sage Press

Name AP World Summer Institute Assignment, 2015 Ms. Scalera. 1.) Define: bipedalism, primary source and Paleolithic Age.

DOWNLOAD OR READ : COOKIES QUICK DROP SIMPLE ICE BOX HAND SHAPED TRADITION HERITAGE BEST EVER BARS FINAL TOUCHES PDF EBOOK EPUB MOBI

cocktails HOT LIPS PLANTERS PUNCH Frozen Daiquiri White Rum, Triple Sec and Lime Juice Peach Kiwi Daiquiri White Rum, Triple Sec, Kiwi and Peach

raspador Documentation

Feasibility of Shortening the. Germination and Fluorescence Test Period. Of Perennial Ryegrass

Difference Cordial Labeling of Graphs Obtained from Triangular Snakes

Suitability for Haul Roads (MI) Macomb County, Michigan, and Oakland County, Michigan (River Bends Park, West Side, Shelby Twp.)

CHRISTMAS 2018 AT THE HAWTHORNS

Bier Distillery spirit and cocktail menu

Herbal Tea Database 1. Herbal Tea Database: Part A2. Beta Version

Who Grew My Soup? Geography and the Story of Food

Can You Tell the Difference? A Study on the Preference of Bottled Water. [Anonymous Name 1], [Anonymous Name 2]

BASIC COCKTAIL CATEGORIES

HdO. vol. 88. Peterson. Historical Muscat. handbook of oriental studies h andbuch d er o rientalistik

US Foods Mobile Tablet Application - User s Guide

User-Centered Design. Steps in the task analysis process. Task Scenario example: Jacques. Scenario development. Conti... (Jacques) Conti...

Table For Two - Back For Seconds By Warren Caterson READ ONLINE

Earth Oven. The Crean Award:

CLASSIC COCKTAILS COCKTAIL NAME PICTURE INGREDIENT METHOD GLASSWARE GARNISH. 60ml Vodka 30ml Kahlua (Dash of Coke Optional)

Tracing the Food System:

A Basic Guide To Hops For Homebrewing

duration NULL oid name dishcost 4 fresh lemonade sandwich 33.98

CHANGING TASTES Classic gin cocktails of the past & their current day interpretations

KOSHER PRODUCTS GARLIC & DILL TIPSY COCKTAIL STIRRERS

Kids' Party Cakes: Quick And Easy Recipes. By Murdoch Books

Cactus Moth Detection & Monitoring Network

Biocides IT training Vienna - 4 December 2017 IUCLID 6

Text Features. RC th Grade ELA Standard

Little Read 2013: Rules by Cynthia Lord

Pizza Builder Bundle. Cut & Build Pizza Toppings Sort Counting Line Tracing Writing Practice Shape Practice

mojitos etc All cocktails 9

DRINKS MENU. quench. Your. thirst

MyPlate Style Guide and Conditions of Use for the Icon

Lesson 11. Classwork. Example 1. Exercise 1. Create four equivalent ratios (2 by scaling up and 2 by scaling down) using the ratio 30 to 80.

Universal Serial Bus Type-C and Power Delivery Source Power Requirements Test Specification

Coffee zone updating: contribution to the Agricultural Sector

0418 INFORMATION TECHNOLOGY

GUIDE TO COCKTAIL CREATIONS

Efficient Image Search and Identification: The Making of WINE-O.AI

Activity 10. Coffee Break. Introduction. Equipment Required. Collecting the Data

FESTIVAL AND INTERNATIONAL COMPETITION OF WAITERS, BARTENDERS, BARISTAS AND RESTAURANTS. November 8 th 11 th 2016 HOTEL METEOR, MAKARSKA

Drinks Menu. Sip, sit, sample & socialise!

Esri Demographic Data Release Notes: Israel

4 : 5. 4 to 5. September 07, can also be written as. Representing Ratios Write the ratio of beavers to flowers

Climate Change and Wine

The Dun & Bradstreet Asia Match Environment. AME FAQ. Warwick R Matthews

Flax The Super Food!: Over 80 Delicious Recipes Using Flax Oil And Ground Flaxseed (Over 80 Delicious Recipes Using Flax Oil & Ground Flaxseed) By

Phenological monitoring guide: Joshua Tree National Park

SAFFRON THE NOBLESSE OF NEW AUTHENTIC PURE AND INTENSE

Gin. Gin 10/4/2017. Compound Gin. Distilled Gin. A spirit that derives its main flavor from juniper berries. US: min 40% abv EU: min 37.

The Columbian Exchange and Global Trade

Chaakoo Party-Pitchers

A Note on H-Cordial Graphs

CONTENTS. Whisky recipes...7-8

Stone Age & Archaeology. Unit Review

Transcription:

An Introduction to DBIx::Class Tom Hukins

Maps Database Structures to Object Oriented Structures

Schema Result Source Result Set Row

DBIx::Class::Schema CREATE DATABASE example; Your Database Schema: All tables, their relationships and contents

DBIx::Class::ResultSource CREATE TABLE foo ( id PRIMARY KEY, first_name VARCHAR(255) NOT NULL, favourite_colour INT REFERENCES colour.id, pointless BIT, skillz CHAR(3) DEFAULT 'lol' ); Your Database Tables and their relationships with each other.

DBIx::Class::ResultSet id name colour 1 Sky Blue 2 Grass Green 3 Clouds Monotonous Zero or more records within a table

DBIx::Class::Row id name colour 1 Sky Blue All the fields within a row

Schema Result Source Result Set Row

Schema Storage Result Source Result Set Row

DBIx::Class::Storage MySQL SQLite Pg DB2 Oracle Sybase / MS-SQL Connects DBIx::Class and DBD:: Database Drivers

Defining a Schema 1 package Drink; 2 3 use base qw/dbix::class::schema/; 4 5 PACKAGE ->load_classes(); 6 7 1;

Defining Result Sources 1 package Drink::Cocktails; 2 3 use base qw/dbix::class/; 4 5 PACKAGE ->load_components(qw/core/); 6 PACKAGE ->table('cocktails'); 7 PACKAGE ->add_columns(qw/id name abv/); 8 PACKAGE ->set_primary_key('id'); 9 PACKAGE ->has_many( 10 ingredients => 'Drink::Ingredients', 'cocktail' 11 ); 12 13 1;

Defining Result Sources 1 package Drink::Ingredients; 2 3 use base qw/dbix::class/; 4 5 PACKAGE ->load_components(qw/core/); 6 PACKAGE ->table('ingredients'); 7 PACKAGE ->add_columns(qw/id cocktail name amount/); 8 PACKAGE ->set_primary_key('id'); 9 PACKAGE ->belongs_to(cocktail => 'Drink::Cocktails'); 10 11 1;

Defining a SQLite Schema 1 CREATE TABLE cocktails ( 2 id INTEGER PRIMARY KEY, 3 name TEXT, 4 abv NUMERIC 5 ); 6 7 CREATE TABLE ingredients ( 8 id INTEGER PRIMARY KEY, 9 cocktail INTEGER, 10 name TEXT, 11 amount TEXT 12 );

Creating Records 1 my $drink = Drink->connect( 2 'dbi:sqlite:dbname=cocktails.db', '', ''); 3 4 my $cocktails = $drink->resultset('cocktails'); 5 my $gin_and_french = $cocktails->create({ 6 name => 'Gin and French', 7 abv => 12.7, 8 }); 9 10 my $ingredients = $drink->resultset('ingredients'); 11 $ingredients->create({ 12 name => 'Gin', 13 amount => '1.5 Shots', 14 cocktail => $gin_and_french, 15 });

Retrieving a Record 1 use Drink; 2 3 my $drink = Drink->connect( 4 'dbi:sqlite:dbname=cocktails.db', '', ''); 5 6 my $cocktails = $drink->resultset('cocktails'); 7 my $cocktail_by_id = $cocktails->find(2); 8 print $cocktail_by_id->name, " contains: \n"; 9 foreach ($cocktail_by_id->ingredients) { 10 print " - ", $_->amount, " of ", $_->name, "\n"; 11 }

Retrieving a Record Martini (Dirty) contains: - 2.5 Shots of Gin - 1 Large Dash of Noilly Prat Dry - 0.5 Shot of Brine from Olives

Tracing Your Queries $ENV{DBIC_TRACE} = 1 $storage->debug(1);

Tracing Your Queries SELECT me.id, me.name, me.abv FROM cocktails me WHERE ( ( me.id =? ) ): '2' SELECT me.id, me.cocktail, me.name, me.amount FROM ingredients me WHERE ( me.cocktail =? ): '2' Martini (Dirty) contains: - 2.5 Shots of Gin - 1 Large Dash of Noilly Prat Dry - 0.5 Shot of Brine from Olives

Improving the SQL 1 my $ingredients = $drink->resultset('ingredients'); 2 my $our_ingredients = $ingredients->search( 3 { cocktail => 2 }, 4 { 5 join => 'cocktail', 6 prefetch => 'cocktail', 7 }, 8 );

Improving the SQL 9 my $first_row = 1; 10 while (my $ingredient = $our_ingredients->next) { 11 if ($first_row) { 12 my $name = $ingredient->cocktail->name; 13 print "$name contains:\n"; 14 } 15 print " - ", $ingredient->amount, " of ", 16 $ingredient->name, "\n"; 17 $first_row = 0; 18 }

Tracing the Improved SQL SELECT me.id, me.cocktail, me.name, me.amount, cocktail.name FROM ingredients me JOIN cocktails cocktail ON ( cocktail.id = me.cocktail ) WHERE ( cocktail =? ): '2' Martini (Dirty) contains: - 2.5 Shots of Gin - 1 Large Dash of Noilly Prat Dry - 0.5 Shot of Brine from Olives

ResultSet::Column 1 use Drink; 2 3 my $drink = Drink->connect( 4 'dbi:sqlite:dbname=cocktails.db', '', ''); 5 6 my $cocktails = $drink->resultset('cocktails'); 7 print $cocktails->get_column('abv')->max, "\n";

ResultSet::Column SELECT MAX( abv ) FROM cocktails me: 32.6

Advanced Searches 1 use Drink; 2 3 my $drink = Drink->connect( 4 'dbi:sqlite:dbname=cocktails.db', '', ''); 5 6 my $cocktails = $drink->resultset('cocktails'); 7 my $max_abv = $cocktails->search( 8 { abv => \'= (SELECT MAX(abv) FROM cocktails)' } 9 )->single; 10 11 print $max_abv->name," is ", $max_abv->abv, "%.\n";

Advanced Searches SELECT me.id, me.name, me.abv FROM cocktails me WHERE ( abv = (SELECT MAX(abv) FROM cocktails) ): Martini (Dirty) is 32.6%.

DBIx::Class::Schema::Loader 1 package My::Schema; 2 use base qw/dbix::class::schema::loader/; 3 4 PACKAGE ->loader_options( 5 relationships => 1, 6 ); 7 8 my $drinks = $connect( 9 'dbi:sqlite:dbname=cocktails.db', '', '');

DBIx::Class::InflateColumns 1 PACKAGE ->load_components(qw/ 2 InflateColumn::DateTime 3 Core 4 /); 5 6 PACKAGE ->add_columns( 7 starts_when => { data_type => 'datetime' } 8 );

Other Useful Things Custom Result Sources Paged Results (uses Data::Page) DBIx::Class::Schema s deploy() populate() in DBIx::Class::Schema and ::ResultClass::HashRefInflator