-
Notifications
You must be signed in to change notification settings - Fork 5
TSQLite3Table
Ivan Semenkov edited this page Feb 1, 2021
·
1 revision
TSQLite3Table class encapsulates the SQLite3 database table.
uses
sqlite3.table;
type
TSQLite3Table = class
A new database table can be created by call its constructor.
constructor Create (AErrorsStack : PSQL3LiteErrorsStack; ADBHandle :
ppsqlite3; ATableName : String);
uses
libpassqlite, sqlite3.errors_stack, sqlite3.connection, sqlite3.table;
var
errors : TSQLite3ErrorsStack;
handle : psqlite3;
connection : TSQLite3DatabaseConnection;
table : TSQLite3Table;
begin
errors := TSQLite3ErrorsStack.Create;
connection := TSQLite3DatabaseConnection.Create(@errors, @handle, 'database',
[SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
table := TSQLite3Table.Create(@errors, @handle, 'table_name');
FreeAndNil(connection);
FreeAndNil(errors);
end;
Or use TSQLite3Builder object.
function Table (ATableName : String) : TSQLite3Table;
uses
sqlite3.builder, sqlite3.table;
var
builder : TSQLite3Builder;
table : TSQLite3Table;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
table := builder.Table('table_name');
FreeAndNil(builder);
end;
Create new table from TSQLite3Schema object.
procedure New (ASchema : TSQLite3Schema);
uses
sqlite3.builder, sqlite3.schema;
var
builder : TSQLite3Builder;
schema : TSQLite3Schema;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
schema := TSQLite3Schema;
schema.Id.Text('value');
builder.Table('table_name').New(schema);
FreeAndNil(builder);
end;
Create new table if it not exists from TSQLite3Schema object.
procedure NewIfNotExists (ASchema : TSQLite3Schema);
uses
sqlite3.builder, sqlite3.schema;
var
builder : TSQLite3Builder;
schema : TSQLite3Schema;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
schema := TSQLite3Schema;
schema.Id.Text('value');
builder.Table('table_name').NewIfNotExists(schema);
FreeAndNil(builder);
end;
Check database table schema.
function CheckSchema (ASchema : TSQLite3Schema) : Boolean;
uses
sqlite3.builder, sqlite3.schema;
var
builder : TSQLite3Builder;
schema : TSQLite3Schema;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
schema := TSQLite3Schema;
schema.Id.Text('value');
builder.Table('table_name').NewIfNotExists(schema);
if builder.Table('table_name').CheckSchema(schema) then
;
FreeAndNil(builder);
end;
Check if table exists.
function Exists : Boolean;
uses
sqlite3.builder;
var
builder : TSQLite3Builder;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
if builder.Table('table_name').Exists then
;
FreeAndNil(builder);
end;
Check if table has column.
function HasColumn (AColumnName : String) : Boolean;
uses
sqlite3.builder;
var
builder : TSQLite3Builder;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
if builder.Table('table_name').HasColumn('value') then
;
FreeAndNil(builder);
end;
Delete table.
procedure Drop;
uses
sqlite3.builder;
var
builder : TSQLite3Builder;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
builder.Table('table_name').Drop;
FreeAndNil(builder);
end;
Delete table if it exists.
procedure DropIfExists;
uses
sqlite3.builder;
var
builder : TSQLite3Builder;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
builder.Table('table_name').DropIfExists;
FreeAndNil(builder);
end;
Rename table.
procedure Rename (ANewName : String);
uses
sqlite3.builder;
var
builder : TSQLite3Builder;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
builder.Table('table_name').Rename('new_name');
FreeAndNil(builder);
end;
Create new TSQLite3Select object for table.
function Select : TSQLite3Select;
uses
sqlite3.builder, 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;
Create new TSQLite3Insert object for table.
function Insert : TSQLite3Insert;
uses
sqlite3.builder, sqlite3.insert;
var
builder : TSQLite3Builder;
insert : TSQLite3Insert;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
insert := builder.Table('table_name').Insert;
FreeAndNil(builder);
end;
Create new TSQLite3Update object for table.
function Update : TSQLite3Update;
uses
sqlite3.builder, sqlite3.update;
var
builder : TSQLite3Builder;
update : TSQLite3Update;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
update := builder.Table('table_name').Update;
FreeAndNil(builder);
end;
Create new TSQLite3Delete object for table.
function Delete : TSQLite3Delete;
uses
sqlite3.builder, sqlite3.delete;
var
builder : TSQLite3Builder;
delete : TSQLite3Delete;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
delete := builder.Table('table_name').Delete;
FreeAndNil(builder);
end;