library/src/databases/bpdb/BP-DB.cc
// this takes in mariadb metadata and provides a numeric type
this takes in mariadb metadata and provides a numeric type
this takes in mariadb metadata and provides a numeric type
BPDB_MARIADB_COLUMN_TYPE BP_DBResolveMariadbColumnMetadataToType(P_BPDB_MARIADB_TABLE_COLUMN_METADATA mariadb_column_metadata)
{
	if(!mariadb_column_metadata)
		return BPDB_MARIADB_COLUMN_TYPE_UNSET;
	if(!BP_StringIsReasonableForColumnTypeFieldDB(mariadb_column_metadata->data_type, mariadb_column_metadata->data_type_data_len))
	{
		return BPDB_MARIADB_COLUMN_TYPE_UNSET;
	}
	enum
	{
		IDX_TINYINT,
		IDX_SMALLINT,
		IDX_MEDIUMINT,
		IDX_INT,
		IDX_BIGINT,
		IDX_DECIMAL,
		IDX_FLOAT,
		IDX_DOUBLE,
		IDX_BIT,
		IDX_CHAR,
		IDX_VARCHAR,
		IDX_BINARY,
		IDX_VARBINARY,
		IDX_TINYBLOB,
		IDX_BLOB,
		IDX_MEDIUMBLOB,
		IDX_LONGBLOB,
		IDX_TINYTEXT,
		IDX_TEXT,
		IDX_MEDIUMTEXT,
		IDX_LONGTEXT,
		IDX_ENUM,
		IDX_DATE,
		IDX_TIME,
		IDX_DATETIME,
		IDX_TIMESTAMP,
		IDX_YEAR,
		IDXTOP
	};
	size_t idx_iter = 0;
	char * type_prefixes[IDXTOP+1];
	type_prefixes[IDX_TINYINT]     = "tinyint";
	type_prefixes[IDX_SMALLINT]    = "smallint";
	type_prefixes[IDX_MEDIUMINT]   = "mediumint";
	type_prefixes[IDX_INT]         = "int";
	type_prefixes[IDX_BIGINT]      = "bigint";
	type_prefixes[IDX_DECIMAL]     = "decimal";
	type_prefixes[IDX_FLOAT]       = "float";
	type_prefixes[IDX_DOUBLE]      = "double";
	type_prefixes[IDX_BIT]         = "bit";
	type_prefixes[IDX_CHAR]        = "char";
	type_prefixes[IDX_VARCHAR]     = "varchar";
	type_prefixes[IDX_BINARY]      = "binary";
	type_prefixes[IDX_VARBINARY]   = "varbinary";
	type_prefixes[IDX_TINYBLOB]    = "tinyblob";
	type_prefixes[IDX_BLOB]        = "blob";
	type_prefixes[IDX_MEDIUMBLOB]  = "mediumblob";
	type_prefixes[IDX_LONGBLOB]    = "longblob";
	type_prefixes[IDX_TINYTEXT]    = "tinytext";
	type_prefixes[IDX_TEXT]        = "text";
	type_prefixes[IDX_MEDIUMTEXT]  = "mediumtext";
	type_prefixes[IDX_LONGTEXT]    = "longtext";
	type_prefixes[IDX_ENUM]        = "enum";
	type_prefixes[IDX_DATE]        = "date";
	type_prefixes[IDX_TIME]        = "time";
	type_prefixes[IDX_DATETIME]    = "datetime";
	type_prefixes[IDX_TIMESTAMP]   = "timestamp";
	type_prefixes[IDX_YEAR]        = "year";
	type_prefixes[IDXTOP]          = NULL;
	size_t type_iter = 0;
	for(; type_prefixes[type_iter]; type_iter++)
	{
		size_t prefix_len  = 0;
		size_t compare_len = 0;
		prefix_len = bpstrlen(type_prefixes[type_iter]);
		compare_len = bpstrlen(mariadb_column_metadata->data_type);
		if(prefix_len > compare_len)
			continue;
		if(memcmp(mariadb_column_metadata->data_type, type_prefixes[type_iter], prefix_len) != 0)
			continue;
		switch(type_iter)
		{
			case IDX_TINYINT:
				return BPDB_MARIADB_COLUMN_TYPE_TINYINT;
			case IDX_SMALLINT:
				return BPDB_MARIADB_COLUMN_TYPE_SMALLINT;
			case IDX_MEDIUMINT:
				return BPDB_MARIADB_COLUMN_TYPE_MEDIUMINT;
			case IDX_INT:
				return BPDB_MARIADB_COLUMN_TYPE_INT;
			case IDX_BIGINT:
				return BPDB_MARIADB_COLUMN_TYPE_BIGINT;
			case IDX_DECIMAL:
				return BPDB_MARIADB_COLUMN_TYPE_DECIMAL;
			case IDX_FLOAT:
				return BPDB_MARIADB_COLUMN_TYPE_FLOAT;
			case IDX_DOUBLE:
				return BPDB_MARIADB_COLUMN_TYPE_DOUBLE;
			case IDX_BIT:
				return BPDB_MARIADB_COLUMN_TYPE_BIT;
			case IDX_CHAR:
				return BPDB_MARIADB_COLUMN_TYPE_CHAR;
			case IDX_VARCHAR:
				return BPDB_MARIADB_COLUMN_TYPE_VARCHAR;
			case IDX_BINARY:
				return BPDB_MARIADB_COLUMN_TYPE_BINARY;
			case IDX_VARBINARY:
				return BPDB_MARIADB_COLUMN_TYPE_VARBINARY;
			case IDX_TINYBLOB:
				return BPDB_MARIADB_COLUMN_TYPE_TINYBLOB;
			case IDX_BLOB:
				return BPDB_MARIADB_COLUMN_TYPE_BLOB;
			case IDX_MEDIUMBLOB:
				return BPDB_MARIADB_COLUMN_TYPE_MEDIUMBLOB;
			case IDX_LONGBLOB:
				return BPDB_MARIADB_COLUMN_TYPE_LONGBLOB;
			case IDX_TINYTEXT:
				return BPDB_MARIADB_COLUMN_TYPE_TINYTEXT;
			case IDX_TEXT:
				return BPDB_MARIADB_COLUMN_TYPE_TEXT;
			case IDX_MEDIUMTEXT:
				return BPDB_MARIADB_COLUMN_TYPE_MEDIUMTEXT;
			case IDX_LONGTEXT:
				return BPDB_MARIADB_COLUMN_TYPE_LONGTEXT;
			case IDX_ENUM:
				return BPDB_MARIADB_COLUMN_TYPE_ENUM;
			case IDX_DATE:
				return BPDB_MARIADB_COLUMN_TYPE_DATE;
			case IDX_TIME:
				return BPDB_MARIADB_COLUMN_TYPE_TIME;
			case IDX_DATETIME:
				return BPDB_MARIADB_COLUMN_TYPE_DATETIME;
			case IDX_TIMESTAMP:
				return BPDB_MARIADB_COLUMN_TYPE_TIMESTAMP;
			case IDX_YEAR:
				return BPDB_MARIADB_COLUMN_TYPE_YEAR;
			default:
				break;
		}
	}
	return BPDB_MARIADB_COLUMN_TYPE_UNSET;
}
BPDB_MARIADB_COLUMN_TYPE BP_DBResolveMariadbColumnMetadataToType ( P_BPDB_MARIADB_TABLE_COLUMN_METADATA mariadb_column_metadata ) { if ( ! mariadb_column_metadata ) return BPDB_MARIADB_COLUMN_TYPE_UNSET ; if ( ! BP_StringIsReasonableForColumnTypeFieldDB ( mariadb_column_metadata -> data_type , mariadb_column_metadata -> data_type_data_len ) ) { return BPDB_MARIADB_COLUMN_TYPE_UNSET ; } enum { IDX_TINYINT , IDX_SMALLINT , IDX_MEDIUMINT , IDX_INT , IDX_BIGINT , IDX_DECIMAL , IDX_FLOAT , IDX_DOUBLE , IDX_BIT , IDX_CHAR , IDX_VARCHAR , IDX_BINARY , IDX_VARBINARY , IDX_TINYBLOB , IDX_BLOB , IDX_MEDIUMBLOB , IDX_LONGBLOB , IDX_TINYTEXT , IDX_TEXT , IDX_MEDIUMTEXT , IDX_LONGTEXT , IDX_ENUM , IDX_DATE , IDX_TIME , IDX_DATETIME , IDX_TIMESTAMP , IDX_YEAR , IDXTOP } ; size_t idx_iter = 0 ; char * type_prefixes [ IDXTOP + 1 ] ; type_prefixes [ IDX_TINYINT ] = " " ; type_prefixes [ IDX_SMALLINT ] = " " ; type_prefixes [ IDX_MEDIUMINT ] = " " ; type_prefixes [ IDX_INT ] = " " ; type_prefixes [ IDX_BIGINT ] = " " ; type_prefixes [ IDX_DECIMAL ] = " " ; type_prefixes [ IDX_FLOAT ] = " " ; type_prefixes [ IDX_DOUBLE ] = " " ; type_prefixes [ IDX_BIT ] = " " ; type_prefixes [ IDX_CHAR ] = " " ; type_prefixes [ IDX_VARCHAR ] = " " ; type_prefixes [ IDX_BINARY ] = " " ; type_prefixes [ IDX_VARBINARY ] = " " ; type_prefixes [ IDX_TINYBLOB ] = " " ; type_prefixes [ IDX_BLOB ] = " " ; type_prefixes [ IDX_MEDIUMBLOB ] = " " ; type_prefixes [ IDX_LONGBLOB ] = " " ; type_prefixes [ IDX_TINYTEXT ] = " " ; type_prefixes [ IDX_TEXT ] = " " ; type_prefixes [ IDX_MEDIUMTEXT ] = " " ; type_prefixes [ IDX_LONGTEXT ] = " " ; type_prefixes [ IDX_ENUM ] = " " ; type_prefixes [ IDX_DATE ] = " " ; type_prefixes [ IDX_TIME ] = " " ; type_prefixes [ IDX_DATETIME ] = " " ; type_prefixes [ IDX_TIMESTAMP ] = " " ; type_prefixes [ IDX_YEAR ] = " " ; type_prefixes [ IDXTOP ] = NULL ; size_t type_iter = 0 ; for ( ; type_prefixes [ type_iter ] ; type_iter ++ ) { size_t prefix_len = 0 ; size_t compare_len = 0 ; prefix_len = bpstrlen ( type_prefixes [ type_iter ] ) ; compare_len = bpstrlen ( mariadb_column_metadata -> data_type ) ; if ( prefix_len > compare_len ) continue ; if ( memcmp ( mariadb_column_metadata -> data_type , type_prefixes [ type_iter ] , prefix_len ) != 0 ) continue ; switch ( type_iter ) { case IDX_TINYINT : return BPDB_MARIADB_COLUMN_TYPE_TINYINT ; case IDX_SMALLINT : return BPDB_MARIADB_COLUMN_TYPE_SMALLINT ; case IDX_MEDIUMINT : return BPDB_MARIADB_COLUMN_TYPE_MEDIUMINT ; case IDX_INT : return BPDB_MARIADB_COLUMN_TYPE_INT ; case IDX_BIGINT : return BPDB_MARIADB_COLUMN_TYPE_BIGINT ; case IDX_DECIMAL : return BPDB_MARIADB_COLUMN_TYPE_DECIMAL ; case IDX_FLOAT : return BPDB_MARIADB_COLUMN_TYPE_FLOAT ; case IDX_DOUBLE : return BPDB_MARIADB_COLUMN_TYPE_DOUBLE ; case IDX_BIT : return BPDB_MARIADB_COLUMN_TYPE_BIT ; case IDX_CHAR : return BPDB_MARIADB_COLUMN_TYPE_CHAR ; case IDX_VARCHAR : return BPDB_MARIADB_COLUMN_TYPE_VARCHAR ; case IDX_BINARY : return BPDB_MARIADB_COLUMN_TYPE_BINARY ; case IDX_VARBINARY : return BPDB_MARIADB_COLUMN_TYPE_VARBINARY ; case IDX_TINYBLOB : return BPDB_MARIADB_COLUMN_TYPE_TINYBLOB ; case IDX_BLOB : return BPDB_MARIADB_COLUMN_TYPE_BLOB ; case IDX_MEDIUMBLOB : return BPDB_MARIADB_COLUMN_TYPE_MEDIUMBLOB ; case IDX_LONGBLOB : return BPDB_MARIADB_COLUMN_TYPE_LONGBLOB ; case IDX_TINYTEXT : return BPDB_MARIADB_COLUMN_TYPE_TINYTEXT ; case IDX_TEXT : return BPDB_MARIADB_COLUMN_TYPE_TEXT ; case IDX_MEDIUMTEXT : return BPDB_MARIADB_COLUMN_TYPE_MEDIUMTEXT ; case IDX_LONGTEXT : return BPDB_MARIADB_COLUMN_TYPE_LONGTEXT ; case IDX_ENUM : return BPDB_MARIADB_COLUMN_TYPE_ENUM ; case IDX_DATE : return BPDB_MARIADB_COLUMN_TYPE_DATE ; case IDX_TIME : return BPDB_MARIADB_COLUMN_TYPE_TIME ; case IDX_DATETIME : return BPDB_MARIADB_COLUMN_TYPE_DATETIME ; case IDX_TIMESTAMP : return BPDB_MARIADB_COLUMN_TYPE_TIMESTAMP ; case IDX_YEAR : return BPDB_MARIADB_COLUMN_TYPE_YEAR ; default : break ; } } return BPDB_MARIADB_COLUMN_TYPE_UNSET ; }
this takes in mariadb metadata and provides a numeric type
this takes in mariadb metadata and provides a numeric type
// ensure we have metadata to check // check to ensure that the type field is reasonable // these are all the type prefixes found within mariadb (https://mariadb.com/kb/en/mariadb/data-types/) // iterator through the type prefixes // declare all prefixes as values from the above enum // initialize the type iterator // walk type prefixes // initialize prefix and compare lengths // calculate type prefix length // calculate the compare length // stop the loop if the prefix length is greater than the compare length // if the prefix doesn't match at this position, continue the loop // switch on the type iter and return the correct type where necessary // return unset if we couldn't resolve the value