JPortal ships with a number of built-in procs to allow easy querying of a database.
Let assume the following table structure in our SI, with various procs defined to retrieve records:
DATABASE ExampleDatabase
PACKAGE com.example.db
SERVER ExampleServer
SCHEMA BooksAndAuthors
TABLE Authors
ID SEQUENCE
Bio CHAR
BirthDate DATE
LastUpdated TIMESTAMP
KEY PKEY PRIMARY
ID
//Simple CRUD queries are available out of the box with JPortal2
PROC SelectOnePROC SelectAllPROC SelectOneBy Bio
PROC SelectBy BirthDate Returning Bio
PROC SelectBy BirthDate As FancySelectByBirthDate
OUTPUT
ID =
Bio =
BirthDate =
SelectOne will select all the fields of a table, by primary key.
Adding the FOR UPDATE clause will cause JPortal2 to add the text FOR UPDATE to the query. Use this to lock a record
for when you want to select it, change on or more fields, and then update it.
Adding the FOR READONLY clause will cause JPortal2 to add the text "FOR READONLY" to the query. Use this to
put a READONLY lock on a record.
@dataclassclassDB_AuthorsSelectOne:#OutputsBio:strBirthDate:datetimeLastUpdated:datetime@classmethoddefget_statement(cls,ID:int)->TextAsFrom:statement=sa.text(f"/* PROC BooksAndAuthors.Authors.SelectOne */"f"select"f" Bio"f", BirthDate"f", LastUpdated"f" from BooksAndAuthors.Authors"f" where ID = :ID")text_statement=statement.columns(Bio=db_types.NonNullableString,BirthDate=sa.types.DateTime,LastUpdated=sa.types.DateTime,)text_statement=text_statement.bindparams(ID=ID,)returntext_statement@classmethoddefexecute(cls,session:Session,ID:int)->Optional['DB_AuthorsSelectOne']:params=process_bind_params(session,[sa.types.Integer,],[ID,])res=session.execute(cls.get_statement(*params))rec=res.fetchone()ifrec:res.close()returnprocess_result_rec(DB_AuthorsSelectOne,session,[db_types.NonNullableString,sa.types.DateTime,sa.types.DateTime,],rec)returnNone
@dataclassclassDB_AuthorsSelectOneUpd:#OutputsBio:strBirthDate:datetimeLastUpdated:datetime@classmethoddefget_statement(cls,ID:int)->TextAsFrom:statement=sa.text(f"/* PROC BooksAndAuthors.Authors.SelectOneUpd */"f"select"f" Bio"f", BirthDate"f", LastUpdated"f" from BooksAndAuthors.Authors"f" where ID = :ID"f" for update")text_statement=statement.columns(Bio=db_types.NonNullableString,BirthDate=sa.types.DateTime,LastUpdated=sa.types.DateTime,)text_statement=text_statement.bindparams(ID=ID,)returntext_statement@classmethoddefexecute(cls,session:Session,ID:int)->Optional['DB_AuthorsSelectOneUpd']:params=process_bind_params(session,[sa.types.Integer,],[ID,])res=session.execute(cls.get_statement(*params))rec=res.fetchone()ifrec:res.close()returnprocess_result_rec(DB_AuthorsSelectOneUpd,session,[db_types.NonNullableString,sa.types.DateTime,sa.types.DateTime,],rec)returnNone
@dataclassclassDB_AuthorsSelectOneReadOnly:#OutputsBio:strBirthDate:datetimeLastUpdated:datetime@classmethoddefget_statement(cls,ID:int)->TextAsFrom:statement=sa.text(f"/* PROC BooksAndAuthors.Authors.SelectOneUpd */"f"select"f" Bio"f", BirthDate"f", LastUpdated"f" from BooksAndAuthors.Authors"f" where ID = :ID"f" for read only")text_statement=statement.columns(Bio=db_types.NonNullableString,BirthDate=sa.types.DateTime,LastUpdated=sa.types.DateTime,)text_statement=text_statement.bindparams(ID=ID,)returntext_statement@classmethoddefexecute(cls,session:Session,ID:int)->Optional['DB_AuthorsSelectOneUpd']:params=process_bind_params(session,[sa.types.Integer,],[ID,])res=session.execute(cls.get_statement(*params))rec=res.fetchone()ifrec:res.close()returnprocess_result_rec(DB_AuthorsSelectOneUpd,session,[db_types.NonNullableString,sa.types.DateTime,sa.types.DateTime,],rec)returnNone
publicbooleanselectOne()throwsSQLException{Stringstatement="/* PROC ToDoList_App.Authors.SelectOne */"+"select"+" Bio"+", BirthDate"+", LastUpdated"+" from ToDoList_App.Authors"+" where ID = ?";PreparedStatementprep=connector.prepareStatement(statement);prep.setInt(1,id);ResultSetresult=prep.executeQuery();if(!result.next()){result.close();prep.close();returnfalse;}bio=result.getString(1);birthDate=result.getDate(2);lastUpdated=result.getTimestamp(3);result.close();prep.close();returntrue;}
publicbooleanselectOneUpd()throwsSQLException{Stringstatement="/* PROC ToDoList_App.Authors.SelectOneUpd */"+"select"+" Bio"+", BirthDate"+", LastUpdated"+" from ToDoList_App.Authors"+" where ID = ?"+" for update";PreparedStatementprep=connector.prepareStatement(statement);prep.setInt(1,id);ResultSetresult=prep.executeQuery();if(!result.next()){result.close();prep.close();returnfalse;}bio=result.getString(1);birthDate=result.getDate(2);lastUpdated=result.getTimestamp(3);result.close();prep.close();returntrue;}
publicbooleanselectOneReadOnly()throwsSQLException{Stringstatement="/* PROC ToDoList_App.Authors.SelectOneReadOnly */"+"select"+" Bio"+", BirthDate"+", LastUpdated"+" from ToDoList_App.Authors"+" where ID = ?"+" for read only";PreparedStatementprep=connector.prepareStatement(statement);prep.setInt(1,id);ResultSetresult=prep.executeQuery();if(!result.next()){result.close();prep.close();returnfalse;}bio=result.getString(1);birthDate=result.getDate(2);lastUpdated=result.getTimestamp(3);result.close();prep.close();returntrue;}
Adding the IN ORDER clause will add ORDER BY <specified columns> to the query. Specify
Adding the FOR UPDATE clause will cause JPortal2 to add the text FOR UPDATE to the query. Use this to lock a record
for when you want to select it, change on or more fields, and then update it.
Adding the FOR READONLY clause will cause JPortal2 to add the text "FOR READONLY" to the query. Use this to
put a READONLY lock on a record. See SelectOne for an example of FOR <UPDATE | READONLY>.
@dataclassclassDB_AuthorsSelectAllSorted:#OutputsID:intBio:strBirthDate:datetimeLastUpdated:datetime@classmethoddefget_statement(cls)->TextAsFrom:statement=sa.text(f"/* PROC BooksAndAuthors.Authors.SelectAllSorted */"f"select"f" ID"f", Bio"f", BirthDate"f", LastUpdated"f" from BooksAndAuthors.Authors"f" order by Bio"f", ID desc")text_statement=statement.columns(ID=sa.types.Integer,Bio=db_types.NonNullableString,BirthDate=sa.types.DateTime,LastUpdated=sa.types.DateTime,)returntext_statement@classmethoddefexecute(cls,session:Session)->List['DB_AuthorsSelectAllSorted']:res=session.execute(cls.get_statement())recs=res.fetchall()returnprocess_result_recs(DB_AuthorsSelectAllSorted,session,[sa.types.Integer,db_types.NonNullableString,sa.types.DateTime,sa.types.DateTime,],recs)
1 2 3 4 5 6 7 8 9101112131415161718192021
/*** Returns any number of records.* @return result set of records found* @exception SQLException is passed through*/publicQueryselectAll()throwsSQLException{Stringstatement="/* PROC ToDoList_App.Authors.SelectAllSorted */"+"select"+" ID"+", Bio"+", BirthDate"+", LastUpdated"+" from ToDoList_App.Authors";PreparedStatementprep=connector.prepareStatement(statement);ResultSetresult=prep.executeQuery();Queryquery=newQuery(prep,result);returnquery;}
1 2 3 4 5 6 7 8 91011121314151617181920212223
/*** Returns any number of records.* @return result set of records found* @exception SQLException is passed through*/publicQueryselectAllSorted()throwsSQLException{Stringstatement="/* PROC ToDoList_App.Authors.SelectAllSorted */"+"select"+" ID"+", Bio"+", BirthDate"+", LastUpdated"+" from ToDoList_App.Authors"+" order by Bio"+", ID desc";PreparedStatementprep=connector.prepareStatement(statement);ResultSetresult=prep.executeQuery();Queryquery=newQuery(prep,result);returnquery;}
SelectOneBy <SelectColumns>+
[ FOR <UPDATE | READONLY> ]
SelectOneBy will select all the fields of a table, by the keys specified by SelectColumns.
Adding the FOR UPDATE clause will cause JPortal2 to add the text FOR UPDATE to the query. Use this to lock a record
for when you want to select it, change on or more fields, and then update it.
Adding the FOR READONLY clause will cause JPortal2 to add the text "FOR READONLY" to the query. Use this to
put a READONLY lock on a record. See SelectOne for an example of FOR <UPDATE | READONLY>.
@dataclassclassDB_AuthorsSelectOneByBio:#OutputsID:intBio:strBirthDate:datetimeLastUpdated:datetime@classmethoddefget_statement(cls,Bio:str)->TextAsFrom:class_ret:sequence="default,"#postgres uses default for sequencesoutput=" OUTPUT (ID,Bio,BirthDate,LastUpdated)"tail=" RETURNING ID Bio BirthDate LastUpdated"#session.bind.dialect.namestatement=sa.text(f"/* PROC BooksAndAuthors.Authors.SelectOneByBio */"f"select"f" ID"f", Bio"f", BirthDate"f", LastUpdated"f" from BooksAndAuthors.Authors"f" for update"f" where Bio = :Bio")text_statement=statement.columns(ID=sa.types.Integer,Bio=db_types.NonNullableString,BirthDate=sa.types.DateTime,LastUpdated=sa.types.DateTime,)text_statement=text_statement.bindparams(Bio=Bio,)returntext_statement@classmethoddefexecute(cls,session:Session,Bio:str)->Optional['DB_AuthorsSelectOneByBio']:params=process_bind_params(session,[db_types.NonNullableString,],[Bio,])res=session.execute(cls.get_statement(*params))rec=res.fetchone()ifrec:res.close()returnprocess_result_rec(DB_AuthorsSelectOneByBio,session,[sa.types.Integer,db_types.NonNullableString,sa.types.DateTime,sa.types.DateTime,],rec)returnNone
/*** Returns at most one record.* @return true if a record is found* @exception SQLException is passed through*/publicbooleanselectOneByBio()throwsSQLException{Stringstatement="/* PROC ToDoList_App.Authors.SelectOneByBio */"+"select"+" ID"+", Bio"+", BirthDate"+", LastUpdated"+" from ToDoList_App.Authors"+" for update"+" where Bio = ?";PreparedStatementprep=connector.prepareStatement(statement);prep.setString(1,bio);ResultSetresult=prep.executeQuery();if(!result.next()){result.close();prep.close();returnfalse;}id=result.getInt(1);bio=result.getString(2);birthDate=result.getDate(3);lastUpdated=result.getTimestamp(4);result.close();prep.close();returntrue;}
Selects record by user specified keys,columns returning columns specified by user
12345
SelectBy <SelectColumns>+
[ [IN] ORDER <OrderColumnName>* [DESC]]
[ FOR UPDATE | READONLY ]
[AS <alias>]
[RETURNING <columns to return>]
SelectBy will select the fields of a table that are specified by the RETURNING clause, by the keys specified
by SelectColumns.
Adding the FOR UPDATE clause will cause JPortal2 to add the text FOR UPDATE to the query. Use this to lock a record
for when you want to select it, change on or more fields, and then update it.
Adding the FOR READONLY clause will cause JPortal2 to add the text "FOR READONLY" to the query. Use this to
put a READONLY lock on a record. See SelectOne for an example of FOR <UPDATE | READONLY>.
the AS clause specifies a custom name for the proc. If AS is omitted, the name will default to
SelectBy<SelectColumns>.
The RETURNING clause specifies the fields that must be returned, If it is omitted, all the fields in the table
will be returned.
@dataclassclassDB_AuthorsSelectByBirthDate:#OutputsBio:str@classmethoddefget_statement(cls,BirthDate:datetime)->TextAsFrom:class_ret:sequence="default,"#postgres uses default for sequencesoutput=" OUTPUT (Bio)"tail=" RETURNING Bio"#session.bind.dialect.namestatement=sa.text(f"/* PROC BooksAndAuthors.Authors.SelectByBirthDate */"f"select"f" Bio"f" from BooksAndAuthors.Authors"f" where BirthDate = :BirthDate")text_statement=statement.columns(Bio=db_types.NonNullableString,)text_statement=text_statement.bindparams(BirthDate=BirthDate,)returntext_statement@classmethoddefexecute(cls,session:Session,BirthDate:datetime)->List['DB_AuthorsSelectByBirthDate']:params=process_bind_params(session,[sa.types.DateTime,],[BirthDate,])res=session.execute(cls.get_statement(*params))recs=res.fetchall()returnprocess_result_recs(DB_AuthorsSelectByBirthDate,session,[db_types.NonNullableString,],recs)
1 2 3 4 5 6 7 8 91011121314151617181920
/** * Returns any number of records. * @return result set of records found * @exception SQLException is passed through */publicQueryselectByBirthDate()throwsSQLException{Stringstatement="/* PROC ToDoList_App.Authors.SelectByBirthDate */"+"select"+" Bio"+" from ToDoList_App.Authors"+" where BirthDate = ?";PreparedStatementprep=connector.prepareStatement(statement);prep.setDate(1,birthDate);ResultSetresult=prep.executeQuery();Queryquery=newQuery(prep,result);returnquery;}
If the built-in select functions don't give you the power and flexibility you need, you can write your own
select function using a Custom Proc, which is covered in the next section.