PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ main()

int main ( int  argc,
char **  argv 
)

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

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