⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dump.c

📁 这个是内存数据库的客户端
💻 C
📖 第 1 页 / 共 2 页
字号:
		pkeys[nkeys - 1] = c_pcolumn;		fkeys[nkeys - 1] = c_fcolumn;		while ((cnt = mapi_fetch_row(hdl)) != 0 && strcmp(mapi_fetch_field(hdl, 3), "0") != 0) {			nkeys++;			pkeys = realloc(pkeys, nkeys * sizeof(*pkeys));			fkeys = realloc(fkeys, nkeys * sizeof(*fkeys));			pkeys[nkeys - 1] = mapi_fetch_field(hdl, 1);			fkeys[nkeys - 1] = mapi_fetch_field(hdl, 2);		}		fprintf(toConsole, ",\n\t");		if (c_fkname) {			fprintf(toConsole, "CONSTRAINT ");			quoted_print(toConsole, c_fkname);			putc(' ', toConsole);		}		fprintf(toConsole, "FOREIGN KEY (");		for (i = 0; i < nkeys; i++) {			if (i > 0)				fprintf(toConsole, ", ");			quoted_print(toConsole, fkeys[i]);		}		fprintf(toConsole, ") REFERENCES ");		quoted_print(toConsole, c_name);		fprintf(toConsole, " (");		for (i = 0; i < nkeys; i++) {			if (i > 0)				fprintf(toConsole, ", ");			quoted_print(toConsole, pkeys[i]);		}		fprintf(toConsole, ")");		free(fkeys);		free(pkeys);	}	if (mapi_error(mid)) {		mapi_explain_query(hdl, stderr);		mapi_close_handle(hdl);		free(query);		return 1;	}	mapi_close_handle(hdl);	fprintf(toConsole, "\n");	fprintf(toConsole, ");\n");	snprintf(query, maxquerylen,		 "SELECT \"i\".\"name\", "		/* 0 */			"\"k\".\"name\", "		/* 1 */			"\"kc\".\"nr\", "		/* 2 */			"\"c\".\"name\" "		/* 3 */		 "FROM \"idxs\" AS \"i\" LEFT JOIN \"keys\" AS \"k\" "				"ON \"i\".\"name\" = \"k\".\"name\", "		      "\"keycolumns\" AS \"kc\", "		      "\"columns\" AS \"c\", "		      "\"tables\" AS \"t\" "		 "WHERE \"i\".\"table_id\" = \"t\".\"id\" AND "		       "\"i\".\"id\" = \"kc\".\"id\" AND "		       "\"t\".\"id\" = \"c\".\"table_id\" AND "		       "\"kc\".\"column\" = \"c\".\"name\" AND "		       "(\"k\".\"type\" IS NULL OR \"k\".\"type\" = 1) AND "		       "\"t\".\"name\" = '%s' "		 "ORDER BY \"i\".\"name\", \"kc\".\"nr\"", tname);	if ((hdl = mapi_query(mid, query)) == NULL || mapi_error(mid)) {		if (hdl) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);		} else			mapi_explain(mid, stderr);		free(query);		return 1;	}	cnt = 0;	while (mapi_fetch_row(hdl) != 0) {		char *i_name = mapi_fetch_field(hdl, 0);		char *k_name = mapi_fetch_field(hdl, 1);		char *kc_nr = mapi_fetch_field(hdl, 2);		char *c_name = mapi_fetch_field(hdl, 3);		if (mapi_error(mid)) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);			free(query);			return 1;		}		if (k_name != NULL) {			/* unique key, already handled */			continue;		}		if (strcmp(kc_nr, "0") == 0) {			if (cnt)				fprintf(toConsole, ");\n");			fprintf(toConsole, "CREATE INDEX ");			quoted_print(toConsole, i_name);			fprintf(toConsole, " ON ");			quoted_print(toConsole, tname);			fprintf(toConsole, " (");			cnt = 1;		} else			fprintf(toConsole, ", ");		quoted_print(toConsole, c_name);	}	if (cnt)		fprintf(toConsole, ");\n");	if (mapi_error(mid)) {		mapi_explain_query(hdl, stderr);		mapi_close_handle(hdl);		free(query);		return 1;	}	mapi_close_handle(hdl);	snprintf(query, maxquerylen, "SELECT count(*) FROM \"%s\"", tname);	if ((hdl = mapi_query(mid, query)) == NULL || mapi_error(mid)) {		if (hdl) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);		} else			mapi_explain(mid, stderr);		free(query);		return 1;	}	if (mapi_fetch_row(hdl)) {		char *cntfld = mapi_fetch_field(hdl, 0);		fprintf(toConsole, "COPY %s RECORDS INTO ", cntfld);		quoted_print(toConsole, tname);		fprintf(toConsole, " FROM stdin USING DELIMITERS '\\t';\n");	}	mapi_close_handle(hdl);	snprintf(query, maxquerylen, "SELECT * FROM \"%s\"", tname);	if ((hdl = mapi_query(mid, query)) == NULL || mapi_error(mid)) {		if (hdl) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);		} else			mapi_explain(mid, stderr);		free(query);		return 1;	}	cnt = mapi_get_field_count(hdl);	string = malloc(sizeof(int) * cnt);	for (i = 0; i < cnt; i++) {		string[i] = 0;		if (strcmp(mapi_get_type(hdl, i), "char") == 0 || strcmp(mapi_get_type(hdl, i), "varchar") == 0) {			string[i] = 1;		}	}	while (mapi_fetch_row(hdl)) {		char *s;		for (i = 0; i < cnt; i++) {			s = mapi_fetch_field(hdl, i);			if (s == NULL)				fputs("NULL", toConsole);			else if (string[i]) {				/* write double-quoted string with				   certain characters escaped */				quoted_print(toConsole, s);			} else				fputs(s, toConsole);			if (i < cnt - 1)				putc('\t', toConsole);			else				putc('\n', toConsole);		}	}	free(string);	if (mapi_error(mid)) {		mapi_explain_query(hdl, stderr);		mapi_close_handle(hdl);		free(query);		return 1;	}	mapi_close_handle(hdl);	free(query);	return 0;}intdump_tables(Mapi mid, FILE *toConsole){	const char *start = "START TRANSACTION";	const char *end = "COMMIT";	const char *sequences1 = "SELECT \"name\" FROM \"sequences\"";	const char *sequences2 = "SELECT \"name\",get_value_for(\"name\"),\"minvalue\",\"maxvalue\",\"increment\",\"cycle\" FROM \"sequences\"";	const char *tables = "SELECT \"name\" FROM \"_tables\" WHERE "		"\"type\" = 0 AND \"system\" = FALSE";	const char *views = "SELECT \"name\",\"query\" FROM \"_tables\" WHERE "		"\"type\" = 1 AND \"system\" = FALSE";	const char *functions = "SELECT \"func\" FROM \"functions\" WHERE "		"\"sql\" = TRUE";	MapiHdl hdl;	int rc = 0;	/* start a transaction for the dump */	fprintf(toConsole, "START TRANSACTION;\n");	if ((hdl = mapi_query(mid, start)) == NULL || mapi_error(mid)) {		if (hdl) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);		} else			mapi_explain(mid, stderr);		return 1;	}	mapi_close_handle(hdl);	/* dump sequences, part 1 */	if ((hdl = mapi_query(mid, sequences1)) == NULL || mapi_error(mid)) {		if (hdl) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);		} else			mapi_explain(mid, stderr);		return 1;	}	while (mapi_fetch_row(hdl) != 0) {		char *name = mapi_fetch_field(hdl, 0);		fprintf(toConsole, "CREATE SEQUENCE \"%s\" AS INTEGER;\n", name);	}	if (mapi_error(mid)) {		mapi_explain_query(hdl, stderr);		mapi_close_handle(hdl);		return 1;	}	mapi_close_handle(hdl);	/* dump tables */	if ((hdl = mapi_query(mid, tables)) == NULL || mapi_error(mid)) {		if (hdl) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);		} else			mapi_explain(mid, stderr);		return 1;	}	while (mapi_fetch_row(hdl) != 0) {		char *tname = mapi_fetch_field(hdl, 0);		rc += dump_table(mid, tname, toConsole);	}	if (mapi_error(mid)) {		mapi_explain_query(hdl, stderr);		mapi_close_handle(hdl);		return 1;	}	mapi_close_handle(hdl);	/* dump sequences, part 2 */	if ((hdl = mapi_query(mid, sequences2)) == NULL || mapi_error(mid)) {		if (hdl) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);		} else			mapi_explain(mid, stderr);		return 1;	}	while (mapi_fetch_row(hdl) != 0) {		char *name = mapi_fetch_field(hdl, 0);		char *restart = mapi_fetch_field(hdl, 1);		char *minvalue = mapi_fetch_field(hdl, 2);		char *maxvalue = mapi_fetch_field(hdl, 3);		char *increment = mapi_fetch_field(hdl, 4);		char *cycle = mapi_fetch_field(hdl, 5);		fprintf(toConsole, "ALTER SEQUENCE \"%s\" RESTART WITH %s", name, restart);		if (strcmp(increment, "1") != 0)			fprintf(toConsole, " INCREMENT BY %s", increment);		if (strcmp(minvalue, "0") != 0)			fprintf(toConsole, " MINVALUE %s", minvalue);		if (strcmp(maxvalue, "0") != 0)			fprintf(toConsole, " MAXVALUE %s", maxvalue);		fprintf(toConsole, " %sCYCLE;\n", strcmp(cycle, "true") == 0 ? "" : "NO ");	}	if (mapi_error(mid)) {		mapi_explain_query(hdl, stderr);		mapi_close_handle(hdl);		return 1;	}	mapi_close_handle(hdl);	/* dump views */	if ((hdl = mapi_query(mid, views)) == NULL || mapi_error(mid)) {		if (hdl) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);		} else			mapi_explain(mid, stderr);		return 1;	}	while (mapi_fetch_row(hdl) != 0) {		char *vname = mapi_fetch_field(hdl, 0);		char *query = mapi_fetch_field(hdl, 1);		fprintf(toConsole, "CREATE VIEW ");		quoted_print(toConsole, vname);		fprintf(toConsole, " AS %s\n", query);	}	if (mapi_error(mid)) {		mapi_explain_query(hdl, stderr);		mapi_close_handle(hdl);		return 1;	}	mapi_close_handle(hdl);	/* dump functions */	if ((hdl = mapi_query(mid, functions)) == NULL || mapi_error(mid)) {		if (hdl) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);		} else			mapi_explain(mid, stderr);		return 1;	}	while (mapi_fetch_row(hdl) != 0) {		char *query = mapi_fetch_field(hdl, 0);		fprintf(toConsole, "%s\n", query);	}	if (mapi_error(mid)) {		mapi_explain_query(hdl, stderr);		mapi_close_handle(hdl);		return 1;	}	mapi_close_handle(hdl);	if ((hdl = mapi_query(mid, end)) == NULL || mapi_error(mid)) {		if (hdl) {			mapi_explain_query(hdl, stderr);			mapi_close_handle(hdl);		} else			mapi_explain(mid, stderr);		return 1;	}	mapi_close_handle(hdl);	/* finally commit the whole transaction */	fprintf(toConsole, "COMMIT;\n");	return rc;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -