| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  | /*************************************************************************
 | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | *	 YAP Prolog 							 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | *	Yap Prolog was developed at NCCUP - Universidade do Porto	 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997	 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | ************************************************************************** | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | * File:		myddas_mysql.c						 * | 
					
						
							|  |  |  | * Last rev:	22/03/05						 * | 
					
						
							|  |  |  | * mods:									 * | 
					
						
							|  |  |  | * comments:	Predicates for comunicating with a mysql database system * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | *************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  | #include "Yap.h"
 | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2016-01-04 14:11:09 +00:00
										 |  |  | #include <mysql.h>
 | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  | #include <myddas_util.h>
 | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  | #ifdef MYDDAS_MYSQL
 | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  | /* Auxilary function to table_write*/ | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  | static void n_print(Int, char); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Auxilary function to table_write*/ | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  | static void n_print(Int n, char c) { | 
					
						
							|  |  |  |   for (; n > 0; n--) | 
					
						
							|  |  |  |     printf("%c", c); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  | void myddas_util_table_write(MYSQL_RES *res_set) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  |   MYSQL_ROW row; | 
					
						
							|  |  |  |   MYSQL_FIELD *fields; | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Int i, f; | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   if (mysql_num_rows(res_set) == 0) { | 
					
						
							|  |  |  |     printf("Empty Set\n"); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   f = mysql_num_fields(res_set); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fields = mysql_fetch_field(res_set); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   for (i = 0; i < f; i++) { | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  |     printf("+"); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     if (strlen(fields[i].name) > fields[i].max_length) | 
					
						
							|  |  |  |       fields[i].max_length = strlen(fields[i].name); | 
					
						
							|  |  |  |     n_print(fields[i].max_length + 2, '-'); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   printf("+\n"); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (i = 0; i < f; i++) { | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  |     printf("|"); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     printf(" %s ", fields[i].name); | 
					
						
							|  |  |  |     n_print(fields[i].max_length - strlen(fields[i].name), ' '); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  |   printf("|\n"); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (i = 0; i < f; i++) { | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  |     printf("+"); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     n_print(fields[i].max_length + 2, '-'); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   printf("+\n"); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   while ((row = mysql_fetch_row(res_set)) != NULL) { | 
					
						
							|  |  |  |     for (i = 0; i < f; i++) { | 
					
						
							|  |  |  |       printf("|"); | 
					
						
							|  |  |  |       if (row[i] != NULL) { | 
					
						
							|  |  |  |         printf(" %s ", row[i]); | 
					
						
							|  |  |  |         n_print(fields[i].max_length - strlen(row[i]), ' '); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         printf(" NULL "); | 
					
						
							|  |  |  |         n_print(fields[i].max_length - 4, ' '); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     printf("|\n"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   for (i = 0; i < f; i++) { | 
					
						
							|  |  |  |     printf("+"); | 
					
						
							|  |  |  |     n_print(fields[i].max_length + 2, '-'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   printf("+\n"); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:06:15 +00:00
										 |  |  | } |