Essential SQLAlchemy. An Overview of SQLAlchemy. Rick Copeland Author, Essential SQLAlchemy Predictix, LLC

Similar documents
PART I SQLAlchemy Core

BloxAlchemy. A toolkit for using logical databases with Python

SQLAlchemy: an Architectural Retrospective

Update Table Schema Sql Server 2008 Add Column After

Using Relational Databases for Digital Research

Data Base Concepts. Course Guide 2

CS317 File and Database Systems

The Future of Postgres Sharding

HIBERNATE MOCK TEST HIBERNATE MOCK TEST IV

alchimia Documentation

object/relational persistence What is persistence? 5

Bottom line: A database is the data stored and a database system is the software that manages the data. COSC Dr.

Why use a database? You can query the data (run searches) You can integrate with other business systems that use the same database You can store huge

COSC 304 Introduction to Database Systems. Database Introduction. Dr. Ramon Lawrence University of British Columbia Okanagan

Data about data is database Select correct option: True False Partially True None of the Above

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

Get Table Schema In Sql Server 2005 Modify. Column Datatype >>>CLICK HERE<<<

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

INTRODUCTION TO DATABASES IN PYTHON. Creating Databases and Tables

Mysql Insert Manual Timestamp Into Datetime Field

This tutorial is designed for all those Python programmers who would like to understand the ORM framework with SQLAlchemy and its API.

Advanced SQL Tribal Data Workshop Joe Nowinski

CS108 Lecture 18: Databases and SQL

Schema Management with Flyway

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Oral Questions and Answers (DBMS LAB) Questions & Answers- DBMS

CS143: Relational Model

Sql Server Syllabus. Overview

Bonus Content. Glossary

New ways to migrate from Oracle

Developing SQL Databases (762)

Manual Trigger Sql Server 2008 Insert Multiple Rows At Once

Databases - Have it your way

Update Table Schema Sql Server 2008 Add Column Default Value

DATA ACCESS TECHNOLOGIES FOR JAVA GENERAL STUDY

Oracle BI 11g R1: Build Repositories

Sql Server Schema Update Join Multiple Tables In One Query

BIS Database Management Systems.

MIS Database Systems.

Database Visual ARCHITECT 6.3

SQL DDL II. CS121: Relational Databases Fall 2017 Lecture 8

IBM EXAM - C DB Fundamentals. Buy Full Product.

php works 2006 in Toronto Lukas Kahwe Smith

INFORMATION TECHNOLOGY NOTES

The Object-Oriented Paradigm. Employee Application Object. The Reality of DBMS. Employee Database Table. From Database to Application.

Learn Well Technocraft

Today Learning outcomes LO2

STARCOUNTER. Technical Overview

Web Applications. Software Engineering 2017 Alessio Gambi - Saarland University

Oracle Database: Introduction to SQL

SQL DDL. Intro SQL CREATE TABLE ALTER TABLE Data types Service-based database in Visual Studio Database in PHPMyAdmin

The Data Access Layer:

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

Informatics 1: Data & Analysis

Sql Server Get Schema Name Object Id

Copy Data From One Schema To Another In Sql Developer

AO3 - Version: 2. Oracle Database 11g SQL

Relational Database Design: Part I. Introduction to Databases CompSci 316 Fall 2017

Chapter 8: Working With Databases & Tables

Oracle Database: Introduction to SQL

What s New in MySQL 5.7 Geir Høydalsvik, Sr. Director, MySQL Engineering. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

CIB Session 12th NoSQL Databases Structures

About Speaker. Shrwan Krishna Shrestha. /

MariaDB 10.3 vs MySQL 8.0. Tyler Duzan, Product Manager Percona

Database madness with & SQL Alchemy. Jaime Buelta. wrongsideofmemphis.worpress.com

Project # 1: Database Programming

Oracle Database: Introduction to SQL

Postgres Plus and JBoss

Chapter-14 SQL COMMANDS

Generating A Hibernate Mapping File And Java Classes From The Sql Schema

Database Management Systems

Chapter 1 SQL and Data

Oracle PLSQL Training Syllabus

Database principles. Matteo Mazzucato

Setting Schema Name For Native Queries In. Hibernate >>>CLICK HERE<<<

An Brief Introduction to Data Storage

Practical Database Design Methodology and Use of UML Diagrams Design & Analysis of Database Systems

Lightweight J2EE Framework

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

Chapter 6 Object Persistence, Relationships and Queries

CS313D: ADVANCED PROGRAMMING LANGUAGE

Module 9: Managing Schema Objects

Business Analytics. SQL PL SQL [Oracle 10 g] P r i n c e S e t h i w w w. x l m a c r o. w e b s. c o m

TOPLink for WebLogic. Whitepaper. The Challenge: The Solution:

MySQL Command Syntax

Object Persistence and Object-Relational Mapping. James Brucker

GlobAl EDITION. Database Concepts SEVENTH EDITION. David M. Kroenke David J. Auer

An Incredibly Brief Introduction to Relational Databases: Appendix B - Learning Rails

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Get Table Schema In Sql Server 2008 To Add Column If Not Exists >>>CLICK HERE<<<

Hand Coded Applications with SQLAlchemy

CGS 3066: Spring 2017 SQL Reference

Informatics 1: Data & Analysis

DATABASE PART 2. Components and Functions

I need to get the maximum length of data per each column in a bunch of tables. are looking at BEGIN -- loop through column names in all_tab_columns.

Querying Microsoft SQL Server (461)

Dobby Documentation. Release 0.1. Antoine Bertin

Oracle Database 11g: Introduction to SQLRelease 2

Databases and SQL programming overview

Transcription:

Essential SQLAlchemy An Overview of SQLAlchemy Rick Copeland Author, Essential SQLAlchemy Predictix, LLC

SQLAlchemy Philosophy SQL databases behave less like object collections the more size and performance start to matter Object collections behave less like tables and rows the more abstraction starts to matter SQLAlchemy aims to accommodate both of these principles From http://www.sqlalchemy.org/

SQLAlchemy Philosophy (abridged) Let tables be tables Let objects be objects (my book is short)

SQLAlchemy Architecture Object Relational Mapper (ORM) SQL Expression Language Dialect / Execution Schema Management (MetaData) Connection Pooling Types

SQLAlchemy Architecture (Interesting parts) Object Relational Mapper (ORM) SQL Expression Language Dialect / Execution Schema Management (MetaData) Connection Pooling Types

SQLAlchemy Plumbing Object Relational Mapper (ORM) SQL Expression Language Dialect / Execution Schema Management (MetaData) Connection Pooling Types

SQLAlchemy Plumbing Connection Pooling Manage a pool of long lived connections to the database Different strategies available (one connection per thread, one per statement, one per database) Usually just works without intervention Dialect / Execution Provides a database independence layer Postgres, SQLite, MySQL, Oracle, MS SQL, Firebird, Informix,... (more?)

SQLAlchemy Plumbing Types Support for a variety of common SQL types Support for driver specific types (at the cost of portability) TypeEngines convert Python values to SQL values and vice versa Custom TypeEngines easy to implement

Schema Management Object Relational Mapper (ORM) SQL Expression Language Dialect / Execution Schema Management (MetaData) Connection Pooling Types

Schema Management MetaData Table Column Index Constraint

Schema Management For blue sky development, you can define your schema in Python and stay in Python For legacy development, you can tell tables (or even the entire MetaData object!) to autoload from the database The MetaData, Tables, and Columns provide convenient proxy objects for SQL constructs used in the SQL Expression Language Foreign key relationships let SQLAlchemy automatically create join conditions for you

Schema Management Simple syntax for simple constraints Column('col', Integer, index=true, unique=true) Column('col', None, ForeignKey('t2.col')) Default Values Column('col', Integer, default=none) Column('col', DateTime, default=datetime.now) Column('col', Integer, default=select(...)) Column('col', DateTime, PassiveDefault(text('sysdate')))

Schema Management Object Relational Mapper (ORM) SQL Expression Language Dialect / Execution Schema Management (MetaData) Connection Pooling Types

SQL Expression Language DDL (Data Definition Language) Statements users_table.create() # table defined with MetaData users_table.drop() metadata.create_all() metadata.drop_all() DML (Data Manipulation Language) s_ins = users.insert(values=dict(name='rick', pass='foo')) s_del = users.delete(whereclause=users.c.name=='rick') s_upd = users.update(values=dict(age=users.c.age + timedelta(days=1)))

SQL Expression Language Executing DML Statements s_ins.execute() s_ins.execute(a=5, b=6) conn.execute(s_ins, [ dict(a=1,b=1), dict(a=1,b=2)...])

SQL Expression Language DQL (Data Query Language) statements users.select() select([users.c.user_name]) users.select(users.c.user_name=='rick') select([users, addresses], users.c.id==addresses.c.userid) s = text('''select users.fullname FROM users WHERE users.name LIKE :x''') s.execute(x='rick') users.join(addresses).select() users.outerjoin(addresses).select()

Schema Management Object Relational Mapper (ORM) SQL Expression Language Dialect / Execution Schema Management (MetaData) Connection Pooling Types

ORM Design Basic idea: use the database as a persistence layer for Python objects Tables are classes, rows are instances Relationships modeled as properties

ORM Design Active Record wrap every table in a class The class is aware of the mapping Examples: RoR ActiveRecord, SQLObject

ORM Design Data Mapper use a mapper to connect tables to classes The class is ignorant of the mapping Examples: SQLAlchemy, Hibernate

The Session Unlike other ORMs (at least SQLObject), SQLAlchemy uses the Unit of Work (UoW) pattern to collect changes to your objects as you make them At some point, these changes are flushed to the database This is a Good Thing Less chattiness with the DB server Sometimes the DB server can amortize compilation overhead for many updates

Simple Mapping Example: users = Table('users', metadata, Column(...)) class User(object): pass mapper(user, users) All columns are mapped as properties

Mapping Relations users = Table('users', metadata, Column('id',...)) addresses = Table('addresses', metadata, Column('id',...), Column('user_id', None, ForeignKey('users.id')...) class User(object): pass class Address(object): pass mapper(user, users, properties=dict( addresses=relation(address, backref='user'))) mapper(address, addresses)

Cool advanced features I won't go over in detail Eager / lazy loaded relations Deferred column loading Custom collection types Database partitioning Vertical (some tables in DB1, some in DB2) Horizontal (sharding one table partitioned) Mapping classes against arbitrary SELECT statements Inheritance mapping

Questions?