GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
c_create_idx.c
Go to the documentation of this file.
1 /*!
2  * \file db/dbmi_client/c_create_idx.c
3  *
4  * \brief DBMI Library (client) - create index
5  *
6  * (C) 1999-2008 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public
9  * License (>=v2). Read the file COPYING that comes with GRASS
10  * for details.
11  *
12  * \author Joel Jones (CERL/UIUC), Radim Blazek
13  */
14 
15 #include <string.h>
16 #include <grass/dbmi.h>
17 #include "macros.h"
18 
19 /*!
20  \brief Create index
21 
22  \param driver db driver
23  \param index index info (pointer to dbIndex structure)
24 
25  \return DB_OK on success
26  \return DB_FAILED on failure
27  */
29 {
30  int ret_code;
31 
32  /* start the procedure call */
33  db__set_protocol_fds(driver->send, driver->recv);
35 
36  /* send the arguments to the procedure */
37  DB_SEND_INDEX(index);
38 
39  /* get the return code for the procedure call */
40  DB_RECV_RETURN_CODE(&ret_code);
41 
42  if (ret_code != DB_OK)
43  return ret_code; /* ret_code SHOULD == DB_FAILED */
44 
45  /* get results */
46  DB_RECV_STRING(&index->indexName);
47 
48  return DB_OK;
49 }
50 
51 /*!
52  \brief Create unique index
53 
54  \param driver db driver
55  \param table_name table name
56  \param column_name column name (where to create index)
57 
58  \return DB_OK on success
59  \return DB_FAILED on failure
60  */
61 int db_create_index2(dbDriver *driver, const char *table_name,
62  const char *column_name)
63 {
64  int ret;
65  dbIndex index;
66  char buf[1000];
67  const char *tbl;
68 
69  db_init_index(&index);
70  db_alloc_index_columns(&index, 1);
71 
72  tbl = strchr(table_name, '.');
73  if (tbl == NULL)
74  tbl = table_name;
75  else
76  tbl++;
77 
78  sprintf(buf, "%s_%s", tbl, column_name);
79  db_set_index_name(&index, buf);
80 
81  db_set_index_table_name(&index, table_name);
82  db_set_index_column_name(&index, 0, column_name);
84 
85  ret = db_create_index(driver, &index);
86 
87  db_free_index(&index);
88 
89  return ret;
90 }
int db_create_index2(dbDriver *driver, const char *table_name, const char *column_name)
Create unique index.
Definition: c_create_idx.c:61
int db_create_index(dbDriver *driver, dbIndex *index)
Create index.
Definition: c_create_idx.c:28
#define NULL
Definition: ccmath.h:32
#define DB_PROC_CREATE_INDEX
Definition: dbmi.h:61
#define DB_OK
Definition: dbmi.h:71
int db_set_index_name(dbIndex *, const char *)
Set index name.
void db_init_index(dbIndex *)
Initialize dbIndex.
int db_set_index_table_name(dbIndex *, const char *)
Set table name.
int db_set_index_column_name(dbIndex *, int, const char *)
Set column name.
void db__set_protocol_fds(FILE *, FILE *)
?
void db_free_index(dbIndex *)
Free allocated dbIndex.
int db_set_index_type_unique(dbIndex *)
Set index type to unique.
int db_alloc_index_columns(dbIndex *, int)
Allocate index columns.
#define DB_RECV_STRING(x)
Definition: macros.h:39
#define DB_SEND_INDEX(x)
Definition: macros.h:225
#define DB_START_PROCEDURE_CALL(x)
Definition: macros.h:2
#define DB_RECV_RETURN_CODE(x)
Definition: macros.h:7
dbString indexName
Definition: dbmi.h:231
Definition: driver.h:21