1 package util;
2 
3 import java.sql.SQLException;
4 
5 import resource.ResourceLocator;
6 import chemaxon.jchem.db.StructureTableOptions;
7 import chemaxon.jchem.db.TableTypeConstants;
8 import chemaxon.jchem.db.UpdateHandler;
9 import chemaxon.util.ConnectionHandler;
10
11/**
12 * Example codes for creating structure tables in database.
13 * <p>
14 * There are mandatory parameters for creating a database table: open connection handler, name
15 * of the table to create, and table type.
16 * 
17 * @author JChem Base team, ChemAxon Ltd.
18 */
19public final class TableOperations {
20
21    private static final int LARGE_TABLE_IMPORT_COUNT = 10;
22
23    /**
24     * Creates a structure table of type "Molecules". If a table with the same name already
25     * exists, it will be dropped first.
26     * 
27     * @param connHandler an open connection handler
28     * @param tableName name of the table to be created
29     * @throws SQLException if table cannot be created
30     */
31    public static void createMoleculeTable(ConnectionHandler connHandler, String tableName)
32            throws SQLException {
33        createStructureTable(connHandler, tableName, TableTypeConstants.TABLE_TYPE_MOLECULES);
34    }
35
36    /**
37     * Creates a structure table of the given type. If a table with the same name already
38     * exists, it will be dropped first.
39     * 
40     * @param connHandler an open connection handler
41     * @param tableName name of the table to be created
42     * @param tableType table type
43     * @throws SQLException if table cannot be created
44     */
45    public static void createStructureTable(ConnectionHandler connHandler, String tableName,
46            int tableType) throws SQLException {
47        
48        // Drop the table if it already exists
49        if (UpdateHandler.isStructureTable(connHandler, tableName)) {
50            UpdateHandler.dropStructureTable(connHandler, tableName);
51        }
52
53        // Create the table
54        StructureTableOptions tableOptions = new StructureTableOptions(tableName, tableType);
55        UpdateHandler.createStructureTable(connHandler, tableOptions);
56    }
57
58    /**
59     * Creates a molecule table and loads some structures into it. If a table with the same name
60     * already exists, it will be dropped first.
61     * 
62     * @param connHandler an open connection handler
63     * @param tableName name of the table to be created
64     * @throws Exception if an error occurs during table creation or molecule import
65     */
66    public static void setupMoleculeTable(ConnectionHandler connHandler, String tableName)
67            throws Exception {
68
69        System.out.print("Setting up default molecule table... ");
70
71        createMoleculeTable(connHandler, tableName);
72        MolImportUtil.databaseImport(ResourceLocator.getDefaultInputPath(), connHandler,
73                tableName);
74
75        System.out.println("Done.");
76        System.out.println();
77    }
78
79    /**
80     * Creates a molecule table and loads many structures into it. If a table with the same name
81     * already exists, it will be dropped first.
82     * 
83     * @param connHandler an open connection handler
84     * @param tableName name of the table to be created
85     * @throws Exception if an error occurs during table creation or molecule import
86     */
87    public static void setupLargeMoleculeTable(ConnectionHandler connHandler, String tableName)
88            throws Exception {
89
90        System.out.print("Setting up large molecule table...");
91
92        createMoleculeTable(connHandler, tableName);
93        for (int i = 0; i < LARGE_TABLE_IMPORT_COUNT; i++) {
94            MolImportUtil.databaseImport(ResourceLocator.getDefaultInputPath(), connHandler,
95                    tableName);
96            System.out.print(".");
97            System.out.flush();
98        }
99
00        System.out.println("Done.");
01        System.out.println();
02    }
03
04}
05