64
Do more with SQL in FileMaker Koen Van Hulle

Vendor session myFMbutler DoSQL 2

Embed Size (px)

DESCRIPTION

Slides from the vendor session about myFMbutler DoSQL 2 at the FileMaker Devcon 2013 in San Diego.

Citation preview

Page 1: Vendor session myFMbutler DoSQL 2

Do more with SQLin FileMaker

Koen Van Hulle

Page 2: Vendor session myFMbutler DoSQL 2

Koen Van Hulle

Page 3: Vendor session myFMbutler DoSQL 2

Gent

Niels Heyvaert - http://www.sxc.hu

Page 4: Vendor session myFMbutler DoSQL 2

Belgium

Linda DuBose

Vince Varga - http://www.sxc.hu

Page 5: Vendor session myFMbutler DoSQL 2

I work at

• FileMaker “7...12” Certified developer

• 2012 FileMaker Mad Dog PR Award

Page 6: Vendor session myFMbutler DoSQL 2

better known as

• FileMaker Hosting

• Tools and add-ons for FileMaker

FMbutlermy

Page 7: Vendor session myFMbutler DoSQL 2

our product range

Page 8: Vendor session myFMbutler DoSQL 2

the easiest way to send e-mail, text messages and faxes from FileMaker Server 12

• Checks which e-mails need to be sent, based on FileMaker field criteria

• Runs a background process

• Supports plain text as well as HTML e-mail

• Easy implementation

• Ideal for integration in

Page 9: Vendor session myFMbutler DoSQL 2

the ultimate developer tool that saves you tons of development time

• Stores complete FileMaker fields, tables, scripts, script steps and layouts in an easy-to-use library.

• Clip Editor

• Clip History

• easy access with the “snippets” feature.

Page 10: Vendor session myFMbutler DoSQL 2

allows you to easily control printer switching from within your FileMaker

• switches printers on the fly

• generates PDF in runtime solutions

• captures printer settings like page orientation, paper size etc. and restores them at the time of printing

Page 11: Vendor session myFMbutler DoSQL 2

Do more with SQL in FileMaker

• uses SQL statements to select, update, create or delete FileMaker records.

• doesn't require any drivers

• Returns native FileMaker data types like dates, images, formatted text,

• Debug tools

Page 12: Vendor session myFMbutler DoSQL 2

Menu

• FQL Engine demystified

• Overview of most common SQL commands

• Other DoSQL Commands

• Common mistakes

Page 13: Vendor session myFMbutler DoSQL 2

FQL engine?

• FileMaker code that accesses the data in another way

• Providing an alternative way of “talking” to the data

• Using multiple FQL interfaces to the database(s)

Page 14: Vendor session myFMbutler DoSQL 2

One FQL engine, four interfaces

ODBCdriver

JDBCdriver

plug-inAPI

executeSQL

Page 15: Vendor session myFMbutler DoSQL 2

Outside

ODBCdriver

JDBCdriver

plug-inAPI

executeSQL

Inside

Credentials at login Credentials of the current user

Page 16: Vendor session myFMbutler DoSQL 2

plug-inAPI

executeSQL

Inside

Credentials of the current user

Page 17: Vendor session myFMbutler DoSQL 2

Selecting a database

Page 18: Vendor session myFMbutler DoSQL 2

ExecuteSQL()

• only the file of the calculation context

Page 19: Vendor session myFMbutler DoSQL 2

Plug-in API

• any existing open file with second generation plug-ins

Page 20: Vendor session myFMbutler DoSQL 2

What does the FQL engine support?

• SQL-92

• SELECT

• DELETE

• INSERT

• UPDATE

• CREATE TABLE

• ALTER TABLE

• CREATE INDEX

• DROP INDEX

Page 21: Vendor session myFMbutler DoSQL 2

What does executeSQL support?

• SQL-92

• SELECT

• DELETE

• INSERT

• UPDATE

• CREATE TABLE

• ALTER TABLE

• CREATE INDEX

• DROP INDEX

Page 22: Vendor session myFMbutler DoSQL 2

SELECTSELECT [DISTINCT] {* | column_expression [[AS] column_alias],...}FROM table_name [table_alias], ...[ WHERE expr1 rel_operator expr2 ][ GROUP BY {column_expression, ...} ][ HAVING expr1 rel_operator expr2 ][ UNION [ALL] (SELECT...) ][ ORDER BY {sort_expression [DESC | ASC]}, ... ][ OFFSET n {ROWS | ROW} ][ FETCH FIRST [ n [ PERCENT ] ] { ROWS | ROW } {ONLY | WITH TIES } ][ FOR UPDATE [OF {column_expression, ...}] ]

Page 23: Vendor session myFMbutler DoSQL 2

SELECT

SELECT sum(salary), Name, EmpID, Department FROM Employees WHERE Department = ‘marketing’

Page 24: Vendor session myFMbutler DoSQL 2

SELECTExecuteSQL(“SELECT sum(salary), Name, EmpID, Department FROM Employees WHERE Department = ‘marketing’”; ”;” ; “¶” )

1 000 000;Willy Sommers;203;marketing1 000 000;Dana Winner;204;marketing1 000 000;Eva De Waelle;205;marketing1 000 000;Maarten Cox;206;marketing1 000 000;Danny Fabry;207;marketing

Page 25: Vendor session myFMbutler DoSQL 2

SELECTExecuteSQL(“SELECT sum(salary), Name, EmpID, Department FROM Employees WHERE Department = ?”; ”;” ; “¶” ; globals::department )

1 000 000;Willy Sommers;203;marketing1 000 000;Dana Winner;204;marketing1 000 000;Eva De Waelle;205;marketing1 000 000;Maarten Cox;206;marketing1 000 000;Danny Fabry;207;marketing

Page 26: Vendor session myFMbutler DoSQL 2

RESULT• Always TEXT

• Always one TEXT string per query

Page 27: Vendor session myFMbutler DoSQL 2

Result = TextLet(x = ExecuteSQL("SELECT sum(salary) FROM Employees WHERE Department = ?"; "" ; ""; globals::department );x < 20 000)* sum(salary) = 1 000 000

TRUE“1 000 000”< 20 000

Page 28: Vendor session myFMbutler DoSQL 2

Result = TextLet(x = ExecuteSQL("SELECT sum(salary) FROM Employees WHERE Department = ?"; "" ; ""; globals::department);GetAsNumber(x) < 20 000)* sum(salary) = 1 000 000

False1 000 000< 20 000

Page 29: Vendor session myFMbutler DoSQL 2

Result = TEXT

• Date: “2013-08-15”

• Time: “02:49:03”

• Timestamp: “2013-08-15 02:49:03”

• Containers: only the name of the file: e.g. image.jpeg

Page 30: Vendor session myFMbutler DoSQL 2

Select with DoSQL• DoSQL can retrieve the data like ExecuteSQL

if you want

• FQL-engine does support native FileMaker types, so does myFMbutler DoSQL 2

• DoSQL supports container fields

• Compatible with FileMaker Pro 11 and above

Page 31: Vendor session myFMbutler DoSQL 2

Select with DoSQLmFMb_DoSQL_SetParameters ( myTable::Department)

mFMb_DoSQL ("SELECT sum(salary), Name, EmpID, Department FROM Employees WHERE Department = ?"; false)

Page 32: Vendor session myFMbutler DoSQL 2

Result

• Row Count

• Use mFMb_DoSQL_Result ( row ; column )

• Result in native FileMaker Datatypes, no need to convert.

Page 33: Vendor session myFMbutler DoSQL 2

ResultLet([p = mFMb_DoSQL_SetParameters ( myTable::Department);q = mFMb_DoSQL("SELECT sum(salary) FROM Employees WHERE Department = ?"; false) ;x = mFMb_DoSQL_Result ( 1 ; 1 )];x < 20 000) False

1 000 000< 20 000

Page 34: Vendor session myFMbutler DoSQL 2

INSERT, UPDATE & DELETE

• Supported through JDBC, ODBC and the plug-in API

• NOT supported through ExecuteSQL()

• So plug-ins are the only ones capable of supporting FQL fully from the inside

Page 35: Vendor session myFMbutler DoSQL 2

Let’s start with INSERT

• syntax: INSERT INTO table_name [(column_name, ...)] VALUES (expr, ...)

• can be a prepared statement ( it better be )

• VALUES can be a SELECT expression

• can be a lot faster than a script doing new record, set field, set field,… why?

Page 36: Vendor session myFMbutler DoSQL 2

Committing records

• for JDBC & ODBC this is a setting

• the plug-in API is always auto-committing and has no cursor support

Page 37: Vendor session myFMbutler DoSQL 2

INSERT

• No need to change the context

• Logging

• Audit trail

• Creating statistics, reports, ...

Page 38: Vendor session myFMbutler DoSQL 2

So, You Think You Can UPDATE

• UPDATE table_name SET column_name = expr, ... [ WHERE { conditions } ]

• A user can lock a record

• the error codes returned are the FileMaker error codes ( e.g. 301 )!

• compare with SELECT, it will never return error 401

Page 39: Vendor session myFMbutler DoSQL 2

Select for update

• SELECT for UPDATE only locks records on JDBC and ODBC

• plug-in cannot lock a record, but SELECT for UPDATE returns error when records are locked

• user can lock a record

Page 40: Vendor session myFMbutler DoSQL 2

UPDATE

• No need to change the context

• Changing data (in bulk) of related records

• Triggering auto-enter calculations of related records

Page 41: Vendor session myFMbutler DoSQL 2

Delete

• DELETE FROM table_name [ WHERE { conditions } ]

• check for record locking!

• check for privileges!

Page 42: Vendor session myFMbutler DoSQL 2

DELETE

• Conditional deletes (of related records)

Page 43: Vendor session myFMbutler DoSQL 2

Modifying Structure using SQL

• this is dangerous stuff, keep backups

• a calculation field cannot return a result when the schema is being changed, this results in a deadlock

• modifying schema on idle can be done using DoSQL 2.

Page 44: Vendor session myFMbutler DoSQL 2

Other features of DoSQL 2

Page 45: Vendor session myFMbutler DoSQL 2

SQL for dummies

• Generates the SQL for you

• mFMb_DoSQL_Select

• mFMb_DoSQL_Insert

• mFMb_DoSQL_Delete

Page 46: Vendor session myFMbutler DoSQL 2

mFMb_DoSQL_SelectmFMb_DoSQL ("SELECT citizens, categoryFROM citiesWHERE city = ‘Gent’")

Page 47: Vendor session myFMbutler DoSQL 2

mFMb_DoSQL_SelectLet([t able= GetValue(Substitute(GetFieldName(cities::citizens); “::”; “¶”); 1 );field1= GetValue(Substitute(GetFieldName(cities::citizens); “::”; “¶”); 2 );field2= GetValue(Substitute(GetFieldName(cities::category); “::”; “¶”); 2 );field3= GetValue(Substitute(GetFieldName(cities::city); “::”; “¶”); 2 );q = mFMb_DoSQL ("SELECT " & field1 & ", " & field1 &" FROM " & table " WHERE " & field2 & " = ‘Gent’")];q)

Page 48: Vendor session myFMbutler DoSQL 2

mFMb_DoSQL_SelectmFMb_DoSQL _Select(GetFieldName(cities::citizens); GetFieldName(cities::category); “WHERE”; GetFieldName(cities::city); “Gent”)

Page 49: Vendor session myFMbutler DoSQL 2

Debug tools

• Errors

• Log

• Alert dialog

Page 50: Vendor session myFMbutler DoSQL 2

Errors

• mFMb_DoSQL_LastErrNum( )

• returns the last error

• mFMb_DoSQL_LastSQL( )

• returns the last query

Page 52: Vendor session myFMbutler DoSQL 2

Logs

• Logging can be enabled by the funtion:mFMb_DoSQL_Debug( level { ; once }

• Level 1: Only errors

• Level 2: All queries

Page 53: Vendor session myFMbutler DoSQL 2

Logs2012-01-19 10:34:53.674 Select Count ( MAILBOXID_DNR ) from mailboxes where parentmailboxid_dnr = 7009

2012-01-19 10:34:53.674 ERROR: 8310ERROR: FQL0001/(1:18): There is an error in the syntax of the query.

Page 54: Vendor session myFMbutler DoSQL 2

Logs2012-01-19 10:34:54.043 Running Script: "Read Messages"2012-01-19 10:34:54.043 SELECT Count ( "messageid" ) FROM "mailboxes_messagereceivers" WHERE "mbdef_id" = 6 AND "contactid" = 1000843 AND "is_unread" = 12012-01-19 10:34:54.045 rows in result: 12012-01-19 10:34:54.045 columns in result: 12012-01-19 10:34:54.046 resultsize: 22012-01-19 10:34:54.046 result has been retrieved

Page 55: Vendor session myFMbutler DoSQL 2

• mFMb_DoSQL_SupressAlerts()

Alert dialog

Page 56: Vendor session myFMbutler DoSQL 2

Common mistakes

Page 57: Vendor session myFMbutler DoSQL 2

Use hard coded !eld names

• Do not use GetFieldName ( field ) instead

• Do not push FileMaker to solve GetFieldName() validation bugs with unrelated fields

Linda DuBose - http://www.sxc.hu

Page 58: Vendor session myFMbutler DoSQL 2

Use SQL in Stored Calculations

• And see the continents move while your schema recalculates

Page 59: Vendor session myFMbutler DoSQL 2

Never check for errors

• Just assume you SQL queries are perfect

• ExecuteSQL(): never check for Get ( LastError )

• DoSQL: set DoSQL_SupressAlerts( True ) and never use DoSQL_LastErrNum

Michael & Christa Richert - http://www.sxc.hu

Page 60: Vendor session myFMbutler DoSQL 2

Leave your queries in the data viewer

• when you are finished with debuggingand use your computer to grill your meat

Chris Chidsey - http://www.sxc.hu

Page 61: Vendor session myFMbutler DoSQL 2

Do more with SQL

• Even when it’s faster to do it using old school methods

• Know that because SQL calculation are harder to write, they must be the better way do to things

Everythin

g

Page 62: Vendor session myFMbutler DoSQL 2

Do more with SQL in FileMaker

• uses SQL statements to select, update, create or delete FileMaker records.

• doesn't require any drivers

• Returns native FileMaker data types like dates, images, formatted text,

• Debug tools

Page 63: Vendor session myFMbutler DoSQL 2

DoSQL 2License Price FM Server1 user

5 users10 users25 users50 users

Developer (25)Site license

29,- EUR (± 38,- USD)

99,- EUR (± 132,- USD)

169,- EUR (± 226,- USD)

259,- EUR (± 346,- USD)

469,- EUR (± 627,- USD)

339,- EUR (± 453,- USD) YES

559,- EUR (± 747,- USD) YES

Page 64: Vendor session myFMbutler DoSQL 2

Thank you!

www.myfmbutler.comor visit our booth