1/************* TabZip C++ Program Source Code File (.CPP) **************/
2/* PROGRAM NAME: TABZIP Version 1.0 */
3/* (C) Copyright to the author Olivier BERTRAND 2016 */
4/* This program are the TABZIP class DB execution routines. */
5/***********************************************************************/
6
7/***********************************************************************/
8/* Include relevant sections of the MariaDB header file. */
9/***********************************************************************/
10#include <my_global.h>
11
12/***********************************************************************/
13/* Include application header files: */
14/* global.h is header containing all global declarations. */
15/* plgdbsem.h is header containing the DB application declarations. */
16/* (x)table.h is header containing the TDBASE declarations. */
17/* tabzip.h is header containing the TABZIP classes declarations. */
18/***********************************************************************/
19#include "global.h"
20#include "plgdbsem.h"
21#include "xtable.h"
22#include "filamtxt.h"
23#include "filamzip.h"
24#include "resource.h" // for IDS_COLUMNS
25#include "tabdos.h"
26#include "tabzip.h"
27
28/* -------------------------- Class ZIPDEF --------------------------- */
29
30/************************************************************************/
31/* DefineAM: define specific AM block values. */
32/************************************************************************/
33bool ZIPDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
34{
35//target = GetStringCatInfo(g, "Target", NULL);
36 return DOSDEF::DefineAM(g, "ZIP", poff);
37} // end of DefineAM
38
39/***********************************************************************/
40/* GetTable: makes a new Table Description Block. */
41/***********************************************************************/
42PTDB ZIPDEF::GetTable(PGLOBAL g, MODE m)
43{
44 return new(g) TDBZIP(this);
45} // end of GetTable
46
47/* ------------------------------------------------------------------- */
48
49/***********************************************************************/
50/* Implementation of the TDBZIP class. */
51/***********************************************************************/
52TDBZIP::TDBZIP(PZIPDEF tdp) : TDBASE(tdp)
53{
54 zipfile = NULL;
55 zfn = tdp->Fn;
56//target = tdp->target;
57 nexterr = UNZ_OK;
58} // end of TDBZIP standard constructor
59
60/***********************************************************************/
61/* Allocate ZIP column description block. */
62/***********************************************************************/
63PCOL TDBZIP::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
64{
65 return new(g) ZIPCOL(cdp, this, cprec, n);
66} // end of MakeCol
67
68/***********************************************************************/
69/* open a zip file. */
70/* param: filename path and the filename of the zip file to open. */
71/* return: true if open, false otherwise. */
72/***********************************************************************/
73bool TDBZIP::open(PGLOBAL g, const char *fn)
74{
75 char filename[_MAX_PATH];
76
77 PlugSetPath(filename, fn, GetPath());
78
79 if (!zipfile && !(zipfile = unzOpen64(filename)))
80 sprintf(g->Message, "Zipfile open error");
81
82 return (zipfile == NULL);
83} // end of open
84
85/***********************************************************************/
86/* Close the zip file. */
87/***********************************************************************/
88void TDBZIP::close()
89{
90 if (zipfile) {
91 unzClose(zipfile);
92 zipfile = NULL;
93 } // endif zipfile
94
95} // end of close
96
97/***********************************************************************/
98/* ZIP Cardinality: returns table size in number of rows. */
99/***********************************************************************/
100int TDBZIP::Cardinality(PGLOBAL g)
101{
102 if (!g)
103 return 1;
104 else if (Cardinal < 0) {
105 if (!open(g, zfn)) {
106 unz_global_info64 ginfo;
107 int err = unzGetGlobalInfo64(zipfile, &ginfo);
108
109 Cardinal = (err == UNZ_OK) ? (int)ginfo.number_entry : 0;
110 } else
111 Cardinal = 0;
112
113 } // endif Cardinal
114
115 return Cardinal;
116} // end of Cardinality
117
118/***********************************************************************/
119/* ZIP GetMaxSize: returns file size estimate in number of lines. */
120/***********************************************************************/
121int TDBZIP::GetMaxSize(PGLOBAL g)
122{
123 if (MaxSize < 0)
124 MaxSize = Cardinality(g);
125
126 return MaxSize;
127} // end of GetMaxSize
128
129/***********************************************************************/
130/* ZIP Access Method opening routine. */
131/***********************************************************************/
132bool TDBZIP::OpenDB(PGLOBAL g)
133{
134 if (Use == USE_OPEN)
135 // Table already open
136 return false;
137
138 Use = USE_OPEN; // To be clean
139 return open(g, zfn);
140} // end of OpenDB
141
142/***********************************************************************/
143/* ReadDB: Data Base read routine for ZIP access method. */
144/***********************************************************************/
145int TDBZIP::ReadDB(PGLOBAL g)
146{
147 if (nexterr == UNZ_END_OF_LIST_OF_FILE)
148 return RC_EF;
149 else if (nexterr != UNZ_OK) {
150 sprintf(g->Message, "unzGoToNextFile error %d", nexterr);
151 return RC_FX;
152 } // endif nexterr
153
154 int err = unzGetCurrentFileInfo64(zipfile, &finfo, fn,
155 sizeof(fn), NULL, 0, NULL, 0);
156
157 if (err != UNZ_OK) {
158 sprintf(g->Message, "unzGetCurrentFileInfo64 error %d", err);
159 return RC_FX;
160 } // endif err
161
162 nexterr = unzGoToNextFile(zipfile);
163 return RC_OK;
164} // end of ReadDB
165
166/***********************************************************************/
167/* WriteDB: Data Base write routine for ZIP access method. */
168/***********************************************************************/
169int TDBZIP::WriteDB(PGLOBAL g)
170{
171 strcpy(g->Message, "ZIP tables are read only");
172 return RC_FX;
173} // end of WriteDB
174
175/***********************************************************************/
176/* Data Base delete line routine for ZIP access method. */
177/***********************************************************************/
178int TDBZIP::DeleteDB(PGLOBAL g, int irc)
179{
180 strcpy(g->Message, "Delete not enabled for ZIP tables");
181 return RC_FX;
182} // end of DeleteDB
183
184/***********************************************************************/
185/* Data Base close routine for ZIP access method. */
186/***********************************************************************/
187void TDBZIP::CloseDB(PGLOBAL g)
188{
189 close();
190 Use = USE_READY; // Just to be clean
191} // end of CloseDB
192
193/* ---------------------------- ZIPCOL ------------------------------- */
194
195/***********************************************************************/
196/* ZIPCOL public constructor. */
197/***********************************************************************/
198ZIPCOL::ZIPCOL(PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i, PCSZ am)
199 : COLBLK(cdp, tdbp, i)
200{
201 if (cprec) {
202 Next = cprec->GetNext();
203 cprec->SetNext(this);
204 } else {
205 Next = tdbp->GetColumns();
206 tdbp->SetColumns(this);
207 } // endif cprec
208
209 Tdbz = (TDBZIP*)tdbp;
210 flag = cdp->GetOffset();
211} // end of ZIPCOL constructor
212
213/***********************************************************************/
214/* ReadColumn: */
215/***********************************************************************/
216void ZIPCOL::ReadColumn(PGLOBAL g)
217{
218 switch (flag) {
219 case 1:
220 Value->SetValue(Tdbz->finfo.compressed_size);
221 break;
222 case 2:
223 Value->SetValue(Tdbz->finfo.uncompressed_size);
224 break;
225 case 3:
226 Value->SetValue((int)Tdbz->finfo.compression_method);
227 break;
228 case 4:
229 Tdbz->finfo.tmu_date.tm_year -= 1900;
230
231 if (((DTVAL*)Value)->MakeTime((tm*)&Tdbz->finfo.tmu_date))
232 Value->SetNull(true);
233
234 Tdbz->finfo.tmu_date.tm_year += 1900;
235 break;
236 default:
237 Value->SetValue_psz((PSZ)Tdbz->fn);
238 } // endswitch flag
239
240} // end of ReadColumn
241
242/* -------------------------- End of tabzip -------------------------- */
243