añadido sistema de auto actualizacion a travez de scripts sql numerados los cuales son almacenados en un simple archivo zip para evitar editarlos por error por un usuario comun )yn (ya si alguien los edita no es mi problema)
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,7 +1,7 @@
|
||||
out
|
||||
lib
|
||||
.idea
|
||||
dist/data
|
||||
dist/data/*.dat
|
||||
dist/logs
|
||||
dist/informes
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ Para poder hacer build del programa es necesario importarlo a IntelliJ y hacer b
|
||||
|
||||
Luego de hacer la build, en la carpeta `dist` se encontrara el archivo `caja.jar` el cual es el ejecutable, junto al el se encuentra el archivo `properties` que ya esta disponible en el repositorio, en este se puede seleccionar el look and feel que tendra el programa y el sistema de base de datos que se utilizara, por ahora solo tiene soporte para mysql y sqlite, siendo sqlite el que realmente funcionara ya que aun no termino de portar las querys para mysql.
|
||||
|
||||
Para crear la base de datos se tienen los scripts en la carpeta `database` donde estan los archivos `mysql.sql` y `sqlite.sql`, como dije anteriormente sqlite realmente esta al dia asi que ese deberia ser utilizado.
|
||||
La base de datos actualmente deberia crearse por si misma, los scripts que se utilizan son los que estan en la carpeta `database` y estos son comprimidos en un zip el cual se guarda en la carpeta `data` bajo el nombre `version_scripts`, este es el archivo que el programa buscara para descomprimir y ejecutar los scripts cuando encuentre que no existe base de datos, el objetivo de esto es que la base de datos sea facilmente actualizable agregando un script sql el cual sera ejecutado para actualizar de version.
|
||||
Deberia usar un orm pero no e encontrado uno que realmente me venga bien, tomando en cuenta que estoy usando sqlite y no hay mucho soporte para el en los orm para java.
|
||||
|
||||
### Screenshots!
|
||||
|
||||
29
database/1.sql
Normal file
29
database/1.sql
Normal file
@@ -0,0 +1,29 @@
|
||||
pragma foreign_keys = ON;
|
||||
drop table if exists egresos;
|
||||
drop table if exists tipos_egreso;
|
||||
drop table if exists ingresos;
|
||||
drop table if exists tipos_ingreso;
|
||||
drop table if exists efectivos;
|
||||
drop table if exists documentos;
|
||||
drop table if exists caja;
|
||||
create table caja ( id integer primary key, fecha date not null );
|
||||
create table tipos_egreso ( id integer primary key, nombre text not null );
|
||||
create table egresos ( id integer primary key, nro text not null, descripcion text not null, valor integer not null, tipo_egreso_id integer not null, caja_id integer not null, foreign key (tipo_egreso_id) references tipos_egreso (id) on update cascade on delete restrict, foreign key (caja_id) references caja (id) on update cascade on delete restrict );
|
||||
create table tipos_ingreso ( id integer primary key, nombre text not null );
|
||||
create table ingresos ( id integer primary key, valor integer not null, nro_inicial text not null, nro_final text not null, tipo_ingreso_id integer unsigned not null, caja_id integer unsigned not null, foreign key (tipo_ingreso_id) references tipos_ingreso (id) on update cascade on delete restrict, foreign key (caja_id) references caja (id) on update cascade on delete restrict );
|
||||
create table efectivos ( id integer primary key, veinte_mil integer not null, diez_mil integer not null, cinco_mil integer not null, dos_mil integer not null, mil integer not null, quinientos integer not null, cien integer not null, cincuenta integer not null, diez integer not null, caja_id integer not null, foreign key (caja_id) references caja (id) on update cascade on delete restrict );
|
||||
create table documentos ( id integer primary key, cheques integer not null, tarjetas integer not null, caja_id integer not null, foreign key (caja_id) references caja (id) on update cascade on delete restrict );
|
||||
insert into tipos_egreso (nombre) values ('Factura Materia Prima'), ('Factura Gastos Generales'), ('Gasto Menor Materia Prima'), ('Gasto General Sin Respaldo'), ('Gasto General Con Boleta'), ('Guia Materia Prima'), ('Anticipo Arriendo'), ('Anticipo Personal'), ('Pago Partime'), ('Retiros Gerencia'), ('Otro');
|
||||
insert into tipos_ingreso (nombre) values ('Boletas Fiscales'), ('Boletas Manuales'), ('Facturas'), ('Guias');
|
||||
alter table ingresos add column nro_z_inicial text;
|
||||
alter table ingresos add column nro_z_final text;
|
||||
insert into tipos_ingreso (nombre) values ('Boleta Exenta');
|
||||
alter table documentos add column retiros integer default 0;
|
||||
drop table if exists calculo_fondo;
|
||||
create table calculo_fondo ( id integer primary key, valor integer not null, descripcion text not null, caja_id integer not null, foreign key (caja_id) references caja (id) on update cascade on delete restrict );
|
||||
drop table if exists estado_resultado;
|
||||
create table estado_resultado ( id integer primary key, mes date unique not null, costo_venta integer null, cuenta_corriente_factura integer null, cuenta_corriente_boleta integer null, cuenta_corriente_sin_respaldo integer null, remuneraciones integer null, finiquitos integer null, aguinaldo integer null, bonos_personal integer null, honorarios_contador integer null, arriendo integer null, agua integer null, luz integer null, gas integer null, telefono integer null, otro_servicio integer null, ppm real null, ivaFavor int null );
|
||||
drop table if exists version;
|
||||
create table version (version int not null );
|
||||
insert into version (version) values (1);
|
||||
|
||||
2
database/2.sql
Normal file
2
database/2.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
alter table caja add column fondo integer default 0;
|
||||
update version set version = 2;
|
||||
@@ -1,119 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018-2019 Daniel Cortes
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
drop table if exists egresos;
|
||||
drop table if exists tipos_egreso;
|
||||
drop table if exists ingresos;
|
||||
drop table if exists tipos_ingreso;
|
||||
drop table if exists efectivos;
|
||||
drop table if exists documentos;
|
||||
drop table if exists caja;
|
||||
|
||||
create table caja
|
||||
(
|
||||
id int(10) unsigned primary key auto_increment,
|
||||
fecha date not null
|
||||
);
|
||||
|
||||
create table tipos_egreso
|
||||
(
|
||||
id int(10) unsigned primary key auto_increment,
|
||||
nombre varchar(191) not null
|
||||
);
|
||||
|
||||
create table egresos
|
||||
(
|
||||
id int(10) unsigned primary key auto_increment,
|
||||
nro varchar(191) not null,
|
||||
descripcion varchar(191) not null,
|
||||
valor int(10) not null,
|
||||
tipo_egreso_id int(10) unsigned not null,
|
||||
caja_id int(10) unsigned not null,
|
||||
foreign key fk_egresos_tipo_egreso (tipo_egreso_id) references tipos_egreso (id) on update cascade on delete restrict,
|
||||
foreign key fk_egresos_caja (caja_id) references caja (id) on update cascade on delete restrict
|
||||
);
|
||||
|
||||
create table tipos_ingreso
|
||||
(
|
||||
id int(10) unsigned primary key auto_increment,
|
||||
nombre varchar(191) not null
|
||||
);
|
||||
|
||||
create table ingresos
|
||||
(
|
||||
id int(10) unsigned primary key auto_increment,
|
||||
valor int(10) not null,
|
||||
nro_inicial varchar(191) not null,
|
||||
nro_final varchar(191) not null,
|
||||
tipo_ingreso_id int(10) unsigned not null,
|
||||
caja_id int(10) unsigned not null,
|
||||
foreign key fk_tipo_ingreso (tipo_ingreso_id) references tipos_ingreso (id) on update cascade on delete restrict,
|
||||
foreign key fk_ingresos_caja (caja_id) references caja (id) on update cascade on delete restrict
|
||||
);
|
||||
|
||||
create table efectivos
|
||||
(
|
||||
id int(10) unsigned primary key auto_increment,
|
||||
veinte_mil int(10) not null,
|
||||
diez_mil int(10) not null,
|
||||
cinco_mil int(10) not null,
|
||||
dos_mil int(10) not null,
|
||||
mil int(10) not null,
|
||||
quinientos int(10) not null,
|
||||
cien int(10) not null,
|
||||
cincuenta int(10) not null,
|
||||
diez int(10) not null,
|
||||
caja_id int(10) unsigned not null,
|
||||
foreign key fk_efectivos_caja (caja_id) references caja (id) on update cascade on delete restrict
|
||||
);
|
||||
|
||||
create table documentos
|
||||
(
|
||||
id int(10) unsigned primary key auto_increment,
|
||||
cheques int(10) not null,
|
||||
tarjetas int(10) not null,
|
||||
caja_id int(10) unsigned not null,
|
||||
foreign key fk_documentos_caja (caja_id) references caja (id) on update cascade on delete restrict
|
||||
);
|
||||
|
||||
|
||||
insert into tipos_egreso (nombre)
|
||||
values ('Factura Materia Prima'),
|
||||
('Factura Gastos Generales'),
|
||||
('Gasto Menor Materia Prima'),
|
||||
('Gasto General Sin Respaldo'),
|
||||
('Gasto General Con Boleta'),
|
||||
('Guia Materia Prima'),
|
||||
('Anticipo Arriendo'),
|
||||
('Anticipo Personal'),
|
||||
('Pago Partime'),
|
||||
('Retiros Gerencia'),
|
||||
('Otro');
|
||||
|
||||
insert into tipos_ingreso (nombre)
|
||||
values ('Boletas Fiscales'),
|
||||
('Boletas Manuales'),
|
||||
('Facturas'),
|
||||
('Guias')
|
||||
|
||||
@@ -1,173 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018-2019 Daniel Cortes
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
pragma foreign_keys = ON;
|
||||
|
||||
drop table if exists egresos;
|
||||
drop table if exists tipos_egreso;
|
||||
drop table if exists ingresos;
|
||||
drop table if exists tipos_ingreso;
|
||||
drop table if exists efectivos;
|
||||
drop table if exists documentos;
|
||||
drop table if exists caja;
|
||||
|
||||
create table caja
|
||||
(
|
||||
id integer primary key,
|
||||
fecha date not null
|
||||
);
|
||||
|
||||
create table tipos_egreso
|
||||
(
|
||||
id integer primary key,
|
||||
nombre text not null
|
||||
);
|
||||
|
||||
create table egresos
|
||||
(
|
||||
id integer primary key,
|
||||
nro text not null,
|
||||
descripcion text not null,
|
||||
valor integer not null,
|
||||
tipo_egreso_id integer not null,
|
||||
caja_id integer not null,
|
||||
foreign key (tipo_egreso_id) references tipos_egreso (id) on update cascade on delete restrict,
|
||||
foreign key (caja_id) references caja (id) on update cascade on delete restrict
|
||||
);
|
||||
|
||||
create table tipos_ingreso
|
||||
(
|
||||
id integer primary key,
|
||||
nombre text not null
|
||||
);
|
||||
|
||||
create table ingresos
|
||||
(
|
||||
id integer primary key,
|
||||
valor integer not null,
|
||||
nro_inicial text not null,
|
||||
nro_final text not null,
|
||||
tipo_ingreso_id integer unsigned not null,
|
||||
caja_id integer unsigned not null,
|
||||
foreign key (tipo_ingreso_id) references tipos_ingreso (id) on update cascade on delete restrict,
|
||||
foreign key (caja_id) references caja (id) on update cascade on delete restrict
|
||||
);
|
||||
|
||||
create table efectivos
|
||||
(
|
||||
id integer primary key,
|
||||
veinte_mil integer not null,
|
||||
diez_mil integer not null,
|
||||
cinco_mil integer not null,
|
||||
dos_mil integer not null,
|
||||
mil integer not null,
|
||||
quinientos integer not null,
|
||||
cien integer not null,
|
||||
cincuenta integer not null,
|
||||
diez integer not null,
|
||||
caja_id integer not null,
|
||||
foreign key (caja_id) references caja (id) on update cascade on delete restrict
|
||||
);
|
||||
|
||||
create table documentos
|
||||
(
|
||||
id integer primary key,
|
||||
cheques integer not null,
|
||||
tarjetas integer not null,
|
||||
caja_id integer not null,
|
||||
foreign key (caja_id) references caja (id) on update cascade on delete restrict
|
||||
);
|
||||
|
||||
insert into tipos_egreso (nombre)
|
||||
values ('Factura Materia Prima'),
|
||||
('Factura Gastos Generales'),
|
||||
('Gasto Menor Materia Prima'),
|
||||
('Gasto General Sin Respaldo'),
|
||||
('Gasto General Con Boleta'),
|
||||
('Guia Materia Prima'),
|
||||
('Anticipo Arriendo'),
|
||||
('Anticipo Personal'),
|
||||
('Pago Partime'),
|
||||
('Retiros Gerencia'),
|
||||
('Otro');
|
||||
|
||||
insert into tipos_ingreso (nombre)
|
||||
values ('Boletas Fiscales'),
|
||||
('Boletas Manuales'),
|
||||
('Facturas'),
|
||||
('Guias');
|
||||
|
||||
-- Primera Migracion, se agrega las columnas de nro de z inicial y final para el ingreso.
|
||||
|
||||
alter table ingresos
|
||||
add column nro_z_inicial text;
|
||||
alter table ingresos
|
||||
add column nro_z_final text;
|
||||
|
||||
-- Segunda migracion, se necesita un tipo de ingresos de boleta exenta
|
||||
insert into tipos_ingreso (nombre)
|
||||
values ('Boleta Exenta');
|
||||
|
||||
-- Tercera migracion, necesidad de un campo de retiro en documentos
|
||||
alter table documentos
|
||||
add column retiros integer default 0;
|
||||
|
||||
-- Cuarta migracion, es requerida una tabla en la que se pueda calcular el fondo de la caja
|
||||
drop table if exists calculo_fondo;
|
||||
create table calculo_fondo
|
||||
(
|
||||
id integer primary key,
|
||||
valor integer not null,
|
||||
descripcion text not null,
|
||||
caja_id integer not null,
|
||||
foreign key (caja_id) references caja (id) on update cascade on delete restrict
|
||||
);
|
||||
|
||||
-- Quinta migracion, tabla para generar y guardar el estado de resultado
|
||||
drop table if exists estado_resultado;
|
||||
create table estado_resultado
|
||||
(
|
||||
id integer primary key,
|
||||
mes date unique not null,
|
||||
costo_venta integer null,
|
||||
cuenta_corriente_factura integer null,
|
||||
cuenta_corriente_boleta integer null,
|
||||
cuenta_corriente_sin_respaldo integer null,
|
||||
remuneraciones integer null,
|
||||
finiquitos integer null,
|
||||
aguinaldo integer null,
|
||||
bonos_personal integer null,
|
||||
honorarios_contador integer null,
|
||||
arriendo integer null,
|
||||
agua integer null,
|
||||
luz integer null,
|
||||
gas integer null,
|
||||
telefono integer null,
|
||||
otro_servicio integer null,
|
||||
ppm real null,
|
||||
ivaFavor int null
|
||||
);
|
||||
|
||||
-- Sexta migracion, se necesita guardar el fondo diario de una caja asi que se le agrega a la tabla de caja
|
||||
alter table caja
|
||||
add column fondo integer default 0;
|
||||
BIN
dist/Programa Caja.jar
vendored
BIN
dist/Programa Caja.jar
vendored
Binary file not shown.
4
dist/config/logging.properties
vendored
4
dist/config/logging.properties
vendored
@@ -1,10 +1,10 @@
|
||||
#handlers= java.util.logging.ConsoleHandler
|
||||
handlers= java.util.logging.FileHandler
|
||||
java.util.logging.FileHandler.level=FINER
|
||||
java.util.logging.FileHandler.level=SEVERE
|
||||
java.util.logging.FileHandler.pattern=logs/logs-%u-%g.log
|
||||
java.util.logging.FileHandler.formatter=java.util.logging.XMLFormatter
|
||||
java.util.logging.FileHandler.append=true
|
||||
|
||||
#java.util.logging.ConsoleHandler.level = ALL
|
||||
#java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
|
||||
danielcortes.level=FINER
|
||||
danielcortes.level=FINER
|
||||
|
||||
BIN
dist/data/version_scripts
vendored
Normal file
BIN
dist/data/version_scripts
vendored
Normal file
Binary file not shown.
@@ -26,6 +26,7 @@ package danielcortes.xyz;
|
||||
|
||||
import danielcortes.xyz.controllers.BaseLayoutController;
|
||||
import danielcortes.xyz.data.Configuration;
|
||||
import danielcortes.xyz.data.DAOManager;
|
||||
import danielcortes.xyz.views.BaseLayout;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -35,9 +36,11 @@ import java.util.Locale;
|
||||
import java.util.logging.LogManager;
|
||||
|
||||
public class Main {
|
||||
private static final int DATABASE_VERSION = 2;
|
||||
static {
|
||||
setUpLogger();
|
||||
setUpSystemProperties();
|
||||
updateDatabase();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -45,14 +48,15 @@ public class Main {
|
||||
}
|
||||
|
||||
private static void run() {
|
||||
//MainSideBar view = new MainSideBar();
|
||||
//MainSideBarController mainController = new MainSideBarController(view);
|
||||
|
||||
BaseLayout view = new BaseLayout();
|
||||
BaseLayoutController controller = new BaseLayoutController(view);
|
||||
executeView(view.getContentPanel());
|
||||
}
|
||||
|
||||
private static void updateDatabase(){
|
||||
DAOManager.getVersionDAO().updateTo(DATABASE_VERSION);
|
||||
}
|
||||
|
||||
private static void executeView(JComponent view) {
|
||||
JFrame frame = new JFrame(Configuration.get("nombre_caja"));
|
||||
frame.setContentPane(view);
|
||||
|
||||
@@ -46,6 +46,8 @@ import danielcortes.xyz.models.tipo_egreso.SQLiteTipoEgresoDAO;
|
||||
import danielcortes.xyz.models.tipo_egreso.TipoEgresoDAO;
|
||||
import danielcortes.xyz.models.tipo_ingreso.SQLiteTipoIngresoDAO;
|
||||
import danielcortes.xyz.models.tipo_ingreso.TipoIngresoDAO;
|
||||
import danielcortes.xyz.models.version.SQLiteVersionDAO;
|
||||
import danielcortes.xyz.models.version.VersionDAO;
|
||||
|
||||
public class DAOManager {
|
||||
private static final CajaDAO cajaDAO;
|
||||
@@ -59,6 +61,7 @@ public class DAOManager {
|
||||
private static final TipoEgresoDAO tipoEgresoDAO;
|
||||
private static final TipoIngresoDAO tipoIngresoDAO;
|
||||
private static final EstadoResultadoDAO estadoResultadoDAO;
|
||||
private static final VersionDAO versionDAO;
|
||||
|
||||
static {
|
||||
cajaDAO = new SQLiteCajaDAO();
|
||||
@@ -72,6 +75,7 @@ public class DAOManager {
|
||||
tipoEgresoDAO = new SQLiteTipoEgresoDAO();
|
||||
tipoIngresoDAO = new SQLiteTipoIngresoDAO();
|
||||
estadoResultadoDAO = new SQLiteEstadoResultadoDAO();
|
||||
versionDAO = new SQLiteVersionDAO();
|
||||
}
|
||||
|
||||
public static CajaDAO getCajaDAO() {
|
||||
@@ -117,4 +121,8 @@ public class DAOManager {
|
||||
public static EstadoResultadoDAO getEstadoResultadoDAO() {
|
||||
return estadoResultadoDAO;
|
||||
}
|
||||
|
||||
public static VersionDAO getVersionDAO() {
|
||||
return versionDAO;
|
||||
}
|
||||
}
|
||||
|
||||
99
src/danielcortes/xyz/models/version/SQLiteVersionDAO.java
Normal file
99
src/danielcortes/xyz/models/version/SQLiteVersionDAO.java
Normal file
@@ -0,0 +1,99 @@
|
||||
package danielcortes.xyz.models.version;
|
||||
|
||||
import danielcortes.xyz.data.SQLiteConnectionHolder;
|
||||
|
||||
import java.io.*;
|
||||
import java.sql.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
public class SQLiteVersionDAO extends VersionDAO {
|
||||
private static final Logger LOGGER = Logger.getLogger(SQLiteVersionDAO.class.getName());
|
||||
|
||||
public SQLiteVersionDAO() {
|
||||
this.connectionHolder = new SQLiteConnectionHolder();
|
||||
}
|
||||
|
||||
private boolean tableVersionsExists() {
|
||||
try {
|
||||
DatabaseMetaData md = this.connectionHolder.getConnection().getMetaData();
|
||||
ResultSet rs = md.getTables(null, null, "version", null);
|
||||
boolean exists = rs.next();
|
||||
rs.close();
|
||||
|
||||
return exists;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private int getCurrentVersion() {
|
||||
if (tableVersionsExists()) {
|
||||
try (Connection conn = this.connectionHolder.getConnection()) {
|
||||
String query = "SELECT version FROM version LIMIT 1";
|
||||
PreparedStatement ps = conn.prepareStatement(query);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
rs.next();
|
||||
int version = rs.getInt(1);
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
return version;
|
||||
} catch (SQLException e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private InputStream getVersionScript(int version){
|
||||
try {
|
||||
ZipFile zipFile = new ZipFile(new File("data/version_scripts"));
|
||||
ZipEntry zipEntry = zipFile.getEntry(version + ".sql");
|
||||
return zipFile.getInputStream(zipEntry);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new InputStream() {
|
||||
@Override
|
||||
public int read(){
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void executeVersionScript(int version){
|
||||
try (Connection conn = this.connectionHolder.getConnection()) {
|
||||
Statement statement = conn.createStatement();
|
||||
InputStream inputStream = getVersionScript(version);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
statement.addBatch(line);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
statement.executeBatch();
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTo(int version) {
|
||||
int currentVersion = this.getCurrentVersion();
|
||||
while (currentVersion < version) {
|
||||
currentVersion++;
|
||||
executeVersionScript(currentVersion);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
8
src/danielcortes/xyz/models/version/VersionDAO.java
Normal file
8
src/danielcortes/xyz/models/version/VersionDAO.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package danielcortes.xyz.models.version;
|
||||
|
||||
import danielcortes.xyz.data.ConnectionHolder;
|
||||
|
||||
public abstract class VersionDAO {
|
||||
protected ConnectionHolder connectionHolder;
|
||||
public abstract void updateTo(int version);
|
||||
}
|
||||
Reference in New Issue
Block a user