Variant f_mysql_fetch_field(CVarRef result, int field /* = -1 */) { MySQLResult *res = php_mysql_extract_result(result); if (res == NULL) return false; if (field != -1) { if (!res->seekField(field)) return false; } MySQLFieldInfo *info; if (!(info = res->fetchFieldInfo())) return false; Object obj(SystemLib::AllocStdClassObject()); obj->o_set("name", info->name); obj->o_set("table", info->table); obj->o_set("def", info->def); obj->o_set("max_length", (int)info->max_length); obj->o_set("not_null", IS_NOT_NULL(info->flags)? 1 : 0); obj->o_set("primary_key", IS_PRI_KEY(info->flags)? 1 : 0); obj->o_set("multiple_key", info->flags & MULTIPLE_KEY_FLAG? 1 : 0); obj->o_set("unique_key", info->flags & UNIQUE_KEY_FLAG? 1 : 0); obj->o_set("numeric", IS_NUM(info->type)? 1 : 0); obj->o_set("blob", IS_BLOB(info->flags)? 1 : 0); obj->o_set("type", php_mysql_get_field_name(info->type)); obj->o_set("unsigned", info->flags & UNSIGNED_FLAG? 1 : 0); obj->o_set("zerofill", info->flags & ZEROFILL_FLAG? 1 : 0); return obj; }
Variant php_mysql_field_info(const Variant& result, int field, int entry_type) { MySQLResult *res = php_mysql_extract_result(result); if (res == NULL) return false; if (!res->seekField(field)) return false; MySQLFieldInfo *info; if (!(info = res->fetchFieldInfo())) return false; switch (entry_type) { case PHP_MYSQL_FIELD_NAME: return info->name; case PHP_MYSQL_FIELD_TABLE: return info->table; case PHP_MYSQL_FIELD_LEN: return info->length; case PHP_MYSQL_FIELD_TYPE: return php_mysql_get_field_name(info->type); case PHP_MYSQL_FIELD_FLAGS: { char buf[512]; buf[0] = '\0'; unsigned int flags = info->flags; #ifdef IS_NOT_NULL if (IS_NOT_NULL(flags)) { strcat(buf, "not_null "); } #endif #ifdef IS_PRI_KEY if (IS_PRI_KEY(flags)) { strcat(buf, "primary_key "); } #endif #ifdef UNIQUE_KEY_FLAG if (flags & UNIQUE_KEY_FLAG) { strcat(buf, "unique_key "); } #endif #ifdef MULTIPLE_KEY_FLAG if (flags & MULTIPLE_KEY_FLAG) { strcat(buf, "multiple_key "); } #endif #ifdef IS_BLOB if (IS_BLOB(flags)) { strcat(buf, "blob "); } #endif #ifdef UNSIGNED_FLAG if (flags & UNSIGNED_FLAG) { strcat(buf, "unsigned "); } #endif #ifdef ZEROFILL_FLAG if (flags & ZEROFILL_FLAG) { strcat(buf, "zerofill "); } #endif #ifdef BINARY_FLAG if (flags & BINARY_FLAG) { strcat(buf, "binary "); } #endif #ifdef ENUM_FLAG if (flags & ENUM_FLAG) { strcat(buf, "enum "); } #endif #ifdef SET_FLAG if (flags & SET_FLAG) { strcat(buf, "set "); } #endif #ifdef AUTO_INCREMENT_FLAG if (flags & AUTO_INCREMENT_FLAG) { strcat(buf, "auto_increment "); } #endif #ifdef TIMESTAMP_FLAG if (flags & TIMESTAMP_FLAG) { strcat(buf, "timestamp "); } #endif int len = strlen(buf); /* remove trailing space, if present */ if (len && buf[len-1] == ' ') { buf[len-1] = 0; len--; } return String(buf, len, CopyString); } default: break; } return false; }
/* TBL_Layout ** ** Purpose: ** This function returns the columns and types of a particular ** table specified by handle. ** ** Parameter Dictionary: ** char *databaseName: The name of the database to use. ** char *tableName: The name of the table to access. ** CONDITION (*callback)(): The callback function invoked whenever ** a new record is retreived from the database. It is ** invoked with parameters as described below. ** void *ctx: Ancillary data passed through to the callback function ** and untouched by this routine. ** ** Return Values: ** TBL_NORMAL: normal termination. ** TBL_NOCALLBACK: No callback function was specified. ** TBL_DBNOEXIST: The database specified does not exist. ** TBL_TBLNOEXIST: The table specified did not exist in the correct ** internal database table...this may indicate some sort ** of consistency problem withing the database. ** TBL_NOCOLUMNS: The table specified contains no columnns. ** TBL_EARLYEXIT: The callback routine returned something other than ** TBL_NORMAL which caused this routine to cancel the remainder ** of the database operation and return early. ** ** Notes: ** It is an error to specify a null callback function. ** ** Algorithm: ** As each column is retrieved from the specified table, the ** callback function is invoked as follows: ** ** callback(fieldList *fieldList, void *ctx) ** ** fieldList contains the field name and the type of the column from ** the table specified. ** ctx contains any additional information the user originally passed ** to this layout function. If callback returns any value other ** than TBL_NORMAL, it is assumed that this function should terminate ** (i.e. cancel the current db operation), and return an abnormal ** termination message (TBL_EARLYEXIT) to the routine which ** originally invoked TBL_Layout. */ CONDITION TBL_Layout(char *databaseName, char *tableName, CONDITION(*callback) (), void *ctx) { TBL_FIELD field; int i; MYSQL_RES * ans; MYSQL_FIELD * fld; if (callback == NULL) { return COND_PushCondition(TBL_ERROR(TBL_NOCALLBACK), "TBL_Layout"); } if (mysql_select_db(&mysql, databaseName) != 0) { return COND_PushCondition(TBL_ERROR(TBL_DBNOEXIST), "TBL_Layout"); } /* * Set up the select statement */ ans = mysql_list_fields(&mysql, tableName, NULL); if (mysql_num_fields(ans) < 1) { return COND_PushCondition(TBL_ERROR(TBL_NOCOLUMNS), "TBL_Layout"); } else { for (i = 0; i < mysql_num_fields(ans); i++) { /* * Now lets find each column and type for that table... */ fld = mysql_fetch_fields(ans); field.FieldName = fld->name; field.Value.AllocatedSize = fld->length; switch (fld->type) { case FIELD_TYPE_SHORT: field.Value.Type = TBL_SIGNED2; break; case FIELD_TYPE_LONG: field.Value.Type = TBL_SIGNED4; break; case FIELD_TYPE_FLOAT: field.Value.Type = TBL_FLOAT4; break; case FIELD_TYPE_DOUBLE: field.Value.Type = TBL_FLOAT8; break; case FIELD_TYPE_STRING: field.Value.Type = TBL_STRING; break; case FIELD_TYPE_BLOB: if (IS_BLOB(fld->flags)) { field.Value.Type = TBL_BINARYDATA; } else { field.Value.Type = TBL_TEXT; } break; } if (callback != NULL) { if (callback(&field, ctx) != TBL_NORMAL) { return COND_PushCondition(TBL_ERROR(TBL_EARLYEXIT), "TBL_Layout"); } } } } return TBL_NORMAL; }