Yunqa • The Delphi Inspiration

Delphi Components and Applications

User Tools

Site Tools


wiki:sqlite3:disqlite3zlib.pas
no way to compare when less than two revisions

Differences

This shows you the differences between two versions of the page.


wiki:sqlite3:disqlite3zlib.pas [2020/08/28 11:42] (current) – created - external edit 127.0.0.1
Line 1: Line 1:
 +====== DISQLite3: DISQLite3ZLib.pas======
  
 +{{page>:products:sqlite3:header}}
 +{{page>:wiki-header}}
 +
 +<WRAP tip>An updated version of this unit is distributed with the DISQLite3 package.</WRAP>
 +
 +<code delphi>
 +{-------------------------------------------------------------------------------
 +
 + Copyright (c) 1999-2017 Ralf Junker, The Delphi Inspiration
 + Internet: https://www.yunqa.de/delphi/
 + E-Mail:   delphi@yunqa.de
 +
 +-------------------------------------------------------------------------------}
 +
 +unit DISQLite3ZLib;
 +
 +{$I DI.inc}
 +{$I DISQLite3.inc}
 +
 +{$IFDEF DISQLite3_Personal}
 +!!! This unit does not compile with DISQLite3 Personal !!!
 +!!! Download DISQLite3 Pro from www.yunqa.de/delphi/   !!!
 +{$ENDIF DISQLite3_Personal}
 +
 +interface
 +
 +uses
 +  DISQLite3Api;
 +
 +procedure sqlite3_create_function_zlib(const DB: sqlite3);
 +
 +implementation
 +
 +uses
 +  SysUtils, zlib;
 +
 +resourcestring
 +  SCompressError = 'COMPRESS() error';
 +  SDecompressError = 'UNCOMPRESS() error';
 +
 +procedure sqlite3_zlib_compress_func(
 +  pCtx: sqlite3_context;
 +  nArgs: Integer;
 +  Args: PPointerArray);
 +var
 +  Arg0: Pointer;
 +  InBuf, OutBuf: Pointer;
 +  InBytes, OutBytes: Integer;
 +begin
 +  Arg0 := Args[0];
 +  case sqlite3_value_type(Arg0) of
 +    SQLITE_TEXT:
 +      begin
 +        InBuf := sqlite3_value_text(Arg0);
 +        InBytes := sqlite3_value_bytes(Arg0);
 +        if InBytes > 0 then
 +          begin
 +            try
 +              CompressBuf(InBuf, InBytes, OutBuf, OutBytes);
 +              if OutBytes > 0 then
 +                sqlite3_result_text(
 +                  pCtx, OutBuf, OutBytes, sqlite3_destroy_mem)
 +              else
 +                sqlite3_result_text(pCtx, '', 0, SQLITE_STATIC);
 +            except
 +              sqlite3_result_error(
 +                pCtx, PAnsiChar(SCompressError), Length(SCompressError));
 +            end;
 +            Exit;
 +          end;
 +      end;
 +    SQLITE_BLOB:
 +      begin
 +        InBuf := sqlite3_value_blob(Arg0);
 +        InBytes := sqlite3_value_bytes(Arg0);
 +        if InBytes > 0 then
 +          begin
 +            try
 +              CompressBuf(InBuf, InBytes, OutBuf, OutBytes);
 +              if OutBytes > 0 then
 +                sqlite3_result_blob(
 +                  pCtx, OutBuf, OutBytes, sqlite3_destroy_mem)
 +              else
 +                sqlite3_result_blob(pCtx, Pointer(1), 0, SQLITE_STATIC);
 +            except
 +              sqlite3_result_error(
 +                pCtx, PAnsiChar(SCompressError), Length(SCompressError));
 +            end;
 +            Exit;
 +          end;
 +      end;
 +  end;
 +  sqlite3_result_value(pCtx, Arg0);
 +end;
 +
 +procedure sqlite3_zlib_uncompress_func(
 +  pCtx: sqlite3_context;
 +  nArgs: Integer;
 +  Args: PPointerArray);
 +var
 +  Arg0: Pointer;
 +  InBuf, OutBuf: Pointer;
 +  InBytes, OutBytes: Integer;
 +begin
 +  Arg0 := Args[0];
 +  case sqlite3_value_type(Arg0) of
 +    SQLITE_TEXT:
 +      begin
 +        InBuf := sqlite3_value_text(Arg0);
 +        InBytes := sqlite3_value_bytes(Arg0);
 +        if InBytes > 0 then
 +          begin
 +            try
 +              DeCompressBuf(InBuf, InBytes, InBytes * 2, OutBuf, OutBytes);
 +              if OutBytes > 0 then
 +                sqlite3_result_text(
 +                  pCtx, OutBuf, OutBytes, sqlite3_destroy_mem)
 +              else
 +                sqlite3_result_text(pCtx, '', 0, SQLITE_STATIC);
 +            except
 +              sqlite3_result_error(
 +                pCtx, PAnsiChar(SDecompressError), Length(SDecompressError));
 +            end;
 +            Exit;
 +          end;
 +      end;
 +    SQLITE_BLOB:
 +      begin
 +        InBuf := sqlite3_value_blob(Arg0);
 +        InBytes := sqlite3_value_bytes(Arg0);
 +        if InBytes > 0 then
 +          begin
 +            try
 +              DeCompressBuf(InBuf, InBytes, InBytes * 2, OutBuf, OutBytes);
 +              if OutBytes > 0 then
 +                sqlite3_result_blob(
 +                  pCtx, OutBuf, OutBytes, sqlite3_destroy_mem)
 +              else
 +                sqlite3_result_blob(pCtx, Pointer(1), 0, SQLITE_STATIC);
 +            except
 +              sqlite3_result_error(
 +                pCtx, PAnsiChar(SDecompressError), Length(SDecompressError));
 +            end;
 +            Exit;
 +          end;
 +      end;
 +  end;
 +  sqlite3_result_value(pCtx, Arg0);
 +end;
 +
 +procedure sqlite3_create_function_zlib(const DB: sqlite3);
 +begin
 +  sqlite3_check(sqlite3_create_function(DB, 'COMPRESS', 1,
 +    SQLITE_ANY, nil, sqlite3_zlib_compress_func, nil, nil), DB);
 +  sqlite3_check(sqlite3_create_function(DB, 'UNCOMPRESS', 1,
 +    SQLITE_ANY, nil, sqlite3_zlib_uncompress_func, nil, nil), DB);
 +end;
 +
 +end.
 +</code>
wiki/sqlite3/disqlite3zlib.pas.txt · Last modified: 2020/08/28 11:42 by 127.0.0.1