programando sql server 2005 con el clr integración sqlclr

40
Programando SQL Server 2005 con el CLR Integración SQLCLR

Upload: jasmine-griffin

Post on 26-Mar-2015

238 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Programando SQL Server 2005 con el CLR Integración SQLCLR

Programando SQL Server 2005 con el CLRIntegración SQLCLR

Page 2: Programando SQL Server 2005 con el CLR Integración SQLCLR

2

SQL Server 2005 para desarrolladores Solid Quality Learning

Adolfo [email protected]

Microsoft Regional Director - http://msdn.microsoft.com/isv/rd Mentor Solid Quality Learning - http://www.solidqualitylearning.com Fundador, Costa Rica User Group .NET - http://www.crug.net Orador INETA Latinoamérica - http://www.ineta.org/latam Blog - http://www.wiernik.net

Jose Ricardo [email protected]

En Microsoft desde 1998 Desde el 2003 - Regional Program Manager SQL Server Latinoamérica

Page 3: Programando SQL Server 2005 con el CLR Integración SQLCLR

3

SQL Server 2005 para desarrolladores Solid Quality Learning

Series de Webcasts Introducción a SQL Server 2005 para desarrolladores

Viernes, 22 de Julio de 2005 06:00 p.m.(GMT)http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277969&Culture=es-MX

Nuevas características del lenguaje T-SQL en SQL Server 2005Lunes, 25 de Julio de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277973&Culture=es-MX   

Aprovechando XML dentro de la base de datos con SQL Server 2005Viernes, 29 de Julio de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277975&Culture=es-MX

Programando SQL Server 2005 con el CLR – Integración SQL-CLRLunes, 01 de Agosto de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277977&Culture=es-MX  

Nuevas características en ADO.NET 2.0Viernes, 05 de Agosto de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277978&Culture=es-MX 

Page 4: Programando SQL Server 2005 con el CLR Integración SQLCLR

4

SQL Server 2005 para desarrolladores Solid Quality Learning

Agenda Integración SQL Server - Common Language Runtime

• Beneficios• Ventajas• Porque• Objetivos de Diseño• Arquitectura

Catalogando Assemblies

Construyendo objetos SQLCLR• User Defined Functions• User Defined Aggregate Functions• User Defined Types• CLR Stored Procedures• CLR Triggers

Page 5: Programando SQL Server 2005 con el CLR Integración SQLCLR

5

SQL Server 2005 para desarrolladores Solid Quality Learning

.NET FrameworkCommon Language Runtime IntegrationUser-defined AggregatesUser-defined Data TypesUser-defined FunctionsSQL Server .NET Data ProviderExtended Triggers

Data TypesManaged SQL TypesNew XML DatatypeVarchar (MAX) Varbinary (MAX)

XMLXQUERY Support XML Data Manipulation Language FOR XML EnhancementsXML Schema (XSD) Support MSXML 6.0 (Native).Net XML Framework

Full-text SearchIndexing of XML Datatype

MDAC SNACMicrosoft Installer base setup

ADO.NET 2.0Notification SupportObject Model enhancements

SQL Client .NET Data ProviderServer Cursor SupportAsynchronous ExecutionSystem.Transactions

SecuritySeparation of Users and SchemaData encryption primitives

AdministrationSQL Management Objects (SMO)Analysis Management Objects

(AMO)Replication Management Objects

(RMO)T-SQL

Recursive QueriesCommon Table ExpressionsPIVOT – UNPIVOT OperatorsAPPLY OperatorException Handling

SQL Server EngineSQL Service BrokerHTTP Support (Native HTTP)Multiple Active Result Sets (MARS)Snapshot Isolation Level

Reporting ServicesMultiple Output Formats Parameters (Static, Dynamic,

Hierarchical)Bulk Delivery of Personalized

ContentSupport Multiple Data Sources STS (Web Parts, Doc Libraries)Visual Design ToolCharting, Sorting, Filtering, Drill-

ThroughScheduling, CachingComplete Scripting EngineScale Out architectureOpen XML Report Definition

Notification ServicesSQL Server Mobile Edition

Nuevas Características para Desarrollo

Page 6: Programando SQL Server 2005 con el CLR Integración SQLCLR

6

SQL Server 2005 para desarrolladores Solid Quality Learning

SQL Server as a runtime host Using assemblies Procedural code or Transact-SQL Writing .NET procedural code .NET data types and System.Data.SqlTypes Attributes

Agenda

Page 7: Programando SQL Server 2005 con el CLR Integración SQLCLR

7

SQL Server 2005 para desarrolladores Solid Quality Learning

Benefits

Better programming model Improved safety and security Ability to define data types and aggregate

functions Streamlined development through a

standardized environment Potential for improved performance and

scalability

Page 8: Programando SQL Server 2005 con el CLR Integración SQLCLR

8

SQL Server 2005 para desarrolladores Solid Quality Learning

Advantages? When to use Transact-SQL:

• designed for direct data access and manipulation in the database

Transact-SQL does not support :• arrays • Collections• for-each loops• bit shifting• classes

When to use Managed Code:• Calculations• complicated execution logic• extensive support for many complex tasks including string

handling and regular expressions, advanced math operations, file access, cryptography

• access to thousands of pre-built classes and routines

Page 9: Programando SQL Server 2005 con el CLR Integración SQLCLR

9

SQL Server 2005 para desarrolladores Solid Quality Learning

Why?

You can author (in managed code)• stored procedures• Triggers• user-defined functions• user-defined types• user-defined aggregates

Page 10: Programando SQL Server 2005 con el CLR Integración SQLCLR

10

SQL Server 2005 para desarrolladores Solid Quality Learning

Design Goals for SQL CLR Integration Reliability

• User code should not be allowed to perform operations that compromise the integrity of the database engine process or overwrite database engine memory buffers or internal data structures

Scalability• ensure that the scalability of the system is not compromised by

user code calling application programming interfaces (APIs) for threading, memory, and synchronization primitives directly

Security• User code must follow SQL Server authentication and

authorization rules when accessing database objects such as tables and columns, database administrators should be able to control access to operating system resources from user code

Performance• must perform as well or better than equivalent implementations

by native database engine functionality or through Transact-SQL

Page 11: Programando SQL Server 2005 con el CLR Integración SQLCLR

11

SQL Server 2005 para desarrolladores Solid Quality Learning

SQL CLR Integration Architecture

CLR calls low-level routines implemented by SQL Server for threading, scheduling, synchronization, and memory management

Same primitives that the rest of the SQL Server engine uses

Enforce: Scalability, Reliability, Security & PerformanceWin32 API

Windows OS

Low level routinesSQL Server

JIT CompilerCommon Language Runtime

MetadataMSIL

Assembly

Page 12: Programando SQL Server 2005 con el CLR Integración SQLCLR

12

SQL Server 2005 para desarrolladores Solid Quality Learning

Scalability Memory Management:

• CLR calls SQL Server primitives for allocating and de-allocating its memory • ensures that the CLR and SQL Server are not competing with each other

for memory • This allows SQL Server memory manager to:

• reject CLR memory requests when system memory is constrained• ask CLR to reduce its memory use when other tasks need memory

Threading• CLR calls SQL Server APIs for creating threads

• Both for running user code and for its own internal use • To synchronize, CLR calls SQL Server synchronization objects • This allows the SQL Server scheduler to control threading, for example:

• schedule other tasks when a thread is waiting on a synchronization object (for example, while GC)

• detect deadlocks that involve locks taken by CLR synchronization objects • ability to detect and stop threads that have not yielded for a significant

amount of time • identify "runaway" threads in the CLR and manage their priority

Page 13: Programando SQL Server 2005 con el CLR Integración SQLCLR

13

SQL Server 2005 para desarrolladores Solid Quality Learning

Reliability

Programming Model:• The programming model for managed code in SQL Server involves writing

functions, procedures, and types which typically do not require the use of state held across multiple invocations or the sharing of state across multiple user sessions.

• The presence of shared state can cause critical exceptions that impact the scalability and the reliability of the application.

• Avoid the use of static variables and static data members of classes used in SQL Server. For SAFE and EXTERNAL-ACCESS assemblies, SQL Server examines the metadata of the assembly at CREATE ASSEMBLY time, and fails the creation of such assemblies if it finds the use of static data members and variables

SAFE and EXTERNAL ACCESS assemblies cannot be annotated with some custom attributes

• Example: System.Security.SuppressUnmanagedCodeSecurityAttribute, System.ContextStaticAttribute System.MTAThreadAttribute

• Check complete list in BOL Depend only on “blessed” list of assemblies

• Hard coded list of revised assemblies• List can not be extended

Page 14: Programando SQL Server 2005 con el CLR Integración SQLCLR

14

SQL Server 2005 para desarrolladores Solid Quality Learning

Security Code Access Security (CAS)

• Machine Policy• User Policy

• specific to the Windows account that the SQL Server service is set up to run as

• Host Policy (SQL Server) The permissions granted to code in SQL Server is the intersection of permissions

granted by the above three policy levels The set of CAS permissions granted to assemblies by the SQL Server Host policy

level is determined by the permission set specified when creating the assembly• SAFE• EXTERNAL_ACCESS• UNSAFE

Microsoft Frameworks assemblies and SQL Server system assemblies use a fixed policy that grants them full trust

Page 15: Programando SQL Server 2005 con el CLR Integración SQLCLR

15

SQL Server 2005 para desarrolladores Solid Quality Learning

Security - SAFE

Default permission set Code cannot access external system

resources:• files, the network, environment variables, or

the registry can only access data from the local SQL

Server databases • And perform any computation on that data

Recommended permission set

Page 16: Programando SQL Server 2005 con el CLR Integración SQLCLR

16

SQL Server 2005 para desarrolladores Solid Quality Learning

Security – EXTERNAL_ACCESS

Allows access to certain external system resources• files, networks, web services, environmental variables, and the

registry Can only access assemblies that are verifiably type-safe Only SQL Server logins with EXTERNAL ACCESS permissions can

create EXTERNAL_ACCESS assemblies Code in EXTERNAL_ACCESS assemblies runs by default under

the SQL Server service account and accesses external resources under that account (unless the code explicitly impersonates the caller).

• Permission to create EXTERNAL_ACCESS assemblies should be granted only to logins who are trusted to execute code under the SQL Server service account

Page 17: Programando SQL Server 2005 con el CLR Integración SQLCLR

17

SQL Server 2005 para desarrolladores Solid Quality Learning

Security - UNSAFE

Unrestricted access to resources, both within and outside SQL Server

Can call unmanaged code Can execute type-unsafe code Should be granted only to highly trusted

assemblies Only members of the sysadmin fixed server

role can create UNSAFE assemblies

Page 18: Programando SQL Server 2005 con el CLR Integración SQLCLR

18

SQL Server 2005 para desarrolladores Solid Quality Learning

Enabling CLR Integration Off by default

EXEC sp_configure 'show advanced options', 1GORECONFIGUREGOsp_configure 'clr enabled', 1GORECONFIGUREGO

Page 19: Programando SQL Server 2005 con el CLR Integración SQLCLR

19

SQL Server 2005 para desarrolladores Solid Quality Learning

Cataloging assemblies

CREATE ASSEMBLY is used to catalog an assembly• bits can be loaded from disk or stream• assembly is assigned a symbolic name• bits are stored in system table• SQL Server principal running create must have

• assembly catalog permissions• file system access to the code

CREATE ASSEMBLY math FROM 'c:\types\math.dll'

symbolic name assembly name

Page 20: Programando SQL Server 2005 con el CLR Integración SQLCLR

20

SQL Server 2005 para desarrolladores Solid Quality Learning

Removing assemblies

DROP ASSEMBLY removes an assembly from catalog• dependent sprocs, udfs, triggers, udts must be dropped first

• includes tables and computed columns that use them• will fail if another assembly depends on it

DROP ASSEMBLY math

removes math assembly

Page 21: Programando SQL Server 2005 con el CLR Integración SQLCLR

21

SQL Server 2005 para desarrolladores Solid Quality Learning

Assembly metadata information

SQL Server has revised system metadata views• no direct access to system metadata• assembly information lives in

• sys.assemblies• sys.assembly_files• sys.assembly_references

Page 22: Programando SQL Server 2005 con el CLR Integración SQLCLR

22

SQL Server 2005 para desarrolladores Solid Quality Learning

What can be built?

User Defined Functions (UDFs) ¤

• Scalar valued (scalar UDF)• Table valued (TVF)

User Defined Procedures (UDP) ¤

User Defined Types (UDTs) ŧ

User Defined Triggers ¤

User Defined Aggregates ŧ

¤ Mapped to public static functionŧ Mapped to entire class

Page 23: Programando SQL Server 2005 con el CLR Integración SQLCLR

23

SQL Server 2005 para desarrolladores Solid Quality Learning

Basics

Needed namespaces:• System.Data • System.Data.Sql • Microsoft.SqlServer.Server

• Server-side provider SqlContext• System.Data.SqlTypes

Page 24: Programando SQL Server 2005 con el CLR Integración SQLCLR

24

SQL Server 2005 para desarrolladores Solid Quality Learning

System.Data.SqlTypes Provides data types with same semantics & precision

as in SQL Server NULL values

• Not supported for value types in .NET 1.*• .NET 2.* presents Nullable<T> generic

Boolean values with 3 values• True, False & Unknown• IsTrue(), IsFalse(), IsNull() in SqlBoolean

Arithmetic & Bitwise Operators should accept null values

Overflow detection off by default in .NET, on by default on SqlTypes

Page 25: Programando SQL Server 2005 con el CLR Integración SQLCLR

25

SQL Server 2005 para desarrolladores Solid Quality Learning

User Defined Functions Implemented as public static method in class

• Can take parameters• Should return a result

Type of UDFs:• Scalar, returns a single value• Table, returns result set

Benefits:• Allow modular programming• Allow faster execution• Can reduce network traffic

• Can be invoked on WHERE to filter rows sent to client

Page 26: Programando SQL Server 2005 con el CLR Integración SQLCLR

26

SQL Server 2005 para desarrolladores Solid Quality Learning

User Defined Functions

Scalar UDFs : Can return any scalar data type except text, ntext, image

TVFs, return type is IEnumerable / IEnumerator • Returns a streaming result set

• Results can be consumed as soon as first row is available

• Results don’t have to be loaded in memory as a whole

Page 27: Programando SQL Server 2005 con el CLR Integración SQLCLR

27

SQL Server 2005 para desarrolladores Solid Quality Learning

User Defined Functions

Steps for scalar UDFs:

1. Create a class with public static method• Mark method with SqlFunction attribute

2. Register assembly

CREATE ASSEMBLY asm_Calculos

FROM 'Calculos.dll'

3. Register function

CREATE FUNCTION GetList(@p int)

RETURNS int

AS EXTERNAL NAME asm_Calculos.[FuncionesCalculos].GetList

Page 28: Programando SQL Server 2005 con el CLR Integración SQLCLR

28

SQL Server 2005 para desarrolladores Solid Quality Learning

User Defined Aggregate Functions

Operate on a set of input scalars Generate a single aggregate result

• Examples: SUM, AVG, MAX Previously one needed to write complex cursor

logic (either client side or server side)• Query processor manages iteration• Magnitud faster than using cursors

Page 29: Programando SQL Server 2005 con el CLR Integración SQLCLR

29

SQL Server 2005 para desarrolladores Solid Quality Learning

User Defined Aggregate Functions CLR must:

• Mark type with SqlUserDefinedAggregate attribute• IsInvariantToDuplicates {true, false}

• Example: MIN, MAX• IsInvariantToNulls {true, false}

• Example: AVG, MIN, SUM, MAX• IsInvariantToOrder {true, false}

• Example: AVG, MIN, SUM, MAX• IsNullIfEmpty {true, false}

• Have a public constructor• Special aggregation methods

• Init()• Accumulate()• Merge()• Terminate()

Page 30: Programando SQL Server 2005 con el CLR Integración SQLCLR

30

SQL Server 2005 para desarrolladores Solid Quality Learning

User Defined Aggregate Functions

CREATE AGGREGATE name(input params)RETURNS out paramEXTERNAL NAME assembly.class name

Page 31: Programando SQL Server 2005 con el CLR Integración SQLCLR

31

SQL Server 2005 para desarrolladores Solid Quality Learning

User Defined Types

Can be used wherever a SQL Server type can be used Tip :: create it as a structure, although class is also

supported Requirements:

• Mark with SqlUserDefinedType attribute• Must have a public constructor

Restrictions:• Name must be less than 128 characters• No method overloading (except constructor)• No support for inheritance• Static members can’t be mutable (must be const or

readonly)

Page 32: Programando SQL Server 2005 con el CLR Integración SQLCLR

32

SQL Server 2005 para desarrolladores Solid Quality Learning

User Defined Type

Things to keep in mind:• Nullability• Serialization

• Binary, for comparisons, indexing, contraining, etc.

• XML, for exchanging and searching• String comparisons• Validation & Comparisons

Page 33: Programando SQL Server 2005 con el CLR Integración SQLCLR

33

SQL Server 2005 para desarrolladores Solid Quality Learning

User Defined Type

CREATE TYPE name[NULL | NOT NULL]EXTERNAL NAME assembly.class

Assembly and type are registered per database• If want to share between databases, then it must be

registered in all databases• Assembly must be equivalent strong name match• If want to use UDT in tempDB, then it must be

registered also in tempDB

Page 34: Programando SQL Server 2005 con el CLR Integración SQLCLR

34

SQL Server 2005 para desarrolladores Solid Quality Learning

CLR Stored Procedures Can’t be used in scalar expressions Can return:

• Output Parameters• Use byref parameters in CLR

• Tabular Results• Use SqlContext.Pipe.Execute()

• Most efficient, sends stream directly to client• Use SqlContext.Pipe.Send()

• Allows for manipulation of results before sending to client• Can return SqlDataReader

• SqlDataRecord• Allows for dynamic result sets, manually populated• Send through SqlContext.Pipe.Send

• Messages• Use SqlContenxt.Pipe.Send()• Same as PRINT

CREATE PROCEDURE nameAS EXTERNAL NAME Assembly.Class.Metodo

Page 35: Programando SQL Server 2005 con el CLR Integración SQLCLR

35

SQL Server 2005 para desarrolladores Solid Quality Learning

CLR Triggers

With CLR triggers, you can:• Reference data in the INSERTED and DELETED tables• Determine which columns have been modified as a

result of an UPDATE operation• Access information about database objects affected by

the execution of DDL statements SqlContext.GetTriggerContext() is a factory for

SqlTriggerContext class• Provides context information about the trigger

• Type of action that triggered• Modified columns in UPDATE• If DDL, XML EventData structure with description of

operation

Page 36: Programando SQL Server 2005 con el CLR Integración SQLCLR

36

SQL Server 2005 para desarrolladores Solid Quality Learning

CLR Trigger

CREATE TRIGGER triggerName

ON DBObject

FOR INSERT{action}

AS EXTERNAL NAME assembly.class.method

Page 37: Programando SQL Server 2005 con el CLR Integración SQLCLR

37

SQL Server 2005 para desarrolladores Solid Quality Learning

Conclusiones Integración SQL Server - Common Language Runtime

• Beneficios• Ventajas• Porque• Objetivos de Diseño• Arquitectura

Catalogando Assemblies

Construyendo objetos SQLCLR• User Defined Functions• User Defined Aggregate Functions• User Defined Types• CLR Stored Procedures• CLR Triggers

Page 38: Programando SQL Server 2005 con el CLR Integración SQLCLR

SQL Server 2005 para desarrolladores Solid Quality Learning

38

Recursos SQL Server 2005 – Laboratorios Virtuales

http://msdn.demoservers.com/login.aspx?group=sql2005

http://www.microsoft.com/technet/traincert/virtuallab/sql.mspx(only supports SQL 2000)

SQL Server 2005http://www.microsoft.com/sql/2005

SQL Server Express http://www.microsoft.com/sql/express

Visual Studio 2005http://lab.msdn.microsoft.com/vs2005

Page 39: Programando SQL Server 2005 con el CLR Integración SQLCLR

39

SQL Server 2005 para desarrolladores Solid Quality Learning

Series de Webcasts

Introducción a SQL Server 2005 para desarrolladoresViernes, 22 de Julio de 2005 06:00 p.m.(GMT)http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277969&Culture=es-MX

Nuevas características del lenguaje T-SQL en SQL Server 2005Lunes, 25 de Julio de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277973&Culture=es-MX   

Aprovechando XML dentro de la base de datos con SQL Server 2005Viernes, 29 de Julio de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277975&Culture=es-MX

Programando SQL Server 2005 con el CLR – Integración SQL-CLRLunes, 01 de Agosto de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277977&Culture=es-MX  

Nuevas características en ADO.NET 2.0Viernes, 05 de Agosto de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277978&Culture=es-MX 

Page 40: Programando SQL Server 2005 con el CLR Integración SQLCLR

Gracias

[email protected]

[email protected]

Weblog: www.wiernik.net