-
Notifications
You must be signed in to change notification settings - Fork 5
TSQLite3Select
TSQLite3Select class encapsulates the SQLite3 database table select part of query.
uses
sqlite3.select;
type
TSQLite3Select = class
A new select query can be created by call its constructor.
constructor Create (AErrorsStack : PSQL3LiteErrorsStack; ADBHandle :
ppsqlite3; ATableName : String);
uses
libpassqlite, sqlite3.errors_stack, sqlite3.connection, sqlite3.select;
var
errors : TSQLite3ErrorsStack;
handle : psqlite3;
connection : TSQLite3DatabaseConnection;
select : TSQLite3Select;
begin
errors := TSQLite3ErrorsStack.Create;
connection := TSQLite3DatabaseConnection.Create(@errors, @handle, 'database',
[SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := TSQLite3Select.Create(@errors, @handle, 'table_name');
FreeAndNil(select);
FreeAndNil(connection);
FreeAndNil(errors);
end;
Or use TSQLite3Builder object.
function Select : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select;
FreeAndNil(builder);
end;
Select all data from table. Details on SQLite C/C++ API page.
function All : TSQLite3Select;
SQL query SELECT *
.
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select.All;
FreeAndNil(builder);
end;
Select concrete column. The function can be called multiple times. Details on SQLite C/C++ API page.
function Field (AColumnName : String) : TSQLite3Select;
SQL query SELECT AColumnName
.
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.Field('id')
.Field('value');
FreeAndNil(builder);
end;
Select concrete column and set its alias. The function can be called multiple times. Details on SQLite C/C++ API page.
function Field (AColumnName, AColumnAlias : String) : TSQLite3Select;
SQL query SELECT AColumnName AS AColumnAlias
.
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.Field('id')
.Field('value', 'val');
FreeAndNil(builder);
end;
Duplicate rows are removed from the set of result rows. Details on SQLite C/C++ API page.
function Distinct : TSQLite3Select;
SQL query SELECT ... DISTINCT
.
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Distinct;
FreeAndNil(builder);
end;
There are several comparison operators which can used in where clause.
TWhereComparisonOperator = (
Equal comparison operator.
COMPARISON_EQUAL, { = }
Not equal comparison operator.
COMPARISON_NOT_EQUAL, { <> }
Less comparison operator.
COMPARISON_LESS, { < }
Greater comparison operator.
COMPARISON_GREATER, { > }
Less or equal comparison operator.
COMPARISON_LESS_OR_EQUAL, { <= }
Greater or equal comparison operator.
COMPARISON_GREATER_OR_EQUAL, { >= }
Not comparison operator.
COMPARISON_NOT { IS NOT }
);
If a WHERE clause is specified, the WHERE expression is evaluated for each row in the input data as a boolean expression. Only rows for which the WHERE clause expression evaluates to true are included from the dataset before continuing. Rows are excluded from the result if the WHERE clause evaluates to either false or NULL. Details on SQLite C/C++ API page.
function Where (AColumnName : String; AComparison :
TWhereComparisonOperator; AValue : String) : TSQLite3Select;
function Where (AColumnName : String; AComparison :
TWhereComparisonOperator; AValue : Integer) : TSQLite3Select;
function Where (AColumnName : String; AComparison :
TWhereComparisonOperator; AValue : Double) : TSQLite3Select;
For equal comparison operator there are present short form.
function Where (AColumnName : String; AValue : String) : TSQLite3Select;
function Where (AColumnName : String; AValue : Integer) : TSQLite3Select;
function Where (AColumnName : String; AValue : Double) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Where('id', COMPARISON_GREATER, 2);
FreeAndNil(builder);
end;
It is a short form of where clause with value equal NULL
.
function WhereNull (AColumnName : String) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.WhereNull('value');
FreeAndNil(builder);
end;
It is a short form of where clause with value not equal NULL
.
function WhereNotNull (AColumnName : String) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.WhereNotNull('value');
FreeAndNil(builder);
end;
Adds AND WHERE
clause to query.
function AndWhere (AColumnName : String; AComparison :
TWhereComparisonOperator; AValue : String) : TSQLite3Select;
function AndWhere (AColumnName : String; AComparison :
TWhereComparisonOperator; AValue : Integer) : TSQLite3Select;
function AndWhere (AColumnName : String; AComparison :
TWhereComparisonOperator; AValue : Double) : TSQLite3Select;
For equal comparison operator there are present short form.
function AndWhere (AColumnName : String; AValue : String) : TSQLite3Select;
function AndWhere (AColumnName : String; AValue : Integer) : TSQLite3Select;
function AndWhere (AColumnName : String; AValue : Double) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Where('value', 2)
.AndWhere('value', COMPARISON_GREATER, 45);
FreeAndNil(builder);
end;
It is a short form of where clause with value equal NULL
.
function AndWhereNull (AColumnName : String) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Where('value', 2)
.AndWhereNull('value');
FreeAndNil(builder);
end;
It is a short form of where clause with value not equal NULL
.
function AndWhereNotNull (AColumnName : String) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Where('id', COMPARISON_GREATER, 45)
.AndWhereNotNull('value');
FreeAndNil(builder);
end;
Adds OR WHERE
clause to query.
function OrWhere (AColumnName : String; AComparison :
TWhereComparisonOperator; AValue : String) : TSQLite3Select;
function OrWhere (AColumnName : String; AComparison :
TWhereComparisonOperator; AValue : Integer) : TSQLite3Select;
function OrWhere (AColumnName : String; AComparison :
TWhereComparisonOperator; AValue : Double) : TSQLite3Select;
For equal comparison operator there are present short form.
function OrWhere (AColumnName : String; AValue : String) : TSQLite3Select;
function OrWhere (AColumnName : String; AValue : Integer) : TSQLite3Select;
function OrWhere (AColumnName : String; AValue : Double) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Where('value', 2)
.OrWhere('value', 45);
FreeAndNil(builder);
end;
It is a short form of where clause with value equal NULL
.
function OrWhereNull (AColumnName : String) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Where('value', 2)
.OrWhereNull('value');
FreeAndNil(builder);
end;
It is a short form of where clause with value not equal NULL
.
function OrWhereNotNull (AColumnName : String) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Where('id', COMPARISON_GREATER, 45)
.OrWhereNotNull('value');
FreeAndNil(builder);
end;
There are several join types which can used.
TJoinType = (
SELECT a1, a2, b1, b2
FROM A
INNER JOIN B on B.f = A.f;
For each row in the A table, the INNER JOIN
clause compares the value of the f column with the value of the f column in the B table. If the value of the f column in the A table equals the value of the f column in the B table, it combines data from a1, a2, b1, b2, columns and includes this row in the result set.
JOIN_INNER,
SELECT
a,
b
FROM
A
LEFT JOIN B ON A.f = B.f
WHERE search_condition;
The statement returns a result set that includes:
- Rows in table A (left table) that have corresponding rows in table B.
- Rows in the table A and the rows in the table B filled with
NULL
values in case the row from table A does not have any corresponding rows in table B.
In other words, all rows in table A are included in the result set whether there are matching rows in table B or not.
JOIN_OUTER_LEFT,
SELECT *
FROM A
INNER JOIN B;
Suppose, the A table has N rows and B table has M rows, the CROSS JOIN
of these two tables will produce a result set that contains NxM
rows.
JOIN_CROSS
);
To query data from multiple tables, uses JOIN
clause. This clause combines columns from correlated tables.
function Join (ATableName : String; AJoinType : TJoinType; AColumnName :
String; ACurrentTableColumn : String) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Join('table2', JOIN_OUTER_LEFT, 'id');
FreeAndNil(builder);
end;
For most used join types there are present short forms.
function InnerJoin (ATableName : String; AColumnName : String;
ACurrentTableColumn : String) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.InnerJoin('table2', 'value', 'id');
FreeAndNil(builder);
end;
function LeftJoin (ATableName : String; AColumnName : String;
ACurrentTableColumn : String) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.LeftJoin('table2', 'value', 'id');
FreeAndNil(builder);
end;
There are several order by types which can used.
TOrderByType = (
The ASC
keyword means ascending.
ORDER_ASC,
And the DESC
keyword means descending.
ORDER_DESC
);
Order by clause.
function OrderBy (AColumnName : String; AOrderBy : TOrderByType) :
TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.OrderBy(ORDER_ASC);
FreeAndNil(builder);
end;
Group by clause.
function GroupBy (AColumnName : String) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.OrderBy(ORDER_ASC)
.GroupBy('id')
.GroupBy('value');
FreeAndNil(builder);
end;
Use the LIMIT
clause to constrain the number of rows returned by the query.
function Limit (ACount : Cardinal) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Limit(2);
FreeAndNil(builder);
end;
function Offset (ACount : Cardinal) : TSQLite3Select;
uses
sqlite3.builder, sqlite3.table, sqlite3.select;
var
builder : TSQLite3Builder;
select : TSQLite3Select;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
select := builder.Table('table_name').Select
.All
.Limit(2)
.Offset(4);
FreeAndNil(builder);
end;
Run query and get result.
function Get : TSQLite3Result;
Result presents by TSQLite3Result object.
uses
sqlite3.builder, sqlite3.table, sqlite3.result;
var
builder : TSQLite3Builder;
res : TSQLite3Result;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
res := builder.Table('table_name').Select
.All
.Get;
FreeAndNil(builder);
end;