File indexing completed on 2024-09-29 09:15:49

0001 /*
0002     SPDX-FileCopyrightText: 2008 Akarsh Simha <akarshsimha@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // Program to convert star data stored in a MySQL database into
0008 // the binary file format used by KStars
0009 
0010 // This version generates several split up data files with no headers
0011 // The headers / index table must be generated separately and the files
0012 // must be concatenated appropriately to generate the data file.
0013 
0014 #define DB_TBL                "nomad"
0015 #define DB_DB                 "stardb"
0016 #define VERBOSE               0
0017 #define HTM_LEVEL             6
0018 #define NTRIXELS              32768 // TODO: Change if HTM Level Changes
0019 #define INDEX_ENTRY_SIZE      12
0020 #define MYSQL_STARS_PER_QUERY 1000000
0021 
0022 #include "binfile.h"
0023 
0024 #include <mysql/mysql.h>
0025 #include <sys/types.h>
0026 #include <stdlib.h>
0027 #include <stdio.h>
0028 #include <math.h>
0029 #include <string.h>
0030 
0031 /*
0032  * struct to store star data, to be written in this format, into the binary file.
0033  */
0034 
0035 typedef struct deepStarData
0036 {
0037     int32_t RA;
0038     int32_t Dec;
0039     int16_t dRA;
0040     int16_t dDec;
0041     int16_t B;
0042     int16_t V;
0043 } deepStarData;
0044 
0045 int main(int argc, char *argv[])
0046 {
0047     /* === Declarations === */
0048 
0049     int ret, i;
0050 
0051     double Mag;
0052 
0053     u_int8_t htm_level;
0054     u_int16_t MSpT_unnamed;
0055 
0056     u_int32_t trixel_number;
0057 
0058     char query[512];
0059 
0060     /* File streams */
0061     FILE *f;   /* Pointer to "current" file */
0062     FILE *usf; /* Handle to star data file */
0063 
0064     /* deepStarData structure */
0065     deepStarData data;
0066 
0067     /* MySQL structures */
0068     MYSQL link;
0069     MYSQL_RES *result;
0070     MYSQL_ROW row;
0071 
0072     /* Check the number of arguments */
0073     if (argc <= 6)
0074     {
0075         fprintf(stderr, "USAGE %s DBUserName DBPassword DeepStarDataFile TrixelNumber DBName TableName\n", argv[0]);
0076         fprintf(stderr, "The database used is a MySQL DB on localhost. The default table name is `nomad`\n");
0077     }
0078 
0079     /* == Open all file streams required == */
0080     /* Unnamed Star Handling */
0081     usf = fopen(argv[3], "wb");
0082     if (usf == NULL)
0083     {
0084         fprintf(stderr, "ERROR: Could not open %s [Deep Star Data File] for binary write.\n", argv[3]);
0085         return 1;
0086     }
0087 
0088     trixel_number = atoi(argv[4]);
0089 
0090     /* MySQL connect */
0091     if (mysql_init(&link) == NULL)
0092     {
0093         fprintf(stderr, "ERROR: Failed to initialize MySQL connection!\n");
0094         return 1;
0095     }
0096 
0097     ret = mysql_real_connect(&link, "localhost", argv[1], argv[2], argv[5], 0, NULL, 0);
0098 
0099     if (!ret)
0100     {
0101         fprintf(stderr, "ERROR: MySQL connect failed for the following reason: %s\n", mysql_error(&link));
0102         fcloseall();
0103         return 1;
0104     }
0105 
0106     if (mysql_select_db(&link, argv[5]))
0107     {
0108         fprintf(stderr, "ERROR: Could not select MySQL database %s. MySQL said: %s", argv[5], mysql_error(&link));
0109         fcloseall();
0110         mysql_close(&link);
0111         return 1;
0112     }
0113 
0114     if (VERBOSE)
0115         fprintf(stdout, "Size of deepStarData structure: %d\n", sizeof(deepStarData));
0116 
0117     /* Build MySQL query for stars of the trixel */
0118     sprintf(query,
0119             "SELECT `Trixel`, `RA`, `Dec`, `dRA`, `dDec`, `B`, `V`, `UID` FROM `%s` WHERE `Trixel`=\'%d\' ORDER BY "
0120             "`Mag` ASC",
0121             (argc >= 7) ? argv[6] : DB_TBL, trixel_number);
0122 
0123     if (VERBOSE)
0124     {
0125         fprintf(stderr, "SQL Query: %s\n", query);
0126     }
0127 
0128     /* Execute MySQL query and get the result */
0129     mysql_real_query(&link, query, (unsigned int)strlen(query));
0130     result = mysql_use_result(&link);
0131 
0132     if (!result)
0133     {
0134         fprintf(stderr, "MySQL returned NULL result: %s\n", mysql_error(&link));
0135         fcloseall();
0136         mysql_close(&link);
0137         return 1;
0138     }
0139 
0140     /* Process the result row by row */
0141     while (row = mysql_fetch_row(result))
0142     {
0143         /* Very verbose details */
0144         if (VERBOSE > 1)
0145         {
0146             fprintf(stderr, "UID = %s\n", row[12]);
0147             for (i = 0; i <= 14; ++i)
0148                 fprintf(stderr, "\tField #%d = %s\n", i, row[i]);
0149         }
0150 
0151         f = usf;
0152 
0153         /* Convert various fields and make entries into the deepStarData structure */
0154         str2int32(&data.RA, row[1], 6);
0155         str2int32(&data.Dec, row[2], 5);
0156         str2int16(&data.dRA, row[3], 3);
0157         str2int16(&data.dDec, row[4], 3);
0158         str2int16(&data.B, row[5], 3);
0159         str2int16(&data.V, row[6], 3);
0160 
0161         /* Write the data into the appropriate data file and any names into the name file */
0162         if (VERBOSE)
0163             fprintf(stderr, "Writing UID = %s...", row[7]);
0164         fwrite(&data, sizeof(deepStarData), 1, f);
0165         if (VERBOSE)
0166             fprintf(stderr, "Done.\n");
0167     }
0168     mysql_free_result(result);
0169 
0170     fcloseall();
0171     mysql_close(&link);
0172 
0173     return 0;
0174 }