Simple database: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: syntax coloured, marked p2js incompatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 41:
{{trans|Kotlin}}
<
String name, date, category
Line 119:
printUsage()
E
printUsage()</
{{out}}
Line 126:
=={{header|Bracmat}}==
This is a rather minimal solution. The program is run from the command line of the operating system, in this example the Windows command prompt. The program is stored in a file called 'sdb':
<
' ( arg$:?command
& ( get'db
Line 205:
)
);
</syntaxhighlight>
First we add some records, a some ships that arrived at the harbour in Rotterdam today.
<pre>bracmat "get$sdb" add name "CORONA BULKER" tag "BULK CARRIER" date "2014.10.21.04:00"
Line 258:
"Treasure Beach","Argus","Jemky","09-22-1999","Lancast"
<
#include <stdlib.h> /* malloc */
#include <string.h> /* strlen */
Line 450:
}
else return ((*p1)->date > (*p2)->date);
}</
=={{header|C sharp|C#}}==
<
using System;
using System.IO;
Line 777:
}
}
</syntaxhighlight>
{{out| Program Input and Output}}
<pre>
Line 815:
This is a souped-up version of the task from [[Take notes on the command line]]. It stores the current date, a tag, a title and a note as an entry in a file. The database produced is not particularly human-readable or easy to modify, but it is in a well-structured format.
{{works with|OpenCOBOL}}
<
PROGRAM-ID. simple-database.
Line 1,076:
"The title should be specified as shown for -c."
DISPLAY " -t - Show all the entries sorted by tag."
.</
Sample session:
Line 1,115:
Tested with [[Common Lisp]]. ''(Save the code below as db.lisp)''
<
(defvar *db-cat* (make-hash-table :test 'equal))
Line 1,232:
nil))
(db-cmd-run (db-argv))</
Line 1,282:
=={{header|D}}==
<
std.file, std.csv, std.datetime;
Line 1,368:
default: printUsage(); break;
}
}</
{{out}}
<pre>C:\>simdb add item1 cat1
Line 1,419:
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
#! /usr/bin/env escript
Line 1,474:
io:fwrite( "Data stored in ~p~n", [file()] ),
init:stop().
</syntaxhighlight>
Command line session started with these file contents as database:
<pre>
Line 1,573:
Code:
<
' noop is bootmessage
Line 1,701:
cr cr ;
current !
SEAL godb</
=={{header|Go}}==
<
import (
Line 2,043:
fmt.Println(err)
}
}</
=={{header|Haskell}}==
Line 2,051:
Item {description = "La traviata", category = ["Classical"], date = Date 2012 10 19, optional = ["Giuseppe Verdi","1853"]}
<
import Data.List (sortBy, nub)
import System.Environment (getArgs, getProgName)
Line 2,179:
mapM_ (hPutStrLn hw . show) v
hClose hw
</syntaxhighlight>
=={{header|J}}==
Line 2,185:
J comes with a sql database, jdb. Jdb's columns are memory mapped files with header information. These won't meet the human readable data file requirement. Hence, this program:
<
Commands:
Line 2,293:
exit 0
</syntaxhighlight>
Assume the j code is stored in file s . These bash commands, stored in file input , create a database using add .
<
$D add name expression algebraic rank valence example explanation
Line 2,308:
$D add atop 'x f@g y' 'f(g(x,y))' 'rank of g' dyad '>@{.' '(lisp) open the car'
$D add 'many more!'
</syntaxhighlight>
Now we look up data from the bash command line.
<syntaxhighlight lang="sh">
$ . input # source the input
$ echo $D
Line 2,386:
'2012-02-08:23:45:06.515';'atop';'x f@g y';'f(g(x,y))';'rank of g';'dyad';'>@{.';'(lisp) open the car'
'2012-02-08:23:45:06.539';'many more!'
$ </
=={{header|Java}}==
{{trans|D}}
{{works with|Java|7}}
<
import java.text.*;
import java.util.*;
Line 2,515:
+ "\"some category name\"");
}
}</
Output:
Line 2,559:
Sandy Allen,2002-03-09,Colorado,friend,sandya@mail.com
Fred Kobo,1967-10-10,Colorado,friend,fkobo@example.net</pre>
<
setting = ArgParseSettings()
Line 2,619:
println("Changes written to file $filename.")
end
</syntaxhighlight>
=={{header|Kotlin}}==
{{trans|Java}}
... though not quite the same.
<
import java.text.SimpleDateFormat
Line 2,733:
else -> printUsage()
}
}</
{{out}}
Line 2,762:
There are some important differences with D version and Kotlin versions. Firstly, the way to manage the command arguments is somewhat different. And secondly, the database is stored in JSON format rather than in CSV format. Indeed, if Nim provides a parser of CSV files, it provides better support for JSON with procedures for serialization of Nim objects to JSON and deserialization from JSON to Nim objects. This can be seen in the following program.
<
const FileName = "simdb.json"
Line 2,862:
of "all":
if params.len != 0: printUsage()
printAll()</
{{out}}
Line 2,885:
=={{header|Perl}}==
<
use warnings;
use strict;
Line 3,002:
if $by_date;
}
}</
Sample session<pre> ~ $ db.pl n 'Donald Trump' Republican 2017-01-20
~ $ db.pl n 'Barack Obama' Democratic 2009-01-20
Line 3,021:
=={{header|Phix}}==
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Simple_db.exw
Line 3,121:
<span style="color: #000000;">process</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..$])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</
Sample session
<pre>
Line 3,152:
=={{header|PicoLisp}}==
The '[http://software-lab.de/doc/refR.html#rc rc]' resource file handling function is used typically for such tasks. It also takes care of proper locking and protection.
<
(de usage ()
Line 3,187:
(T (usage)) ) )
(bye)</
Test:
<pre>$ sdb CDs add "Title 1" "Category 1" 2011-11-13
Line 3,212:
=={{header|Pike}}==
{{trans|Common Lisp}} (simplified)
<
mapping make_episode(string series, string title, string episode, array date)
Line 3,365:
else
watch_list(db);
}</
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function db
{
Line 3,450:
db -Name Vince -Category family -Birthday 3/10/1960
db -Name Wayne -Category coworker -Birthday 5/29/1962
</syntaxhighlight>
Here is the data from the CSV file as a PowerShell Object:
<syntaxhighlight lang="powershell">
db
</syntaxhighlight>
{{Out}}
<pre>
Line 3,467:
</pre>
The latest entry:
<syntaxhighlight lang="powershell">
db -Latest
</syntaxhighlight>
{{Out}}
<pre>
Line 3,477:
</pre>
The latest entries by category:
<syntaxhighlight lang="powershell">
db -LatestByCategory
</syntaxhighlight>
{{Out}}
<pre>
Line 3,489:
</pre>
The database sorted on the Birthday property:
<syntaxhighlight lang="powershell">
db -SortedByDate
</syntaxhighlight>
{{Out}}
<pre>
Line 3,505:
=={{header|Python}}==
<
'''\
Line 3,612:
now = datetime.datetime.utcnow()
args._date = now.isoformat()
do_command[args.command](args, dbname)</
;Sample session (Unix):
Line 3,660:
=={{header|Racket}}==
<
#!/usr/bin/env racket
#lang racket
Line 3,687:
(for-each (compose1 show last/cat) (remove-duplicates (map cadr data)))]
[else (error 'sdb "bad printout mode")])])
</syntaxhighlight>
Sample run:
Line 3,727:
A generic client/server JSON database.<br>
<b>server.raku:</b>
<syntaxhighlight lang="raku"
use JSON::Fast ;
sub MAIN( :$server='0.0.0.0', :$port=3333, :$dbfile='db' ) {
Line 3,830:
spurt "{$dbfile}_index.json", to-json(%index) ;
}
}</
<b>client.raku:</b>
<syntaxhighlight lang="raku"
use JSON::Fast ;
multi MAIN('set', $topic, $message='', :$server='localhost', :$port='3333', :$json='') {
Line 3,881:
}
}
}</
Example:
<pre>./client.raku addindex constructor
Line 3,903:
=={{header|REBOL}}==
<
write/append %rdb "" db: load %rdb
switch system/options/args/1 [
Line 3,917:
"sort" [probe sort/skip db 4]
]
halt</
=={{header|REXX}}==
<
* 05.10.2014
*--------------------------------------------------------------------*/
Line 3,996:
Say 'end to end this program'
Say 'Use category - to list items without category'
Return</
{{out}}
<pre> Enter your commands, ?, or end
Line 4,033:
=={{header|Ruby}}==
<
require 'json'
require 'securerandom'
Line 4,219:
end
process_command_line *ARGV</
Sample session
Line 4,298:
=={{header|Run BASIC}}==
<
' -------------------------------
Line 4,397:
' ------ the end -------
[exit]
end</
Output:
Line 4,433:
=={{header|Scala}}==
<
type Entry = Array[String]
def asTSV(e: Entry) = e mkString "\t"
Line 4,470:
case _ => println("Usage: SimpleDatabase filename.tsv [all [latest]| latest [CATEGORY] | add [DESCRIPTION [CATEGORY [OPTIONAL]...]]]")
}
}</
{{out}}
<pre>> SimpleDatabase
Line 4,508:
=={{header|Tcl}}==
The format used is that of a Tcl dictionary, where each entry uses the title as a key and the remaining information (category, date and miscellaneous metadata) is the value associated with it. The only variation from the standard internal format is that entries are separated by newlines instead of spaces; this is still a legal value, but is a non-canonical.
<
package require Tcl 8.6
namespace eval udb {
Line 4,608:
}
udb::Store [lindex $argv 0]</
Sample session:
<
wrong # args: should be "udb.tcl dbfile subcommand ?args...?"
bash$ udb.tcl db ?
Line 4,700:
Title: Title 3
Category: bar
Date: Tue Nov 15 18:12:07 GMT 2011</
=={{header|ToffeeScript}}==
<
prog = require 'commander'
Line 4,787:
printFormatted entry
prog.parse process.argv</
=={{header|UNIX Shell}}==
This format is guaranteed to be human readable: if you can type it, you can read it.
<
db_create() {
Line 4,886:
show_help
;;
esac</
Sample usage (assuming script is named "sdb"):<syntaxhighlight lang="text">$ sdb create CDs
Create DB `CDs'
$ sdb add CDs Bookends
Line 4,923:
$ sdb drop CDs
Delete DB `CDs'
$</
=={{header|Wren}}==
Line 4,933:
{{libheader|Wren-str}}
Note that since Wren CLI currently has no way of determining the current date/time, a date needs to be input for each item to be added.
<
import "os" for Process
Line 5,051:
} else {
printUsage.call()
}</
{{out}}
|