The MySQL General Purpose Routine library Giuseppe Maxia.

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 taming PHP Thomas Krichel
Advertisements

PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Session 2Introduction to Database Technology Data Types and Table Creation.
CC SQL Utilities.
Transact-SQL. 1. Declare float = 10 select * from customers where discnt
Drop in replacement of MySQL. Agenda MySQL branch GPL licence Maria storage engine Virtual columns FederatedX storage engine PBXT storage engine XtraDB.
PRACTICAL PHP AND MYSQL WALKTHROUGH USING SAMPLE CODES – MAX NG.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
PHP (2) – Functions, Arrays, Databases, and sessions.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
MySQL Dr. Hsiang-Fu Yu National Taipei University of Education
Databases with PHP A quick introduction. Y’all know SQL and Databases  You put data in  You get data out  You can do processing on it very easily 
INTERNET APPLICATION DEVELOPMENT For More visit:
ASP.NET Programming with C# and SQL Server First Edition
Module 8 Improving Performance through Nonclustered Indexes.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Computer Science Standard Level Mastery Aspects. Mastery Item Claimed JustificationWhere Listed Arrays Used to store the student data Lines P.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Python MySQL Database Access
PHP meets MySQL.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
Oracle Data Integrator Procedures, Advanced Workflows.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
Most information comes from Chapter 3, MySQL Tutorial: 1 MySQL: Part.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
MySQL More… 1. More on SQL In MySQL, the Information Schema is the “Catalog” in the SQL standard SQL has three components: Data definition Data manipulation.
Visual Programing SQL Overview Section 1.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Connecting to MySQL using Java By:. – Required to use Java.sql so that we can use Connection and Queries using strings. – Javax.swing.* needed for components.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Oracle Data Integrator User Functions, Variables and Advanced Mappings
CHAPTER 10 PHP MySQL Database
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
There are two types of MySQL instructions (Data Definition Language) DDL: Create database, create table, alter table,,,. (Data Manipulation Language) DML.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Working With Database Library And Helpers. Connecting to your Database First you need to set parameters in you database.php file residing in config folder.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
IS232 Lab 9. CREATE USER Purpose: Use the CREATE USER statement to create and configure a database user, which is an account through which you can log.
Web Database Programming Using PHP
ASP.NET Programming with C# and SQL Server First Edition
Review 3 csc242 – web programming.
Stored Procedures.
Unix System Administration
Introduction to MySQL.
Web Database Programming Using PHP
Database Application Development
Error Handling Summary of the next few pages: Error Handling Cursors.
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
CS122B: Projects in Databases and Web Applications Winter 2018
Information Management
CS122B: Projects in Databases and Web Applications Spring 2018
SQL Transactions 4/23/2019 See scm-intranet.
SQL – Application Persistence Design Patterns
CS122B: Projects in Databases and Web Applications Winter 2019
Database Application Development
Presentation transcript:

The MySQL General Purpose Routine library Giuseppe Maxia

About me

MySQL General Purpose Stored Routines Library A public repository under GPL license routines for general database usage IT IS NOT FROM MySQL AB!

Components global variables simple and complex data structures loops syntax helpers named parameters test units  globals  arrays  for_each_loops  syntax  simple_sp  test_utilities

INSTALLATION create a database download the library from run library.mysql run the tests under the./test directory

INSTALLATION (1) $ mysql -e 'create schema lib' $ mysql lib < library.mysql globals test_utilities arrays for each simple sp syntax

INSTALLATION (2) $ cd tests $ mysql -t lib < test_library.mysql | total tests | passed | failed | passed tests percentage | | 56 | 56 | 0 | | | total tests | passed | failed | passed tests percentage | | 8 | 8 | 0 | |

Docs reference manual on the library web site Articles on Embedded syntax (we'll see it later)

globals (1) persistent variables (aren't invalidated when the session ends) Multi-user variables (each user can name his/her variables without conflicts) general utility functions

globals (2) call global_var_set('max_val',10); select | global_var_get('max_val') | | 10 | 10 |

globals (3) connect; Connection id: 416 Current database: lib select | global_var_get('max_val') | | 10 | NULL |

globals (4) drop table if exists t1; Query OK, 0 rows affected, 1 warning (0.01 sec) select table_exists(schema(), 't1'); | table_exists(schema(), 't1') | | 0 |

globals (5) create table t1 (i int); Query OK, 0 rows affected (0.01 sec) select table_exists(schema(), 't1'); | table_exists(schema(), 't1') | | 1 |

globals (6) # more functions view_exists(db, nome) table_or_view_exists(db, nome) function_exists(db,nome) procedure exists(db,nome routine_exists(db,nome,tipo) global_var_drop(nome) global_var_exists(nome)

arrays (1) persistent vectors Multi-user structures (each user can name his/her variables without conflicts) each array is at the same time a list, a hash, a queque and a heap sorting and merging routines

arrays (2) call array_set('myvals', 1, '100'); call array_set('myvals', 'foo', 200); call array_show('myvals'); | array_index | array_key | "myvals" values | | 1 | NULL | 100 | | 2 | foo | 200 |

arrays (3) select array_get('myvals',2); | array_get('myvals',2) | | 200 | select array_get('myvals','foo'); | array_get('myvals','foo') | | 200 |

arrays (as heaps) (4) call array_drop('myvals'); call array_push('myvals', '100'); call array_push('myvals', 200); call array_show('myvals'); | array_index | array_key | "myvals" values | | 0 | NULL | 100 | | 1 | NULL | 200 |

arrays (as heaps) (5) select array_pop('myvals'); | array_pop('myvals') | | 200 | select array_pop('myvals'); | array_pop('myvals') | | 100 |

arrays (as heaps) (6) select array_pop('myvals'); | array_pop('myvals') | | NULL | call array_show('myvals'); empty set (0.0 sec)

arrays (from list) (7) call array_from_list('100,200,300','myvals'); call array_show('myvals'); | array_index | array_key | "myvals" values | | 0 | NULL | 100 | | 1 | NULL | 200 | | 2 | NULL | 300 |

arrays (from list) (8) call array_from_pair_list( 'foo=>100,bar=>200,baz=>300','myvals'); call array_show('myvals'); | array_index | array_key | "myvals" values | | 0 | foo | 100 | | 1 | bar | 200 | | 2 | baz | 300 |

arrays (9) # more routines array_exists(name) array_create(name, how_many) array_clear(nome) array_get_by_index(name,num) array_get_by_key(name,key) array_set_by_index(name,num) array_set_by_key(name,key) # more : copy, merge, append, sort, grep, etc

„for_each“ loops (1) on-the-fly loops predefined loops counters set of tables table rows array elements

„for_each“ loops (2) call for_each_counter(1,3,1,'create table foo$N(i int)'); show tables like 'foo%'; | Tables_in_lib (foo%) | | foo1 | | foo2 | | foo3 |

„for_each“ loops (3) call for_each_table(schema(), 'table_name like "foo%"', 'insert into $D.$T values ($N)'); select * from foo1; | i | | 1 | select * from foo2; | i | | 2 |

„for_each“ loops (4) call for_each_counter(1,3,1,'drop table foo$N'); show tables like 'foo%'; empty set (0.00 sec)

syntax (1) provide usage syntax for each routine list of routines with filters support for named parameters (next section)

syntax (2) call my_routines('global'); | routine_schema | routine_name | routine_type | | lib | global_var_drop | function | | lib | global_var_drop | procedure | | lib | global_var_exists | function | | lib | global_var_get | function | | lib | global_var_set | function | | lib | global_var_set | procedure |

syntax (3) call my_procedures('global'); | routine_schema | routine_name | | lib | global_var_drop | | lib | global_var_set |

syntax (4) call my_functions('global'); | routine_schema | routine_name | | lib | global_var_drop | | lib | global_var_exists | | lib | global_var_get | | lib | global_var_set |

syntax (5) select fsyntax('global_var_get')\G ** function lib.global_var_get function global_var_get p_var_name varchar(50) -- Returns the value of a global variable p_var_name -- returns the variable value, null if variable does not exist -- see also: global_var_set, global_var_exists -- module : globals

simple_sp (1) lets you call routines with named parameters, rather than „by position“ requires the „syntax“ module to be installed as well

simple_sp (2) select psyntax('for_each_counter_complete')\G procedure for_each_counter_complete counter_start INT, # 1 counter_end INT, # 2 counter_delta INT, # 3 sql_command text, # 4 sql_before text, # 5 sql_after text, # 6 ba_mode enum("once","many") # 7 # 7 parameters!

simple_sp (3) # calling parameters by position call for_each_counter_complete( 1, 10, 1, + 1', = 0', 'once');

simple_sp (3) # calling parameters by name call simple_spl( 'for_each_counter_complete', = 0; counter_start=>1; counter_end=>10; counter_delta=>1; + 1; ba_mode=>once' );

test_utilities (1) implements utility routines to create unit tests good programming practice

test_utilities (2) # test_tutorial.mysql (1) call initialize_tests(); call check_routine( database(), 'make_table', 'procedure'); call check_routine( database(), 'get_result', 'function');

test_utilities (3) # test_tutorial.mysql (2) connect; insert into my_data values ('first', 1, 2), ('second',10,20), ('third', 100, 200); = get_result('first'); call log_test('get_result = = 3 );

test_utilities (4) # test_tutorial.mysql (3) = get_result('second'); call log_test('get_result = = 30 ); = get_result('third'); call log_test('get_result = = 300 );

test_utilities (5) # test_tutorial.mysql (4) = get_result('fourth'); call log_test('get_result is is null ); call show_test_results(); # So far, the routine to test is yet # to be created!

test_utilities (6) # test1.mysql (1) delimiter $$ drop procedure if exists make_table $$ create procedure make_table () deterministic begin drop table if exists my_data; create table my_data ( name char(10) not null primary key, val1 int, val2 int ); end $$

test_utilities (7) # test1.mysql (2) drop function if exists get_result $$ create function get_result ( p_name char(10)) returns int reads sql data begin declare mysum int; select val1 * val2 into mysum # mistake ! from my_data where name = p_name; return mysum; end $$ delimiter ;

test_utilities (8) # Let's try it! (1) $ mysql lib < examples/test1.mysql $ mysql -t lib < examples/test_tutorial.mysql | test no | description | result | expected | outcome | | 1 | make_table p exists | 1 | routine_exists = true | pass | | 2 | get_result f exists | 1 | routine_exists = true | pass | | 3 | table my_data exists | 1 | table_exists = true | pass | | 4 | get_result first | 2 = 3 | ** fail ** | | 5 | get_result second | 200 = 30 | ** fail ** | | 6 | get_result third | = 300 | ** fail ** | | 7 | get_result non-existent | NULL is null | pass |

test_utilities (9) # Let's try it! (2) $ mysql -t lib < examples/test_tutorial.mysql | total tests | passed | failed | passed tests percentage | | 7 | 4 | 3 | | | test no | description | result | expected | outcome | | 4 | get_result first | 2 = 3 | ** fail ** | | 5 | get_result second | 200 = 30 | ** fail ** | | 6 | get_result third | = 300 | ** fail ** |

test_utilities (7) # let's fix the mistake drop function if exists get_result $$ create function get_result ( p_name char(10)) returns int reads sql data begin declare mysum int; select val1 + val2 into mysum # correct ! from my_data where name = p_name; return mysum; end $$ delimiter ;

test_utilities (8) # Let's try again $ mysql lib < examples/test1.mysql $ mysql -t lib < examples/test_tutorial.mysql | test no | description | result | expected | outcome | | 1 | make_table p exists | 1 | routine_exists = true | pass | | 2 | get_result f exists | 1 | routine_exists = true | pass | | 3 | table my_data exists | 1 | table_exists = true | pass | | 4 | get_result first | 3 = 3 | pass | | 5 | get_result second | 30 = 30 | pass | | 6 | get_result third | 300 = 300 | pass | | 7 | get_result non-existent | NULL is null | pass | | total tests | passed | failed | passed tests percentage | | 7 | 7 | 0 | |

THANKS Any questions?