Results 1 to 5 of 5

Thread: SQLite Plugin

  1. #1
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default SQLite Plugin

    I've seen multiple requests for an SQLite-Plugin for Simba. I wrote one.. Should be enough exports for our needs.

    I wrote it in such a way that there are no memory allocations other than what SQLite does itself. For you, there's no "Init" or "Free" functions.

    Source:
    https://github.com/Brandon-T/XIO-SQLitePlugin

    Release (Windows & OSX):
    https://github.com/Brandon-T/XIO-SQLitePlugin/releases


    Example:

    Simba Code:
    {$loadlib SQLitePlugin}

    var
      db: SQLite;
      res: Integer;

    begin
      res := db.open('C:/Users/Brandon/Desktop/Contacts.db', $00000002 or $00000004); //readwrite | create
      if (res <> 0) then
        writeln('Error Creating DB');

      db.close();
    end.


    The data types are fairly simple (Context is the `self` pointer -- You can copy, move, read, do whatever you want -- Do NOT assign to it):

    Simba Code:
    type SQLiteContextRecord = packed record
      context: pointer;
    end;

    type SQLiteBackupRecord = packed record
      context: pointer;
    end;

    type SQLiteValueRecord = packed record
      context: pointer;
    end;

    type SQLiteStatementRecord = packed record
      context: pointer;
    end;

    type SQLiteBlobRecord = packed record
      context: pointer;
    end;

    type SQLiteRecord = packed record
      context: pointer;
    end;

    and the methods that are exported are:

    Simba Code:
    function SQLiteContext.aggregate(nBytes: Integer): SQLiteContext;
    function SQLiteContext.count(): Integer;
    function SQLiteContext.dbHandle(): SQLite;

    function SQLiteBackup.finish(): Integer;
    function SQLiteBackup.pageCount(): Integer;
    function SQLiteBackup.remaining(): Integer;
    function SQLiteBackup.step(nPage: Integer): Integer;

    function SQLiteValue.blobValue(): SQLiteBlob;
    function SQLiteValue.bytesValue(): Integer;
    function SQLiteValue.doubleValue(): Double;
    function SQLiteValue.intValue(): Integer;
    function SQLiteValue.int64Value(): Int64;
    function SQLiteValue.textValue(): PChar;
    function SQLiteValue.valueType(): Integer;
    function SQLiteValue.numericType(): Integer;

    function SQLiteStatement.bindBlob(I: Integer; zData: Pointer; nData: Integer; xDel: procedure(ptr: Pointer)): Integer;
    function SQLiteStatement.bindBlob64(I: Integer; zData: Pointer; nData: UInt64; xDel: procedure(ptr: Pointer)): Integer;
    function SQLiteStatement.bindDouble(I: Integer; rValue: Double): Integer;
    function SQLiteStatement.bindInt(I: Integer; iValue: Integer): Integer;
    function SQLiteStatement.bindInt64(I: Integer; iValue: Int64): Integer;
    function SQLiteStatement.bindNull(I: Integer): Integer;
    function SQLiteStatement.bindParameterCount(): Integer;
    function SQLiteStatement.bindParameterIndex(name: String): Integer;
    function SQLiteStatement.bindParameterName(i: Integer): PChar;
    function SQLiteStatement.bindText(I: Integer; zData: String; nData: Integer; xDel: procedure(ptr: Pointer)): Integer;
    function SQLiteStatement.bindText64(I: Integer; zData: String; nData: Int64; xDel: procedure(ptr: Pointer); encoding: UInt8): Integer;
    function SQLiteStatement.bindValue(I: Integer; value: SQLiteValue): Integer;
    function SQLiteStatement.bindZeroBlob(i: Integer; n: Integer): Integer;
    function SQLiteStatement.bindZeroBlob64(i: Integer; n: Int64): Integer;
    function SQLiteStatement.clearBindings(): Integer;
    function SQLiteStatement.columnBlob(iCol: Integer): Pointer;
    function SQLiteStatement.columnBytes(iCol: Integer): Integer;
    function SQLiteStatement.columnCount(): Integer;
    function SQLiteStatement.columnDeclType(N: Integer): PChar;
    function SQLiteStatement.columnDouble(iCol: Integer): Double;
    function SQLiteStatement.columnInt(iCol: Integer): Integer;
    function SQLiteStatement.columnInt64(iCol: Integer): Int64;
    function SQLiteStatement.columnName(iCol: Integer): PChar;
    function SQLiteStatement.columnText(iCol: Integer): PChar;
    function SQLiteStatement.columnType(iCol: Integer): Integer;
    function SQLiteStatement.columnValue(iCol: Integer): SQLiteValue;
    function SQLiteStatement.dataCount(): Integer;
    function SQLiteStatement.finalize(): Integer;
    function SQLiteStatement.reset(): Integer;
    function SQLiteStatement.step(): Integer;

    function SQLiteBlob.bytes(): Integer;
    function SQLiteBlob.close(): Integer;
    function SQLiteBlob.read(z: Pointer; n: Integer; iOffset: Integer): Integer;
    function SQLiteBlob.reopen(iRow: Integer): Integer;
    function SQLiteBlob.write(z: Pointer; n: Integer; iOffset: Integer): Integer;

    function SQLite.backup(pDestDb: SQLite; zDestDb: String): SQLiteBackup;
    function SQLite.changes(): Integer;
    function SQLite.close(): Integer;
    function SQLite.createFunction(zFunc: String; nArg: Integer; enc: Integer; pCtx: Pointer; xSFunc: procedure(ctx: pointer; i: Integer; value: pointer); xDestroy: procedure(ptr: pointer)): Integer;
    function SQLite.cacheFlush(): Integer;
    function SQLite.errorCode(): Integer;
    function SQLite.errorMessage(): PChar;
    function SQLite.errorString(errorCode: Integer): PChar;
    function SQLite.exec(sql: String): Integer;
    function SQLite.lastInsertedRowId(): Int64;
    function SQLite.libVersion(): PChar;
    function SQLite.libVersionNumber(): Integer;
    function SQLite.open(fileName: String; flags: Integer): Integer;
    function SQLite.prepare(sql: String; nBytes: Integer; var statement: SQLiteStatement): Integer;

    P.S. I'd export all the constants and enums but Simba has problems with those OR I'm doing it wrong..
    Last edited by Brandon; 08-01-2016 at 11:13 PM.
    I am Ggzz..
    Hackintosher

  2. #2
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    You could probably write it purely in Simba 1.2 as an include instead, as it allows you to call various methods with different calling conventions.
    Also helps, since plugins doesn't allow you to export variables / constants.

    Anyway, good job

    Edit: an include might not work after all, I see some functions as parameters there, an issue in lape makes it impossible to specify calling for lape functions (native funcs defaults to register atm).
    Last edited by slacky; 08-02-2016 at 12:23 AM.
    !No priv. messages please

  3. #3
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    You could probably write it purely in Simba 1.2 as an include instead, as it allows you to call various methods with different calling conventions.
    Also helps, since plugins doesn't allow you to export variables / constants.

    Anyway, good job

    Edit: an include might not work after all, I see some functions as parameters there, an issue in lape makes it impossible to specify calling for lape functions (native funcs defaults to register atm).

    Simba 1.2? Is there an API doc? Purely as include (Meaning there'd be no plugin -- if so, how do you access the sqlite functions)?

    99% of the time the function pointer parameters are set to nil. I was actually going to set them to nil and export the signature without it because no one ever specifies it. Example is `Exec` (in SQLite3 it has 4-5 parameters but the last few are almost always nil).
    I am Ggzz..
    Hackintosher

  4. #4
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

  5. #5
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Ahh I see. In that case, users would just download sqlite official precompiled library/binary and use an include that wraps it instead of a custom plugin. Pretty cool.
    I am Ggzz..
    Hackintosher

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •