summaryrefslogtreecommitdiff
path: root/support/nlatexdb/nlatexdb/SqlQuery.cs
blob: 68934a2c7864f436d8ab803e3985aa70df756550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//    nlatexdb Version 0.03
//    Database Access in LaTeX
//    Copyright (C) 2011 Robin Höns, Integranova GmbH
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//    For more information see the web page http://hoens.net/robin

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nlatexdb
{
    class SqlQuery
    {
        public SqlQuery(string querytext, string queryvars)
        {
            m_querytext = querytext;
            string[] qvars = queryvars.Split(m_variablesplitter);
            m_queryvars = new List<SqlQueryVar>();
            foreach (string s in qvars)
            {
				Processer.Debug("Var: {0}", s);
                m_queryvars.Add(new SqlQueryVar(s.Trim()));
            }
        }
				
		public static void SetVariableSplitter(string var_splitter)
		{
			m_variablesplitter = var_splitter.ToCharArray();
		}


        private void ReplaceVariables(System.Data.Common.DbCommand comm,
            System.Collections.Generic.Dictionary<string, SqlQueryVar> varvalues)
        {
            string querytext = m_querytext;
            System.Collections.Generic.SortedDictionary<int, KeyValuePair<string, object>> repls =
               new SortedDictionary<int, KeyValuePair<string, object>>();
            foreach (KeyValuePair<string, SqlQueryVar> var in varvalues)
            {
                int startindex = 0;
                while (startindex >= 0 && startindex < querytext.Length)
                {
                    int varindex = querytext.IndexOf(var.Key, startindex);
                    if (varindex >= 0)
                    {
                        repls[varindex] = new KeyValuePair<string, object>(var.Key, var.Value.getValueSql());
                        startindex = varindex + 1;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            int j = 1;
            // Alle Variablenvorkommen sind nun sortiert im repl-Verzeichnis.
            foreach (KeyValuePair<string, object> replacement in repls.Values)
            {
                string parname = String.Format("@{0}", j);
                querytext = querytext.Replace(replacement.Key, parname);
                System.Data.Common.DbParameter par = comm.CreateParameter();
                par.Value = replacement.Value;
                par.ParameterName = parname;
                comm.Parameters.Add(par);
                j++;
            }
            comm.CommandText = querytext;
        }
		
		private List<object[]> ExecuteSql(System.Data.Common.DbConnection connection,
		                                  System.Collections.Generic.Dictionary<string, SqlQueryVar> varvalues)
		{
            List<object[]> alldata = new List<object[]>();
            using (System.Data.Common.DbCommand comm = connection.CreateCommand())
            {
                ReplaceVariables(comm, varvalues);

                using (System.Data.Common.DbDataReader rdr = comm.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        object[] myline = new object[rdr.FieldCount];
                        rdr.GetValues(myline);
                        alldata.Add(myline);
                    }
                    rdr.Close();
                }
            }
			return alldata;
		}

        public void Execute(List<string> texstuff, System.Data.Common.DbConnection connection,
		                    System.Collections.Generic.Dictionary<string, SqlQueryVar> varvalues, 
		                    Processer proc,
		                    string filenamepattern)
        {
			List<object[]> alldata = ExecuteSql(connection, varvalues);
            foreach (object[] objline in alldata)
            {

                for (int i = 0; i < m_queryvars.Count; i++)
                {
                    m_queryvars[i].setValue(objline[i]);
                    if (!varvalues.ContainsKey(m_queryvars[i].Name))
                    {
                        varvalues.Add(m_queryvars[i].Name, m_queryvars[i]);
                    }
                }
				
				string outpath = null;
				if (!String.IsNullOrEmpty(filenamepattern))
				{
					outpath = proc.VariablenEinfuegen(filenamepattern);
					proc.OpenOutstream(outpath);
				}

                foreach (string line in texstuff)
                {
                    proc.HandleLine(line);
                }

				if (!String.IsNullOrEmpty(outpath))
				{
					proc.CloseOutstream();
					proc.LatexPostprocess(outpath, false);
				}
            }
        }
        

        public void ExecuteIf(List<string> texstuff, System.Data.Common.DbConnection connection,
            System.Collections.Generic.Dictionary<string, SqlQueryVar> varvalues, Processer proc)
        {
            using (System.Data.Common.DbCommand comm = connection.CreateCommand())
            {
                ReplaceVariables(comm, varvalues);

                using (System.Data.Common.DbDataReader rdr = comm.ExecuteReader())
                {
                    if (!rdr.HasRows)
                    {
                        rdr.Close();
                        return;
                    }
                    rdr.Close();
                }
            } 
            foreach (string line in texstuff)
            {
                proc.HandleLine(line);
            }
        }

        private string m_querytext;
        private List<SqlQueryVar> m_queryvars;
		private static char[] m_variablesplitter = { ',' };
    }
}