github.com/256dpi/max-go@v0.7.0/lib/max/ext_database.h (about)

     1  /* 
     2  	ext_database.h
     3  	Copyright 2008 - Cycling '74
     4  	Timothy Place, tim@cycling74.com	
     5  */
     6  
     7  #ifndef _EXT_DATABASE_H_
     8  #define _EXT_DATABASE_H_
     9  
    10  #if !defined( DEBUG ) && !defined ( NDEBUG ) && !defined (_MAX_CORE_H_)
    11  #include "ext.h"
    12  #include "ext_obex.h"
    13  #endif
    14  
    15  #include "ext_prefix.h"
    16  #include "ext_mess.h"
    17  #include "stdarg.h"
    18  
    19  /**	A database object.  
    20  	Use db_open() and db_close() to create and free database objects.
    21  	@ingroup	database	*/
    22  typedef t_object t_database;
    23  
    24  
    25  /**	A database result object.  
    26  	This is what the database object returns when a query is executed.
    27  	@ingroup	database	*/
    28  typedef t_object t_db_result;
    29  
    30  
    31  /**	A database view object.  
    32  	A database view wraps a query and a result for a given database, and is always updated and in-sync with the database.
    33  	@ingroup	database	*/
    34  typedef t_object t_db_view;
    35  
    36  
    37  BEGIN_USING_C_LINKAGE
    38  
    39  
    40  /**	Create an instance of a database.
    41  	@ingroup	database
    42  	@param		dbname		The name of the database.
    43  	@param		fullpath	If a database with this dbname is not already open,
    44  							this will specify a full path to the location where the database is stored on disk.
    45  							If NULL is passed for this argument, the database will reside in memory only.
    46  							The path should be formatted as a Max style path.
    47  	@param		db			The address of a #t_database pointer that will be set to point to the new database instance.
    48  							If the pointer is not NULL, then it will be treated as a pre-existing database instance 
    49  							and thus will be freed.
    50  	@return					An error code.		*/
    51  t_max_err db_open(t_symbol *dbname, const char *fullpath, t_database **db);
    52  
    53  typedef enum _db_open_flags {
    54  	DB_OPEN_FLAGS_NONE = 0,
    55  	DB_OPEN_FLAGS_READONLY = 0x01
    56  } t_db_open_flags;
    57  
    58  /**	Create an instance of a database.
    59  	@ingroup	database
    60  	@param		dbname		The name of the database.
    61  	@param		fullpath	If a database with this dbname is not already open,
    62  							this will specify a full path to the location where the database is stored on disk.
    63  							If NULL is passed for this argument, the database will reside in memory only.
    64  							The path should be formatted as a Max style path.
    65  	@param		db			The address of a #t_database pointer that will be set to point to the new database instance.
    66  							If the pointer is not NULL, then it will be treated as a pre-existing database instance
    67  							and thus will be freed.
    68  	@param		flags		Any flags to be passed to the database backend while opening the db. At this time,
    69  							DB_OPEN_FLAGS_READONLY (0x01) is the only flag available.
    70  	@return					An error code.		*/
    71  t_max_err db_open_ext(t_symbol *dbname, const char *fullpath, t_database **db, long flags);
    72  
    73  
    74  /**	Close an open database.
    75  	@ingroup	database
    76  	@param		db			The address of the #t_database pointer for your database instance.
    77  							The pointer will be freed and set NULL upon return.
    78  	@return					An error code.		*/
    79  t_max_err db_close(t_database **db);
    80  
    81  
    82  /**	Execute a SQL query on the database.
    83  	@ingroup	database
    84  	@param		db			The #t_database pointer for your database instance.
    85  	@param		dbresult	The address of a #t_db_result pointer.
    86  							If the pointer is passed-in set to NULL then a new dbresult will be created.
    87  							If the pointer is not NULL then it is assumed to be a valid dbresult, which will be filled in with the query results.
    88  							When you are done with the dbresult you should free it with object_free().
    89  	@param		sql			A C-string containing a valid SQL query, possibly with sprintf() formatting codes.
    90  	@param		...			If an sprintf() formatting codes are used in the sql string, these values will be interpolated into the sql string.
    91  	@return					An error code.		*/
    92  t_max_err db_query(t_database *db, t_db_result **dbresult, const char *sql, ...);
    93  
    94  
    95  /**	Execute a SQL query on the database.
    96  	@ingroup	database
    97  	@param		db			The #t_database pointer for your database instance.
    98  	@param		dbresult	The address of a #t_db_result pointer.
    99  							If the pointer is passed-in set to NULL then a new dbresult will be created.
   100  							If the pointer is not NULL then it is assumed to be a valid dbresult, which will be filled in with the query results.
   101  							When you are done with the dbresult you should free it with object_free().
   102  	@param		sql			A C-string containing a valid SQL query, possibly with sprintf() formatting codes.
   103  	@param		ap			va_list containing any additional arguments necessary for sprintf()-format processing of the sql string.
   104  	@return					An error code.		*/
   105  t_max_err db_vquery(t_database *db, t_db_result **dbresult, const char *s, va_list ap);
   106  
   107  
   108  /**	Execute a SQL query on the database.
   109  	 @ingroup	database
   110  	 @param		db			The #t_database pointer for your database instance.
   111  	 @param		dbresult	The address of a #t_db_result pointer.
   112  							If the pointer is passed-in set to NULL then a new dbresult will be created.
   113  							If the pointer is not NULL then it is assumed to be a valid dbresult, which will be filled in with the query results.
   114  							When you are done with the dbresult you should free it with object_free().
   115  	 @param		sql			A C-string containing a valid SQL query.
   116  	 @return				An error code.		*/
   117  t_max_err db_query_direct(t_database *db, t_db_result **dbresult, const char *sql);
   118  
   119  
   120  /**	Execute a SQL query on the database, temporarily overriding the database's error logging attribute.
   121  	@ingroup	database
   122  	@param		db			The #t_database pointer for your database instance.
   123  	@param		dbresult	The address of a #t_db_result pointer.
   124  							If the pointer is passed-in set to NULL then a new dbresult will be created.
   125  							If the pointer is not NULL then it is assumed to be a valid dbresult, which will be filled in with the query results.
   126  							When you are done with the dbresult you should free it with object_free().
   127  	@param		sql			A C-string containing a valid SQL query, possibly with sprintf() formatting codes.
   128  	@param		...			If an sprintf() formatting codes are used in the sql string, these values will be interpolated into the sql string.
   129  	@return					An error code.		*/
   130  t_max_err db_query_silent(t_database *db, t_db_result **dbresult, const char *sql, ...);
   131  
   132  
   133  /**	Determine the id (key) number for the most recent INSERT query executed on the database.
   134  	@ingroup	database
   135  	@param		db			The #t_database pointer for your database instance.
   136  	@param		id			The address of a variable to hold the result on return.
   137  	@return					An error code.		*/
   138  t_max_err db_query_getlastinsertid(t_database *db, long *id);
   139  
   140  
   141  /**	Create a new table in a database.
   142  	@ingroup	database
   143  	@param		db			The #t_database pointer for your database instance.
   144  	@param		tablename	The name to use for the new table.
   145  							The new table will be created with one column, which holds the primary key for the table,
   146  							and is named according the form {tablename}_id.
   147  	@return					An error code.		*/
   148  t_max_err db_query_table_new(t_database *db, const char *tablename);
   149  
   150  
   151  /**	Add a new column to an existing table in a database.
   152  	@ingroup	database
   153  	@param		db			The #t_database pointer for your database instance.
   154  	@param		tablename	The name of the table to which the column should be added.
   155  	@param		columnname	The name to use for the new column.
   156  	@param		columntype	The SQL type for the data that will be stored in the column.
   157  							For example: "INTEGER" or "VARCHAR"
   158  	@param		flags		If you wish to specify any additional information for the column, then pass that here.
   159  							Otherwise pass NULL.
   160  	@return					An error code.		*/
   161  t_max_err db_query_table_addcolumn(t_database *db, const char *tablename, const char *columnname, const char *columntype, const char *flags);
   162  
   163  
   164  /**	Begin a database transaction.
   165  	When you are working with a file-based database, then the database will not be flushed to disk until db_transacation_end() is called.
   166  	This means that you can _much_ more efficiently execute a sequence of queries in one transaction rather than independently.
   167  	
   168  	That database object reference counts transactions, so it is possible nest calls to db_transacation_start() and db_transacation_end().
   169  	It is important to balance all calls with db_transacation_end() or the database contents will never be flushed to disk. 
   170  
   171  	@ingroup	database
   172  	@param		db			The #t_database pointer for your database instance.
   173  	@return					An error code.		*/
   174  t_max_err db_transaction_start(t_database *db);
   175  
   176  
   177  /**	Finalize a database transaction.
   178  	@ingroup	database
   179  	@param		db			The #t_database pointer for your database instance.
   180  	@return					An error code.		*/
   181  t_max_err db_transaction_end(t_database *db);
   182  
   183  
   184  /**	Force any open transactions to close.
   185  	@ingroup	database
   186  	@param		db			The #t_database pointer for your database instance.
   187  	@return					An error code.		*/
   188  t_max_err db_transaction_flush(t_database *db);
   189  
   190  
   191  // DB VIEWS
   192  
   193  /**	A database view is a way of looking at a particular set of records in the database.
   194  	This particular set of records is defined with a standard SQL query, 
   195  	and the view maintains a copy of the results of the query internally.
   196  	Any time the database is modified the internal result set is updated, 
   197  	and any objects listening to the view are notified via object_notify().
   198  	
   199  	@ingroup	database
   200  	@param		db			The #t_database pointer for your database instance.
   201  	@param		sql			A SQL query that defines the set of results provided by the view.
   202  	@param		dbview		The address of a NULL #t_db_view pointer which will be set with the new view upon return.
   203  	@return					An error code.		*/
   204  t_max_err db_view_create(t_database *db, const char *sql, t_db_view **dbview);
   205  
   206  
   207  /**	Remove a database view created using db_view_create().
   208  	@ingroup	database
   209  	@param		db			The #t_database pointer for your database instance for which this view was created.
   210  	@param		dbview		The address of the #t_db_view pointer for the view.
   211  							This pointer will be freed and set NULL upon return.
   212  	@return					An error code.		*/
   213  t_max_err db_view_remove(t_database *db, t_db_view **dbview);
   214  
   215  
   216  /**	Fetch the pointer for a #t_db_view's query result.
   217  	@ingroup	database
   218  	@param		dbview		The #t_db_view pointer for your database view instance.
   219  	@param		result		The address of a pointer to a #t_db_result object.
   220  							This pointer will be overwritten with the view's result pointer upon return.
   221  	@return					An error code.		*/
   222  t_max_err db_view_getresult(t_db_view *dbview, t_db_result **result);
   223  
   224  
   225  /**	Set the query used by the view.
   226  	@ingroup	database
   227  	@param		dbview		The #t_db_view pointer for your database view instance.
   228  	@param		newquery	The SQL string to define a new query for the view, replacing the old query.
   229  	@return					An error code.		*/
   230  t_max_err db_view_setquery(t_db_view *dbview, char *newquery);
   231  
   232  
   233  // DB RESULTS
   234  
   235  /**	Return the next record from a set of results that you are walking.
   236  	When you are returned a result from a query of the database, 
   237  	the result is prepared for walking the results from the beginning.
   238  	You can also reset the result manually to the beginning of the record list 
   239  	by calling db_result_reset().
   240  	
   241  	@ingroup	database
   242  	@param		result		The #t_db_result pointer for your query results.
   243  	@return					An array of C-Strings with the values for every requested column (field) of a database record.	
   244  							To find out how many columns are represented in the array, use db_result_numfields(). */
   245  char** db_result_nextrecord(t_db_result *result);
   246  
   247  
   248  /**	Reset the interface for walking a result's record list to the first record.
   249  	@ingroup	database
   250  	@param		result		The #t_db_result pointer for your query results.	 */
   251  void db_result_reset(t_db_result *result);
   252  
   253  
   254  /**	Zero-out a database result.
   255  	@ingroup	database
   256  	@param		result		The #t_db_result pointer for your query results.	*/
   257  void db_result_clear(t_db_result *result);
   258  
   259  
   260  /**	Return a count of all records in the query result.
   261  	@ingroup	database
   262  	@param		result		The #t_db_result pointer for your query results.
   263  	@return					The count of records in the query result.	*/
   264  long db_result_numrecords(t_db_result *result);
   265  
   266  
   267  /**	Return a count of all fields (columns) in the query result.
   268  	@ingroup	database
   269  	@param		result		The #t_db_result pointer for your query results.
   270  	@return					The count of fields in the query result.	*/
   271  long db_result_numfields(t_db_result *result);
   272  
   273  
   274  /**	Return the name of a field specified by its index number.
   275  	@ingroup	database
   276  	@param		result		The #t_db_result pointer for your query results.
   277  	@param		fieldindex	The zero-based index number of the field (column) in the result.
   278  	@return					A C-String with the name of the field.		*/
   279  char* db_result_fieldname(t_db_result *result, long fieldindex);
   280  
   281  
   282  /**	Return a single value from a result according to its index and field coordinates.
   283  	@ingroup	database
   284  	@param		result			The #t_db_result pointer for your query results.
   285  	@param		recordindex		The zero-based index number of the record (row) in the result.
   286  	@param		fieldindex		The zero-based index number of the field (column) in the result.
   287  	@return						A C-String with the content of the specified cell in the result.	*/
   288  char* db_result_string(t_db_result *result, long recordindex, long fieldindex);
   289  
   290  /**	Return a single value from a result according to its index and field coordinates.
   291  	@ingroup	database
   292  	@param		result			The #t_db_result pointer for your query results.
   293  	@param		recordindex		The zero-based index number of the record (row) in the result.
   294  	@param		fieldindex		The zero-based index number of the field (column) in the result.
   295  	@return						The content of the specified cell from the result scanned out to a long int.	*/
   296  long db_result_long(t_db_result *result, long recordindex, long fieldindex);
   297  
   298  /**	Return a single value from a result according to its index and field coordinates.
   299  	@ingroup	database
   300  	@param		result			The #t_db_result pointer for your query results.
   301  	@param		recordindex		The zero-based index number of the record (row) in the result.
   302  	@param		fieldindex		The zero-based index number of the field (column) in the result.
   303  	@return						The content of the specified cell from the result scanned out to a float.		*/
   304  float db_result_float(t_db_result *result, long recordindex, long fieldindex);
   305  
   306  /**	Return a single value from a result according to its index and field coordinates.
   307  	The value will be coerced from an expected datetime field into seconds.
   308  	@ingroup	database
   309  	@param		result			The #t_db_result pointer for your query results.
   310  	@param		recordindex		The zero-based index number of the record (row) in the result.
   311  	@param		fieldindex		The zero-based index number of the field (column) in the result.
   312  	@return						The datetime represented in seconds.							*/
   313  t_ptr_uint db_result_datetimeinseconds(t_db_result *result, long recordindex, long fieldindex);
   314  
   315  
   316  // UTILITIES
   317  
   318  /**	A utility to convert from a sql datetime string into seconds.
   319  	@ingroup	database
   320  	@param		string			A C-string containing a date and time in SQL format.
   321  	@param		date			The datetime represented in seconds upon return. */
   322  void db_util_stringtodate(const char *string, t_ptr_uint *date);
   323  
   324  
   325  /**	A utility to convert from seconds into a sql-ready datetime string.
   326  	@ingroup	database
   327  	@param		date			The datetime represented in seconds.
   328  	@param		string			The address of a valid C-string 
   329  								whose contents will be set to a SQL-ready string format upon return. */
   330  void db_util_datetostring(const t_ptr_uint date, char *string);
   331  
   332  
   333  END_USING_C_LINKAGE
   334  
   335  
   336  #endif // _EXT_DATABASE_H_