嵌入SQL 编程 ( 40 ).pdf
《嵌入SQL 编程 ( 40 ).pdf》由会员分享,可在线阅读,更多相关《嵌入SQL 编程 ( 40 ).pdf(21页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、112 Embedding SQL in Programming 12 Embedding SQL in Programming languageslanguages12.1 Introduction:using SQL from programs 12.2 Embedded SQL12.2.1 Static and dynamic embedding12.2.2 Cursors12.2.3.ESQL/C12.2.4 Positioned Update12.3 Transactions in application programs12.3.1 Definition12.3.2 Isolati
2、on levels12.4 SQL and Java12.4.1 JDBC12.4.2 SQLJKemper/Eickler:chap.4.19-4.23;Melton:chap.12,13,17-19,Widom,Ullman,Garcia-Molina:chapt.8 Christian UllenboomJava ist auch eine Insel,Kap.20,Galileo Comp.HS/DBS05-15-ProgLang 2UsingUsing SQL SQL fromfrom ProgramsProgramsIntroductionIntroduction SQL is a
3、 data sublanguage Needs a host language Control structures User interface:output formatting,forms Transactions:more than one DB interaction as a unit of work Issues Language mismatch(impedance mismatch)Set oriented operations versus manipulation of individuals How to interconnect program variables a
4、nd e.g attributesin SQL statements?Should an SQL-statement as part of a program be compiled,when?Question:could you imagine a language bringing both worlds together?2HS/DBS05-15-ProgLang 3ThreeThree-tiertier architecturearchitecture(exampleexample)GUI clientWeb browserWeb browserDB clientWeb ServerD
5、B ApplicationDB ApplicationDB-ServerDB-ServerDB-ServerMiddleware layerMiddle tierFile SystemHS/DBS05-15-ProgLang 4UsingUsing SQL SQL fromfrom ProgramsProgramsIntroductionIntroductionOverview of language/DB integration concepts FourthGeneration Languages Module Language-PSM(PL/SQL,PLpgSQL)Standardize
6、d in SQL-99 Interface of standard programming languages Call level interface,proprietary library routines,APIStandardized:SQL CLI Open Database connection(ODBC),Embedded C/Java/.Standardized language extensions Standardized API Java DBC Fourth generation Language Stored Procedures C/Java/Perl/Python
7、,.Component architectures:hiding the details of DB interaction,Enterprise Java Beans(EJB)3HS/DBS05-15-ProgLang 5SQL SQL fromfrom ProgramsPrograms 4.Generation 4.Generation LanguagesLanguages Proprietary Fourth generation language(4GL)Underlying assumption:most application programs are algorithmicall
8、y simple sophisticated output formatting needed it should be difficult for users to switch from one DBS to another Technical conceptClient evolved from simple Terminal to 4GL-Interpreter Open systems movement and HTTP/HTML/Java makes 4GL less importantClientDBSProprietary protocolHS/DBS05-15-ProgLan
9、g 6UsingUsing SQL SQL fromfrom ProgramsProgramsModulesModules Standardization efforts(SQL 89/SQL99)Modules and Embedded SQL SQL Modules Separate parameterized Modules of SQL statements Compiled for a particular language(e.g.COBOL,C,ADA.)Linked to application program (statically?)Disadvantage SQL cod
10、e hidden in application and vice versa Not widely used Superseded by flexible stored procedure concept4HS/DBS05-15-ProgLang 7UsingUsing SQL SQL fromfrom ProgramsProgramsCallCall interfaceinterface Call level interface Language and DBS specific library of procedures to access the DB Example:MySQL C A
11、PI Communication buffer for transfering commands and results API data types likeMYSQLhandle for db connectionsMYSQL_RES structure which represents result set API functionsmysql_real_query()mysql_real_query(MYSQL*mysql,const char*query,unsigned int length)query of length of character string in buffer
12、 and many more.Standard:Open Database Connection(ODBC)Predecessor of Java Database Connection(JDBC),see belowHS/DBS05-15-ProgLang 8SQL Call level interface(SQL/CLI)SQL Call level interface(SQL/CLI)Standardized Interface to C/C+defined by X/OPEN and SQL Accesss group Main advantages DBS-independent A
13、pplication development independent from DBS(as opposed to Embedded SQL precompilerapproach,see below)Easy to connect to multiple DB Microsoft implementation ODBC(=Open Database Connectivity)de facto standard,available not only for MS products5Main cycle of transaction execution withSQL/CLICalls are
14、embeddedin the applicationprogramSee also JDBC,ESQLsource:IBM DB2 manualHS/DBS05-15-ProgLang 1012.2 12.2 EmbeddedEmbedded SQLSQL Embedded SQL the most important(?)approach Concepts Program consists of native and SQL-likestatements Precompiler compiles it to native code,includes calls to DBSresources
15、 Employs call level interface in most implementations Most popular:Embedded C (Oracle:PRO*C)SQLJ =Embedded JavaESQLNative Language codePrecompilerObjectcodeCompilerLibraryExcecu-tableLinker6HS/DBS05-15-ProgLang 11EmbeddedEmbedded SQL(ESQL)SQL(ESQL)Syntax and Syntax and moremore Well defined type map
16、ping (for different languages)Exception handling (WHENEVER condition action)SQLSTATE,SQLCODE(deprecated)Syntax for embedded SQL statements Binding to host language variables#sqlSELECT m#FROM M WHERE titel=:titleString;.#sql FETCH.INTO:var1 hypothetical syntax,like SQLJ HS/DBS05-15-ProgLang 12ESQLESQ
17、L C/Java embeddingESQL/C SQLJEXEC SQL UPDATE staff SET job=Clerk WHERE job=Mgr;if(SQLCODE 0 printf(Update Error:.);try#sql UPDATE staff SET job=Clerk WHERE job=Mgr;catch(SQLException e)println(Update Error:SQLCODE=+.);7HS/DBS05-15-ProgLang 13ESQL code generationESQL code generationCode generatedbasi
18、cally atcompile time.DBS and DBmust be known beforeruntime in orderto generate executablesfrom:DB2 manualHS/DBS05-15-ProgLang 1412.2.1 ESQL 12.2.1 ESQL StaticStatic/dynamicdynamic embeddingembeddingStatic versus dynamic SQL:Static:all SQL commands are known in advance,SQL-compilation and language bi
19、nding at precompile time Dynamic(i)SQL-String executed by DBS:Operator tree,optimization,code binding.(ii)SQL-String prepared(compiled)at runtime.Performance gain in loops etc.8HS/DBS05-15-ProgLang 1512.2.2 ESQL12.2.2 ESQLCursorsCursorsCursor concept How to process a result set one tuple after the o
20、ther?CURSOR:name of an SQL statement and a handle for processing the result set record by record Cursor is defined,opened at runtime(=SQL-statement is excecuted)and used for FETCHing single result records OPEN cFETCH cDBSBuffers for application program cursorsDBS may determine result set in a lazyor
21、 eager way cDECLARE c.Cursor concept used in most language embeddings of SQL(ESQL-C,PL/SQL,JDBC and more)HS/DBS05-15-ProgLang 16ESQLESQLCursorsCursorsExplicit cursors:Declared and named by the programmer Sometimes implicit cursors for individual SQL statements areused in 4GL Cursor assigns a name to
22、 an SQL statement.Cursor/SQL statement do not bind the result attributes to variables allows to traverse the result set(the active set)row by rowActive setActive setCurrent rowCurrent rowCursor curs7369SMITHTo be or.7566JONESMetropolis7788SCOTTForest Gump7876ADAMSForest Gump7902FORDStar Wars IDeclar
23、e curs for Select c#,lname,m.title from C,R,M where.9HS/DBS05-15-ProgLang 17ESQLESQLCursorsCursors Controlling a cursor:the necessary steps Identify the Identify the active setactive setOPENOPENOPENNoNo Load the Load the current row current row into into variablesvariablesFETCHFETCHFETCH Test for Te
24、st for existing existing rowsrowsEMPTY?EMPTY?Release the Release the active setactive setCLOSECLOSECLOSEYesYes Create a Create a named named SQL areaSQL areaDECLAREDECLAREDECLAREExecutes the queryHS/DBS05-15-ProgLang 18ESQLESQLCursorsCursors OpeningOPENcursor_name;OPENcursor_name;In a compiled langu
25、age environment(e.g.embedded C):bind input variables execute query put(first)results into communication(context)area no exception if result is empty has to be checked when fetching the results positions the cursor before the first row of the result set(1)First steps in an interpreted language(e.g.4G
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 嵌入SQL 编程 40 嵌入 SQL
限制150内