PostgreSQL Source Code: src/interfaces/ecpg/ecpglib/connect.c File Reference (original) (raw)

261{

265 int i,

266 connect_params = 0;

267 bool alloc_failed = (sqlca == NULL);

269 *host = NULL,

270 *tmp,

271 *port = NULL,

272 *realname = NULL,

274 const char **conn_keywords;

275 const char **conn_values;

276

277 if (alloc_failed)

278 {

283 return false;

284 }

285

287

288

289

290

291

293

295 {

296 char *envname;

297

298

299

300

301

302

303 envname = getenv("PG_DBPATH");

304 if (envname)

305 {

308 }

309 }

310

311 if (dbname == NULL && connection_name == NULL)

312 connection_name = "DEFAULT";

313

315

316

318 {

320 ecpg_log("ECPGconnect: connection identifier %s is already in use\n",

321 connection_name);

322 return false;

323 }

324

326 {

328 return false;

329 }

330

332 {

333

334 if (strncmp(dbname, "tcp:", 4) == 0 || strncmp(dbname, "unix:", 5) == 0)

335 {

336 int offset = 0;

337

338

339

340

341 if (strncmp(dbname, "tcp:", 4) == 0)

342 offset = 4;

343 else if (strncmp(dbname, "unix:", 5) == 0)

344 offset = 5;

345

346 if (strncmp(dbname + offset, "postgresql://", strlen("postgresql://")) == 0)

347 {

348

349

350

351

352

353

354 offset += strlen("postgresql://");

355

356 tmp = strrchr(dbname + offset, '?');

357 if (tmp != NULL)

358 {

360 *tmp = '\0';

361 }

362

364 if (tmp != NULL)

365 {

366 if (tmp[1] != '\0')

367 {

368 realname = ecpg_strdup(tmp + 1, lineno, &alloc_failed);

369 connect_params++;

370 }

371 *tmp = '\0';

372 }

373

374 tmp = strrchr(dbname + offset, ':');

375 if (tmp != NULL)

376 {

377 *tmp = '\0';

379 connect_params++;

380 }

381

382 if (strncmp(dbname, "unix:", 5) == 0)

383 {

384

385

386

387

388

389 if (strcmp(dbname + offset, "localhost") != 0 &&

390 strcmp(dbname + offset, "127.0.0.1") != 0)

391 {

392 ecpg_log("ECPGconnect: non-localhost access via sockets on line %d\n", lineno);

394 if (host)

400 if (realname)

405 return false;

406 }

407 }

408 else

409 {

410 if (*(dbname + offset) != '\0')

411 {

413 connect_params++;

414 }

415 }

416 }

417 }

418 else

419 {

420

421 tmp = strrchr(dbname, ':');

422 if (tmp != NULL)

423 {

425 connect_params++;

426 *tmp = '\0';

427 }

428

429 tmp = strrchr(dbname, '@');

430 if (tmp != NULL)

431 {

432 host = ecpg_strdup(tmp + 1, lineno, &alloc_failed);

433 connect_params++;

434 *tmp = '\0';

435 }

436

437 if (strlen(dbname) > 0)

438 {

440 connect_params++;

441 }

442 else

443 realname = NULL;

444 }

445 }

446 else

447 realname = NULL;

448

449

450

451

452

456 connect_params++;

457

458 if (user && strlen(user) > 0)

459 connect_params++;

460 if (passwd && strlen(passwd) > 0)

461 connect_params++;

462

463

464

465

466

467

468 conn_keywords = (const char **) ecpg_alloc((connect_params + 1) * sizeof(char *), lineno);

469 conn_values = (const char **) ecpg_alloc(connect_params * sizeof(char *), lineno);

470

471

472 if (connection_name != NULL || realname != NULL)

473 {

474 this->name = ecpg_strdup(connection_name ? connection_name : realname,

475 lineno, &alloc_failed);

476 }

477 else

478 this->name = NULL;

479

480

481 if (conn_keywords == NULL || conn_values == NULL || alloc_failed)

482 {

483 if (host)

489 if (realname)

493 if (conn_keywords)

495 if (conn_values)

497 if (this->name)

500 return false;

501 }

502

503

505

506

507

508

509

510#ifdef HAVE_USELOCALE

511 if (!ecpg_clocale)

512 {

513 ecpg_clocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);

514 if (!ecpg_clocale)

515 {

519 if (host)

525 if (realname)

529 if (conn_keywords)

531 if (conn_values)

533 if (this->name)

536 return false;

537 }

538 }

539#endif

540

543

545 this->next = NULL;

546 else

548

552

553 ecpg_log("ECPGconnect: opening database %s on %s port %s %s%s %s%s\n",

554 realname ? realname : "",

555 host ? host : "",

558 (user && strlen(user) > 0) ? "for user " : "", user ? user : "");

559

560 i = 0;

561 if (realname)

562 {

563 conn_keywords[i] = "dbname";

564 conn_values[i] = realname;

565 i++;

566 }

567 if (host)

568 {

569 conn_keywords[i] = "host";

570 conn_values[i] = host;

571 i++;

572 }

574 {

575 conn_keywords[i] = "port";

576 conn_values[i] = port;

577 i++;

578 }

579 if (user && strlen(user) > 0)

580 {

581 conn_keywords[i] = "user";

582 conn_values[i] = user;

583 i++;

584 }

585 if (passwd && strlen(passwd) > 0)

586 {

587 conn_keywords[i] = "password";

588 conn_values[i] = passwd;

589 i++;

590 }

592 {

593 char *str;

594

595

596

597

598

599

600

601

602

603

605 {

606 int e,

607 a;

608 char *token1,

609 *token2;

610

611

612 for (token1 = str; *token1 == ' '; token1++)

613 ;

614

615 for (e = 0; token1[e] && token1[e] != '='; e++)

616 ;

617 if (token1[e])

618 {

619 token1[e] = '\0';

620

621 for (token2 = token1 + e + 1; *token2 == ' '; token2++)

622 ;

623

624 for (a = 0; token2[a] && token2[a] != '&'; a++)

625 ;

626 if (token2[a])

627 {

628 token2[a] = '\0';

629 str = token2 + a + 1;

630 }

631 else

632 str = token2 + a;

633

634 conn_keywords[i] = token1;

635 conn_values[i] = token2;

636 i++;

637 }

638 else

639 {

640

641 str = token1 + e;

642 }

643 }

644 }

645

646 Assert(i <= connect_params);

647 conn_keywords[i] = NULL;

648

650

651 if (host)

661

663 {

665 const char *db = realname ? realname : ecpg_gettext("");

666

667

669

672

674 if (realname)

676

677 return false;

678 }

679

680 if (realname)

682

684

686

688

689 return true;

690}

static void ecpg_finish(struct connection *act)

static void ECPGnoticeReceiver(void *arg, const PGresult *result)

struct connection * ecpg_get_connection(const char *connection_name)

#define ECPG_OUT_OF_MEMORY

#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY

char * ecpg_strdup(const char *string, int lineno, bool *alloc_failed)

char * ecpg_alloc(long size, int lineno)

void ecpg_clear_auto_mem(void)

void ecpg_init_sqlca(struct sqlca_t *sqlca)

#define ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION

void ecpg_raise(int line, int code, const char *sqlstate, const char *str)

bool ecpg_internal_regression_mode

int errmsg(const char *fmt,...)

ConnStatusType PQstatus(const PGconn *conn)

PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn, PQnoticeReceiver proc, void *arg)

char * PQerrorMessage(const PGconn *conn)

PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)

Assert(PointerIsAligned(start, uint64))

struct sqlca_t * ECPGget_sqlca(void)

char * last_dir_separator(const char *filename)

struct prepared_statement * prep_stmts