Improve XML parsing to handle attributes as well.
[cfb.git] / prod / net / jaekl / cfb / db / driver / DbDriver.java
1 package net.jaekl.cfb.db.driver;
2
3 // Copyright (C) 2015 Christian Jaekl
4
5 import static net.jaekl.cfb.db.Column.Null.*;
6
7 import java.sql.Connection;
8 import java.sql.PreparedStatement;
9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11
12 import net.jaekl.cfb.db.Column;
13 import net.jaekl.cfb.db.Column.Type;
14 import net.jaekl.cfb.db.Table;
15
16 public abstract class DbDriver {
17         DbDriver() {
18                 
19         }
20         
21         // Load the JDBC driver
22         public abstract void load() throws ClassNotFoundException;
23         
24         public abstract Connection connect(String host, int port, String dbName, String user, String pass) throws SQLException;
25         
26         public boolean createTable(Connection con, Table table) throws SQLException {
27                 String sql = createTableSql(table);
28                 try (PreparedStatement ps = con.prepareStatement(sql)) {
29                         ps.executeUpdate();
30                 }
31                 catch (SQLException exc) {
32                         throw new SQLException("Failed to executeUpdate:  " + sql, exc);
33                 }
34                 
35                 return true;
36         }
37         
38         public abstract ResultSet selectColumnsFromWhere(Column[] columns, Table[] tables, String where);
39         
40         protected String typeName(Type type) {
41                 return type.toString();
42         }
43         
44         protected String createColumnSql(Column column) 
45         {
46                 String result = column.getName() + " " + typeName(column.getType());
47                 if (column.getWidth() > 0) {
48                         result += "(" + column.getWidth() + ")";
49                 }
50                 
51                 if (NOT_NULL == column.getNull()) {
52                         result += " NOT NULL";
53                 }
54                 else {
55                         result += " NULL";
56                 }
57             
58                 return result;
59         }
60         
61         protected String createTableSql(Table table) 
62         {
63                 assert(null != table);
64                 assert(null != table.getName());
65                 assert(table.getNumColumns() > 0);
66                 
67                 StringBuilder sb = new StringBuilder();
68                 
69                 sb.append("CREATE TABLE ")
70                   .append(table.getName())
71                   .append("(");
72                 
73                 for (int idx = 0; idx < table.getNumColumns(); ++idx) {
74                         if (idx > 0) {
75                                 sb.append(", ");
76                         }
77                         sb.append(createColumnSql(table.getColumn(idx)));
78                 }
79                 
80                 sb.append(")");
81                 
82                 return sb.toString();
83         }       
84 }