001: /*
002: * Piscator: a small SQL/XML search engine
003: * Copyright (C) 2007 Luk Morbee
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: */
019: package piscator.dao.jdbc;
020:
021: import java.util.Map;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.springframework.jdbc.core.support.JdbcDaoSupport;
026: import org.w3c.dom.Element;
027: import org.w3c.dom.NodeList;
028:
029: import piscator.dao.SideTableDao;
030:
031: public class SideTableDaoImpl extends JdbcDaoSupport implements
032: SideTableDao {
033:
034: private static Log LOG = LogFactory.getLog(SideTableDaoImpl.class);
035:
036: /* (non-Javadoc)
037: * @see piscator.dao.SideTableDao#create(org.w3c.dom.Element)
038: */
039: public void create(Element config) {
040: if (config != null) {
041: StringBuffer sql = new StringBuffer();
042: NodeList sideTableNodes = config
043: .getElementsByTagName("side-table");
044: for (int i = 0; i < sideTableNodes.getLength(); i++) {
045: Element tableElement = (Element) sideTableNodes.item(i);
046: String tableName = tableElement.getAttribute("name");
047: sql.append("DROP TABLE " + tableName + " IF EXISTS;\n");
048: sql.append("CREATE TABLE ").append(tableName).append(
049: " (");
050: sql.append("doc_id INTEGER IDENTITY PRIMARY KEY");
051: NodeList columnNodes = tableElement
052: .getElementsByTagName("column");
053: for (int j = 0; j < columnNodes.getLength(); j++) {
054: Element columnElement = (Element) columnNodes
055: .item(j);
056: String columnName = columnElement
057: .getAttribute("name");
058: String type = columnElement.getAttribute("type");
059: sql.append(", ").append(columnName).append(" ")
060: .append(type);
061: }
062: sql.append(");\n");
063: for (int j = 0; j < columnNodes.getLength(); j++) {
064: Element columnElement = (Element) columnNodes
065: .item(j);
066: String columnName = columnElement
067: .getAttribute("name");
068: sql.append("CREATE INDEX ").append(columnName)
069: .append("_index");
070: sql.append(" ON ").append(tableName);
071: sql.append(" (").append(columnName).append(");\n");
072: }
073: }
074: LOG.info(sql.toString());
075: getJdbcTemplate().execute(sql.toString());
076: }
077: }
078:
079: /* (non-Javadoc)
080: * @see piscator.dao.SideTableDao#insert(java.lang.String, java.util.Map)
081: */
082: public void insert(String table, Map row) {
083: if (row.size() > 1) {
084: StringBuffer sql = new StringBuffer();
085: sql.append("INSERT INTO ").append(table).append(" ");
086: String searchFields = row.keySet().toString().replace('[',
087: '(').replace(']', ')');
088: sql.append(searchFields);
089: sql.append(" VALUES(");
090: for (int i = 0; i < row.size(); i++) {
091: if (i > 0) {
092: sql.append(", ");
093: }
094: sql.append("?");
095: }
096: sql.append(")");
097: getJdbcTemplate().update(sql.toString(),
098: row.values().toArray());
099: }
100: }
101:
102: }
|