Simple database: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured, marked p2js incompatible)
m (syntax highlighting fixup automation)
Line 41:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="11l">T Item
String name, date, category
 
Line 119:
printUsage()
E
printUsage()</langsyntaxhighlight>
 
{{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':
<langsyntaxhighlight lang="bracmat"> whl
' ( arg$:?command
& ( get'db
Line 205:
)
);
</syntaxhighlight>
</lang>
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"
 
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* strlen */
Line 450:
}
else return ((*p1)->date > (*p2)->date);
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight Clang="c sharp">
using System;
using System.IO;
Line 777:
}
}
</syntaxhighlight>
</lang>
{{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}}
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
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."
.</langsyntaxhighlight>
 
Sample session:
Line 1,115:
Tested with [[Common Lisp]]. ''(Save the code below as db.lisp)''
 
<langsyntaxhighlight lang="lisp">(defvar *db* nil)
 
(defvar *db-cat* (make-hash-table :test 'equal))
Line 1,232:
nil))
 
(db-cmd-run (db-argv))</langsyntaxhighlight>
 
 
Line 1,282:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.string, std.conv, std.array,
std.file, std.csv, std.datetime;
 
Line 1,368:
default: printUsage(); break;
}
}</langsyntaxhighlight>
{{out}}
<pre>C:\>simdb add item1 cat1
Line 1,419:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
#! /usr/bin/env escript
 
Line 1,474:
io:fwrite( "Data stored in ~p~n", [file()] ),
init:stop().
</syntaxhighlight>
</lang>
Command line session started with these file contents as database:
<pre>
Line 1,573:
 
Code:
<langsyntaxhighlight lang="forth">\ sdb.fs Simple database. Gforth 0.7.0 specific
' noop is bootmessage
 
Line 1,701:
cr cr ;
current !
SEAL godb</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,043:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
Line 2,051:
Item {description = "La traviata", category = ["Classical"], date = Date 2012 10 19, optional = ["Giuseppe Verdi","1853"]}
 
<langsyntaxhighlight Haskelllang="haskell">import Control.Monad.State
import Data.List (sortBy, nub)
import System.Environment (getArgs, getProgName)
Line 2,179:
mapM_ (hPutStrLn hw . show) v
hClose hw
</syntaxhighlight>
</lang>
 
=={{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:
 
<langsyntaxhighlight lang="j">HELP=: 0 :0
Commands:
 
Line 2,293:
 
exit 0
</syntaxhighlight>
</lang>
Assume the j code is stored in file s . These bash commands, stored in file input , create a database using add .
<langsyntaxhighlight lang="sh">D='jconsole s dataflow'
$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>
</lang>
Now we look up data from the bash command line.
<syntaxhighlight lang="sh">
<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!'
$ </langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|D}}
{{works with|Java|7}}
<langsyntaxhighlight lang="java">import java.io.*;
import java.text.*;
import java.util.*;
Line 2,515:
+ "\"some category name\"");
}
}</langsyntaxhighlight>
 
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>
<langsyntaxhighlight lang="julia">using CSV, DataFrames, ArgParse, Dates
 
setting = ArgParseSettings()
Line 2,619:
println("Changes written to file $filename.")
end
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Java}}
... though not quite the same.
<langsyntaxhighlight lang="scala">// version 1.2.31
 
import java.text.SimpleDateFormat
Line 2,733:
else -> printUsage()
}
}</langsyntaxhighlight>
 
{{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.
 
<langsyntaxhighlight Nimlang="nim">import algorithm, json, os, strformat, strutils, times
 
const FileName = "simdb.json"
Line 2,862:
of "all":
if params.len != 0: printUsage()
printAll()</langsyntaxhighlight>
 
{{out}}
Line 2,885:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 3,002:
if $by_date;
}
}</langsyntaxhighlight>
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}}==
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<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>
<!--</langsyntaxhighlight>-->
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.
<langsyntaxhighlight PicoLisplang="picolisp">#!/usr/bin/pil
 
(de usage ()
Line 3,187:
(T (usage)) ) )
 
(bye)</langsyntaxhighlight>
Test:
<pre>$ sdb CDs add "Title 1" "Category 1" 2011-11-13
Line 3,212:
=={{header|Pike}}==
{{trans|Common Lisp}} (simplified)
<langsyntaxhighlight Pikelang="pike">mapping db = ([]);
 
mapping make_episode(string series, string title, string episode, array date)
Line 3,365:
else
watch_list(db);
}</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<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>
</lang>
Here is the data from the CSV file as a PowerShell Object:
<syntaxhighlight lang="powershell">
<lang PowerShell>
db
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,467:
</pre>
The latest entry:
<syntaxhighlight lang="powershell">
<lang PowerShell>
db -Latest
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,477:
</pre>
The latest entries by category:
<syntaxhighlight lang="powershell">
<lang PowerShell>
db -LatestByCategory
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,489:
</pre>
The database sorted on the Birthday property:
<syntaxhighlight lang="powershell">
<lang PowerShell>
db -SortedByDate
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,505:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/python3
 
'''\
Line 3,612:
now = datetime.datetime.utcnow()
args._date = now.isoformat()
do_command[args.command](args, dbname)</langsyntaxhighlight>
 
;Sample session (Unix):
Line 3,660:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="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>
</lang>
 
Sample run:
Line 3,727:
A generic client/server JSON database.<br>
<b>server.raku:</b>
<syntaxhighlight lang="raku" perl6line>#!/usr/bin/env 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) ;
}
}</langsyntaxhighlight>
<b>client.raku:</b>
<syntaxhighlight lang="raku" perl6line>#!/usr/bin/env raku
use JSON::Fast ;
multi MAIN('set', $topic, $message='', :$server='localhost', :$port='3333', :$json='') {
Line 3,881:
}
}
}</langsyntaxhighlight>
Example:
<pre>./client.raku addindex constructor
Line 3,903:
 
=={{header|REBOL}}==
<langsyntaxhighlight lang="rebol">rebol [author: "Nick Antonaccio"]
write/append %rdb "" db: load %rdb
switch system/options/args/1 [
Line 3,917:
"sort" [probe sort/skip db 4]
]
halt</langsyntaxhighlight>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 05.10.2014
*--------------------------------------------------------------------*/
Line 3,996:
Say 'end to end this program'
Say 'Use category - to list items without category'
Return</langsyntaxhighlight>
{{out}}
<pre> Enter your commands, ?, or end
Line 4,033:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'date'
require 'json'
require 'securerandom'
Line 4,219:
end
 
process_command_line *ARGV</langsyntaxhighlight>
 
Sample session
Line 4,298:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">sqliteconnect #sql, "f:\client.db" ' Connect to the DB
 
' -------------------------------
Line 4,397:
' ------ the end -------
[exit]
end</langsyntaxhighlight>
Output:
 
Line 4,433:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object SimpleDatabase extends App {
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]...]]]")
}
}</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="tcl">#!/usr/bin/env tclsh8.6
package require Tcl 8.6
namespace eval udb {
Line 4,608:
}
 
udb::Store [lindex $argv 0]</langsyntaxhighlight>
Sample session:
<langsyntaxhighlight lang="bash">bash$ udb.tcl db
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</langsyntaxhighlight>
 
=={{header|ToffeeScript}}==
<langsyntaxhighlight lang="coffeescript">#!/usr/local/bin/toffee
 
prog = require 'commander'
Line 4,787:
printFormatted entry
 
prog.parse process.argv</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
This format is guaranteed to be human readable: if you can type it, you can read it.
<langsyntaxhighlight lang="bash">#!/bin/sh
 
db_create() {
Line 4,886:
show_help
;;
esac</langsyntaxhighlight>
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'
$</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="ecmascript">/* simdb.wren */
 
import "os" for Process
Line 5,051:
} else {
printUsage.call()
}</langsyntaxhighlight>
 
{{out}}
10,327

edits