//C写MySQL连接程序所用的数据结构 struct harvis_mysql_struct { MYSQL *conn; //建立的MySql连接,全局变量 int id; //连接MySql的顺序号,从0开始,初始值为-1 unsigned int query_times; //一个MySql连接上执行的查询次数,初始值为0 struct harvis_mysql_struct *next, *prev;//指向下一个结构体 }; //max harvis_mysql number #define MAXSKILLMYSQL 16 //现在支持与MySql同时建立16个db的连接 static struct harvis_mysql_struct *harvis_mysql_entry[MAXSKILLMYSQL]; //a array of struct harvis_mysql_struct typed pointer static struct harvis_mysql_struct harvis_mysql_entry2[MAXSKILLMYSQL]; static struct harvis_mysql_struct *harvis_mysql_free_head = NULL; //未使用的struct harvis_mysql_struct单链的头指针 static struct harvis_mysql_struct *harvis_mysql_using_head = NULL; //使用着的struct harvis_mysql_struct单链的头指针 static int harvis_mysql_entry_id = -1;//序号,每使用一个entry它就加1,每放弃一个entry它就减1. /* * init_harvis_mysql_list -将数组中的元素连接成一个链表 * void: */ void init_harvis_mysql_list (void) { int i; for (i = 0; i< MAXSKILLMYSQL; i++) harvis_mysql_entry[i] = &harvis_mysql_entry2[i]; for (i = 0; i < MAXSKILLMYSQL - 1; i++) {//one's next point the next one harvis_mysql_entry[i]->next = harvis_mysql_entry[i + 1]; harvis_mysql_entry[i]->conn = NULL; harvis_mysql_entry[i]->id = -1; harvis_mysql_entry[i]->query_times = 0; } //对数组的最后一个元素进行初始化 harvis_mysql_entry[i]->next = NULL;//last element's next pointer harvis_mysql_entry[i]->conn = NULL; harvis_mysql_entry[i]->id = -1; harvis_mysql_entry[i]->query_times = 0; //对前置指针进行初始化 for (i = 1; i < MAXSKILLMYSQL; i++) harvis_mysql_entry[i]->prev = harvis_mysql_entry[i - 1]; harvis_mysql_entry[0]->prev = NULL; //对空闲列表的头指针进行赋值。 harvis_mysql_free_head = harvis_mysql_entry[0];//free point to the first element of the list } //从表头取一个struct harvis_mysql_struct类型的元素 struct harvis_mysql_struct *get_harvis_mysql_entry() { struct harvis_mysql_struct *entry; //检测是否还有空闲的entry if (harvis_mysql_free_head == NULL) return (struct harvis_mysql_struct *)0;//没有可用的entry了,直接返回空指针 entry = harvis_mysql_free_head; harvis_mysql_free_head = harvis_mysql_free_head->next;//move free_head to the next entry if (harvis_mysql_free_head != NULL) harvis_mysql_free_head->prev = NULL;//前置指针赋值为NULL //entry->next = NULL; //将entry添加到已用的列表中 if (harvis_mysql_using_head == NULL) { entry->next = NULL; harvis_mysql_using_head = entry; harvis_mysql_using_head->prev = NULL;//前置指针赋值为NULL } else { entry->next = harvis_mysql_using_head; harvis_mysql_using_head = entry; harvis_mysql_using_head->prev = NULL;//前置指针赋值为NULL } harvis_mysql_entry_id++; entry->conn = NULL; entry->id = harvis_mysql_entry_id; entry->query_times = 0; return (entry); } //从正在应用的链表中按序号查找 struct harvis_mysql_struct *search_harvis_mysql(int id) { struct harvis_mysql_struct *entry; if (id < 0 || id > MAXSKILLMYSQL - 1) return (struct harvis_mysql_struct *)0;//顺序号非法 if (harvis_mysql_using_head == NULL) return (struct harvis_mysql_struct *)0;//正在应用的链表为空 for (entry = harvis_mysql_using_head; entry != NULL; entry = entry->next) { if (entry->id == id) return entry;//找到了则返回此结构体的指针 } return (struct harvis_mysql_struct *)0;//没有查找的到,则返回空指针 } //将一个struct harvis_mysql_struct类型的元素(清空后)放到列表的头部 void free_harvis_mysql_entry(int i) { struct harvis_mysql_struct *entry; entry = search_harvis_mysql(i); if (entry != NULL) { if (entry->next != NULL && entry->prev != NULL) { entry->prev->next = entry->next; //脱链操作 entry->next->prev = entry->prev; //从正在使用的链表中脱去 } else { harvis_mysql_using_head = NULL; } harvis_mysql_entry_id--; //对entry进行清空,以备下次使用 entry->conn = NULL; entry->id = -1; entry->query_times = 0; entry->next = harvis_mysql_free_head;//添加到空闲的实体列表头部 entry->prev = NULL;//前置指针赋值为NULL harvis_mysql_free_head = entry; } } //往列表的头部添加一个元素 //struct harvis_mysql_struct *add_harvis_mysql_entry(struct harvis_mysql_struct *head, struct harvis_mysql_struct *entry) //{ //} //获取列表的尾部元素 struct harvis_mysql_struct *get_harvis_mysql_tail(struct harvis_mysql_struct *head) { struct harvis_mysql_struct *entry; for (entry = head; entry->next != NULL; entry = entry->next) ;//空语句,不能够省略掉 return (entry); } int harvis_mysql_length(struct harvis_mysql_struct *head) { int counter = 0; struct harvis_mysql_struct *entry; for (entry = head; entry != NULL; entry = entry->next) counter++; return counter; }