Skip to content

Checking if a record exists

To check if a row with a specific primary key exists, we use the Exists proc.


DATABASE ExampleDatabase
PACKAGE com.example.db
SERVER ExampleServer
SCHEMA ToDoList_App

TABLE ToDoList
   ID               SEQUENCE
   ListName         CHAR(255)
   ListType         SHORT (Private=1, Public=2)
   Description      CHAR(255)
   LastUpdated      TIMESTAMP

KEY PKEY PRIMARY
    ID

PROC Exists

Exist

Checks if a record with a specific primary key exists.

Exists

Exists will check if a record with a specific primary key exists.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@dataclass
class DB_ToDoListExists:
    #Outputs
    noOf: int

    @classmethod
    def get_statement(cls
                     , ID: int
                     ) -> TextAsFrom:
        class _ret:
            sequence = "default," #postgres uses default for sequences
            output = " OUTPUT (noOf)"
            tail = " RETURNING noOf"
            #session.bind.dialect.name

        statement = sa.text(
                        f"/* PROC ToDoList_App.ToDoList.Exists */"
                        f"select count(*) noOf from ToDoList_App.ToDoList"
                        f" where ID = :ID")

        text_statement = statement.columns(noOf=sa.types.Integer,
                                      )
        text_statement = text_statement.bindparams(ID=ID,
                                         )
        return text_statement

    @classmethod
    def execute(cls, session: Session, ID: int
                     ) -> Optional['DB_ToDoListExists']:
        params = process_bind_params(session, [sa.types.Integer,
                                        ], [ID,
                                        ])
        res = session.execute(cls.get_statement(*params))
        rec = res.fetchone()
        if rec:
            res.close()
            return process_result_rec(DB_ToDoListExists, session, [sa.types.Integer,
                                        ], rec)

        return None
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public boolean exists() throws SQLException
{
  String statement = 
    "/* PROC BooksAndAuthors.ToDoList.Exists */"
  + "select count(*) noOf from BooksAndAuthors.ToDoList"
  + " where ID = ?"
  ;
  PreparedStatement prep = connector.prepareStatement(statement);
  prep.setInt(1, id);
  ResultSet result = prep.executeQuery();
  if (!result.next())
  {
    result.close();
    prep.close();
    return false;
  }
  noOf =  result.getInt(1);
  result.close();
  prep.close();
  return true;
}