This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
amalgalite / examples / blob.rb
| 7751e6eb » | copiousfreetime | 2008-07-04 | 1 | #!/usr/bin/env ruby | |
| 2 | |||||
| 3 | # | ||||
| 4 | # An Amalgalite example showing how Blob's can be utilized | ||||
| 5 | # | ||||
| 6 | # We'll make a database with one table, that we store files in. We'll use the | ||||
| 7 | # Blob incremental IO to store the files and retrieve them from the database | ||||
| 8 | # | ||||
| 9 | # This little program will store 1 or more files in the sqlite3 database when | ||||
| 10 | # the 'store' action is given, and cat a file to stdout on 'retrieve' | ||||
| 11 | # | ||||
| 12 | # e.g. | ||||
| 13 | # | ||||
| 14 | # ruby blob.rb store a.rb b.rb c.rb # => stores a.rb b.rb and c.rb in the db | ||||
| 15 | # | ||||
| 16 | # ruby blob.rb retrieve a.rb # => dumps a.rb to stdout | ||||
| 17 | # | ||||
| 18 | |||||
| 19 | require 'rubygems' | ||||
| 1e931bd1 » | copiousfreetime | 2008-11-02 | 20 | $: << "../lib" | |
| 21 | $: << "../ext" | ||||
| 7751e6eb » | copiousfreetime | 2008-07-04 | 22 | require 'amalgalite' | |
| 9103eceb » | copiousfreetime | 2008-11-16 | 23 | require 'amalgalite/packer' | |
| af4db9b7 » | copiousfreetime | 2008-07-04 | 24 | VALID_ACTIONS = %w[ list retrieve store ] | |
| 7751e6eb » | copiousfreetime | 2008-07-04 | 25 | def usage | |
| af4db9b7 » | copiousfreetime | 2008-07-04 | 26 | STDERR.puts "Usage: #{File.basename($0)} ( #{VALID_ACTIONS.join(' | ')} ) file(s)" | |
| 7751e6eb » | copiousfreetime | 2008-07-04 | 27 | exit 1 | |
| 28 | end | ||||
| 29 | |||||
| 30 | # | ||||
| 31 | # This does the basic command line parsing | ||||
| 32 | # | ||||
| af4db9b7 » | copiousfreetime | 2008-07-04 | 33 | usage if ARGV.size < 1 | |
| 7751e6eb » | copiousfreetime | 2008-07-04 | 34 | action = ARGV.shift | |
| af4db9b7 » | copiousfreetime | 2008-07-04 | 35 | usage unless VALID_ACTIONS.include? action | |
| 7751e6eb » | copiousfreetime | 2008-07-04 | 36 | file_list = ARGV | |
| 37 | |||||
| 38 | # | ||||
| 39 | # create the database if it doesn't exist | ||||
| 40 | # | ||||
| 41 | db = Amalgalite::Database.new( "filestore.db" ) | ||||
| 42 | |||||
| 43 | case action | ||||
| 44 | # | ||||
| af4db9b7 » | copiousfreetime | 2008-07-04 | 45 | # list all the files that are stored in the database | |
| 46 | # | ||||
| 47 | when 'list' | ||||
| ae1a6632 » | copiousfreetime | 2008-10-22 | 48 | db.execute("SELECT filename FROM rubylibs") do |row| | |
| 49 | puts row['filename'] | ||||
| af4db9b7 » | copiousfreetime | 2008-07-04 | 50 | end | |
| 51 | |||||
| 52 | # | ||||
| 7751e6eb » | copiousfreetime | 2008-07-04 | 53 | # if we are doing the store action, then loop over the files and store them in | |
| 54 | # the database. This will use incremental IO to store the files directly from | ||||
| 55 | # the file names. | ||||
| 56 | # | ||||
| 57 | # It is slightly strange in that you have to tell the Blob object what column | ||||
| 58 | # it is going to, but that is necessary at this point to be able to hook | ||||
| 59 | # automatically into the lower level incremental blob IO api. | ||||
| 60 | # | ||||
| 61 | # This also shows using the $var syntax for binding name sql values in a | ||||
| 62 | # prepared statement. | ||||
| 63 | # | ||||
| 64 | when 'store' | ||||
| af4db9b7 » | copiousfreetime | 2008-07-04 | 65 | usage if file_list.empty? | |
| 1e931bd1 » | copiousfreetime | 2008-11-02 | 66 | require 'amalgalite/packer' | |
| ae1a6632 » | copiousfreetime | 2008-10-22 | 67 | ||
| 1e931bd1 » | copiousfreetime | 2008-11-02 | 68 | packer = Amalgalite::Packer.new( :dbfile => 'filestore.db', | |
| 9103eceb » | copiousfreetime | 2008-11-16 | 69 | :compressed => false ) | |
| 1e931bd1 » | copiousfreetime | 2008-11-02 | 70 | packer.pack( file_list ) | |
| 7751e6eb » | copiousfreetime | 2008-07-04 | 71 | ||
| 72 | # | ||||
| 73 | # dump the file that matches the right path to stdout. This also shows | ||||
| 74 | # positional sql varible binding using the '?' syntax. | ||||
| 75 | # | ||||
| 76 | when 'retrieve' | ||||
| af4db9b7 » | copiousfreetime | 2008-07-04 | 77 | usage if file_list.empty? | |
| 1e931bd1 » | copiousfreetime | 2008-11-02 | 78 | db.execute("SELECT id, compressed, filename, contents FROM rubylibs WHERE filename = ?", file_list.first) do |row| | |
| 79 | STDERR.puts "Dumping #{row['filename']} to stdout" | ||||
| 80 | if row['compressed'] then | ||||
| 81 | s = row['contents'].to_s | ||||
| 9103eceb » | copiousfreetime | 2008-11-16 | 82 | STDOUT.puts Amalgalite::Packer.gunzip( s ) | |
| 1e931bd1 » | copiousfreetime | 2008-11-02 | 83 | else | |
| 84 | row['contents'].write_to_io( STDOUT ) | ||||
| 85 | end | ||||
| 7751e6eb » | copiousfreetime | 2008-07-04 | 86 | end | |
| 87 | end | ||||
| 88 | db.close | ||||







