SlideShare a Scribd company logo
Accessing MySQL from Ruby
Contents
1.      Introduction .................................................................................................................................... 2
2.      Prerequisites ................................................................................................................................... 2
3.      Setup ............................................................................................................................................... 2
     3.1.       Populate MySQL Database ...................................................................................................... 2
     3.2.       Set up ODBC Access ................................................................................................................ 3
4.      Discovery ......................................................................................................................................... 4
     4.1.       Web Service Creation using SOA Gateway ............................................................................. 4
     4.2.       Accessing the WSDL ................................................................................................................ 6
5.      Accessing a Web Service with Ruby ................................................................................................ 8
     Ruby Editor.......................................................................................................................................... 8
     Ruby Code ........................................................................................................................................... 8
     Debugging Code .................................................................................................................................. 9
6.      Conclusion ..................................................................................................................................... 10
7.      Appendix ....................................................................................................................................... 10




Accessing MySQL from Ruby                                                                              Author: John O’Mahony, Risaris Ltd
1. Introduction
In this tutorial we will show you how to build a Ruby application to access MySQL via the SOA
Gateway.


2. Prerequisites
It is assumed that you are running the 3 components, MySQL, Ruby and the SOA Gateway on
Windows.

It is assumed you already have a SOA Gateway server and Control Centre installed. See here for
more info about installing the SOA Gateway.


3. Setup
Download the latest binaries for Ruby from the Ruby downloads page, install it
according to the install.txt document in the Ruby distribution library. For this tutorial I downloaded
Ruby 1.8.6 One-Click Installer and followed the default options.

You will also need a MySQL database. Again, the Open Source version (known as the MySQL
Community Server) can be freely downloaded from the MySQL website. See this link for download,
and here to step you through the installation and configuration.

    3.1.Populate MySQL Database
    Now that you’ve got MySQL installed and configured, you will need to populate it with some
    demo data. For this we use the RisarisBank sample. This is available here.

           Save this file to “C:TempRisarisBank.sql”
           Connect to the MySQL Server using the mysql command.

            E.g shell> mysql –u root –p

            This command connects to the server using the MySQL root account to make sure that
            you'll have permission to create the RisarisBank database. The -p option tells mysql
            to prompt you for the root password. Enter the password when prompted. (Remember
            that the MySQL root account is not the same as the operating system root account
            and probably will have a different password.)

           Create the RisarisBank database.

            mysql> CREATE DATABASE RisarisBank;

            mysql> use RisarisBank;

           Load the contents of RisarisBank.sql into the RisarisBank database. E.g.

            mysql> SOURCE c:TempRisarisBank.sql

           After the SOURCE command finishes, you can view your new tables.




Accessing MySQL from Ruby                                          Author: John O’Mahony, Risaris Ltd
mysql> SHOW TABLES;

           mysql> DESCRIBE CustomerInformation;

           mysql> DESCRIBE Branch;

           etc …

   3.2.Set up ODBC Access
   The final thing to do with your MySQL Database is to set up an ODBC DSN which will be used by
   the SOA Gateway to access this database.

   Click Start, Control Panel, Administrative Tools, Data Sources (ODBC)

   From the resulting screen, choose the “System DSN” Tab.

   Click Add

   From the list of data source drivers, select “MySQL ODBC 3.51 Driver”.

       If you do not see this driver in the list, you need to install the MySQL Connector. See here for
       more information. We recommend installing v3.51.

   Click Finish, and a window will appear allowing you to enter the DSN information. Add the
   following:

       Data Source Name: RisarisBank

       Description: The Risaris Bank Sample in MySQL

       Server: localhost

       User: root

       Password: *** your MySQL root password ***

       Database: RisarisBank (select from the drop down list)




Accessing MySQL from Ruby                                          Author: John O’Mahony, Risaris Ltd
All other options can be left as-is. Click OK.


4. Discovery
At this stage you’ve got Ruby installed, and a MySQL database with some sample data in it. In this
section we’ll show you how to create web services from each of the MySQL tables. These web
services can be used by the Ruby language (and many others) to give you direct real-time access to
your MySQL Data.

   4.1.Web Service Creation using SOA Gateway
   Start your SOA Gateway Control Centre. See here for an introduction to the Control Centre.

   In your servers view, right click the entry which represents your local SOA Gateway Server. Select
   “Create New Web Services”.




Accessing MySQL from Ruby                                        Author: John O’Mahony, Risaris Ltd
From the next dialog, choose “MySQL Driver”. If you do not see have a MySQL Driver in the list,
   see how to create one here.




   Click Next.

   The next screen gives you the ability to add information about your DSN




Accessing MySQL from Ruby                                       Author: John O’Mahony, Risaris Ltd
Enter the above information and click Discover.

   The wizard will display all the tables it finds at this (RisarisBank) DSN.

   Click “Select All”, and click “Import”.

   The wizard will create web services from each one of these tables.




   You’ve just created 8 Web Services from your 8 MySQL Tables!



   4.2.Accessing the WSDL
   Web Service Description Language (WSDL) is a standard, XML-based language that is used to
   describe a Web Service.

   For each of the 8 web services you’ve created in the previous section, the SOA Gateway provides
   you with a WSDL to describe the Web Service. The WSDL itself is usually interpreted by a web



Accessing MySQL from Ruby                                            Author: John O’Mahony, Risaris Ltd
service client, such as Ruby, but it is useful to know where to find the WSDL for each of your
   Web Services.

   As WSDL is XML-based, it will open in your browser of choice. To see the WSDL for one of your
   Risaris Bank web services, do the following in your SOA Gateway Control Centre:

          Click on the web service you are interested in, for example the “branch” web service.
          The properties for this web service should appear in your Properties View. If you do not
           see the Properties view, select Window -> Show View -> Other -> General -> Properties
           and click OK.
          In the properties view, there is a link to your WSDL. Click it to open the WSDL in a
           browser.




Accessing MySQL from Ruby                                         Author: John O’Mahony, Risaris Ltd
You can view the WSDL for the other web services by clicking the link from their properties
       view.
       This WSDL is the starting point for using Web Services, and can be used time and again by
       different web service clients.




5. Accessing a Web Service with Ruby
We will use a Ruby script to access our new Risaris Bank Web Services via the WSDL. Those familiar
with Ruby will probably want to change the script provided but it can be run with minimum changes
described below. The whole script is provided in APPENDIX A.

    Ruby Editor
   Use the editor provided, SciTE.exe, which is located in folder scite where you installed Ruby.
   Copy the code from Appendix A and save the code as the filename RBCustomerTutorial.rb (as in
   example below).

   Hit F5 and the results will appear in the right-hand pane:




   Ruby Code
    Depending on your MySQL settings you will have to change the username/password provided in
   the on_simple_outbound method. In this example the user is root with no password.


   "<UsernameToken><Username>root</Username><Password></Password></UsernameToken>"



Accessing MySQL from Ruby                                       Author: John O’Mahony, Risaris Ltd
Note also the WSDL URL http://localhost:56000/customerinformation?WSDL. Again depending
   on your SOA Gateway configuration you may need to change the server/port number.

   In this example provided we are issuing a list request on the customer information table in the
   RisarisBank database. The list request can handle a wilcarded customer number key:

   param = {"CustomerNumber" => "1*"}

   result = soap.list(param)

   Finally we can query the result for the customer’s first name:

   for cust in (result. customerinformationRoot.customerinformationGroup)

    print(cust.firstName, "n")

   end

   Obviously this can be replaced by any of the column names in the customer information table.

   Debugging Code
   The code provided should work off the bat. However, depending on your MySQL configuration,
   the RisarisBank tables may not be in lowercase as in our example e.g. customerinformation
   versus CustomerInformation. If this is the case the code will not work as is and all occurrences of
   customerinformation should be replaced by CustomerInformation.

   The p and puts functions are useful for checking the content of variables. For example p result
   after the result = soap.list(param) call gives the following information and is useful for checking
   column names for adding new code.

   #<SOAP::Mapping::Object:0x3240c04
   {}customerinformationRoot=#<SOAP::Mapping::Object:0x3240ad8
   {}customerinformationGroup=[#<SOAP::Mapping::Object:0x32409ac
   {}CustomerNumber="1" {}FirstName="Casper" {}Surname="Ankergren"
   {}AddressLine1="34 Green Street" {}AddressLine2="Crosses Street"
   {}City="Mansfield" {}Postcode="MA5 9AJ" {}DateOfBirth="30/05/1978">,
   #<SOAP::Mapping::Object:0x323f4bc {}CustomerNumber="10"
   {}FirstName="Grenville" {}Surname="Dekker" {}AddressLine1="22 Uttoxter
   New Road" {}AddressLine2="Chaddesden" {}City="Leeds" {}Postcode="LS4
   9WB" {}DateOfBirth="21/10/1974">, #<SOAP::Mapping::Object:0x323e0b2
   {}CustomerNumber="11" {}FirstName="Jane" {}Surname="Freeman"
   {}AddressLine1="3 Portland Street" {}AddressLine2="Lichfield"
   {}City="Gloucester" {}Postcode="GL4 8KD" {}DateOfBirth="26/10/1980">,
   #<SOAP::Mapping::Object:0x323ccb2 {}CustomerNumber="12"
   {}FirstName="Dale" {}Surname="Rolling" {}AddressLine1="The Hunters Rest"
   {}AddressLine2="Branston" {}City="Preston" {}Postcode="PR4 4HG"
   {}DateOfBirth="06/12/1989">, #<SOAP::Mapping::Object:0x323b8b2
   {}CustomerNumber="13" {}FirstName="Arnold" {}Surname="Corrigan"
   {}AddressLine1="14 Anderson Street" {}AddressLine2="Allestree"
   {}City="Derby" {}Postcode="DE6 8FT" {}DateOfBirth="10/06/1970">,
   #<SOAP::Mapping::Object:0x323a4b2 {}CustomerNumber="14"
   {}FirstName="Alfred" {}Surname="Summerscale" {}AddressLine1="3701 S.



Accessing MySQL from Ruby                                           Author: John O’Mahony, Risaris Ltd
George Mason" {}AddressLine2="Melbourne" {}City="Bath" {}Postcode="BA6
   8UU" {}DateOfBirth="07/08/1982">, #<SOAP::Mapping::Object:0x32390b2
   {}CustomerNumber="15" {}FirstName="Peter" {}Surname="Spiegel"
   {}AddressLine1="11663 Charter Oak Co" {}AddressLine2="Foremark"
   {}City="Manchester" {}Postcode="M98 4GT" {}DateOfBirth="29/12/1971">,
   #<SOAP::Mapping::Object:0x3237cb2 {}CustomerNumber="16"
   {}FirstName="John" {}Surname="Sviridov" {}AddressLine1="3319 Rosemere
   Court" {}AddressLine2="Brailsford" {}City="Leeds" {}Postcode="LS8 5QQ"
   {}DateOfBirth="12/06/1984">, #<SOAP::Mapping::Object:0x32368b2
   {}CustomerNumber="17" {}FirstName="Sarah" {}Surname="Hall"
   {}AddressLine1="4 Denmark Lane" {}AddressLine2="East Bridgford"
   {}City="Portsmouth" {}Postcode="PO9 7QN" {}DateOfBirth="12/02/1983">,
   #<SOAP::Mapping::Object:0x32354b2 {}CustomerNumber="18"
   {}FirstName="Elspeth" {}Surname="Jones" {}AddressLine1="12375 W. Ohio
   Circle" {}AddressLine2="Risley" {}City="Birmingham" {}Postcode="B93 1IO"
   {}DateOfBirth="28/08/1986">, #<SOAP::Mapping::Object:0x32340b2
   {}CustomerNumber="19" {}FirstName="Nigel" {}Surname="Wyllis"
   {}AddressLine1="14 Borough Road" {}AddressLine2="Ockbrook"
   {}City="Leeds" {}Postcode="LS8 2WL" {}DateOfBirth="29/04/1981">]>>



6. Conclusion
This tutorial shows how to access MySQL from Ruby using the SOA Gateway. As you can see, you
have built a powerful application that uses Web Services to retrieve information in real-time.


7. Appendix
require 'soap/wsdlDriver'

require 'soap/header/simplehandler'



# A handler to add authentication parameters to the SOAP header.

  class AutheticationHeaderHandler < SOAP::Header::SimpleHandler

      @@HEADER_NAME = 'Security'

     def initialize(namespace)

           super(XSD::QName.new(namespace, @@HEADER_NAME))

     end



     def on_simple_outbound

       #Change the username/password below to reflect your own environment


"<UsernameToken><Username>root</Username><Password></Password></UsernameTok
en>"




Accessing MySQL from Ruby                                      Author: John O’Mahony, Risaris Ltd
end

  end

wsdl_url = "http://localhost:56000/customerinformation?WSDL"



soap = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver

soap.wiredump_file_base = "soapresult"

soap.headerhandler <<
AutheticationHeaderHandler.new("http://www.risaris.com/security")



param = {"CustomerNumber" => "1*"}



result = soap.list(param)

for cust in (result.customerinformationRoot.customerinformationGroup)

  print(cust.firstName, "n")

end




Accessing MySQL from Ruby                       Author: John O’Mahony, Risaris Ltd

More Related Content

What's hot (15)

PDF
New Flash Builder 4 WSDL and HTTP Connectors
rtretola
 
DOCX
Creating and connecting to odi master and work repositories
Abdoulaye M Yansane
 
PDF
WebLogic FAQs
Amit Sharma
 
PPTX
Nick harris-sic-2011
Seattle Interactive Conference
 
PPTX
Session And Cookies In Servlets - Java
JainamParikh3
 
PDF
Java API for XML Web Services (JAX-WS)
Peter R. Egli
 
PPT
JAX-WS Basics
Wes Williams
 
PPTX
IBM Connect 2016 - Break out of the Box
Karl-Henry Martinsson
 
PPTX
Break out of The Box - Part 2
Karl-Henry Martinsson
 
PDF
AD102 - Break out of the Box
Karl-Henry Martinsson
 
PDF
11-DWR-and-JQuery
tutorialsruby
 
PDF
Dwr
Suhas Kamble
 
PDF
Bt0083 server side programing
Techglyphs
 
PPT
Servlet ppt by vikas jagtap
Vikas Jagtap
 
PDF
Java web services using JAX-WS
IndicThreads
 
New Flash Builder 4 WSDL and HTTP Connectors
rtretola
 
Creating and connecting to odi master and work repositories
Abdoulaye M Yansane
 
WebLogic FAQs
Amit Sharma
 
Nick harris-sic-2011
Seattle Interactive Conference
 
Session And Cookies In Servlets - Java
JainamParikh3
 
Java API for XML Web Services (JAX-WS)
Peter R. Egli
 
JAX-WS Basics
Wes Williams
 
IBM Connect 2016 - Break out of the Box
Karl-Henry Martinsson
 
Break out of The Box - Part 2
Karl-Henry Martinsson
 
AD102 - Break out of the Box
Karl-Henry Martinsson
 
11-DWR-and-JQuery
tutorialsruby
 
Bt0083 server side programing
Techglyphs
 
Servlet ppt by vikas jagtap
Vikas Jagtap
 
Java web services using JAX-WS
IndicThreads
 

Viewers also liked (7)

PDF
rails.html
tutorialsruby
 
PDF
Devoxx%202008%20Tutorial
tutorialsruby
 
PDF
ruby-efl-tutorial-hsyl20
tutorialsruby
 
PDF
lab56_db
tutorialsruby
 
PDF
has_many_and_belongs_to_many
tutorialsruby
 
PDF
wtst3_pettichord3
tutorialsruby
 
PDF
rubyonrails
tutorialsruby
 
rails.html
tutorialsruby
 
Devoxx%202008%20Tutorial
tutorialsruby
 
ruby-efl-tutorial-hsyl20
tutorialsruby
 
lab56_db
tutorialsruby
 
has_many_and_belongs_to_many
tutorialsruby
 
wtst3_pettichord3
tutorialsruby
 
rubyonrails
tutorialsruby
 
Ad

Similar to Accessing_MySQL_from_Ruby (20)

PDF
Accessing my sql_from_java
Tran Rean
 
DOC
Database Security Explained
wensheng wei
 
PDF
Mysql In A Nutshell In A Nutshell Oreilly Second Edition Russell Dyer
atchaaslaney
 
PDF
Php mysql-tutorial-en
soft deal solution
 
PDF
SULTHAN's PHP, MySQL & wordpress
SULTHAN BASHA
 
PDF
Mysql tutorial 5257
Phuong Do Anh
 
PDF
MySQL en el mundo real. Evolución desde la compra por Oracle
LibreCon
 
PDF
MySQL Reference Manual
webhostingguy
 
PPT
Mysql ppt
Sanmuga Nathan
 
PPT
Ruby - The Hard Bits
Paul Gallagher
 
PPS
Linux17 MySQL_installation
Jainul Musani
 
PPT
Mysql
guest817344
 
PPTX
Clustrix Database Percona Ruby on Rails benchmark
Clustrix
 
PDF
Playing in the Same Sandbox: MySQL and Oracle
lynnferrante
 
PDF
digital marketing training in bangalore
Venus Tech Inc.
 
DOC
My sql technical reference manual
Mir Majid
 
PDF
Learning Mysql Seyed Mm Saied Tahaghoghi Hugh Williams
margasmst
 
ODP
Sql installation
Balakumaran Arunachalam
 
PDF
Prototype And Test Eh
novo_ed
 
PDF
Prototype And Test Eh5
novo_ed
 
Accessing my sql_from_java
Tran Rean
 
Database Security Explained
wensheng wei
 
Mysql In A Nutshell In A Nutshell Oreilly Second Edition Russell Dyer
atchaaslaney
 
Php mysql-tutorial-en
soft deal solution
 
SULTHAN's PHP, MySQL & wordpress
SULTHAN BASHA
 
Mysql tutorial 5257
Phuong Do Anh
 
MySQL en el mundo real. Evolución desde la compra por Oracle
LibreCon
 
MySQL Reference Manual
webhostingguy
 
Mysql ppt
Sanmuga Nathan
 
Ruby - The Hard Bits
Paul Gallagher
 
Linux17 MySQL_installation
Jainul Musani
 
Clustrix Database Percona Ruby on Rails benchmark
Clustrix
 
Playing in the Same Sandbox: MySQL and Oracle
lynnferrante
 
digital marketing training in bangalore
Venus Tech Inc.
 
My sql technical reference manual
Mir Majid
 
Learning Mysql Seyed Mm Saied Tahaghoghi Hugh Williams
margasmst
 
Sql installation
Balakumaran Arunachalam
 
Prototype And Test Eh
novo_ed
 
Prototype And Test Eh5
novo_ed
 
Ad

More from tutorialsruby (20)

PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PDF
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
tutorialsruby
 
PDF
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
tutorialsruby
 
PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PDF
Standardization and Knowledge Transfer – INS0
tutorialsruby
 
PDF
xhtml_basics
tutorialsruby
 
PDF
xhtml_basics
tutorialsruby
 
PDF
xhtml-documentation
tutorialsruby
 
PDF
xhtml-documentation
tutorialsruby
 
PDF
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
PDF
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
PDF
HowTo_CSS
tutorialsruby
 
PDF
HowTo_CSS
tutorialsruby
 
PDF
BloggingWithStyle_2008
tutorialsruby
 
PDF
BloggingWithStyle_2008
tutorialsruby
 
PDF
cascadingstylesheets
tutorialsruby
 
PDF
cascadingstylesheets
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Standardization and Knowledge Transfer – INS0
tutorialsruby
 
xhtml_basics
tutorialsruby
 
xhtml_basics
tutorialsruby
 
xhtml-documentation
tutorialsruby
 
xhtml-documentation
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
HowTo_CSS
tutorialsruby
 
HowTo_CSS
tutorialsruby
 
BloggingWithStyle_2008
tutorialsruby
 
BloggingWithStyle_2008
tutorialsruby
 
cascadingstylesheets
tutorialsruby
 
cascadingstylesheets
tutorialsruby
 

Recently uploaded (20)

PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Python basic programing language for automation
DanialHabibi2
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 

Accessing_MySQL_from_Ruby

  • 1. Accessing MySQL from Ruby Contents 1. Introduction .................................................................................................................................... 2 2. Prerequisites ................................................................................................................................... 2 3. Setup ............................................................................................................................................... 2 3.1. Populate MySQL Database ...................................................................................................... 2 3.2. Set up ODBC Access ................................................................................................................ 3 4. Discovery ......................................................................................................................................... 4 4.1. Web Service Creation using SOA Gateway ............................................................................. 4 4.2. Accessing the WSDL ................................................................................................................ 6 5. Accessing a Web Service with Ruby ................................................................................................ 8 Ruby Editor.......................................................................................................................................... 8 Ruby Code ........................................................................................................................................... 8 Debugging Code .................................................................................................................................. 9 6. Conclusion ..................................................................................................................................... 10 7. Appendix ....................................................................................................................................... 10 Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd
  • 2. 1. Introduction In this tutorial we will show you how to build a Ruby application to access MySQL via the SOA Gateway. 2. Prerequisites It is assumed that you are running the 3 components, MySQL, Ruby and the SOA Gateway on Windows. It is assumed you already have a SOA Gateway server and Control Centre installed. See here for more info about installing the SOA Gateway. 3. Setup Download the latest binaries for Ruby from the Ruby downloads page, install it according to the install.txt document in the Ruby distribution library. For this tutorial I downloaded Ruby 1.8.6 One-Click Installer and followed the default options. You will also need a MySQL database. Again, the Open Source version (known as the MySQL Community Server) can be freely downloaded from the MySQL website. See this link for download, and here to step you through the installation and configuration. 3.1.Populate MySQL Database Now that you’ve got MySQL installed and configured, you will need to populate it with some demo data. For this we use the RisarisBank sample. This is available here.  Save this file to “C:TempRisarisBank.sql”  Connect to the MySQL Server using the mysql command. E.g shell> mysql –u root –p This command connects to the server using the MySQL root account to make sure that you'll have permission to create the RisarisBank database. The -p option tells mysql to prompt you for the root password. Enter the password when prompted. (Remember that the MySQL root account is not the same as the operating system root account and probably will have a different password.)  Create the RisarisBank database. mysql> CREATE DATABASE RisarisBank; mysql> use RisarisBank;  Load the contents of RisarisBank.sql into the RisarisBank database. E.g. mysql> SOURCE c:TempRisarisBank.sql  After the SOURCE command finishes, you can view your new tables. Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd
  • 3. mysql> SHOW TABLES; mysql> DESCRIBE CustomerInformation; mysql> DESCRIBE Branch; etc … 3.2.Set up ODBC Access The final thing to do with your MySQL Database is to set up an ODBC DSN which will be used by the SOA Gateway to access this database. Click Start, Control Panel, Administrative Tools, Data Sources (ODBC) From the resulting screen, choose the “System DSN” Tab. Click Add From the list of data source drivers, select “MySQL ODBC 3.51 Driver”. If you do not see this driver in the list, you need to install the MySQL Connector. See here for more information. We recommend installing v3.51. Click Finish, and a window will appear allowing you to enter the DSN information. Add the following: Data Source Name: RisarisBank Description: The Risaris Bank Sample in MySQL Server: localhost User: root Password: *** your MySQL root password *** Database: RisarisBank (select from the drop down list) Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd
  • 4. All other options can be left as-is. Click OK. 4. Discovery At this stage you’ve got Ruby installed, and a MySQL database with some sample data in it. In this section we’ll show you how to create web services from each of the MySQL tables. These web services can be used by the Ruby language (and many others) to give you direct real-time access to your MySQL Data. 4.1.Web Service Creation using SOA Gateway Start your SOA Gateway Control Centre. See here for an introduction to the Control Centre. In your servers view, right click the entry which represents your local SOA Gateway Server. Select “Create New Web Services”. Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd
  • 5. From the next dialog, choose “MySQL Driver”. If you do not see have a MySQL Driver in the list, see how to create one here. Click Next. The next screen gives you the ability to add information about your DSN Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd
  • 6. Enter the above information and click Discover. The wizard will display all the tables it finds at this (RisarisBank) DSN. Click “Select All”, and click “Import”. The wizard will create web services from each one of these tables. You’ve just created 8 Web Services from your 8 MySQL Tables! 4.2.Accessing the WSDL Web Service Description Language (WSDL) is a standard, XML-based language that is used to describe a Web Service. For each of the 8 web services you’ve created in the previous section, the SOA Gateway provides you with a WSDL to describe the Web Service. The WSDL itself is usually interpreted by a web Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd
  • 7. service client, such as Ruby, but it is useful to know where to find the WSDL for each of your Web Services. As WSDL is XML-based, it will open in your browser of choice. To see the WSDL for one of your Risaris Bank web services, do the following in your SOA Gateway Control Centre:  Click on the web service you are interested in, for example the “branch” web service.  The properties for this web service should appear in your Properties View. If you do not see the Properties view, select Window -> Show View -> Other -> General -> Properties and click OK.  In the properties view, there is a link to your WSDL. Click it to open the WSDL in a browser. Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd
  • 8. You can view the WSDL for the other web services by clicking the link from their properties view. This WSDL is the starting point for using Web Services, and can be used time and again by different web service clients. 5. Accessing a Web Service with Ruby We will use a Ruby script to access our new Risaris Bank Web Services via the WSDL. Those familiar with Ruby will probably want to change the script provided but it can be run with minimum changes described below. The whole script is provided in APPENDIX A. Ruby Editor Use the editor provided, SciTE.exe, which is located in folder scite where you installed Ruby. Copy the code from Appendix A and save the code as the filename RBCustomerTutorial.rb (as in example below). Hit F5 and the results will appear in the right-hand pane: Ruby Code Depending on your MySQL settings you will have to change the username/password provided in the on_simple_outbound method. In this example the user is root with no password. "<UsernameToken><Username>root</Username><Password></Password></UsernameToken>" Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd
  • 9. Note also the WSDL URL http://localhost:56000/customerinformation?WSDL. Again depending on your SOA Gateway configuration you may need to change the server/port number. In this example provided we are issuing a list request on the customer information table in the RisarisBank database. The list request can handle a wilcarded customer number key: param = {"CustomerNumber" => "1*"} result = soap.list(param) Finally we can query the result for the customer’s first name: for cust in (result. customerinformationRoot.customerinformationGroup) print(cust.firstName, "n") end Obviously this can be replaced by any of the column names in the customer information table. Debugging Code The code provided should work off the bat. However, depending on your MySQL configuration, the RisarisBank tables may not be in lowercase as in our example e.g. customerinformation versus CustomerInformation. If this is the case the code will not work as is and all occurrences of customerinformation should be replaced by CustomerInformation. The p and puts functions are useful for checking the content of variables. For example p result after the result = soap.list(param) call gives the following information and is useful for checking column names for adding new code. #<SOAP::Mapping::Object:0x3240c04 {}customerinformationRoot=#<SOAP::Mapping::Object:0x3240ad8 {}customerinformationGroup=[#<SOAP::Mapping::Object:0x32409ac {}CustomerNumber="1" {}FirstName="Casper" {}Surname="Ankergren" {}AddressLine1="34 Green Street" {}AddressLine2="Crosses Street" {}City="Mansfield" {}Postcode="MA5 9AJ" {}DateOfBirth="30/05/1978">, #<SOAP::Mapping::Object:0x323f4bc {}CustomerNumber="10" {}FirstName="Grenville" {}Surname="Dekker" {}AddressLine1="22 Uttoxter New Road" {}AddressLine2="Chaddesden" {}City="Leeds" {}Postcode="LS4 9WB" {}DateOfBirth="21/10/1974">, #<SOAP::Mapping::Object:0x323e0b2 {}CustomerNumber="11" {}FirstName="Jane" {}Surname="Freeman" {}AddressLine1="3 Portland Street" {}AddressLine2="Lichfield" {}City="Gloucester" {}Postcode="GL4 8KD" {}DateOfBirth="26/10/1980">, #<SOAP::Mapping::Object:0x323ccb2 {}CustomerNumber="12" {}FirstName="Dale" {}Surname="Rolling" {}AddressLine1="The Hunters Rest" {}AddressLine2="Branston" {}City="Preston" {}Postcode="PR4 4HG" {}DateOfBirth="06/12/1989">, #<SOAP::Mapping::Object:0x323b8b2 {}CustomerNumber="13" {}FirstName="Arnold" {}Surname="Corrigan" {}AddressLine1="14 Anderson Street" {}AddressLine2="Allestree" {}City="Derby" {}Postcode="DE6 8FT" {}DateOfBirth="10/06/1970">, #<SOAP::Mapping::Object:0x323a4b2 {}CustomerNumber="14" {}FirstName="Alfred" {}Surname="Summerscale" {}AddressLine1="3701 S. Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd
  • 10. George Mason" {}AddressLine2="Melbourne" {}City="Bath" {}Postcode="BA6 8UU" {}DateOfBirth="07/08/1982">, #<SOAP::Mapping::Object:0x32390b2 {}CustomerNumber="15" {}FirstName="Peter" {}Surname="Spiegel" {}AddressLine1="11663 Charter Oak Co" {}AddressLine2="Foremark" {}City="Manchester" {}Postcode="M98 4GT" {}DateOfBirth="29/12/1971">, #<SOAP::Mapping::Object:0x3237cb2 {}CustomerNumber="16" {}FirstName="John" {}Surname="Sviridov" {}AddressLine1="3319 Rosemere Court" {}AddressLine2="Brailsford" {}City="Leeds" {}Postcode="LS8 5QQ" {}DateOfBirth="12/06/1984">, #<SOAP::Mapping::Object:0x32368b2 {}CustomerNumber="17" {}FirstName="Sarah" {}Surname="Hall" {}AddressLine1="4 Denmark Lane" {}AddressLine2="East Bridgford" {}City="Portsmouth" {}Postcode="PO9 7QN" {}DateOfBirth="12/02/1983">, #<SOAP::Mapping::Object:0x32354b2 {}CustomerNumber="18" {}FirstName="Elspeth" {}Surname="Jones" {}AddressLine1="12375 W. Ohio Circle" {}AddressLine2="Risley" {}City="Birmingham" {}Postcode="B93 1IO" {}DateOfBirth="28/08/1986">, #<SOAP::Mapping::Object:0x32340b2 {}CustomerNumber="19" {}FirstName="Nigel" {}Surname="Wyllis" {}AddressLine1="14 Borough Road" {}AddressLine2="Ockbrook" {}City="Leeds" {}Postcode="LS8 2WL" {}DateOfBirth="29/04/1981">]>> 6. Conclusion This tutorial shows how to access MySQL from Ruby using the SOA Gateway. As you can see, you have built a powerful application that uses Web Services to retrieve information in real-time. 7. Appendix require 'soap/wsdlDriver' require 'soap/header/simplehandler' # A handler to add authentication parameters to the SOAP header. class AutheticationHeaderHandler < SOAP::Header::SimpleHandler @@HEADER_NAME = 'Security' def initialize(namespace) super(XSD::QName.new(namespace, @@HEADER_NAME)) end def on_simple_outbound #Change the username/password below to reflect your own environment "<UsernameToken><Username>root</Username><Password></Password></UsernameTok en>" Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd
  • 11. end end wsdl_url = "http://localhost:56000/customerinformation?WSDL" soap = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver soap.wiredump_file_base = "soapresult" soap.headerhandler << AutheticationHeaderHandler.new("http://www.risaris.com/security") param = {"CustomerNumber" => "1*"} result = soap.list(param) for cust in (result.customerinformationRoot.customerinformationGroup) print(cust.firstName, "n") end Accessing MySQL from Ruby Author: John O’Mahony, Risaris Ltd