\def\filedate{2015/02/23}
\def\fileversion{1.0}
\NeedsTeXFormat{LaTeX2e}
\AtEndOfPackage{\ProvidesPackage{pdbf}
             [\filedate\space\space\space\space\fileversion\space\space\space\space(Patrick Bender)]}
\RequirePackage{zref-savepos}
\RequirePackage{zref-abspage}
\RequirePackage{zref-user}
\RequirePackage{xcolor}
\RequirePackage{graphicx}
\RequirePackage{xstring}
\RequirePackage{xparse}
\RequirePackage{array}

\newcounter{visual}
\newcounter{overlay}
\stepcounter{visual}
\stepcounter{overlay}

\newwrite\pdbf@tempfile
\immediate\openout\pdbf@tempfile=pdbf-config.json
\immediate\write\pdbf@tempfile{[}

\AtBeginDocument{
	\newlength{\emptylen} 
	\settowidth{\emptylen}{} 
	
	\ifdim\emptylen=\paperwidth
	  \errmessage{^^J ^^J Error: paperwidth value missing! Did you forgot to specify the papersize via the geometry package? ^^J}%
	\fi
	
	\ifdim\emptylen=\paperheight
	  \errmessage{^^J ^^J Error: paperheight value missing! Did you forgot to specify the papersize via the geometry package? ^^J}%
	\fi
	
	\newwrite\pdbf@temp
	\immediate\openout\pdbf@temp=pdbf-dim.json
	\immediate\write\pdbf@temp{\@charlb "width": "\number\paperwidth",}
	\immediate\write\pdbf@temp{"height": "\number\paperheight" \@charrb}
	\immediate\closeout\pdbf@temp
}

\AtEndDocument{
	\immediate\write\pdbf@tempfile{]}
	\immediate\closeout\pdbf@tempfile
}

\zref@addprops{savepos}{abspage}

\DeclareRobustCommand\pdbf@save[1]{%
      \zsavepos{#1}%
}

%DataText
\define@key{pdbf}{color}{\def\pdbfDynamicTextColor{#1}}
\def\pdbfDynamicTextColor{blue}
\define@key{pdbf}{linkTo}{\def\pdbf@linkTo{#1}}
\def\pdbf@linkTo{}
\define@key{pdbf}{linkSelector}{\def\pdbf@linkSelector{#1}}
\def\pdbf@linkSelector{}
\define@key{pdbf}{linkLabel}{\def\pdbf@linkLabel{#1}}
\def\pdbf@linkLabel{}

%DataTable
\define@key{pdbf}{verticalLines}{\def\pdbf@verticalLines{#1}}
\def\pdbf@verticalLines{a}
\define@key{pdbf}{horizontalLinesHeader}{\def\pdbf@horizontalLinesHeader{#1}}
\def\pdbf@horizontalLinesHeader{a}
\define@key{pdbf}{horizontalLinesBody}{\def\pdbf@horizontalLinesBody{#1}}
\def\pdbf@horizontalLinesBody{a}

%Common
\define@key{pdbf}{name}{\def\pdbf@name{#1}}
\def\pdbf@name{}
\define@key{pdbf}{width}{\def\pdbf@width{#1}}
\define@key{pdbf}{height}{\def\pdbf@height{#1}}
\def\pdbf@xunitname{}
\define@key{pdbf}{xunit}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@xunitname]}
\def\pdbf@yunitname{}
\define@key{pdbf}{yunit}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@yunitname]}
\def\pdbf@options{{}}
\define@key{pdbf}{options}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@opt]\def\pdbf@options{{\pdbf@opt}}}
\def\pdbfQuality{1.0}
\define@key{pdbf}{quality}{\def\pdbfQuality{#1}}
\def\pdbf@chartType{Line}
\define@key{pdbf}{chartType}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@chartType]}
\define@key{pdbf}{customImage}{\def\pdbf@customImage{#1}}

%LineChart und BarChart
\def\pdbfLogscale{false}
\define@key{pdbf}{logscale}[true]{\def\pdbfLogscale{#1}}
\def\pdbfLegendpos{topright}
\define@key{pdbf}{legendpos}{\def\pdbfLegendpos{#1}}
\def\pdbf@includeZero{false}
\define@key{pdbf}{includeZero}{\def\pdbf@includeZero{#1}}
\def\pdbf@drawPoints{false}
\define@key{pdbf}{drawPoints}{\def\pdbf@drawPoints{#1}}
\def\pdbf@fillGraph{false}
\define@key{pdbf}{fillGraph}{\def\pdbf@fillGraph{#1}}
\def\pdbf@showRangeSelector{false}
\define@key{pdbf}{showRangeSelector}{\def\pdbf@showRangeSelector{#1}}

%BarChart
\def\pdbf@overlap{-1}
\define@key{pdbf}{overlap}[2]{\def\pdbf@overlap{#1}}

%Pivot
\def\pdbf@rows{[]}
\define@key{pdbf}{rows}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@rows]}
\def\pdbf@cols{[]}
\define@key{pdbf}{cols}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@cols]}
\def\pdbf@aggregation{Minimum}
\define@key{pdbf}{aggregation}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@aggregation]}
\def\pdbf@aggregationattribute{}
\define@key{pdbf}{aggregationattribute}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@aggregationattribute]}
\def\pdbf@aggregationBig{}
\define@key{pdbf}{aggregationBig}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@aggregationBig]}
\def\pdbf@aggregationattributeBig{}
\define@key{pdbf}{aggregationattributeBig}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@aggregationattributeBig]}

%Multiplot
\def\pdbf@xCount{0}
\define@key{pdbf}{xCount}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@xCount]}
\def\pdbf@yCount{0}
\define@key{pdbf}{yCount}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@yCount]}
\def\pdbf@leftArr{auto}
\define@key{pdbf}{leftArr}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@leftArr]}
\def\pdbf@rightArr{auto}
\define@key{pdbf}{rightArr}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@rightArr]}
\def\pdbf@topArr{auto}
\define@key{pdbf}{topArr}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@topArr]}
\def\pdbf@bottomArr{auto}
\define@key{pdbf}{bottomArr}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@bottomArr]}
\def\pdbf@xValues{}
\define@key{pdbf}{xValues}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@xValues]}
\def\pdbf@yValues{}
\define@key{pdbf}{yValues}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@yValues]}
\def\pdbf@yFirst{false}
\define@key{pdbf}{yFirst}[true]{\def\pdbf@yFirst{#1}}
\def\pdbf@forceXequal{false}
\define@key{pdbf}{forceXequal}[true]{\def\pdbf@forceXequal{#1}}
\def\pdbf@forceYequal{false}
\define@key{pdbf}{forceYequal}[true]{\def\pdbf@forceYequal{#1}}

%CSV
\define@key{pdbfCSV}{headers}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@headers]}
\def\pdbf@headers{[]}
\define@key{pdbfCSV}{quote}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@quote]}
\def\pdbf@quote{\@backslashchar"}
\define@key{pdbfCSV}{seperator}{\StrSubstitute{\detokenize{#1}}{"}{\@backslashchar"}[\pdbf@seperator]}
\def\pdbf@seperator{,}

%Chart, Pivot table, Multiplot Chart
\NewDocumentCommand\pdbf@chart{o m m m}{%
	\edef\pdbf@fontsave{\f@size}%
	\setkeys{pdbf}{#1}%
	\ifdefined \pdbf@width \else%
  		\errmessage{^^J ^^J Error: width value missing! ^^J}%
	\fi%
	\ifdefined \pdbf@height \else%
		\errmessage{^^J ^^J Error: height value missing! ^^J}%
	\fi%
	\StrSubstitute{#2}{"}{\@backslashchar"}[\pdbf@query]%
	\StrSubstitute{#3}{"}{\@backslashchar"}[\pdbf@queryB]%
	\edef\pdbf@tmpOverlay{Overlay\arabic{visual}}%
	{\fontsize{5pt}{1sp}\selectfont%
		\setlength\tabcolsep{1sp}%
		% Place Image
		\begin{tabular}{ccc}%
		\pdbf@save{Overlay\arabic{visual}1}&&\\%
		\ifdefined\pdbf@customImage%
				&\includegraphics[width=\pdbf@width, height=\pdbf@height]{\pdbf@customImage}&\\%
		\else%
				\IfFileExists{./Overlay\arabic{visual}.pdf}{%
			    		&\includegraphics[width=\pdbf@width, height=\pdbf@height, page=1]{./Overlay\arabic{visual}.pdf}&\\%
				}{%
			    		&\includegraphics[width=\pdbf@width, height=\pdbf@height, page=1]{./dummy.pdf}&\\%
				}%
		\fi%
		&&\zsavepos{Overlay\arabic{visual}2}\\%
		\end{tabular}%
		% Write part of pdbf-config.json
		\ifnum \value{overlay} > 1%
			\immediate\write\pdbf@tempfile{,\@charlb}%
		\else%
			\immediate\write\pdbf@tempfile{\@charlb}%
		\fi%
		\immediate\write\pdbf@tempfile{\space\space\space\space"name": "Overlay\arabic{visual}",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space"type": \@charlb}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space"C": "pdbf.json.#4",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space"I": \@charlb}%
		\ifdefined\pdbf@customImage%
				\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"customImage": "\pdbf@customImage",}%
		\fi%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"name": "\pdbf@name",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"linkTo": "\pdbf@linkTo",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"linkSelector": "\pdbf@linkSelector",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"linkLabel": "\pdbf@linkLabel",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"verticalLines": "\pdbf@verticalLines",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"horizontalLinesHeader": "\pdbf@horizontalLinesHeader",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"horizontalLinesBody": "\pdbf@horizontalLinesBody",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"logScale": \pdbfLogscale,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"xUnitName": "\pdbf@xunitname",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"yUnitName": "\pdbf@yunitname",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"query": "\pdbf@query",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"queryB": "\pdbf@queryB",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"options": "\pdbf@options",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"quality": \pdbfQuality,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"overlap": \pdbf@overlap,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"legendpos": "\pdbfLegendpos",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"includeZero": \pdbf@includeZero,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"drawPoints": \pdbf@drawPoints,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"fillGraph": \pdbf@fillGraph,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"showRangeSelector": \pdbf@showRangeSelector,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"aggregation": "\pdbf@aggregation",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"aggregationattribute": "\pdbf@aggregationattribute",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"aggregationBig": "\pdbf@aggregationBig",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"aggregationattributeBig": "\pdbf@aggregationattributeBig",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"xCount": \pdbf@xCount,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"yCount": \pdbf@yCount,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"leftArr": "\pdbf@leftArr",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"rightArr": "\pdbf@rightArr",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"topArr": "\pdbf@topArr",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"bottomArr": "\pdbf@bottomArr",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"xValues": "\pdbf@xValues",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"yValues": "\pdbf@yValues",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"yFirst": \pdbf@yFirst,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"forceXequal": \pdbf@forceXequal,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"forceYequal": \pdbf@forceYequal,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"cols": "\pdbf@cols",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"rows": "\pdbf@rows",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"chartType": "\pdbf@chartType",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"x1": \zposx{Overlay\arabic{visual}1},}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"x2": \zposx{Overlay\arabic{visual}2},}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"y1": \zposy{Overlay\arabic{visual}1},}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"y2": \zposy{Overlay\arabic{visual}2},}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"fontsize": \pdbf@fontsave,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"page": \zref@extract{Overlay\arabic{visual}1}{abspage}}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\@charrb}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\@charrb}%
		\immediate\write\pdbf@tempfile{\@charrb}%
		\stepcounter{visual}%
		\stepcounter{overlay}%
	}%
	\put(-10000,0){abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789pdbf!\textasciitilde\textbraceleft\textbraceright\textbackslash\textasciicircum\textsection\textdollar\%\&/()=?\#+\textbar\textunderscore[].,;:\pdbf@tmpOverlay-}~%
	\relax
}

\NewDocumentCommand\chart{O{} o m}{%
{\pdbf@chart[#1]{#3}{\IfValueTF{#2}{#2}{#3}}{Chart}}%
}

\NewDocumentCommand\pivotTable{O{} o m}{%
{\pdbf@chart[#1]{#3}{\IfValueTF{#2}{#2}{#3}}{Pivot}}%
}

\NewDocumentCommand\multiplotChart{O{} o m}{%
{\pdbf@chart[#1]{#3}{\IfValueTF{#2}{#2}{#3}}{MultiplotChart}}%
}

%Datatext, Datatable, SQL, etc.
\NewDocumentCommand\pdbf@sqlmult{m m m m}{%
\setkeys{pdbf}{#4}%
\edef\pdbf@fontsave{\f@size}%
\edef\pdbf@fontskipsave{\the\baselineskip}%
\edef\pdbf@tabcolsep{\the\tabcolsep}%
{\fontsize{5pt}{1sp}\selectfont%
		\setlength\tabcolsep{1sp}%
		\begin{tabular}[t]{ccc}%
		\pdbf@save{Overlay\arabic{visual}1}&{\color{\pdbfDynamicTextColor}\fontsize{\pdbf@fontsave}{\pdbf@fontskipsave}\setlength\tabcolsep{\pdbf@tabcolsep}\selectfont#1}&\\%
		&&\zsavepos{Overlay\arabic{visual}2}\\%
		\end{tabular}%
		% Write part of pdbf-config.json
		\StrSubstitute{#2}{"}{\@backslashchar"}[\pdbf@query]%
		\StrSubstitute{#2}{"}{\@backslashchar"}[\pdbf@queryB]%
		\ifnum \value{overlay} > 1%
			\immediate\write\pdbf@tempfile{,\@charlb}%
		\else%
			\immediate\write\pdbf@tempfile{\@charlb}%
		\fi%
		\immediate\write\pdbf@tempfile{\space\space\space\space"name": "Overlay\arabic{visual}",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space"type": \@charlb}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space"C": "#3",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space"I": \@charlb}%
		\ifdefined\pdbf@customImage%
				\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"customImage": "\pdbf@customImage",}%
		\fi%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"name": "\pdbf@name",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"linkTo": "\pdbf@linkTo",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"linkSelector": "\pdbf@linkSelector",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"linkLabel": "\pdbf@linkLabel",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"verticalLines": "\pdbf@verticalLines",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"horizontalLinesHeader": "\pdbf@horizontalLinesHeader",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"horizontalLinesBody": "\pdbf@horizontalLinesBody",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"logScale": \pdbfLogscale,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"xUnitName": "\pdbf@xunitname",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"yUnitName": "\pdbf@yunitname",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"query": "\pdbf@query",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"queryB": "\pdbf@queryB",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"options": "\pdbf@options",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"quality": \pdbfQuality,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"overlap": \pdbf@overlap,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"legendpos": "\pdbfLegendpos",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"includeZero": \pdbf@includeZero,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"drawPoints": \pdbf@drawPoints,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"fillGraph": \pdbf@fillGraph,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"showRangeSelector": \pdbf@showRangeSelector,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"aggregation": "\pdbf@aggregation",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"aggregationattribute": "\pdbf@aggregationattribute",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"aggregationBig": "\pdbf@aggregationBig",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"aggregationattributeBig": "\pdbf@aggregationattributeBig",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"xCount": \pdbf@xCount,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"yCount": \pdbf@yCount,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"leftArr": "\pdbf@leftArr",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"rightArr": "\pdbf@rightArr",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"topArr": "\pdbf@topArr",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"bottomArr": "\pdbf@bottomArr",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"xValues": "\pdbf@xValues",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"yValues": "\pdbf@yValues",}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"yFirst": \pdbf@yFirst,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"forceXequal": \pdbf@forceXequal,}% 
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"forceYequal": \pdbf@forceYequal,}%  
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"cols": "\pdbf@cols",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"rows": "\pdbf@rows",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"chartType": "\pdbf@chartType",}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"x1": \zposx{Overlay\arabic{visual}1},}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"x2": \zposx{Overlay\arabic{visual}2},}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"y1": \zposy{Overlay\arabic{visual}1},}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"y2": \zposy{Overlay\arabic{visual}2},}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"fontsize": \pdbf@fontsave,}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"page": \zref@extract{Overlay\arabic{visual}1}{abspage}}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\@charrb}%
		\immediate\write\pdbf@tempfile{\space\space\space\space\@charrb}%
		\immediate\write\pdbf@tempfile{\@charrb}%
		\stepcounter{visual}%
		\stepcounter{overlay}%
}%
}

\NewDocumentCommand\sql{O{} o m}{%
\StrSubstitute{#3}{_}{$\string_$}[\pdbf@tmpstore]
{\pdbf@sqlmult{\IfValueTF{#2}{#2}{\pdbf@tmpstore}}{#3}{pdbf.json.Text}{#1}}%
}

\def\pdbf@csxdefaux#1{%
    \protected@write\@mainaux{}{%
       \string\expandafter\string\gdef
        \string\csname\space pdbf@Overlay\arabic{visual}\string\endcsname{\expandafter\detokenize\expandafter\expandafter\expandafter{#1}}%
    }%
}

\NewDocumentCommand\dataText{O{} m}{%
{\expandafter\ifx\csname pdbf@Overlay\arabic{visual}\endcsname\relax\pdbf@csxdefaux{dummy}\else\pdbf@csxdefaux{\csname pdbf@Overlay\arabic{visual}\endcsname}\fi%
\pdbf@sqlmult{\csname pdbf@Overlay\arabic{visual}\endcsname}{#2}{pdbf.json.DataText}{#1}}%
}

\NewDocumentCommand\dataTable{O{} m}{%
{\expandafter\ifx\csname pdbf@Overlay\arabic{visual}\endcsname\relax\pdbf@csxdefaux{dummy}\else\pdbf@csxdefaux{\csname pdbf@Overlay\arabic{visual}\endcsname}\fi%
\pdbf@sqlmult{\csname pdbf@Overlay\arabic{visual}\endcsname}{#2}{pdbf.json.DataTable}{#1}}%
}

%Database
\NewDocumentCommand\pdbf@db{O{} m m m m m}{%
	\setkeys{pdbfCSV}{#1}%
	\StrSubstitute{#2}{"}{\@backslashchar"}[\pdbf@valueA]%
	\StrSubstitute{#3}{"}{\@backslashchar"}[\pdbf@valueB]%
	\StrSubstitute{#4}{"}{\@backslashchar"}[\pdbf@valueC]%
	\StrSubstitute{#5}{"}{\@backslashchar"}[\pdbf@valueD]%
	\ifnum \value{overlay} > 1%
		\immediate\write\pdbf@tempfile{,\@charlb}%
	\else%
		\immediate\write\pdbf@tempfile{\@charlb}%
	\fi%
	\immediate\write\pdbf@tempfile{\space\space\space\space"name": "Database",}%
	\immediate\write\pdbf@tempfile{\space\space\space\space"type": \@charlb}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space"C": "pdbf.json.Database",}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space"I": \@charlb}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"type": #6,}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"value1": "\pdbf@valueA",}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"value2": "\pdbf@valueB",}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"value3": "\pdbf@valueC",}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"value4": "\pdbf@valueD",}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"headers": \pdbf@headers,}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"quote": "\pdbf@quote",}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\space\space\space\space"seperator": "\pdbf@seperator"}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\space\space\space\space\@charrb}%
	\immediate\write\pdbf@tempfile{\space\space\space\space\@charrb}%
	\immediate\write\pdbf@tempfile{\@charrb}%
	\stepcounter{overlay}%
}

\NewDocumentCommand\dbSQLText{m}{%
	\pdbf@db{#1}{}{}{}{1}
}

\NewDocumentCommand\dbSQLFile{m}{%
	\pdbf@db{#1}{}{}{}{2}
}

\NewDocumentCommand\dbSQLJDBC{m m m m}{%
	\pdbf@db{#1}{#2}{#3}{#4}{3}
}

\NewDocumentCommand\dbCSVFile{O{} m m}{%
	\pdbf@db[#1]{#2}{#3}{}{}{4}
}

\endinput
