Giter Club home page Giter Club logo

sqlite-vtable__file_system_dr_dobbs's Introduction

About

This is example code corresponding to a Dr. Dobbs article I wrote a long time ago called Query Anything with SQLite. It contains two examples of SQLite virtual tables. One example creates a virtual table out of the file system. Another is just a skeleton to use as a blank slate.

The filesystem virtual table gives you a relational interface into your filesystem. With it you can search for files with SQL like so:

select
  ctime, atime, mtime, uid, gid, size/(1024*1024) as 'size (MB)', 
  path || '/' || name as file 
from fs 
where 
    path match '/usr/lib, /var/log'
    and ((name like '%.so') || (name like '%.log'))
    order by size desc;  

The results will look something like the following:

ctime     mtime     uid    gid    size (MB)  file                                              
--------  --------  -----  -----  ---------  --------------------------------------------------
14334643  14313426  0      0      66         /usr/lib/firefox/libxul.so                        
14334646  14313440  0      0      64         /usr/lib/thunderbird/libxul.so                    
14338094  14298261  0      0      51         /usr/lib/libreoffice/program/libmergedlo.so       
14334659  14174942  0      0      37         /usr/lib/nvidia-331/libnvidia-glcore.so.331.113   
14334793  13970685  0      0      33         /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4.10.2   
14334732  13970682  0      0      32         /usr/lib/i386-linux-gnu/libQtWebKit.so.4.10.2     
14334731  14291730  0      0      30         /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar  
14334641  14225935  0      0      30         /usr/lib/x86_64-linux-gnu/libwebkitgtk-1.0.so.0.22
14334641  14225935  0      0      30         /usr/lib/x86_64-linux-gnu/libwebkitgtk-3.0.so.0.22
14336011  14237675  0      0      28         /usr/lib/x86_64-linux-gnu/libLLVM-3.5.so.1        

The table has the following available columns:

  "name  text, "  /* col 0  : name             */
  "path  text, "  /* col 1  : path             */
  "type  int,  "  /* col 2  : type             */
  "size  int,  "  /* col 3  : size             */
  "uid   int,  "  /* col 4  : uid              */
  "gid   int,  "  /* col 5  : gid              */
  "prot  int,  "  /* col 6  : protection bits  */
  "mtime int,  "  /* col 7  : modified time    */
  "ctime int,  "  /* col 8  : create time      */
  "atime int,  "  /* col 9  : access time      */
  "dev   int,  "  /* col 10 : device           */
  "nlink int,  "  /* col 11 : number of links  */
  "inode int   "  /* col 12 : dir inode        */
  "dir   int   "  /* col 13 : dir inode        */

Building

You must have the Apache Portable Runtime and the SQLite libraries installed on your system and within you search path.

Linux, MinGW and Unix-like Systems:

autogen.sh
./configure
make

You only have to run the first two lines once. You may need to use the --prefix switch to point to where your standard /include and /lib files are (e.g. --prefix=/usr will use /usr/include and /usr/lib respectively. )

The end result will be a shared library called libvtable.so and an executable named program (from main.c). If libvtable.so is not in your library search path, you will need to specify it using LD_LIBRARY_PATH, e.g.

export LD_LIBRARY_PATH=`pwd`

Then you can run the program, or run the test.sql script using the sqlite3 command line utility as follows:

sqlite3 db < test.sql

Windows MS Visual C++

If you want to use the virtual table as a dynamically loadable module, you will need to create a DLL project that contains the following files:

lib.c example.c fs.c common.c

Then create a console application that uses main.c. This must link to the SQLite and Apache Portable Runtime (1.0 or greater).

The example.c file contains a bare-bones working example of a virtual table, so if you don't have APR for Windows handy and just want to get something to compile, you can use this as a starting point.

sqlite-vtable__file_system_dr_dobbs's People

Contributors

bradparks avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.