1 module gdaDemo.TestGda;
2 
3 import gdac.gdatypes;
4 import gda.Gda;
5 import gda.Client;
6 import gda.Config;
7 import gda.Connection;
8 import gda.Command;
9 import gda.DataModel;
10 import gda.DataSourceInfo;
11 import gda.ProviderInfo;
12 import gda.Value;
13 import gda.ErrorGda;
14 import glib.ListG;
15 
16 version(Tango)
17 {
18 	import tango.io.Stdout;
19 	import tango.text.Util : substitute;
20 	import tango.stdc.stdlib : exit;
21 
22     void writef( string frm, ... ){
23         string frm2 = substitute( frm, "%s", "{}" );
24         Stdout( Stdout.layout.convert( _arguments, _argptr, frm2 ))();
25     }
26 
27     void writefln( string frm, ... ){
28         string frm2 = substitute( frm, "%s", "{}" );
29         Stdout( Stdout.layout.convert( _arguments, _argptr, frm2 )).newline;
30     }
31 }
32 else
33 {
34 	import std.stdio;
35 	import std.c.process;
36 }
37 
38 void main (string[] args)
39 {
40 	Gda.init("TestGDA", "0.1", args);
41 	save_ds();
42 	do_stuff();
43 }   
44 
45 void save_ds ()
46 {
47 	Config.saveDataSource("calvaris", "MySQL", "DATABASE=calvaris", "cosa de calvaris", null, null);
48 }
49 
50 void do_stuff ()
51 {
52 	list_providers ();
53 	list_datasources ();
54 
55 	Client client = new Client();
56 	Connection connection = client.openConnection("calvaris", null, null, GdaConnectionOptions.READ_ONLY);
57 	if (!connection) {
58 		writefln("Connection failed!");
59 		exit(1);
60 	}
61 	if (!connection.isOpen()) {
62 		writefln("no connection?");
63 		get_errors(connection);
64 		exit(1);
65 	}
66 	writefln("about to do some queries");
67 	execute_some_queries (connection);
68 
69 	//process_accounts(connection);
70 	client.closeAllConnections();
71 
72 	//gda_main_quit();
73 }
74 
75 void execute_some_queries (Connection  connection)
76 {
77 	execute_sql_non_query (connection, "DELETE FROM cliente");
78 	execute_sql_non_query (connection,
79 				 "INSERT INTO cliente(cd_cli, dni, nombr, direc, telef) "
80 				 "VALUES ('1', '1234', 'Xabier',"
81 				 "'Rua Unha calquera', '123')"
82 				 "; INSERT INTO cliente(cd_cli, dni, nombr, direc, telef) "
83 				 "VALUES ('2', '2345', 'Rodriguez',"
84 				 "'Rua Outra calquera', '234')");
85 	execute_sql_non_query (connection,
86 				 "INSERT INTO cliente(cd_cli, dni, nombr, direc, telef) "
87 				 "VALUES ('1', '1234', 'Xabier',"
88 				 "'Rua Unha calquera', '123')"
89 				 "; INSERT INTO cliente(cd_cli, dni, nombr, direc, telef) "
90 				 "VALUES ('2', '2345', 'Rodriguez',"
91 				 "'Rua Outra calquera', '234')");
92 	execute_sql_command (connection, "SELECT * FROM cliente");
93 
94 
95 	execute_sql_non_query (connection,
96 				 "DELETE FROM accounts;"
97 				 "INSERT INTO accounts"
98 				 "(client_code, account_code, balance)"
99 				 "VALUES (123, 456, 1000);"
100 				 "INSERT INTO accounts"
101 				 "(client_code, account_code, balance)"
102 				 "VALUES (789, 012, 5000);");
103 
104 	execute_sql_command (connection, "SELECT * FROM Client");
105 } 
106 
107 void list_datasources ()
108 {
109 	ListG ds_list = Config.getDataSourceList();
110 
111 	writef("\n");
112 
113 	for (int n=0;n<ds_list.length();n++)
114 	{
115 		ListG node = ds_list.nth(n);
116 
117 		DataSourceInfo info = new DataSourceInfo(node);
118 
119 		writefln("NAME: %s PROVIDER: %s CNC: %s DESC: %s USER: %s PASSWORD: %s\n",
120 			info.name, info.provider, info.cncString, info.description,
121 			info.username, info.password);
122 	}
123 
124 	//gda_config_free_data_source_list (ds_list);
125 }
126 
127 void list_providers ()
128 {
129 	ListG prov_list = Config.getProviderList();
130 
131 	for (int n =0; n < prov_list.length(); n++)
132 	{
133 		ListG node = prov_list.nth(n);
134 		 
135 		ProviderInfo info = new ProviderInfo(node);
136 
137 		writefln("ID: %s", info.id);
138 	}
139 
140 	//gda_config_free_provider_list (prov_list);
141 }
142 
143 int execute_sql_non_query (Connection connection, string buffer) 
144 {
145 	writefln("execute_sql_non_query %s", buffer);
146 	int number;
147 	auto command = new Command(buffer, GdaCommandType.TYPE_SQL, GdaCommandOptions.STOP_ON_ERRORS);
148 
149 	number  = connection.executeNonQuery(command, null);
150 
151 	return number;
152 } 
153 
154 bool execute_sql_command (Connection connection, string buffer)
155 {
156 	writefln("execute_sql_command %s", buffer); 
157 	bool errors=false;
158 
159 	auto command = new Command(buffer, GdaCommandType.TYPE_SQL, GdaCommandOptions.STOP_ON_ERRORS);
160 	ListG list = connection.executeCommand(command, null);
161 
162 	if (list is null)
163 	{
164 		writefln("no data");
165 		return true;
166 	}
167 
168 	writefln("got %d datamodels", list.length());
169 	get_errors(connection);
170 	for (int n=0; n<list.length();n++)
171 	{
172 		// one for the wrapper?
173 
174 		DataModel dm= new DataModel( list.nth(n));
175 
176 		if (dm is null)
177 		{
178 			errors=true;
179 			continue;
180 		}
181 			  
182 		show_table (dm);
183 		//g_object_unref(dm);
184 	}
185 
186 	//gda_command_free (command);
187 
188 	return errors;
189 }
190 
191 void show_table (DataModel dm)
192 {
193 	int row_id;
194 	int column_id;
195 
196 	for (column_id = 0; column_id < dm.getNColumns(); column_id++)
197 	{
198 		writef("%s\t", dm.getColumnTitle(column_id));
199 	}
200  
201 	writef("\n");
202 
203 	for (row_id = 0; row_id < dm.getNRows(); row_id++)
204 	{
205 		for (column_id = 0; column_id < dm.getNColumns(); column_id++)
206 		{
207 			Value value = dm.getValueAt(column_id, row_id);
208 			writefln("%s\t", value.stringify());
209 		}
210 		writef("\n");
211 	}
212 }
213 
214 bool get_errors (Connection connection) 
215 {
216 	ListG list = connection.getErrors();
217 	bool ret=false;
218 
219 	for (int n = 0; n<list.length(); n++)
220 	{
221 		ListG  node  = list.nth(n);
222 		
223 		ErrorGda error = new ErrorGda(node);
224 			 
225 		writefln("Error no: %d\tdesc: %s\t source: %s\tsqlstate: %s  ", 
226 				error.getNumber(),
227 				error.getDescription(),
228 				error.getSource(),
229 				error.getSqlstate());
230 	}
231 
232 	ret = true;
233          
234 	return ret;
235 }