created mirror
This commit is contained in:
227
Modules/database/DB0.java
Normal file
227
Modules/database/DB0.java
Normal file
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
DB0.java
|
||||
*
|
||||
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
|
||||
*
|
||||
* You may use and distribute under the terms of either the GNU Lesser
|
||||
* General Public License, either version 2 of the license or,
|
||||
* at your choice, any later version. Alternatively, you may use and
|
||||
* distribute under the terms of the XPL.
|
||||
*
|
||||
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
|
||||
* the licenses.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
|
||||
* file for more details.
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* Written by Tuomas Lukka
|
||||
*/
|
||||
|
||||
package org.gzigzag.module;
|
||||
|
||||
import org.gzigzag.*;
|
||||
import java.sql.*;
|
||||
import java.io.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DB0 {
|
||||
public static final String rcsid = "$Id: DB0.java,v 1.9 2000/10/18 14:35:32 tjl Exp $";
|
||||
|
||||
public static boolean dbg = true;
|
||||
static final void p(String s) { if(dbg) System.out.println(s); }
|
||||
static final void pa(String s) { System.out.println(s); }
|
||||
|
||||
|
||||
static public class StrComp implements ZZUtil.Comparator {
|
||||
public int compare(ZZCell c1, ZZCell c2) {
|
||||
return c1.getText().compareTo(c2.getText());
|
||||
}
|
||||
}
|
||||
|
||||
static public class DHeadComp implements ZZUtil.Comparator {
|
||||
DHeadComp(String dim) { this.dim = dim; }
|
||||
String dim;
|
||||
public int compare(ZZCell c1, ZZCell c2) {
|
||||
return c1.h(dim, -1).getText().compareTo(
|
||||
c2.h(dim, -1).getText());
|
||||
}
|
||||
}
|
||||
|
||||
static public void insertCells(ZZCell head, String x, String y,
|
||||
ResultSet rs,
|
||||
String maincol,
|
||||
Hashtable id2col
|
||||
) throws SQLException {
|
||||
ResultSetMetaData meta = rs.getMetaData();
|
||||
int n = meta.getColumnCount();
|
||||
ZZCell[] hdr = new ZZCell[n];
|
||||
ZZCell[] cur = new ZZCell[n];
|
||||
ZZCell nextnew = head;
|
||||
int mcid = -1;
|
||||
int idid = -1;
|
||||
for(int i=0; i<n; i++) {
|
||||
nextnew = cur[i] = hdr[i] = nextnew.N(x, 1);
|
||||
String name = meta.getColumnName(i+1);
|
||||
hdr[i].setText(name);
|
||||
if(maincol != null && name.equals(maincol))
|
||||
mcid = i;
|
||||
if(name.equals("id"))
|
||||
idid = i;
|
||||
}
|
||||
if(maincol != null && mcid < 0) throw new Error("Invalid main col id");
|
||||
ZZCell curhead = head;
|
||||
while(rs.next()) {
|
||||
String id = null;
|
||||
ZZCell mcol = null;
|
||||
ZZCell prev = null;
|
||||
for(int i=0; i<n; i++) {
|
||||
String s = rs.getString(i+1);
|
||||
if(s==null) s="(null)";
|
||||
cur[i] = cur[i].N(y, 1);
|
||||
cur[i].setText(s);
|
||||
if(prev != null)
|
||||
prev.connect(x, 1, cur[i]);
|
||||
prev = cur[i];
|
||||
if(i == mcid) {
|
||||
mcol = cur[i];
|
||||
/* curhead.connect("d.2", cur[i]);
|
||||
curhead = cur[i]; */
|
||||
}
|
||||
if(i == idid)
|
||||
id = s;
|
||||
}
|
||||
if(id != null && mcol != null)
|
||||
id2col.put(id, mcol);
|
||||
}
|
||||
for(int i=0; i<n; i++) {
|
||||
ZZUtil.sortRank(hdr[i], y, 1, new StrComp(), false);
|
||||
}
|
||||
// ZZUtil.sortRank(head,"d.2", 1, new StrComp(), false);
|
||||
rs.close();
|
||||
|
||||
}
|
||||
|
||||
static public ZZModule module = new ZZModule() {
|
||||
public void action(String id, ZZCell code,
|
||||
ZZCell target,
|
||||
ZZView v, ZZView cv,
|
||||
String key, Point pt,
|
||||
ZZScene xi) {
|
||||
try {
|
||||
if(id.equals("READMEMS")) {
|
||||
synchronized(target.getSpace()) {
|
||||
|
||||
ZZSpace zz = target.getSpace();
|
||||
|
||||
String x = "d.1";
|
||||
String y = "d.2";
|
||||
|
||||
Hashtable id2mem = new Hashtable();
|
||||
Hashtable id2sig = new Hashtable();
|
||||
|
||||
|
||||
String url = "jdbc:postgresql://himalia.mit.jyu.fi/zz";
|
||||
String usr = "tjl";
|
||||
String pwd = "";
|
||||
Class.forName("org.postgresql.Driver");
|
||||
|
||||
Connection db = DriverManager.getConnection(url, usr, pwd);
|
||||
Statement st = db.createStatement();
|
||||
|
||||
ZZCell n = target.N("d.3", 1);
|
||||
n.setText("members");
|
||||
|
||||
ResultSet rs = st.executeQuery("select * from members");
|
||||
insertCells(n, x, y, rs, "name", id2mem);
|
||||
|
||||
n = n.N("d.3", 1); n.setText("sigs");
|
||||
rs = st.executeQuery("select * from sigs");
|
||||
insertCells(n, x, y, rs, "name", id2sig);
|
||||
|
||||
n = n.N("d.3", 1); n.setText("positions");
|
||||
rs = st.executeQuery("select * from positions");
|
||||
insertCells(n, x, y, rs, null, null);
|
||||
|
||||
// ZZCell title = target.N();
|
||||
// title.setText("<Linux>");
|
||||
// Create people rank in alphabetical order
|
||||
rs = st.executeQuery("select * from members order by name");
|
||||
ZZCell prev = null;
|
||||
while(rs.next()) {
|
||||
ZZCell curr = (ZZCell)id2mem.get(rs.getString(1));
|
||||
|
||||
if(prev != null) prev.connect("d.people", 1, curr);
|
||||
prev = curr;
|
||||
}
|
||||
|
||||
// Create sig rank in alphabetical order
|
||||
rs = st.executeQuery("select * from sigs order by name");
|
||||
prev = null;
|
||||
while(rs.next()) {
|
||||
ZZCell curr = (ZZCell)id2sig.get(rs.getString(1));
|
||||
|
||||
if(prev != null) prev.connect("d.insigs", 1, curr);
|
||||
prev = curr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
p("Rels: ");
|
||||
|
||||
// Insert relcells after the positions table
|
||||
rs = st.executeQuery("select * from positions");
|
||||
while(rs.next()) {
|
||||
String pers = rs.getString(1);
|
||||
String sig = rs.getString(2);
|
||||
// p("Rel: "+pers+" "+sig);
|
||||
ZZCell pc = (ZZCell)id2mem.get(pers);
|
||||
ZZCell sc = (ZZCell)id2sig.get(sig);
|
||||
if(pc == null || sc == null)
|
||||
throw new Error("No such relation "+pers+" "+sig);
|
||||
ZZCell relc = pc.N("d.insigs",1);
|
||||
sc.insert("d.people", 1, relc);
|
||||
relc.setText("+");
|
||||
}
|
||||
rs.close();
|
||||
|
||||
// Sort the ranks of the in-which-sigs tables.
|
||||
|
||||
for(Enumeration e = id2mem.elements();
|
||||
e.hasMoreElements(); ) {
|
||||
ZZCell c = (ZZCell)e.nextElement();
|
||||
ZZUtil.sortRank(c, "d.insigs", 1,
|
||||
new DHeadComp("d.people"), false);
|
||||
}
|
||||
|
||||
for(Enumeration e = id2sig.elements();
|
||||
e.hasMoreElements(); ) {
|
||||
ZZCell c = (ZZCell)e.nextElement();
|
||||
ZZUtil.sortRank(c, "d.people", 1,
|
||||
new DHeadComp("d.insigs"), false);
|
||||
}
|
||||
|
||||
st.close();
|
||||
db.close();
|
||||
|
||||
}
|
||||
} /* else if(id.equals("IMPORT")) {
|
||||
// XXX How to give 2 arguments: the file name and the home cell
|
||||
(new XML()).load(new File(target.getText()),
|
||||
}*/
|
||||
} catch(SQLException e) {
|
||||
System.out.println(e);
|
||||
e.printStackTrace();
|
||||
} catch(ClassNotFoundException e) {
|
||||
System.out.println(e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
68
Modules/database/createscript.pl
Normal file
68
Modules/database/createscript.pl
Normal file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# Create a sample database.
|
||||
# Use the linux kernel CREDITS file as input.
|
||||
|
||||
|
||||
print <<'STOP';
|
||||
DROP TABLE members;
|
||||
DROP TABLE positions;
|
||||
DROP TABLE sigs;
|
||||
|
||||
CREATE TABLE members (
|
||||
id INT4,
|
||||
name TEXT,
|
||||
address TEXT,
|
||||
phone TEXT,
|
||||
email TEXT,
|
||||
PRIMARY KEY(id)
|
||||
);
|
||||
|
||||
CREATE TABLE positions (
|
||||
person INT4,
|
||||
sig INT4
|
||||
/* type TEXT */
|
||||
);
|
||||
|
||||
CREATE TABLE sigs (
|
||||
id INT4,
|
||||
name TEXT,
|
||||
PRIMARY KEY(id)
|
||||
);
|
||||
STOP
|
||||
|
||||
$/ = "\n\n";
|
||||
|
||||
$id = 0;
|
||||
|
||||
|
||||
while(<>) {
|
||||
/^\s*N:/ or next;
|
||||
s/'//g;
|
||||
my %h;
|
||||
for(split "\n") {
|
||||
/^([A-Z]): (.*)$/ or next;
|
||||
push @{$h{$1}}, $2;
|
||||
}
|
||||
$phone = int(1000000 + rand 9000000);
|
||||
$phone =~ s/(...)(....)/\1-\2/g;
|
||||
print "INSERT INTO members VALUES ($id, '$h{N}[0]', '$h{S}[0]', '$phone', '$h{E}[0]');\n";
|
||||
$id++;
|
||||
last if $id > 100;
|
||||
}
|
||||
|
||||
$sid = 0;
|
||||
for(qw/filesys mm drivers boot console vfs/) {
|
||||
print "INSERT INTO sigs VALUES ($sid, '$_');\n";
|
||||
$sid++;
|
||||
}
|
||||
|
||||
my %pos;
|
||||
|
||||
for(1..120) {
|
||||
$pers = int rand $id;
|
||||
$sig = int rand $sid;
|
||||
next if $pos{$pers}{$sig}++;
|
||||
print "INSERT INTO positions VALUES ($pers, $sig);\n";
|
||||
}
|
||||
|
||||
18
Modules/database/zz.queries
Normal file
18
Modules/database/zz.queries
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
-- Taulut
|
||||
|
||||
SELECT * FROM members;
|
||||
|
||||
SELECT * FROM positions;
|
||||
|
||||
SELECT * FROM sigs;
|
||||
|
||||
-- Kaikkien tekijöiden kaikki projektit
|
||||
|
||||
SELECT members.name,sigs.name FROM members, positions, sigs WHERE members.id = positions.person AND sigs.id = positions.sig ORDER BY members.name, sigs.name;
|
||||
|
||||
-- Kaikkien projektien kaikki tekijät
|
||||
|
||||
SELECT sigs.name,members.name FROM members, positions, sigs WHERE members.id = positions.person AND sigs.id = positions.sig ORDER BY sigs.name, members.name;
|
||||
|
||||
|
||||
29
Modules/database/zz.sql
Normal file
29
Modules/database/zz.sql
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
CREATE TABLE members (
|
||||
id INT4,
|
||||
fname TEXT,
|
||||
lname TEXT,
|
||||
address TEXT,
|
||||
phone TEXT,
|
||||
email TEXT,
|
||||
PRIMARY KEY(id)
|
||||
);
|
||||
|
||||
CREATE TABLE positions (
|
||||
person INT4,
|
||||
sig INT4
|
||||
/* type TEXT */
|
||||
);
|
||||
|
||||
CREATE TABLE sigs (
|
||||
id INT4,
|
||||
name TEXT,
|
||||
PRIMARY KEY(id)
|
||||
);
|
||||
|
||||
INSERT INTO members VALUES
|
||||
(1,'Henna','Koskinen','Kuusikkokuja 4','321-564356','henna43@hotmail.com');
|
||||
INSERT INTO sigs VALUES
|
||||
(1,'helicopters');
|
||||
INSERT INTO positions VALUES
|
||||
(1,1);
|
||||
Reference in New Issue
Block a user