Chapter 10 Introduction to SQL Programming Techniques 20180427




















![General Options for a Cursor Declaration カーソル宣言時にオプションも宣言できる DECLARE <cursor name> [ INSENSITIVE ] [ General Options for a Cursor Declaration カーソル宣言時にオプションも宣言できる DECLARE <cursor name> [ INSENSITIVE ] [](https://slidetodoc.com/presentation_image_h2/c66abf1d5a2261f354342e81188bb80d/image-21.jpg)



























- Slides: 48

Chapter 10 Introduction to SQL Programming Techniques 2018/04/27 otawa 1

Outline • Introduction • 10. 1 Overview of Database Programming Techniques and Issues • 10. 2 Embedded SQL, Dynamic SQL, and SQL J • 10. 3 Database Programming with Function Calls and Class. Libraries: SQL/CLI and JDBC • 10. 4 Database Stored Procedures and SQL/PSM • 10. 5 Comparing the Three Approaches • 10. 6 Summary 2



10. 1. 1 Approaches to Database Programming データベースプログラミングには主に3つの手法が存在 する • Embedding database commands in a general-purpose programming language • Using a library of database functions or classes • Designing a brand-new language 5









Connecting to the Database • SQLでデータベースとの接続を確立するコマンド CONNECT TO <server name> AS <connection name> AUTHORIZATION<user account name and password>; • 同時に複数のデータベースに接続できるが、一つの接 続しか有効化できない 有効な接続を切り替えるには次のコマンドを使う SET CONNECTION <connection name>; • 接続を終了するコマンド DISCONNECT <connection name>; 14





19

![General Options for a Cursor Declaration カーソル宣言時にオプションも宣言できる DECLARE cursor name INSENSITIVE General Options for a Cursor Declaration カーソル宣言時にオプションも宣言できる DECLARE <cursor name> [ INSENSITIVE ] [](https://slidetodoc.com/presentation_image_h2/c66abf1d5a2261f354342e81188bb80d/image-21.jpg)
General Options for a Cursor Declaration カーソル宣言時にオプションも宣言できる DECLARE <cursor name> [ INSENSITIVE ] [ SCROLL ] CURSOR [ WITH HOLD ] FOR <query specification> [ ORDER BY <ordering specification> ] [ FOR READ ONLY | FOR UPDATE [ OF <attribute list> ] ] ; • INSENSITIVE, WITH HOLD トランザクションに関するオプション(20章参照) • SCROLL クエリ結果内のタプルを柔軟に探索するためのオプション FETCH [ [ < fetch orientation > ] FROM ] < cursor name > INTO < fetch target list > ; fetch orientation : NEXT, PRIOR, FIRST, LAST, ABSOLUTE i, RELATIVE i. デフォルトはNEXT • ORDER BY タプルの順番に関するオプション 21




10. 2. 4 SQLJ: Embedding SQL Commands in Java • get. Connection データベースに接続するための関数 default context という型の返り値を持つ public static Default. Context get. Connection(String url, String user, String password, Boolean auto. Commit) throws SQLException ; 25

10. 2. 4 SQLJ: Embedding SQL Commands in Java • Javaにはエラーを扱うために例外という概念がある SQLの例外を扱う → SQLException embedded SQLにおけるSQLCODE や SQLSTATEと似た役割 • Java内でSQLを使うときは前に #sql をつける 26


named iterator 28

positional iterator • あ 29




Handles to environment, connection, statement, and description records • それぞれのレコードは handle(ハンドル) というC言語のポインタを用いてプログラムに アクセス可能 • レコードを生成しハンドルを返す関数 SQLAlloc. Handle( <handle_type>, <handle_1>, <handle_2> ) • <handle_type> 生成されるレコードの型 SQL_HANDLE_ENV, SQL_HANDLE_DBC, SQL_HANDLE_STMTの4種類 • <handle_1> 生成されるハンドルをしまうコンテナ connection recordを生成する場合はenvironment、statement recordを生成する場合は connection • <handle_2> 新しく生成されるレコードへのポインタ 33


35


10. 3. 2 JDBC: SQL Class Library for Java Programming • JDBC driverを読み込むには… 例:Class. for. Name("oracle. jdbc. driver. Oracle. Driver") • コマンドラインでも読み込める 例:-Djdbc. drivers = oracle. jdbc. driver 37


39


10. 4. 1 Database Stored Procedures and Functions • ストアドプロシージャの宣言 CREATE PROCEDURE <procedure name> (<parameters>) <local declarations> <procedure body> ; • 関数の宣言 CREATE FUNCTION <procedure name> (<parameter>) RETURNS <return type> <local declarations> <function body>; 返り値の型も書く 41



10. 4. 2 SQL/PSM: Extending SQL for Specifying Persistent Stored Modules • 条件分岐 44

10. 4. 2 SQL/PSM: Extending SQL for Specifying Persistent Stored Modules 45

10. 4. 2 SQL/PSM: Extending SQL for Specifying Persistent Stored Modules • ループ • カーソルのようにタプル一つずつに処理をするループ 46


10. 6 Summary • 埋め込み型クエリ • embedded SQL, • SQLJ • 関数/クラスライブラリ • SQL/CLI • JDBC • Database Stored Procedures • 永続格納モジュール • SQL/PSM 48