commit adf0f3a86cdd66fc5463f642678fa32afa7d8344 Author: Christian P. MOMON Date: Mon Sep 2 12:31:06 2013 +0200 First commit. diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..4cc5a1c --- /dev/null +++ b/.classpath @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..fbbd2ff --- /dev/null +++ b/.project @@ -0,0 +1,30 @@ + + + SikevaDB + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8000cd6 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..8557b74 --- /dev/null +++ b/.settings/org.eclipse.wst.common.component @@ -0,0 +1,6 @@ + + + + + + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..d6c31ad --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/bin/META-INF/MANIFEST.MF b/bin/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5e94951 --- /dev/null +++ b/bin/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/bin/META-INF/context.xml b/bin/META-INF/context.xml new file mode 100644 index 0000000..aca0577 --- /dev/null +++ b/bin/META-INF/context.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/bin/fr/devinsy/sikevadb/AlphaTest.class b/bin/fr/devinsy/sikevadb/AlphaTest.class new file mode 100644 index 0000000..acba22f Binary files /dev/null and b/bin/fr/devinsy/sikevadb/AlphaTest.class differ diff --git a/bin/fr/devinsy/sikevadb/Element.class b/bin/fr/devinsy/sikevadb/Element.class new file mode 100644 index 0000000..eee2b6c Binary files /dev/null and b/bin/fr/devinsy/sikevadb/Element.class differ diff --git a/bin/fr/devinsy/sikevadb/Elements.class b/bin/fr/devinsy/sikevadb/Elements.class new file mode 100644 index 0000000..242edcf Binary files /dev/null and b/bin/fr/devinsy/sikevadb/Elements.class differ diff --git a/bin/fr/devinsy/sikevadb/FileSikevaDB.class b/bin/fr/devinsy/sikevadb/FileSikevaDB.class new file mode 100644 index 0000000..a303586 Binary files /dev/null and b/bin/fr/devinsy/sikevadb/FileSikevaDB.class differ diff --git a/bin/fr/devinsy/sikevadb/SQLSikevaDB.class b/bin/fr/devinsy/sikevadb/SQLSikevaDB.class new file mode 100644 index 0000000..4fc777c Binary files /dev/null and b/bin/fr/devinsy/sikevadb/SQLSikevaDB.class differ diff --git a/bin/fr/devinsy/sikevadb/SikevaDB.class b/bin/fr/devinsy/sikevadb/SikevaDB.class new file mode 100644 index 0000000..928f713 Binary files /dev/null and b/bin/fr/devinsy/sikevadb/SikevaDB.class differ diff --git a/bin/fr/devinsy/sikevadb/SikevaDBFactory.class b/bin/fr/devinsy/sikevadb/SikevaDBFactory.class new file mode 100644 index 0000000..8194aef Binary files /dev/null and b/bin/fr/devinsy/sikevadb/SikevaDBFactory.class differ diff --git a/bin/fr/devinsy/sikevadb/SikevaDBUtils.class b/bin/fr/devinsy/sikevadb/SikevaDBUtils.class new file mode 100644 index 0000000..2545828 Binary files /dev/null and b/bin/fr/devinsy/sikevadb/SikevaDBUtils.class differ diff --git a/build.num b/build.num new file mode 100644 index 0000000..c74c55d --- /dev/null +++ b/build.num @@ -0,0 +1,3 @@ +#Build Number for ANT. Do not edit! +#Sat Aug 24 15:54:52 CEST 2013 +build.number=1 diff --git a/build.properties b/build.properties new file mode 100644 index 0000000..f704c19 --- /dev/null +++ b/build.properties @@ -0,0 +1,3 @@ +product.name=sikevadb +product.revision.major=0 +product.revision.minor=1 diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..7e3fcb8 --- /dev/null +++ b/build.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/buildjar.xml b/buildjar.xml new file mode 100644 index 0000000..680e3c5 --- /dev/null +++ b/buildjar.xml @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Type ant -p + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..9f12cb8 --- /dev/null +++ b/lib/README @@ -0,0 +1,8 @@ +Description of used libraries: +- commons-codec: digest tools +- commons-lang: useful tools (StringUtils...) +- hamcrest-core: required by junit +- junit: unit tests API +- log4j: log API +- slf4j-api: facade log API +- slf4j-log4j12: adaptation layer between slf4j and log4j diff --git a/lib/commons-codec-1.8-sources.jar b/lib/commons-codec-1.8-sources.jar new file mode 100644 index 0000000..900af03 Binary files /dev/null and b/lib/commons-codec-1.8-sources.jar differ diff --git a/lib/commons-codec-1.8.jar b/lib/commons-codec-1.8.jar new file mode 100644 index 0000000..32f84c9 Binary files /dev/null and b/lib/commons-codec-1.8.jar differ diff --git a/lib/commons-lang3-3.1-sources.jar b/lib/commons-lang3-3.1-sources.jar new file mode 100644 index 0000000..02cd2f2 Binary files /dev/null and b/lib/commons-lang3-3.1-sources.jar differ diff --git a/lib/commons-lang3-3.1.jar b/lib/commons-lang3-3.1.jar new file mode 100644 index 0000000..a85e539 Binary files /dev/null and b/lib/commons-lang3-3.1.jar differ diff --git a/lib/devinsy-utils-0.2.4-sources.zip b/lib/devinsy-utils-0.2.4-sources.zip new file mode 100644 index 0000000..ac88eb7 Binary files /dev/null and b/lib/devinsy-utils-0.2.4-sources.zip differ diff --git a/lib/devinsy-utils-0.2.4.jar b/lib/devinsy-utils-0.2.4.jar new file mode 100644 index 0000000..802aefa Binary files /dev/null and b/lib/devinsy-utils-0.2.4.jar differ diff --git a/lib/hamcrest-core-1.3-sources.jar b/lib/hamcrest-core-1.3-sources.jar new file mode 100644 index 0000000..c3c110b Binary files /dev/null and b/lib/hamcrest-core-1.3-sources.jar differ diff --git a/lib/hamcrest-core-1.3.jar b/lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/lib/hamcrest-core-1.3.jar differ diff --git a/lib/junit-4.11-sources.jar b/lib/junit-4.11-sources.jar new file mode 100644 index 0000000..bab4cdd Binary files /dev/null and b/lib/junit-4.11-sources.jar differ diff --git a/lib/junit-4.11.jar b/lib/junit-4.11.jar new file mode 100644 index 0000000..aaf7444 Binary files /dev/null and b/lib/junit-4.11.jar differ diff --git a/lib/log4j-1.2.17-source.zip b/lib/log4j-1.2.17-source.zip new file mode 100644 index 0000000..2a9a7f4 Binary files /dev/null and b/lib/log4j-1.2.17-source.zip differ diff --git a/lib/log4j-1.2.17.jar b/lib/log4j-1.2.17.jar new file mode 100644 index 0000000..068867e Binary files /dev/null and b/lib/log4j-1.2.17.jar differ diff --git a/lib/slf4j-api-1.7.5-sources.jar b/lib/slf4j-api-1.7.5-sources.jar new file mode 100644 index 0000000..51abe2b Binary files /dev/null and b/lib/slf4j-api-1.7.5-sources.jar differ diff --git a/lib/slf4j-api-1.7.5.jar b/lib/slf4j-api-1.7.5.jar new file mode 100644 index 0000000..8766455 Binary files /dev/null and b/lib/slf4j-api-1.7.5.jar differ diff --git a/lib/slf4j-log4j12-1.7.5-sources.jar b/lib/slf4j-log4j12-1.7.5-sources.jar new file mode 100644 index 0000000..ea1b93b Binary files /dev/null and b/lib/slf4j-log4j12-1.7.5-sources.jar differ diff --git a/lib/slf4j-log4j12-1.7.5.jar b/lib/slf4j-log4j12-1.7.5.jar new file mode 100644 index 0000000..afce5c2 Binary files /dev/null and b/lib/slf4j-log4j12-1.7.5.jar differ diff --git a/resources/sql/create.sql b/resources/sql/create.sql new file mode 100644 index 0000000..9dde68f --- /dev/null +++ b/resources/sql/create.sql @@ -0,0 +1,28 @@ +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET time_zone = "+00:00"; + +-- +-- Base de données: `sikevadb-kiwa` +-- +CREATE DATABASE IF NOT EXISTS `sikevadb-test` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; +USE `sikevadb-kiwa`; + +-- -------------------------------------------------------- + +-- +-- Structure de la table `elements` +-- + +DROP TABLE IF EXISTS `elements`; +CREATE TABLE IF NOT EXISTS `elements` ( + `TOPKEY` varchar(255) COLLATE utf8_unicode_ci NOT NULL, + `SUBKEY` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, + `SIZE` int(11) NOT NULL DEFAULT '0', + `DIGEST` varchar(255) COLLATE utf8_unicode_ci NOT NULL, + `CREATION_DATE` date NOT NULL, + `EDITION_DATE` date NOT NULL, + `ARCHIVE_DATE` date DEFAULT NULL, + `VALUE` longtext COLLATE utf8_unicode_ci, + PRIMARY KEY (`TOPKEY`), + KEY `XKEYINDEX` (`TOPKEY`,`SUBKEY`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5e94951 --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/src/META-INF/context.xml b/src/META-INF/context.xml new file mode 100644 index 0000000..aca0577 --- /dev/null +++ b/src/META-INF/context.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/src/fr/devinsy/sikevadb/Element.java b/src/fr/devinsy/sikevadb/Element.java new file mode 100644 index 0000000..d750e00 --- /dev/null +++ b/src/fr/devinsy/sikevadb/Element.java @@ -0,0 +1,141 @@ +/** + * Copyright (C) 2013 DEVINSY & Christian P. MOMON + * + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June + * 2007; or any later version you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package fr.devinsy.sikevadb; + +import java.util.Date; + +import org.apache.commons.codec.digest.DigestUtils; + +/** + * + * + * @author Christian P. Momon + */ +public class Element { + + private Date creationDate; + private Date editionDate; + private Date archiveDate; + private String key; + private String subkey; + private long size; + private String digest; + private String value; + + /** + * + * @param key + * @param value + */ + public Element() { + } + + /** + * + * @param key + * @param value + */ + public Element(final String key, final String subkey, final String value) { + updateValue(value); + this.creationDate = this.editionDate; + this.archiveDate = null; + this.key = key; + this.subkey = subkey; + } + + /** + * + */ + public void archive() { + this.archiveDate = new Date(); + } + + public Date getArchiveDate() { + return archiveDate; + } + + public Date getCreationDate() { + return creationDate; + } + + public String getDigest() { + return digest; + } + + public Date getEditionDate() { + return editionDate; + } + + public String getKey() { + return key; + } + + public long getSize() { + return size; + } + + public String getSubkey() { + return subkey; + } + + public String getValue() { + return value; + } + + public void setArchiveDate(final Date archiveDate) { + this.archiveDate = archiveDate; + } + + public void setCreationDate(final Date creationDate) { + this.creationDate = creationDate; + } + + public void setDigest(final String digest) { + this.digest = digest; + } + + public void setEditionDate(final Date editionDate) { + this.editionDate = editionDate; + } + + public void setKey(final String key) { + this.key = key; + } + + public void setSize(final long size) { + this.size = size; + } + + public void setSubkey(final String subkey) { + this.subkey = subkey; + } + + public void setValue(final String value) { + this.value = value; + } + + public void updateValue(final String value) { + setValue(value); + this.editionDate = new Date(); + if (value == null) { + this.size = 0; + this.digest = null; + } else { + this.size = this.value.length(); + this.digest = DigestUtils.md5Hex(this.value); + } + } +} diff --git a/src/fr/devinsy/sikevadb/Elements.java b/src/fr/devinsy/sikevadb/Elements.java new file mode 100644 index 0000000..e1299a4 --- /dev/null +++ b/src/fr/devinsy/sikevadb/Elements.java @@ -0,0 +1,43 @@ +/** + * Copyright (C) 2013 DEVINSY & Christian P. MOMON + * + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June + * 2007; or any later version you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package fr.devinsy.sikevadb; + +import java.util.ArrayList; + +/** + * + * + * @author Christian P. Momon + */ +public class Elements extends ArrayList { + + private static final long serialVersionUID = 6298920093179395791L; + + /** + * + */ + public Elements() { + super(); + } + + /** + * + */ + public Elements(final int initialCapacity) { + super(initialCapacity); + } + +} diff --git a/src/fr/devinsy/sikevadb/FileSikevaDB.java b/src/fr/devinsy/sikevadb/FileSikevaDB.java new file mode 100644 index 0000000..401a3a2 --- /dev/null +++ b/src/fr/devinsy/sikevadb/FileSikevaDB.java @@ -0,0 +1,243 @@ +/** + * Copyright (C) 2013 DEVINSY & Christian P. MOMON + * + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June + * 2007; or any later version you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package fr.devinsy.sikevadb; + +import fr.devinsy.util.StringList; + +/** + * + * + * @author Christian P. Momon + */ +public class FileSikevaDB implements SikevaDB { + /** + * + * @param path + */ + public FileSikevaDB(final String path) { + + } + + @Override + public void archive(final String key) throws Exception { + // TODO Auto-generated method stub + + } + + @Override + public void archive(final String key, final String subkey) throws Exception { + // TODO Auto-generated method stub + + } + + @Override + public void close() { + // TODO Auto-generated method stub + + } + + @Override + public long countOfAllElements() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long countOfAllElements(final String key) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long countOfAllElements(final String key, final String subkey) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long countOfArchivedElements() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long countOfArchivedElements(final String key) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long countOfArchivedElements(final String key, final String subkey) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long countOfElements() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long countOfElements(final String key) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long countOfElements(final String key, final String subkey) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public Elements getAllElements(final String key) throws Exception { + // TODO Auto-generated method stub + return null; + } + + @Override + public Elements getAllElements(final String key, final String subkey) throws Exception { + // TODO Auto-generated method stub + return null; + } + + @Override + public StringList getAllValues(final String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public StringList getAllValues(final String key, final String subkey) { + // TODO Auto-generated method stub + return null; + } + + @Override + public StringList getArchivedValues(final String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public StringList getArchivedValues(final String key, final String subkey) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Element getElement(final String key) throws Exception { + // TODO Auto-generated method stub + return null; + } + + @Override + public Element getElement(final String key, final String subkey) throws Exception { + // TODO Auto-generated method stub + return null; + } + + @Override + public Elements getElements(final String key) throws Exception { + // TODO Auto-generated method stub + return null; + } + + @Override + public Elements getElements(final String key, final String subkey) throws Exception { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getValue(final String key) throws Exception { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getValue(final String key, final String subkey) { + // TODO Auto-generated method stub + return null; + } + + @Override + public StringList getValues(final String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public StringList getValues(final String key, final String subkey) { + // TODO Auto-generated method stub + return null; + } + + @Override + public long memorySize() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long memorySize(final String key) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long memorySize(final String key, final String subkey) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void open() { + // TODO Auto-generated method stub + + } + + @Override + public void put(final Element element) throws Exception { + // TODO Auto-generated method stub + + } + + @Override + public void put(final String key, final String value) { + // TODO Auto-generated method stub + + } + + @Override + public void put(final String key, final String subkey, final String value) { + // TODO Auto-generated method stub + + } + + @Override + public void remove(final String key) throws Exception { + // TODO Auto-generated method stub + + } + + @Override + public void remove(final String key, final String subkey) throws Exception { + // TODO Auto-generated method stub + + } +} diff --git a/src/fr/devinsy/sikevadb/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/SQLSikevaDB.java new file mode 100644 index 0000000..b9a34f1 --- /dev/null +++ b/src/fr/devinsy/sikevadb/SQLSikevaDB.java @@ -0,0 +1,1325 @@ +/** + * Copyright (C) 2013 DEVINSY & Christian P. MOMON + * + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June + * 2007; or any later version you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package fr.devinsy.sikevadb; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Date; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; + +import org.apache.commons.lang3.StringUtils; + +import fr.devinsy.util.StringList; + +/** + * + * + * @author Christian P. Momon + */ +public class SQLSikevaDB implements SikevaDB { + private String driverClassname; + private String url; + private String login; + private String password; + private Connection singleConnection; + + private String contextName; + private DataSource dataSource; + + /** + * + * @param contextName + */ + public SQLSikevaDB(final String contextName) { + this.contextName = contextName; + } + + /** + * + * @param host + * @param port + * @param login + * @param password + */ + public SQLSikevaDB(final String driverClassName, final String url, final String login, final String password) { + + if (StringUtils.isBlank(driverClassName)) { + throw new NullPointerException("driverClassName is null."); + } else if (StringUtils.isBlank(url)) { + throw new NullPointerException("url is null."); + } else if (StringUtils.isBlank(login)) { + throw new NullPointerException("login is null."); + } else if (StringUtils.isBlank(password)) { + throw new NullPointerException("password is null"); + } else { + this.driverClassname = driverClassName; + this.url = url; + this.login = login; + this.password = password; + this.dataSource = null; + } + } + + /** + * {@inheritDoc} + */ + @Override + public void archive(final String key) throws SQLException { + archive(key, null); + } + + /** + * {@inheritDoc} + */ + @Override + public void archive(final String key, final String subkey) throws SQLException { + + // + Element element = getElement(key, null); + + if (element == null) { + throw new NullPointerException("Undefined element [key=" + key + "][subkey=" + subkey + "]"); + } else { + // + element.archive(); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + // + connection = getConnection(); + statement = connection.prepareStatement("UPDATE elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=?"); + + statement.setDate(1, dateToDate(element.getArchiveDate())); + statement.setString(2, element.getKey()); + statement.setString(3, element.getSubkey()); + + statement.executeQuery(); + + } finally { + close(connection, statement, resultSet); + } + } + } + + /** + * + */ + @Override + public void close() { + // + if (this.singleConnection != null) { + try { + this.singleConnection.close(); + } catch (SQLException exception) { + exception.printStackTrace(); + } finally { + this.singleConnection = null; + } + } + + // + this.dataSource = null; + } + + /** + * + * @param connection + */ + public void close(final Connection connection, final Statement statement, final ResultSet resultSet) { + + // + if ((connection != null) && (this.singleConnection != connection)) { + try { + connection.close(); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + + // + if (statement != null) { + try { + statement.close(); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + + // + if (resultSet != null) { + try { + resultSet.close(); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public long countOfAllElements() throws SQLException { + long result; + + Connection connection = null; + Statement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.createStatement(); + resultSet = statement.executeQuery("SELECT count(*) FROM elements"); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public long countOfAllElements(final String key) throws SQLException { + long result; + + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE AND TOPKEY=?"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public long countOfAllElements(final String key, final String subkey) throws SQLException { + long result; + + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE TOPKEY=? AND SUBKEY=?"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public long countOfArchivedElements() throws SQLException { + long result; + + Connection connection = null; + Statement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.createStatement(); + resultSet = statement.executeQuery("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NOT NULL"); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public long countOfArchivedElements(final String key) throws SQLException { + long result; + + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=?"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public long countOfArchivedElements(final String key, final String subkey) throws SQLException { + long result; + + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=?"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + * + * @throws SQLException + */ + @Override + public long countOfElements() throws SQLException { + long result; + + Connection connection = null; + Statement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.createStatement(); + resultSet = statement.executeQuery("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NULL"); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + * + * @throws SQLException + */ + @Override + public long countOfElements(final String key) throws SQLException { + long result; + + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=?"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public long countOfElements(final String key, final String subkey) throws SQLException { + long result; + + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public Elements getAllElements(final String key) throws SQLException { + Elements result; + + // + result = new Elements((int) countOfAllElements(key)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection + .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE TOPKEY=? ORDER BY ARCHIVE_DATE ASC"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + Element element = new Element(); + element.setKey(resultSet.getString(1)); + element.setSubkey(resultSet.getString(2)); + element.setSize(resultSet.getLong(3)); + element.setDigest(resultSet.getString(4)); + element.setCreationDate(dateToDate(resultSet.getDate(5))); + element.setEditionDate(dateToDate(resultSet.getDate(6))); + element.setArchiveDate(dateToDate(resultSet.getDate(7))); + element.setValue(resultSet.getString(8)); + + // + result.add(element); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public Elements getAllElements(final String key, final String subkey) throws SQLException { + Elements result; + + // + result = new Elements((int) countOfAllElements(key, subkey)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection + .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + Element element = new Element(); + element.setKey(resultSet.getString(1)); + element.setSubkey(resultSet.getString(2)); + element.setSize(resultSet.getLong(3)); + element.setDigest(resultSet.getString(4)); + element.setCreationDate(dateToDate(resultSet.getDate(5))); + element.setEditionDate(dateToDate(resultSet.getDate(6))); + element.setArchiveDate(dateToDate(resultSet.getDate(7))); + element.setValue(resultSet.getString(8)); + + // + result.add(element); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public StringList getAllValues(final String key) throws SQLException { + StringList result; + + // + result = new StringList((int) countOfAllElements(key)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE TOPKEY=? ORDER BY ARCHIVE_DATE ASC"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + result.add(resultSet.getString(1)); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public StringList getAllValues(final String key, final String subkey) throws SQLException { + StringList result; + + // + result = new StringList((int) countOfAllElements(key, subkey)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + result.add(resultSet.getString(1)); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public StringList getArchivedValues(final String key) throws SQLException { + StringList result; + + // + result = new StringList((int) countOfArchivedElements(key)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? ORDER BY CREATION_DATE"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + result.add(resultSet.getString(1)); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public StringList getArchivedValues(final String key, final String subkey) throws SQLException { + StringList result; + + // + result = new StringList((int) countOfArchivedElements(key, subkey)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection + .prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + result.add(resultSet.getString(1)); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * + * @return + * @throws SQLException + */ + public Connection getConnection() throws SQLException { + Connection result; + + if (this.singleConnection != null) { + result = this.singleConnection; + } else if (this.dataSource != null) { + result = this.dataSource.getConnection(); + } else { + throw new NullPointerException("Connection is not initialized."); + } + + // + return result; + } + + /** + * + * @return + */ + public String getDriverClassname() { + return driverClassname; + } + + /** + * {@inheritDoc} + */ + @Override + public Element getElement(final String key) throws SQLException { + Element result; + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + // + connection = getConnection(); + statement = connection + .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=?"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + // + if (resultSet.next()) { + // + result = new Element(); + result.setKey(resultSet.getString(1)); + result.setSubkey(resultSet.getString(2)); + result.setSize(resultSet.getLong(3)); + result.setDigest(resultSet.getString(4)); + result.setCreationDate(dateToDate(resultSet.getDate(5))); + result.setEditionDate(dateToDate(resultSet.getDate(6))); + result.setArchiveDate(dateToDate(resultSet.getDate(7))); + result.setValue(resultSet.getString(8)); + } else { + result = null; + } + + // + if (resultSet.next()) { + throw new SQLException("More than only once result."); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public Element getElement(final String key, final String subkey) throws SQLException { + Element result; + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + // + connection = getConnection(); + statement = connection + .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=?"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + // + if (resultSet.next()) { + // + result = new Element(); + result.setKey(resultSet.getString(1)); + result.setSubkey(resultSet.getString(2)); + result.setSize(resultSet.getLong(3)); + result.setDigest(resultSet.getString(4)); + result.setCreationDate(dateToDate(resultSet.getDate(5))); + result.setEditionDate(dateToDate(resultSet.getDate(6))); + result.setArchiveDate(dateToDate(resultSet.getDate(7))); + result.setValue(resultSet.getString(8)); + } else { + result = null; + } + + // + if (resultSet.next()) { + throw new SQLException("More than only once result."); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public Elements getElements(final String key) throws SQLException { + Elements result; + + // + result = new Elements((int) countOfElements(key)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection + .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? ORDER BY CREATION_DATE ASC"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + Element element = new Element(); + element.setKey(resultSet.getString(1)); + element.setSubkey(resultSet.getString(2)); + element.setSize(resultSet.getLong(3)); + element.setDigest(resultSet.getString(4)); + element.setCreationDate(dateToDate(resultSet.getDate(5))); + element.setEditionDate(dateToDate(resultSet.getDate(6))); + element.setArchiveDate(dateToDate(resultSet.getDate(7))); + element.setValue(resultSet.getString(8)); + + // + result.add(element); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public Elements getElements(final String key, final String subkey) throws SQLException { + Elements result; + + // + result = new Elements((int) countOfElements(key, subkey)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection + .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=? ORDER BY CREATION_DATE ASC"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + Element element = new Element(); + element.setKey(resultSet.getString(1)); + element.setSubkey(resultSet.getString(2)); + element.setSize(resultSet.getLong(3)); + element.setDigest(resultSet.getString(4)); + element.setCreationDate(dateToDate(resultSet.getDate(5))); + element.setEditionDate(dateToDate(resultSet.getDate(6))); + element.setArchiveDate(dateToDate(resultSet.getDate(7))); + element.setValue(resultSet.getString(8)); + + // + result.add(element); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + public String getLogin() { + return login; + } + + public String getPassword() { + return password; + } + + public String getUrl() { + return url; + } + + /** + * {@inheritDoc} + */ + @Override + public String getValue(final String key) throws SQLException { + String result; + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + // + connection = getConnection(); + statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=?"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + // + if (resultSet.next()) { + // + result = resultSet.getString(1); + } else { + result = null; + } + + // + if (resultSet.next()) { + throw new SQLException("More than only once result."); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public String getValue(final String key, final String subkey) throws SQLException { + String result; + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + // + connection = getConnection(); + statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + // + if (resultSet.next()) { + // + result = resultSet.getString(1); + } else { + result = null; + } + + // + if (resultSet.next()) { + throw new SQLException("More than only once result."); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public StringList getValues(final String key) throws SQLException { + StringList result; + + // + result = new StringList((int) countOfElements(key)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? ORDER BY ARCHIVE_DATE ASC"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + result.add(resultSet.getString(1)); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public StringList getValues(final String key, final String subkey) throws SQLException { + StringList result; + + // + result = new StringList((int) countOfElements(key, subkey)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection + .prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + result.add(resultSet.getString(1)); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public long memorySize() throws SQLException { + long result; + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements"); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public long memorySize(final String key) throws SQLException { + long result; + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements WHERE TOPKEY=?"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public long memorySize(final String key, final String subkey) throws SQLException { + long result; + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements WHERE TOPKEY=? AND SUBKEY=?"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public void open() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, NamingException { + + // + close(); + + // + if (this.url != null) { + Class.forName(this.driverClassname).newInstance(); + this.singleConnection = DriverManager.getConnection(this.url, this.login, this.password); + } else if (this.contextName != null) { + Context initialContext = new InitialContext(); + this.dataSource = (DataSource) initialContext.lookup(this.contextName); + } else { + throw new IllegalArgumentException("Undefined source."); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void put(final Element element) throws SQLException { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + statement = connection + .prepareStatement("INSERT INTO elements (TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE) VALUES(1, 2, 3, 4, 5,6,7,8)"); + + statement.setString(1, element.getKey()); + statement.setString(2, element.getSubkey()); + statement.setLong(3, element.getSize()); + statement.setString(4, element.getDigest()); + statement.setDate(5, dateToDate(element.getCreationDate())); + statement.setDate(6, dateToDate(element.getEditionDate())); + statement.setDate(7, dateToDate(element.getArchiveDate())); + statement.setString(8, element.getValue()); + + resultSet = statement.executeQuery(); + + } finally { + close(connection, statement, resultSet); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void put(final String key, final String value) throws SQLException { + put(key, null, value); + } + + /** + * {@inheritDoc} + */ + @Override + public void put(final String key, final String subkey, final String value) throws SQLException { + Element element = getElement(key, null); + + if (element == null) { + + } else { + // + element.updateValue(value); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + // + connection = getConnection(); + connection.setAutoCommit(false); + + // Archive existing element. + statement = connection.prepareStatement("UPDATE elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=?"); + + statement.setDate(1, dateToDate(element.getEditionDate())); + statement.setString(2, element.getKey()); + statement.setString(3, element.getSubkey()); + + resultSet = statement.executeQuery(); + statement.clearParameters(); + + // Insert new version of the element. + statement.clearParameters(); + statement = connection + .prepareStatement("INSERT INTO elements (TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE) VALUES(1, 2, 3, 4, 5,6,7,8)"); + + statement.setString(1, element.getKey()); + statement.setString(2, element.getSubkey()); + statement.setLong(3, element.getSize()); + statement.setString(4, element.getDigest()); + statement.setDate(5, dateToDate(element.getCreationDate())); + statement.setDate(6, dateToDate(element.getEditionDate())); + statement.setDate(7, dateToDate(element.getArchiveDate())); + statement.setString(8, element.getValue()); + + resultSet = statement.executeQuery(); + + connection.commit(); + + } finally { + connection.rollback(); + close(connection, statement, resultSet); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void remove(final String key) throws Exception { + remove(key, null); + } + + /** + * {@inheritDoc} + */ + @Override + public void remove(final String key, final String subkey) throws SQLException { + + // + Element element = getElement(key, null); + + if (element == null) { + throw new NullPointerException("Undefined element [key=" + key + "][subkey=" + subkey + "]"); + } else { + // + element.archive(); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + // + connection = getConnection(); + statement = connection.prepareStatement("DELETE FROM elements WHERE WHERE TOPKEY=? AND SUBKEY=?"); + + statement.setString(1, element.getKey()); + statement.setString(2, element.getSubkey()); + + statement.executeQuery(); + + } finally { + close(connection, statement, resultSet); + } + } + } + + public void setDriverClassname(final String driverClassname) { + this.driverClassname = driverClassname; + } + + public void setLogin(final String login) { + this.login = login; + } + + public void setPassword(final String password) { + this.password = password; + } + + public void setUrl(final String url) { + this.url = url; + } + + /** + * + * @param source + * @return + */ + public static java.sql.Date dateToDate(final Date source) { + java.sql.Date result; + + if (source == null) { + result = null; + } else { + result = new java.sql.Date(source.getTime()); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static Date dateToDate(final java.sql.Date source) { + Date result; + + if (source == null) { + result = null; + } else { + result = new Date(source.getTime()); + } + + // + return result; + } + +} diff --git a/src/fr/devinsy/sikevadb/SikevaDB.java b/src/fr/devinsy/sikevadb/SikevaDB.java new file mode 100644 index 0000000..0905fc7 --- /dev/null +++ b/src/fr/devinsy/sikevadb/SikevaDB.java @@ -0,0 +1,98 @@ +/** + * Copyright (C) 2013 DEVINSY & Christian P. MOMON + * + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June + * 2007; or any later version you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package fr.devinsy.sikevadb; + +import fr.devinsy.util.StringList; + +/** + * All elements = archived elements + elements. + * + * + * + * @author Christian P. Momon + */ +public interface SikevaDB { + + public void archive(final String key) throws Exception; + + public void archive(final String key, final String subkey) throws Exception; + + public void close() throws Exception; + + public long countOfAllElements() throws Exception; + + public long countOfAllElements(String key) throws Exception; + + public long countOfAllElements(String key, String subkey) throws Exception; + + public long countOfArchivedElements() throws Exception; + + public long countOfArchivedElements(String key) throws Exception; + + public long countOfArchivedElements(String key, String subkey) throws Exception; + + public long countOfElements() throws Exception; + + public long countOfElements(String key) throws Exception; + + public long countOfElements(String key, String subkey) throws Exception; + + public Elements getAllElements(String key) throws Exception; + + public Elements getAllElements(String key, String subkey) throws Exception; + + public StringList getAllValues(String key) throws Exception; + + public StringList getAllValues(String key, String subkey) throws Exception; + + public StringList getArchivedValues(String key) throws Exception; + + public StringList getArchivedValues(String key, String subkey) throws Exception; + + public Element getElement(String key) throws Exception; + + public Element getElement(String key, String subkey) throws Exception; + + public Elements getElements(String key) throws Exception; + + public Elements getElements(String key, String subkey) throws Exception; + + public String getValue(String key) throws Exception; + + public String getValue(String key, String subkey) throws Exception; + + public StringList getValues(String key) throws Exception; + + public StringList getValues(String key, String subkey) throws Exception; + + public long memorySize() throws Exception; + + public long memorySize(String key) throws Exception; + + public long memorySize(String key, String subkey) throws Exception; + + public void open() throws Exception; + + public void put(Element element) throws Exception; + + public void put(String key, String value) throws Exception; + + public void put(String key, String subkey, String value) throws Exception; + + public void remove(final String key) throws Exception; + + public void remove(final String key, final String subkey) throws Exception; +} diff --git a/src/fr/devinsy/sikevadb/SikevaDBFactory.java b/src/fr/devinsy/sikevadb/SikevaDBFactory.java new file mode 100644 index 0000000..49f5347 --- /dev/null +++ b/src/fr/devinsy/sikevadb/SikevaDBFactory.java @@ -0,0 +1,54 @@ +/** + * Copyright (C) 2013 DEVINSY & Christian P. MOMON + * + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June + * 2007; or any later version you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package fr.devinsy.sikevadb; + +/** + * + * + * @author Christian P. Momon + */ +public class SikevaDBFactory { + /** + * + * @param path + * @return + */ + public static SikevaDB get(final String path) { + SikevaDB result; + + result = new FileSikevaDB(path); + + // + return result; + } + + /** + * + * @param host + * @param port + * @param login + * @param password + * @return + */ + public static SikevaDB get(final String host, final String port, final String login, final String password) { + SikevaDB result; + + result = new SQLSikevaDB(host, port, login, password); + + // + return result; + } +} diff --git a/src/fr/devinsy/sikevadb/SikevaDBUtils.java b/src/fr/devinsy/sikevadb/SikevaDBUtils.java new file mode 100644 index 0000000..ff606ca --- /dev/null +++ b/src/fr/devinsy/sikevadb/SikevaDBUtils.java @@ -0,0 +1,29 @@ +/** + * Copyright (C) 2013 KINSOURCES + * + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June + * 2007; or any later version you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package fr.devinsy.sikevadb; + +import java.util.Date; + +/** + * + * + * @author Christian P. Momon + */ +public class SikevaDBUtils { + public static long now() { + return new Date().getTime(); + } +} diff --git a/test/fr/devinsy/sikevadb/AlphaTest.java b/test/fr/devinsy/sikevadb/AlphaTest.java new file mode 100644 index 0000000..f53704e --- /dev/null +++ b/test/fr/devinsy/sikevadb/AlphaTest.java @@ -0,0 +1,40 @@ +package fr.devinsy.sikevadb; + +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * + * @author Christian P. Momon + */ +public class AlphaTest { + static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(AlphaTest.class); + + /** + * + */ + @Before + public void before() { + BasicConfigurator.configure(); + Logger.getRootLogger().setLevel(Level.ERROR); + } + + /** + * + */ + @Test + public void testFoo01() { + // + logger.debug("===== test starting..."); + + // + Assert.assertEquals('a', 'a'); + + // + logger.debug("===== test done."); + } +}