Simple database: Difference between revisions

Content added Content deleted
m (Link YAML.)
(add Ruby)
Line 302: Line 302:
watch_list(db);
watch_list(db);
}</lang>
}</lang>

=={{header|Ruby}}==

<lang ruby>require 'date'
require 'json'
require 'securerandom'

class SimpleDatabase
def initialize(dbname, *fields)
@dbname = dbname
@filename = @dbname + ".dat"
@fields = fields
@maxl = @fields.collect {|f| f.length}.max
@data = {
'fields' => fields,
'items' => {},
'history' => [],
'tags' => {},
}
end
attr_reader :dbname, :fields

def self.open(dbname)
db = new(dbname)
db.read
db
end

def read()
if not File.exists?(@filename)
raise ArgumentError, "Database #@dbname has not been created"
end
@data = JSON.parse(File.read(@filename))
@fields = @data['fields']
@maxl = @fields.collect {|f| f.length}.max
end

def write()
File.open(@filename, 'w') {|f| f.write(JSON.generate(@data))}
end

def add(*values)
id = SecureRandom.uuid
@data['items'][id] = Hash[ @fields.zip(values) ]
@data['history'] << [Time.now.to_f, id]
id
end

def tag(id, *tags)
tags.each do |tag|
if @data['tags'][tag].nil?
@data['tags'][tag] = [id]
else
@data['tags'][tag] << id
end
end
id
end

def latest
@data['history'].sort_by {|val| val[0]}.last.last
end

def get_item(id)
@data['items'][id]
end

def tags()
@data['tags'].keys.sort
end

def ids_for_tag(tag)
@data['tags'][tag]
end

def tags_for_id(id)
@data['tags'].keys.inject([]) do |tags, tag|
tags << tag if @data['tags'][tag].include?(id)
tags
end
end

def display(id)
item = get_item(id)
fmt = "%#{@maxl}s - %s\n"
puts fmt % ['id', id]
@fields.each {|f| print fmt % [f, item[f]]}
puts fmt % ['tags', tags_for_id(id).join(',')]
added = @data['history'].find {|x| x[1] == id}.first
puts fmt % ['date added', Time.at(added).ctime]
puts ""
end

def each()
@data['history'].each {|time, id| yield id}
end

def each_item_with_tag(tag)
@data['tags'][tag].each {|id| yield id}
end
end
def usage()
puts <<END
usage: #{$0} command args ...

commands:
help
create dbname field ...
fields dbname
add dbname value ...
tag dbname id tag ...
tags dbname
list dbname [tag ...]
latest dbname
latest_by_tag dbname
END
end

def open_database(args)
dbname = args.shift
begin
SimpleDatabase.open(dbname)
rescue ArgumentError => e
STDERR.puts e.message
exit 1
end
end

def process_command_line(command, *args)
case command
when 'help'
usage

when 'create'
db = SimpleDatabase.new(*args)
db.write
puts "Database #{args[0]} created"

when 'fields'
db = open_database(args)
puts "Database #{db.dbname} fields:"
puts db.fields.join(',')

when 'add'
db = open_database(args)
id = db.add(*args)
db.write
puts "Database #{db.dbname} added id #{id}"

when 'tag'
db = open_database(args)
id = args.shift
db.tag(id, *args)
db.write
db.display(id)
when 'tags'
db = open_database(args)
puts "Database #{db.dbname} tags:"
puts db.tags.join(',')

when 'list'
db = open_database(args)
if args.empty?
db.each {|id| db.display(id)}
else
args.each do |tag|
puts "Items tagged #{tag}"
db.each_item_with_tag(tag) {|id| db.display(id)}
end
end

when 'latest'
db = open_database(args)
db.display(db.latest)

when 'latest_by_tag'
db = open_database(args)
db.tags.each do |tag|
puts tag
db.display(db.ids_for_tag(tag).last)
end

else
puts "Error: unknown command '#{command}'"
usage
end
end

process_command_line *ARGV</lang>

Sample session
<pre "style='height: 40ex; overflow: scroll">$ ruby simple_database.rb create appointments event_title start_time stop_time location event_description
Database appointments created
$ ruby simple_database.rb add appointments "Wife's Birthday" "2011-11-01" "2011-11-01" "" "happy 39th"
Database appointments added id 6dd02195-1efe-40d1-b43e-c2efd852cd1d
$ ruby simple_database.rb tag appointments 6dd02195-1efe-40d1-b43e-c2efd852cd1d birthday family
id - 6dd02195-1efe-40d1-b43e-c2efd852cd1d
event_title - Wife's Birthday
start_time - 2011-11-01
stop_time - 2011-11-01
location -
event_description - happy 39th
tags - birthday,family
date added - Thu Nov 3 15:52:31 2011

$ ruby simple_database.rb add appointments "Parent-Teacher Conference" "2011-11-03 19:30" "2011-11-03 20:00" "school" "desc"
Database appointments added id 0190b835-401d-42da-9ed3-1d335d27b83c
$ ruby simple_database.rb tag appointments 0190b835-401d-42da-9ed3-1d335d27b83c school family
id - 0190b835-401d-42da-9ed3-1d335d27b83c
event_title - Parent-Teacher Conference
start_time - 2011-11-03 19:30
stop_time - 2011-11-03 20:00
location - school
event_description - desc
tags - family,school
date added - Thu Nov 3 15:54:05 2011

$ ruby simple_database.rb add appointments "Buy gift for wife" "2011-10-31 16:00" "2011-10-31 16:30" "the mall" "hmm, maybe jewelery?"
Database appointments added id 4023e6f1-bcc1-49e5-a59f-138157b413f4
$ ruby simple_database.rb tag appointments 4023e6f1-bcc1-49e5-a59f-138157b413f4 family last-minute
id - 4023e6f1-bcc1-49e5-a59f-138157b413f4
event_title - Buy gift for wife
start_time - 2011-10-31 16:00
stop_time - 2011-10-31 16:30
location - the mall
event_description - hmm, maybe jewelery?
tags - family,last-minute
date added - Thu Nov 3 15:55:02 2011

$ ruby simple_database.rb fields appointments
Database appointments fields:
event_title,start_time,stop_time,location,event_description
$ ruby simple_database.rb tags appointments
Database appointments tags:
birthday,family,last-minute,school
$ ruby simple_database.rb list appointments
id - 6dd02195-1efe-40d1-b43e-c2efd852cd1d
event_title - Wife's Birthday
start_time - 2011-11-01
stop_time - 2011-11-01
location -
event_description - happy 39th
tags - birthday,family
date added - Thu Nov 3 15:52:31 2011

id - 0190b835-401d-42da-9ed3-1d335d27b83c
event_title - Parent-Teacher Conference
start_time - 2011-11-03 19:30
stop_time - 2011-11-03 20:00
location - school
event_description - desc
tags - family,school
date added - Thu Nov 3 15:54:05 2011

id - 4023e6f1-bcc1-49e5-a59f-138157b413f4
event_title - Buy gift for wife
start_time - 2011-10-31 16:00
stop_time - 2011-10-31 16:30
location - the mall
event_description - hmm, maybe jewelery?
tags - family,last-minute
date added - Thu Nov 3 15:55:02 2011
$ cat appointments.dat
{"fields":["event_title","start_time","stop_time","location","event_description"],"items":{"6dd02195-1efe-40d1-b43e-c2efd852cd1d":{"event_title":"Wife's Birthday","start_time":"2011-11-01","stop_time":"2011-11-01","location":"","event_description":"happy 39th"},"0190b835-401d-42da-9ed3-1d335d27b83c":{"event_title":"Parent-Teacher Conference","start_time":"2011-11-03 19:30","stop_time":"2011-11-03 20:00","location":"school","event_description":"desc"},"4023e6f1-bcc1-49e5-a59f-138157b413f4":{"event_title":"Buy gift for wife","start_time":"2011-10-31 16:00","stop_time":"2011-10-31 16:30","location":"the mall","event_description":"hmm, maybe jewelery?"}},"history":[[1320349951.000625,"6dd02195-1efe-40d1-b43e-c2efd852cd1d"],[1320350045.4736252,"0190b835-401d-42da-9ed3-1d335d27b83c"],[1320350102.9486248,"4023e6f1-bcc1-49e5-a59f-138157b413f4"]],"tags":{"birthday":["6dd02195-1efe-40d1-b43e-c2efd852cd1d"],"family":["6dd02195-1efe-40d1-b43e-c2efd852cd1d","0190b835-401d-42da-9ed3-1d335d27b83c","4023e6f1-bcc1-49e5-a59f-138157b413f4"],"school":["0190b835-401d-42da-9ed3-1d335d27b83c"],"last-minute":["4023e6f1-bcc1-49e5-a59f-138157b413f4"]}}
</pre>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==