sql pl4ocl an automatic code generator from ocl to sql
play

SQL-PL4OCL: an automatic code generator from OCL to SQL procedural - PowerPoint PPT Presentation

SQL-PL4OCL: an automatic code generator from OCL to SQL procedural language Marina Egea and Carolina Dania Sep. 19, 2017 MoDELS. Austin, Texas Outline Motivation Background Mapping OCL to SQL-PL How to map data models


  1. SQL-PL4OCL: an automatic code generator from OCL to SQL procedural language Marina Egea and Carolina Dania Sep. 19, 2017 MoDELS. Austin, Texas

  2. Outline Motivation • Background • Mapping OCL to SQL-PL • How to map data models • How to map OCL expressions • Tool • Benchmark • Conclusions • 2

  3. OCL as a query language Motivation •Evaluation of OCL expression on medium/large scenarios. •Integration of OCL expressions (invariants/queries) into an automated code generation process where the persistent layer are SQL/PL databases 3

  4. Background 4

  5. UML (Unified Modeling Language) Ex. Social Network Class diagram Timeline Post 0..1 posts -age: Integer -id: String postedOn • classes * 0..1 owns • attributes belongsTo 0..1 Photo Status Profile • associations (association-ends) * • inheritance friends * Ph1: Photo Object diagram +id : 2390 T1: Timeline • objects Ph2: Photo +id : 2391 • values • links Ph3: Photo Alice: Profile +id : 2392 +age : 24 5

  6. OCL (Object Constraint Language) • It is a general-purpose (textual) formal language that allows: • retrieve objects and their values • navigate through related objects • It supports a set of types with a set of operations over them, and • primitive types (Integer, String, Boolean), and • collection types (Set, Bag, OrderedSet, and Sequence), and • operators like: +, -, >, <, size, isEmpty, notEmpty, characters, and • iterators like: forAll, exists, collect 6

  7. OCL (Object Constraint Language) Timeline Post 0..1 posts All instances of Timeline -age: Integer • -id: String postedOn * owns 0..1 Timeline . allInstances () belongsTo 0..1 Photo Status Profile * Number of instances • friends * Timeline.allInstances() − > size() Every profile is older than 18 years old • Profile.allInstances() − > forAll(p | p.age > 18) There isn’t any profile older than 18 • Profile.allInstances() − > select(p | p.age > 18) − > isEmpty() Convert the string ‘hi’ in a sequence of characters • ’hi’.characters() 7

  8. Databases • Structured Query language (SQL) • RBMS: MySQL, MariaDB, PostgreSQL, and MS SQL. 1.queries select * from Photo select * from (select * from Photo) as t 8

  9. Databases • Structured Query language (SQL) • RBMS: MySQL, MariaDB, PostgreSQL, and MS SQL. 1.queries 2.sentences create temporary table Photo(pk Int); insert into Photo(val) (select pk from Photo); 8

  10. Databases • Structured Query language (SQL) • RBMS: MySQL, MariaDB, PostgreSQL, and MS SQL. - cursors - conditionals 1.queries 2.sentences 3.store procedures - loops declare procedure nameProc begin ... end; call nameProc; 8

  11. Mapping OCL to SQL-PL 9

  12. Mapping OCL to SQL-PL transformation definiton language language is is is written used written in by in transformation PIM PSM tool 10

  13. Mapping OCL to SQL-PL Formal definition transformation definiton UML/OCL SQL/PL Theoretical framework language language is is is written used written in by in Class/objects Databases diagrams Stored SQL-PL4OCL Queries procedures transformation PIM PSM tool Implementation 10

  14. From OCL to SQL-PL Mapping data/object models a table with a column for each class • a column for each attribute • a table with two columns for each association • Alice: Profile Bob: Profile Object model age: 18 age: 10 a row for each object in the table associated with the class • a row for each link in the corresponding table • table: Profile table: friendship pk myFriends myFriends friendsOf friendsOf pk age age 1 2 1 18 2 10 11

  15. From OCL to SQL-PL Mapping OCL expressions Every expression is mapped into a stored procedure create procedure name begin OCL to SQL-PL expression end ; // call name() // The mapping is recursive over the expression. Depending on the complexity of the OCL expressions, they are mapped: • into a SQL query • into a SQL query and need an auxiliary block definition 12

  16. From OCL to SQL-PL Mapping OCL expressions (cont.) Expressions that are mapping into a SQL query • Timeline . allInstances () create procedure name select Timeline.pk as val begin from Timeline ; end; // call name(); // 13

  17. From OCL to SQL-PL Mapping OCL expressions (cont.) Expressions that are mapping into a SQL query • Timeline . allInstances () create procedure name begin select Timeline.pk as val ; from Timeline end; // call name(); // 13

  18. From OCL to SQL-PL Mapping OCL expressions (cont.) Expressions that are mapping into a SQL query • Timeline . allInstances () create procedure name select Timeline.pk as val begin from Timeline Timeline.allInstances() − > size() ; select count(t1.val) as val end; // from call name(); // ( ) as t1 13

  19. From OCL to SQL-PL Mapping OCL expressions (cont.) Expressions that are mapping into a SQL query • Timeline . allInstances () create procedure name begin Timeline.allInstances() − > size() ; select count(t1.val) as val end; // from call name(); // ( select Timeline.pk as val ) as t1 from Timeline 13

  20. From OCL to SQL-PL Mapping OCL expressions (cont.) Expressions that are mapping into a SQL query • Timeline . allInstances () create procedure name begin select count(t1.val) as val from Timeline.allInstances() − > size() select Timeline.pk as val ( from Timeline ) as t1 ; end; // call name(); // 13

  21. From OCL to SQL-PL Mapping OCL expressions (cont.) Expressions that are mapped into a SQL query and need an auxiliary • block definition ’hi’.characters() pos val 1 h create procedure name 2 i begin begin drop table if exists wchars; create temporary table wchars (pos int not null auto increment, 
 val varchar(250), primary key( pos )); insert into wchars(val) ( select ’ h’ as val); insert into wchars(val) ( select ’i’ as val); end; select val from wchars order by pos; end ; // 14

  22. 
 
 From OCL to SQL-PL 
 Intermediate tables and queries Primitive types, sets, and bags OrderedSets and sequences create temporary table name 
 create temporary table name 
 ( val type ); 
 ( val type , 
 Tables pos int not null auto increment, 
 primary key( pos )); select val 
 select val 
 from name; 
 from name 
 Queries order by pos; 
 15

  23. From OCL to SQL-PL Mapping OCL expressions (cont.) ‘ hi ‘ . characters () − > union (‘ ho ‘ . characters ()) create procedure name begin begin begin drop table if exists wchars1; create temporary table wchars1 (pos int not null auto increment, val varchar(250), primary key( pos )); insert into wchars1(val) ( select ’ h’ as val); insert into wchars1(val) ( select ’i’ as val); end; begin drop table if exists wchars2; create temporary table wchars2 (pos int not null auto increment, val varchar(250), primary key( pos )); insert into wchars2(val) ( select ’ h’ as val); insert into wchars2(val) ( select ’o’ as val); end; create temporary table union(pos int NOT NULL auto_increment, val varchar(250), primary key (pos)); insert into union(val) (select t1.val as val from (select val from wchars1 order by pos asc) as t1); insert into union(val) (select t1.val as val from (select val from wchars2 order by pos asc) as t1); end; select val from union order by pos; end ; // call name(); // 16

  24. From OCL to SQL-PL Mapping OCL expressions (cont.) ‘ hi ‘ . characters () − > union (‘ ho ‘ . characters ()) create procedure name begin begin begin drop table if exists wchars1; ‘ hi ‘ . characters () create temporary table wchars1 (pos int not null auto increment, val varchar(250), primary key( pos )); insert into wchars1(val) ( select ’ h’ as val); insert into wchars1(val) ( select ’i’ as val); end; begin drop table if exists wchars2; ‘ ho ‘ . characters () create temporary table wchars2 (pos int not null auto increment, val varchar(250), primary key( pos )); insert into wchars2(val) ( select ’ h’ as val); insert into wchars2(val) ( select ’o’ as val); end; create temporary table union(pos int NOT NULL auto_increment, val varchar(250), primary key (pos)); insert into union(val) (select t1.val as val from (select val from wchars1 order by pos asc) as t1); insert into union(val) (select t1.val as val from (select val from wchars2 order by pos asc) as t1); end; select val from union order by pos; end ; // call name(); // 16

  25. From OCL to SQL-PL 
 Structures in Store Procedures create procedure name create procedure name begin begin begin begin begin … … end; // … begin … … end; // end; / end; // … end; // end; // call name(); // call name(); // Nested blocks structure Sequencial blocks structure 17

  26. From OCL to SQL-PL 
 Iterators begin src − > it(body) declare done int default 0; declare var; declare crs cursor for ( cursor-specific type - src ); declare continue handler for sqlstate ’02000’ set done = 1; drop table if exists blq_name; create temporary table blq_name ( value-specif type ) open crs; repeat fetch crs into var; if not done then Iterator-specific body query Iterator-specific processing code end if ; 
 until done end repeat ; close crs; end ; // 18

Recommend


More recommend