bool write_rev_nr() { printf("+ writing sd2_revision_nr.h\n"); char rev_str[256]; sprintf(rev_str, "%04d", rev); std::string header = generateNrHeader(rev_str); char prefixed_file[MAX_PATH]; snprintf(prefixed_file, MAX_PATH, "%s%s", path_prefix, rev_nr_file); if (FILE* OutputFile = fopen(prefixed_file, "wb")) { fprintf(OutputFile, "%s", header.c_str()); fclose(OutputFile); // add the file to both indices, to be committed later snprintf(cmd, MAX_CMD, "git add %s", prefixed_file); system_switch_index(cmd); return true; } return false; }
bool generate_sql_makefile() { if (new_sql_updates.empty()) return true; // find all files in the update dir snprintf(cmd, MAX_CMD, "git show HEAD:%s", sql_update_dir); if ((cmd_pipe = popen(cmd, "r")) == NULL) return false; // skip first two lines if (!fgets(buffer, MAX_BUF, cmd_pipe)) { pclose(cmd_pipe); return false; } if (!fgets(buffer, MAX_BUF, cmd_pipe)) { pclose(cmd_pipe); return false; } char newname[MAX_PATH]; std::set<std::string> file_list; sql_update_info info; while (fgets(buffer, MAX_BUF, cmd_pipe)) { buffer[strlen(buffer) - 1] = '\0'; if (buffer[strlen(buffer) - 1] != '/' && strncmp(buffer, "Makefile.am", MAX_BUF) != 0) { if (new_sql_updates.find(buffer) != new_sql_updates.end()) { if (!get_sql_update_info(buffer, info)) return false; snprintf(newname, MAX_PATH, SQL_REV_PRINT "_%s%s%s.sql", rev, info.db, info.has_table ? "_" : "", info.table); file_list.insert(newname); } else file_list.insert(buffer); } } pclose(cmd_pipe); // write the makefile char file_name[MAX_PATH]; snprintf(file_name, MAX_PATH, "%s%s/Makefile.am", path_prefix, sql_update_dir); FILE* fout = fopen(file_name, "w"); if (!fout) { pclose(cmd_pipe); return false; } fprintf(fout, "# Copyright (C) 2005-2011 ScriptDev2 <http://www.scriptdev2.com/>\n" "#\n" "# This program is free software; you can redistribute it and/or modify\n" "# it under the terms of the GNU General Public License as published by\n" "# the Free Software Foundation; either version 2 of the License, or\n" "# (at your option) any later version.\n" "#\n" "# This program is distributed in the hope that it will be useful,\n" "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n" "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" "# GNU General Public License for more details.\n" "#\n" "# You should have received a copy of the GNU General Public License\n" "# along with this program; if not, write to the Free Software\n" "# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n" "\n" "## Process this file with automake to produce Makefile.in\n" "\n" "## Sub-directories to parse\n" "\n" "## Change installation location\n" "# datadir = ScriptDev2/%s\n" "pkgdatadir = $(datadir)/ScriptDev2/%s\n" "\n" "## Files to be installed\n" "# Install basic SQL files to datadir\n" "pkgdata_DATA = \\\n", sql_update_dir, sql_update_dir ); for (std::set<std::string>::iterator itr = file_list.begin(), next; itr != file_list.end(); ++itr) { next = itr; ++next; fprintf(fout, "\t%s%s\n", itr->c_str(), next == file_list.end() ? "" : " \\"); } fclose(fout); snprintf(cmd, MAX_CMD, "git add %s%s/Makefile.am", path_prefix, sql_update_dir); system_switch_index(cmd); return true; }
bool convert_sql_updates() { if (new_sql_updates.empty()) return true; printf("+ converting sql updates\n"); // rename the sql update files and add the required update statement for (std::set<std::string>::iterator itr = new_sql_updates.begin(); itr != new_sql_updates.end(); ++itr) { sql_update_info info; if (!get_sql_update_info(itr->c_str(), info)) return false; if (info.db_idx == NUM_DATABASES) return false; // generating the new name should work for updates with or without a rev char src_file[MAX_PATH], new_name[MAX_PATH], new_req_name[MAX_PATH], dst_file[MAX_PATH]; snprintf(src_file, MAX_PATH, "%s%s/%s", path_prefix, sql_update_dir, itr->c_str()); snprintf(new_name, MAX_PATH, SQL_REV_PRINT "_%s%s%s", rev, info.db, info.has_table ? "_" : "", info.table); snprintf(dst_file, MAX_PATH, "%s%s/%s.sql", path_prefix, sql_update_dir, new_name); if (db_sql_rev_parent[info.db_idx]) snprintf(new_req_name, MAX_PATH, "_%s%s%s", info.db, info.has_table ? "_" : "", info.table); else strncpy(new_req_name, new_name, MAX_PATH); FILE* fin = fopen(src_file, "r"); if (!fin) return false; std::ostringstream out_buff; // add the update requirements for non-parent-controlled revision sql update if (!db_sql_rev_parent[info.db_idx]) { // add the update requirements out_buff << "ALTER TABLE " << db_version_table[info.db_idx] << " CHANGE COLUMN required_" << last_sql_update[info.db_idx] << " required_" << new_name << " bit;\n\n"; // skip the first one or two lines from the input // if it already contains update requirements if (fgets(buffer, MAX_BUF, fin)) { char dummy[MAX_BUF]; if (sscanf(buffer, "ALTER TABLE %s CHANGE COLUMN required_%s required_%s bit", dummy, dummy, dummy) == 3) { if (fgets(buffer, MAX_BUF, fin) && buffer[0] != '\n') out_buff << buffer; } else out_buff << buffer; } } // copy the rest of the file while (fgets(buffer, MAX_BUF, fin)) out_buff << buffer; fclose(fin); FILE* fout = fopen(dst_file, "w"); if (!fout) { fclose(fin); return false; } fprintf(fout, "%s", out_buff.str().c_str()); fclose(fout); // rename the file in git snprintf(cmd, MAX_CMD, "git add %s", dst_file); system_switch_index(cmd); // delete src file if it different by name from dst file if (strncmp(src_file, dst_file, MAX_PATH)) { snprintf(cmd, MAX_CMD, "git rm --quiet %s", src_file); system_switch_index(cmd); } // update the last sql update for the current database strncpy(last_sql_update[info.db_idx], new_req_name, MAX_PATH); } return true; }
bool convert_sql_updates() { if(new_sql_updates.empty()) return true; printf("+ converting sql updates\n"); // rename the sql update files and add the required update statement for(std::set<std::string>::iterator itr = new_sql_updates.begin(); itr != new_sql_updates.end(); ++itr) { sql_update_info info; if(!get_sql_update_info(itr->c_str(), info)) return false; if(info.db_idx == NUM_DATABASES) return false; // generating the new name should work for updates with or without a rev char src_file[MAX_PATH], new_name[MAX_PATH], dst_file[MAX_PATH]; snprintf(src_file, MAX_PATH, "%s%s/%s", path_prefix, sql_update_dir, itr->c_str()); snprintf(new_name, MAX_PATH, "%d_%0*d_%s%s%s", rev, 2, info.nr, info.db, info.has_table ? "_" : "", info.table); snprintf(dst_file, MAX_PATH, "%s%s/%s.sql", path_prefix, sql_update_dir, new_name); FILE * fin = fopen( src_file, "r" ); if(!fin) return false; FILE * fout = fopen( dst_file, "w" ); if(!fout) { fclose(fin); return false; } // add the update requirements fprintf(fout, "ALTER TABLE %s CHANGE COLUMN required_%s required_%s bit;\n\n", db_version_table[info.db_idx], last_sql_update[info.db_idx], new_name); // skip the first one or two lines from the input // if it already contains update requirements if(fgets(buffer, MAX_BUF, fin)) { char dummy[MAX_BUF]; if(sscanf(buffer, "ALTER TABLE %s CHANGE COLUMN required_%s required_%s bit", dummy, dummy, dummy) == 3) { if(fgets(buffer, MAX_BUF, fin) && buffer[0] != '\n') fputs(buffer, fout); } else fputs(buffer, fout); } // copy the rest of the file char c; while( (c = getc(fin)) != EOF ) putc(c, fout); fclose(fin); fclose(fout); // rename the file in git snprintf(cmd, MAX_CMD, "git add %s", dst_file); system_switch_index(cmd); snprintf(cmd, MAX_CMD, "git rm --quiet %s", src_file); system_switch_index(cmd); // update the last sql update for the current database strncpy(last_sql_update[info.db_idx], new_name, MAX_PATH); } return true; }