Skip to content

Commit

Permalink
Merge branch 'release-v01' into 'master'
Browse files Browse the repository at this point in the history
prepare v0.1 to release

See merge request XvayS/libdb!1
  • Loading branch information
XvayS committed Mar 1, 2021
2 parents cb7b064 + 08075ee commit 7c72aa9
Show file tree
Hide file tree
Showing 6 changed files with 448 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# exclude some files from release archives
.* export-ignore
*.md export-ignore
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# compiled byte code
*.psb
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# libdb

libdb - is an external library for the SoldatServer (Windows and Linux) to access SQLite, MySQL/MariaDB, PostgreSQL or any database type (Oracle, Access, etc) via ODBC.
libdb - is an external library for the SoldatServer (Windows and Linux) to access SQLite, MySQL/MariaDB, PostgreSQL or any database type (Oracle, Access, etc) via ODBC.
2 changes: 2 additions & 0 deletions scripts/libdb/Includes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
libdb_windows.pas
//libdb_linux.pas
220 changes: 220 additions & 0 deletions scripts/libdb/libdb_linux.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// =====================================
// C O N S T A N T S
// =====================================
{ Database plugins enumeration
for the DB_Open() function }
Const DB_Plugin_ODBC = 1;
Const DB_Plugin_SQLite = 2;
Const DB_Plugin_PostgreSQL = 3;

{ Database column types enumeration
for the DB_ColumnType() function }
Const DB_Type_Double = 1;
Const DB_Type_Float = 2;
Const DB_Type_Long = 3;
Const DB_Type_String = 4;

// =====================================
// D E C L A R A T I O N S
// =====================================
Procedure DB_Close(DatabaseID: Integer);
External '[email protected] cdecl';

Function DB_ColumnName(DatabaseID, Column: Integer): PChar;
External '[email protected] cdecl';

Function DB_ColumnSize(DatabaseID, Column: Integer): Integer;
External '[email protected] cdecl';

Function DB_ColumnType(DatabaseID, Column: Integer): Integer;
External '[email protected] cdecl';

Function DB_Columns(DatabaseID: Integer): Integer;
External '[email protected] cdecl';

Function DB_Error(): PChar;
External '[email protected] cdecl';

Function DB_Query(DatabaseID: Integer; Query: PChar): Integer;
External '[email protected] cdecl';

Function DB_Update(DatabaseID: Integer; Query: PChar): Integer;
External '[email protected] cdecl';

Procedure DB_FinishQuery(DatabaseID: Integer);
External '[email protected] cdecl';

Function DB_FirstRow(DatabaseID: Integer): Integer;
External '[email protected] cdecl';

Function DB_GetDouble(DatabaseID, Column: Integer): Double;
External '[email protected] cdecl';

Function DB_GetFloat(DatabaseID, Column: Integer): Single;
External '[email protected] cdecl';

Function DB_GetLong(DatabaseID, Column: Integer): LongInt;
External '[email protected] cdecl';

Function DB_GetString(DatabaseID, Column: Integer): PChar;
External '[email protected] cdecl';

Function DB_IsDatabase(DatabaseID: Integer): Integer;
External '[email protected] cdecl';

Function DB_NextRow(DatabaseID: Integer): Integer;
External '[email protected] cdecl';

Function DB_Open(DatabaseID: Integer; DatabaseName, User, Password: PChar; Plugin: Integer): Integer;
External '[email protected] cdecl';

Function DB_ExamineDrivers(): Integer;
External '[email protected] cdecl';

Function DB_NextDriver(): Integer;
External '[email protected] cdecl';

Function DB_DriverDescription(): PChar;
External '[email protected] cdecl';

Function DB_DriverName(): PChar;
External '[email protected] cdecl';

// =====================================
// E X A M P L E S
// =====================================
function CheckDatabaseUpdate(Database: Integer; Query: PChar): Integer;
var
Check: Integer;
begin
WriteLn('>'+Query); //debug output
Check:= DB_Update(Database, Query);

if Check = 0 then
WriteLn(DB_Error());

Result:= Check;
end;


procedure create_db1();
var
db_path: String;
begin
db_path:= 'test.db';
//creating a new file if not exists
if Not FileExists(db_path) then
if WriteFile(db_path,'') then
WriteLn('File "'+db_path+'" created...');

if DB_Open(0, db_path, '', '', DB_Plugin_SQLite) <> 0 then
begin
WriteLn('Database "'+db_path+'" opened...');
CheckDatabaseUpdate(0, 'CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name STRING, value INTEGER);');
CheckDatabaseUpdate(0, 'INSERT INTO test(name, value) VALUES("name1", 5);');
CheckDatabaseUpdate(0, 'INSERT INTO test(name, value) VALUES("name2", 10);');
CheckDatabaseUpdate(0, 'INSERT INTO test(name, value) VALUES(''name3'', 15);');
WriteLn('Values inserted...');
DB_Close(0);
writeln('Database "'+db_path+'" closed...');
end;
end;


procedure create_db2();
var
db_path: String;
query: String;
db, i: Integer;
begin
db_path:= 'test.db';
//creating a new file if not exists
if (Not FileExists(db_path)) then
if (WriteFile(db_path,'')) then
WriteLn('File "'+db_path+'" created...');

db:= 0;
if DB_Open(db, db_path, '', '', DB_Plugin_SQLite) <> 0 then
begin
WriteLn('Database "'+db_path+'" opened...');
query:= 'CREATE TABLE IF NOT EXISTS test(';
query:= query+'id INTEGER PRIMARY KEY, ';
query:= query+'name TEXT NOT NULL, ';
query:= query+'value INTEGER, ';
query:= query+'UNIQUE(name) ON CONFLICT IGNORE);';
if CheckDatabaseUpdate(db, query) <> 0 then
begin
DB_Update(db, 'BEGIN;');
for i:= 1 to 20 do
CheckDatabaseUpdate(db, 'INSERT INTO test(name, value) VALUES("name'+IntToStr(i)+'", '+IntToStr(i)+');');

DB_Update(db, 'COMMIT;');
WriteLn('Values inserted...');
end;
DB_Close(db);
WriteLn('Database "'+db_path+'" closed...');
end;
end;


procedure query_db(param: Integer);
var
db_path, query, result_srt: String;
i, columns: Integer;
begin
db_path:= 'test.db';
if DB_Open(0, db_path, '', '', DB_Plugin_SQLite) <> 0 then
begin
WriteLn('Database "'+db_path+'" opened...');
query:= 'SELECT * FROM test WHERE value > '+IntToStr(param)+';';
if DB_Query(0, query) <> 0 then
begin
WriteLn('>'+query); //debug output
columns:= DB_Columns(0);
while DB_NextRow(0) <> 0 do
begin
result_srt:='';
for i:= 0 to columns - 1 do
result_srt:= result_srt +DB_ColumnName(0, i)+': '+DB_GetString(0, i)+'; ';

WriteLn(result_srt);
end;

DB_FinishQuery(0);
end;

DB_Close(0);
WriteLn('Database "'+db_path+'" closed...');
end
else
WriteLn('Can not open database "'+db_path+'"!');
end;


procedure drivers();
begin
if DB_ExamineDrivers() <> 0 then
begin
WriteLn('ODBC drivers installed:');
while DB_NextDriver() <> 0 do
begin
WriteLn('Name - '+DB_DriverName());
WriteLn('Desc - '+DB_DriverDescription());
WriteLn('* * * * *');
end;
end
else
WriteLn('No ODBC drivers installed!');
end;


procedure OnAdminMessage(IP, Msg: String);
begin
case LowerCase(Msg) of
'/create1' : create_db1();
'/create2' : create_db2();
'/query1' : query_db(7);
'/query2' : query_db(14);
'/drivers' : drivers();
end;
end;
Loading

0 comments on commit 7c72aa9

Please sign in to comment.