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 */
db_create_index(dbDriver * driver,dbIndex * index)28 int db_create_index(dbDriver * driver, dbIndex * index)
29 {
30     int ret_code;
31 
32     /* start the procedure call */
33     db__set_protocol_fds(driver->send, driver->recv);
34     DB_START_PROCEDURE_CALL(DB_PROC_CREATE_INDEX);
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  */
db_create_index2(dbDriver * driver,const char * table_name,const char * column_name)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);
83     db_set_index_type_unique(&index);
84 
85     ret = db_create_index(driver, &index);
86 
87     db_free_index(&index);
88 
89     return ret;
90 }
91