Source code: javatools/applications/DBConstraints/TestConstraints.java
1 /*
2 * TestConstraints.java
3 *
4 * Created on 14 febbraio 2002, 11.19
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.applications.DBConstraints;
26
27 import javatools.db.*;
28 import java.sql.*;
29
30 /**
31 *
32 * @author Antonio Petrelli
33 * @version
34 */
35 public class TestConstraints {
36
37 /** Creates new TestConstraints */
38 public TestConstraints() {
39 }
40
41 /**
42 * @param args the command line arguments
43 */
44 public static void main (String args[]) {
45 DbInserter ins;
46 DbDeleter del;
47 DbUpdater upd;
48 DbSelector selector;
49 DbExpr where;
50
51 manager = DbManager.singleton();
52 db = new DbDatabase(manager, "mysql", "org.gjt.mm.mysql.Driver",
53 "jdbc:mysql://localhost/Testbench", "", "");
54 try {
55 buildTables();/*
56 selector = db.selector();
57 ins = sonTable.inserter(selector); /*
58 ins.addColumn(sonTable.getColumn("SONID"), new Integer(5));
59 ins.addColumn(sonTable.getColumn("FATHERID"), new Integer(1));
60 ins.addColumn(sonTable.getColumn("FATHERIDTWO"), new Integer(2));
61 ins.addColumn(sonTable.getColumn("FAKEDATA"), "ariciao5");
62
63 ins.addColumn(sonTable.getColumn("SONID"), selector.addColumn(retrieveTable.getColumn("RETRIEVEID")));
64 ins.addColumn(sonTable.getColumn("FATHERID"), selector.addColumn(retrieveTable.getColumn("FATHERID")));
65 ins.addColumn(sonTable.getColumn("FATHERIDTWO"), selector.addColumn(retrieveTable.getColumn("FATHERIDTWO")));
66 ins.addColumn(sonTable.getColumn("FAKEDATA"), selector.addColumn("ariciao"));
67 ins.execute();
68 System.out.println(fatherTable.getDefault(0));
69 System.out.println(fatherTable.getDefault(1));
70 System.out.println(fatherTable.getDefault(2));
71 System.out.println(fatherTable.getDefault(3));
72
73 del = fatherTable.deleter();
74 where = fatherTable.getColumn("FATHERID").equal(new Integer(1)).and(
75 fatherTable.getColumn("FATHERIDTWO").equal(new Integer(3)));
76 del.setWhere(where);
77 del.execute(); */
78 upd = fatherTable.updater();
79 upd.addColumn(fatherTable.getColumn("FATHERID"), new Integer(1));
80 upd.addColumn(fatherTable.getColumn("FATHERIDTWO"), new Integer(2));
81 where = fatherTable.getColumn("FATHERID").equal(new Integer(4)).and(
82 fatherTable.getColumn("FATHERIDTWO").equal(new Integer(5)));
83 upd.setWhere(where);
84 upd.execute();
85
86 }
87 catch (DbException e) {
88 System.out.println(e.getMessage());
89 System.exit(1);
90 }
91 }
92
93
94 private static void buildTables() throws DbException {
95 Statement stmt;
96 ResultSet res;
97 Connection con;
98 DbColumn[] cols, refCols;
99
100 try {
101 con = db.getNewConnection().getSqlConnection();
102 stmt = con.createStatement();
103 res = stmt.executeQuery("SELECT FatherID, FatherIDTwo, DataOne, DataTwo from FATHER_TABLE");
104 fatherTable = new DbReferencedTable(db);
105 fatherTable.setTableName("FATHER_TABLE");
106 fatherTable.setResultSet(null, res);
107
108 con = db.getNewConnection().getSqlConnection();
109 stmt = con.createStatement();
110 res = stmt.executeQuery("SELECT SonID, FatherID, FatherIDTwo, FakeData from SON_TABLE");
111 sonTable = new DbReferencedTable(db);
112 sonTable.setTableName("SON_TABLE");
113 sonTable.setResultSet(null, res);
114 cols = new DbColumn[2];
115 refCols = new DbColumn[2];
116 cols[0] = fatherTable.getColumn("FATHERID");
117 refCols[0] = sonTable.getColumn("FATHERID");
118 cols[1] = fatherTable.getColumn("FATHERIDTWO");
119 refCols[1] = sonTable.getColumn("FATHERIDTWO");
120 sonTable.addFatherTable(fatherTable, cols, refCols, DbConstraint.CASCADE, DbConstraint.CASCADE);
121
122 con = db.getNewConnection().getSqlConnection();
123 stmt = con.createStatement();
124 res = stmt.executeQuery("SELECT RetrieveID, FatherID, FatherIDTwo, FakeData from RETRIEVE_TABLE");
125 retrieveTable = new DbReferencedTable(db);
126 retrieveTable.setTableName("RETRIEVE_TABLE");
127 retrieveTable.setResultSet(null, res);
128 }
129 catch (SQLException e) {
130 throw new DbException(e);
131 }
132 }
133
134 private static DbManager manager;
135 private static DbDatabase db;
136 private static DbReferencedTable fatherTable, sonTable, retrieveTable;
137 }