PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 59 of file pgsql2shp-cli.c.

60{
61 SHPDUMPERCONFIG *config;
62 SHPDUMPERSTATE *state;
63
64 int ret, c, i;
65
66 /* If no options are specified, display usage */
67 if (argc == 1)
68 {
69 usage(0); /* TODO: should this exit with error ? */
70 }
71
72 /* Parse command line options and set configuration */
73 config = malloc(sizeof(SHPDUMPERCONFIG));
75
76 while ((c = pgis_getopt(argc, argv, "bf:h:du:p:P:g:rkm:q")) != EOF)
77 {
78 switch (c)
79 {
80 case 'b':
81 config->binary = 1;
82 break;
83 case 'f':
84 config->shp_file = pgis_optarg;
85 break;
86 case 'h':
87 config->conn->host = pgis_optarg;
88 break;
89 case 'd':
90 config->dswitchprovided = 1;
91 break;
92 case 'r':
93 config->includegid = 1;
94 config->unescapedattrs = 1;
95 break;
96 case 'u':
97 config->conn->username = pgis_optarg;
98 break;
99 case 'p':
100 config->conn->port = pgis_optarg;
101 break;
102 case 'P':
103 config->conn->password = pgis_optarg;
104 break;
105 case 'g':
106 config->geo_col_name = pgis_optarg;
107 break;
108 case 'm':
110 break;
111 case 'k':
112 config->keep_fieldname_case = 1;
113 break;
114 case 'q':
115 config->quiet = 1;
116 break;
117 default:
118 usage(pgis_optopt == '?' ? 0 : 1);
119 }
120 }
121
122
123 /* Determine the database name from the next argument, if no database, exit. */
124 if (pgis_optind < argc)
125 {
126 config->conn->database = argv[pgis_optind];
127 pgis_optind++;
128 }
129 else
130 {
131 usage(1);
132 }
133
134
135 /* Determine the table and schema names from the next argument if supplied, otherwise if
136 it's a user-defined query then set that instead */
137 if (pgis_optind < argc)
138 {
139 /* User-defined queries begin with SELECT or WITH */
140 if ( !strncmp(argv[pgis_optind], "SELECT ", 7) ||
141 !strncmp(argv[pgis_optind], "select ", 7) ||
142 !strncmp(argv[pgis_optind], "WITH ", 5) ||
143 !strncmp(argv[pgis_optind], "with ", 5)
144 )
145 {
146 config->usrquery = argv[pgis_optind];
147 }
148 else
149 {
150 /* Schema qualified table name */
151 char *strptr = argv[pgis_optind];
152 char *chrptr = strchr(strptr, '.');
153
154 /* OK, this is a schema-qualified table name... */
155 if (chrptr)
156 {
157 if ( chrptr == strptr )
158 {
159 /* table is ".something" display help */
160 usage(0);
161 exit(0);
162 }
163 /* Null terminate at the '.' */
164 *chrptr = '\0';
165 /* Copy in the parts */
166 config->schema = strdup(strptr);
167 config->table = strdup(chrptr+1);
168 }
169 else
170 {
171 config->table = strdup(strptr);
172 }
173 }
174 }
175 else
176 {
177 usage(1);
178 }
179
180 state = ShpDumperCreate(config);
181
182 ret = ShpDumperConnectDatabase(state);
183 if (ret != SHPDUMPEROK)
184 {
185 fprintf(stderr, "%s\n", state->message);
186 fflush(stderr);
187 exit(1);
188 }
189
190 /* Display a warning if the -d switch is used with PostGIS >= 1.0 */
191 if (state->pgis_major_version > 0 && state->config->dswitchprovided)
192 {
193 fprintf(stderr, _("WARNING: -d switch is useless when dumping from postgis-1.0.0+\n"));
194 fflush(stderr);
195 }
196
197 /* Open the table ready to return rows */
198 if (!state->config->quiet)
199 {
200 fprintf(stdout, _("Initializing... \n"));
201 fflush(stdout);
202 }
203
204 ret = ShpDumperOpenTable(state);
205 if (ret != SHPDUMPEROK)
206 {
207 fprintf(stderr, "%s\n", state->message);
208 fflush(stderr);
209
210 if (ret == SHPDUMPERERR)
211 exit(1);
212 }
213
214 if (!state->config->quiet)
215 {
216 fprintf(stdout, _("Done (postgis major version: %d).\n"), state->pgis_major_version);
217 fprintf(stdout, _("Output shape: %s\n"), shapetypename(state->outshptype));
218 fprintf(stdout, _("Dumping: "));
219 fflush(stdout);
220 }
221
222 for (i = 0; i < ShpDumperGetRecordCount(state); i++)
223 {
224 /* Mimic existing behaviour */
225 if (!state->config->quiet && !(state->currow % state->config->fetchsize))
226 {
227 fprintf(stdout, "X");
228 fflush(stdout);
229 }
230
231 ret = ShpLoaderGenerateShapeRow(state);
232 if (ret != SHPDUMPEROK)
233 {
234 fprintf(stderr, "%s\n", state->message);
235 fflush(stderr);
236
237 if (ret == SHPDUMPERERR)
238 exit(1);
239 }
240 }
241
242 if (!state->config->quiet)
243 {
244 fprintf(stdout, _(" [%d rows].\n"), ShpDumperGetRecordCount(state));
245 fflush(stdout);
246 }
247
248 ret = ShpDumperCloseTable(state);
249 if (ret != SHPDUMPEROK)
250 {
251 fprintf(stderr, "%s\n", state->message);
252 fflush(stderr);
253
254 if (ret == SHPDUMPERERR)
255 ret = 1;
256 else
257 ret = 0;
258 }
259 else
260 ret = 0;
261
262 ShpDumperDestroy(state);
263
264 free(config->conn);
265 free(config->table);
266 free(config->schema);
267 free(config);
268
269 return ret;
270}
int pgis_optind
Definition getopt.c:39
int pgis_optopt
Definition getopt.c:40
int pgis_getopt(int argc, char **argv, char *opts)
Definition getopt.c:44
char * pgis_optarg
Definition getopt.c:41
void * malloc(YYSIZE_T)
void free(void *)
void set_dumper_config_defaults(SHPDUMPERCONFIG *config)
int ShpDumperGetRecordCount(SHPDUMPERSTATE *state)
void ShpDumperDestroy(SHPDUMPERSTATE *state)
int ShpDumperConnectDatabase(SHPDUMPERSTATE *state)
char * shapetypename(int num)
int ShpLoaderGenerateShapeRow(SHPDUMPERSTATE *state)
SHPDUMPERSTATE * ShpDumperCreate(SHPDUMPERCONFIG *config)
int ShpDumperCloseTable(SHPDUMPERSTATE *state)
int ShpDumperOpenTable(SHPDUMPERSTATE *state)
#define SHPDUMPEROK
#define SHPDUMPERERR
static void usage()
#define _(String)
Definition shpcommon.h:24
SHPCONNECTIONCONFIG * conn
SHPDUMPERCONFIG * config
char message[SHPDUMPERMSGLEN]

References _, shp_dumper_config::binary, shp_dumper_config::column_map_filename, shp_dumper_state::config, shp_dumper_config::conn, shp_dumper_state::currow, shp_connection_state::database, shp_dumper_config::dswitchprovided, shp_dumper_config::fetchsize, free(), shp_dumper_config::geo_col_name, shp_connection_state::host, shp_dumper_config::includegid, shp_dumper_config::keep_fieldname_case, malloc(), shp_dumper_state::message, shp_dumper_state::outshptype, shp_connection_state::password, pgis_getopt(), shp_dumper_state::pgis_major_version, pgis_optarg, pgis_optind, pgis_optopt, shp_connection_state::port, shp_dumper_config::quiet, shp_dumper_config::schema, set_dumper_config_defaults(), shapetypename(), shp_dumper_config::shp_file, ShpDumperCloseTable(), ShpDumperConnectDatabase(), ShpDumperCreate(), ShpDumperDestroy(), SHPDUMPERERR, ShpDumperGetRecordCount(), SHPDUMPEROK, ShpDumperOpenTable(), ShpLoaderGenerateShapeRow(), shp_dumper_config::table, shp_dumper_config::unescapedattrs, usage(), shp_connection_state::username, and shp_dumper_config::usrquery.

Here is the call graph for this function: