June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,11 @@
using SQLite
db = SQLite.DB()
SQLite.execute!(db, """\
CREATE TABLE address (
addrID INTEGER PRIMARY KEY AUTOINCREMENT,
addrStreet TEXT NOT NULL,
addrCity TEXT NOT NULL,
addrState TEXT NOT NULL,
addrZIP TEXT NOT NULL)
""")

View file

@ -0,0 +1,89 @@
// Version 1.2.41
import java.io.File
import java.io.RandomAccessFile
fun String.toFixedLength(len: Int) = this.padEnd(len).substring(0, len)
class Address(
var name: String,
var street: String = "",
var city: String = "",
var state: String = "",
var zipCode: String = "",
val autoId: Boolean = true
) {
var id = 0L
private set
init {
if (autoId) id = ++nextId
}
companion object {
private var nextId = 0L
const val RECORD_LENGTH = 127 // including 2 bytes for UTF string length
fun readRecord(file: File, id: Long): Address {
val raf = RandomAccessFile(file, "r")
val seekPoint = (id - 1) * RECORD_LENGTH
raf.use {
it.seek(seekPoint)
val id2 = it.readLong()
if (id != id2) {
println("Database is corrupt")
System.exit(1)
}
val text = it.readUTF()
val name = text.substring(0, 30).trimEnd()
val street = text.substring(30, 80).trimEnd()
val city = text.substring(80, 105).trimEnd()
val state = text.substring(105, 107)
val zipCode = text.substring(107).trimEnd()
val a = Address(name, street, city, state, zipCode, false)
a.id = id
return a
}
}
}
override fun toString() =
"Id : ${this.id}\n" +
"Name : $name\n" +
"Street : $street\n" +
"City : $city\n" +
"State : $state\n" +
"Zip Code : $zipCode\n"
fun writeRecord(file: File) {
val raf = RandomAccessFile(file, "rw")
val text =
name.toFixedLength(30) +
street.toFixedLength(50) +
city.toFixedLength(25) +
state +
zipCode.toFixedLength(10)
val seekPoint = (id - 1) * RECORD_LENGTH
raf.use {
it.seek(seekPoint)
it.writeLong(id)
it.writeUTF(text)
}
}
}
fun main(args: Array<String>) {
val file = File("addresses.dat")
val addresses = listOf(
Address("FSF Inc.", "51 Franklin Street", "Boston", "MA", "02110-1301"),
Address("The White House", "The Oval Office, 1600 Pennsylvania Avenue NW", "Washington", "DC", "20500")
)
// write the address records to the file
addresses.forEach { it.writeRecord(file) }
// now read them back in reverse order and print them out
for (i in 2 downTo 1) {
println(Address.readRecord(file, i.toLong()))
}
}

View file

@ -0,0 +1,18 @@
-- Import module
local sql = require("ljsqlite3")
-- Open connection to database file
local conn = sql.open("address.sqlite")
-- Create address table unless it already exists
conn:exec[[
CREATE TABLE IF NOT EXISTS address(
id INTEGER PRIMARY KEY AUTOINCREMENT,
street TEXT NOT NULL,
city TEXT NOT NULL,
state TEXT NOT NULL,
zip TEXT NOT NULL)
]]
-- Explicitly close connection
conn:close()

View file

@ -0,0 +1,14 @@
use DBIish;
my $dbh = DBIish.connect('SQLite', :database<addresses.sqlite3>);
my $sth = $dbh.do(q:to/STATEMENT/);
DROP TABLE IF EXISTS Address;
CREATE TABLE Address (
addrID INTEGER PRIMARY KEY AUTOINCREMENT,
addrStreet TEXT NOT NULL,
addrCity TEXT NOT NULL,
addrState TEXT NOT NULL,
addrZIP TEXT NOT NULL
)
STATEMENT

View file

@ -1,37 +1,34 @@
/*REXX program creates, builds, and displays a table of given U.S.A. postal addresses.*/
@usa.=; @usa.0=0 /*initialize stemmed array & 1st value.*/
@usa.0=@usa.0+1 /*bump the unique number for usage. */
call USA '_city' , 'Boston'
call USA '_state' , 'MA'
call USA '_addr' , "51 Franklin Street"
call USA '_name' , "FSF Inc."
call USA '_zip' , '02110-1301'
@usa.0=@usa.0+1 /*bump the unique number for usage. */
call USA '_city' , 'Washington'
call USA '_state' , 'DC'
call USA '_addr' , "The Oval Office"
call USA '_addr2' , "1600 Pennsylvania Avenue NW"
call USA '_name' , "The White House"
call USA '_zip' , 20500
call USA 'list'
exit /*stick a fork in it, we're all done. */
@usa.=; @usa.0=0; $='@USA.' /*initialize array and first value.*/
@usa.0=@usa.0 + 1 /*bump the unique number for usage.*/
call USA '_city' , 'Boston'
call USA '_state' , 'MA'
call USA '_addr1' , "51 Franklin Street"
call USA '_name' , "FSF Inc."
call USA '_zip' , '02110-1301'
@usa.0=@usa.0 + 1 /*bump the unique number for usage.*/
call USA '_city' , 'Washington'
call USA '_state' , 'DC'
call USA '_addr1' , "The Oval Office"
call USA '_addr2' , "1600 Pennsylvania Avenue NW"
call USA '_name' , "The White House"
call USA '_zip' , 20500 /*no need for quotes for a number. */
call USA 'list'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
list: call tell '_name'
call tell '_addr'
do j=2 until $==''; call tell "_addr"j; end /*j*/
call tell '_city'
call tell '_state'
call tell '_zip'
say copies('', 40)
tell: parse arg a; z=value($||#"."a); if z\='' then say right(translate(a,,'_'),9) "──►" z
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: $=value('@USA.'#"."arg(1));if $\='' then say right(translate(arg(1),,'_'),6) "──►" $
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
USA: procedure expose @USA.; parse arg what,txt; arg ?; @='@USA.'
if ?=='LIST' then do #=1 for @usa.0; call list; end /*#*/
else do
call value @ || @usa.0 || . || what , txt
call value @ || @usa.0 || . || 'upHist', userid() date() time()
end
return
USA: procedure expose @usa. $; parse arg what; arg ?
if ?=='LIST' then do #=1 for @usa.0
call tell '_name'
do j=1 until z=''; call tell "_addr"j; end
call tell '_city'
call tell '_state'
call tell '_zip'
say copies('', 45)
end /*#*/
else do; call value $ || @usa.0'.'what , arg(2)
call value $ || @usa.0'.upHist' , userid() date() time()
end
return

View file

@ -0,0 +1,18 @@
# Project : Table creation/Postal addresses
# Date : 2018/04/19
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
load "stdlib.ring"
oSQLite = sqlite_init()
sqlite_open(oSQLite,"mytest.db")
sql = "CREATE TABLE ADDRESS (" +
"addrID INT NOT NULL," +
"street CHAR(50) NOT NULL," +
"city CHAR(25) NOT NULL," +
"state CHAR(2), NOT NULL" +
"zip CHAR(20) NOT NULL);"
sqlite_execute(oSQLite,sql)

View file

@ -0,0 +1,7 @@
clear
gen str8 addrid=""
gen str50 street=""
gen str25 city=""
gen str2 state=""
gen str20 zip=""
save address