22.11.2011, 14:31
(
Последний раз редактировалось Slice; 23.11.2011 в 08:38.
)
Useful for debugging. Prints out a human-readable table of a database result.
Example (sorta):
Beware that NULL values will cause it to crash, that's not my fault.
Example output:
As you can see some content is cut-off, it's because of the field length limit (which you can easily modify).
db_print_result
Example (sorta):
pawn Код:
new DBResult:result = db_query(db, "SELECT * FROM blabla");
db_print_result(result);
db_free_result(result);
Example output:
As you can see some content is cut-off, it's because of the field length limit (which you can easily modify).
Код:
/ type | name | tbl_name | rootpage | sql \ |-------|-----------------|-----------------|----------|------------------------------------------------| | table | lw_lvdm_users | lw_lvdm_users | 2 | CREATE TABLE lw_lvdm_users( | | | | | | userid integer primary key, | | | | | | name text, | | | | | | password | |-------|-----------------|-----------------|----------|------------------------------------------------| | table | lw_lvdm_bans | lw_lvdm_bans | 4 | CREATE TABLE lw_lvdm_bans( | | | | | | banid integer primary key, | | | | | | name text, | | | | | | reporter t | |-------|-----------------|-----------------|----------|------------------------------------------------| | index | bans_type | lw_lvdm_bans | 36886 | CREATE INDEX "bans_type" ON lw_lvdm_bans(type) | |-------|-----------------|-----------------|----------|------------------------------------------------| | index | bans_time | lw_lvdm_bans | 40587 | CREATE INDEX "bans_time" ON lw_lvdm_bans(time) | |-------|-----------------|-----------------|----------|------------------------------------------------| | table | lw_lvdm_ip_bans | lw_lvdm_ip_bans | 46040 | CREATE TABLE `lw_lvdm_ip_bans` | | | | | | ( | | | | | | BANID INTEGER PRIMARY KEY, | | | | | | ip TEXT, | | | | | | pla | |-------|-----------------|-----------------|----------|------------------------------------------------| | table | lw_temp_data | lw_temp_data | 20549 | CREATE TABLE "lw_temp_data" ( | | | | | | name text PRIMARY KEY, | | | | | | money INTEGER, | | | | | | bank INTEGER, | | | | | | bount | \-------------------------------------------------------------------------------------------------------/
pawn Код:
stock db_print_result(DBResult:dbrResult, iMaxFieldLength = 20) {
const
MAX_ROWS = 100,
MAX_FIELDS = 20,
MAX_FIELD_LENGTH = 88
;
static
s_aaszFields[MAX_ROWS + 1][MAX_FIELDS][MAX_FIELD_LENGTH char],
s_aiFieldMaxLength[MAX_FIELDS],
s_szBuffer[(MAX_FIELD_LENGTH + 4) * MAX_FIELDS]
;
static const
szcSpacePadding[MAX_FIELD_LENGTH] = {' ', ...},
szcDashPadding[MAX_FIELD_LENGTH] = {'-', ...}
;
if (iMaxFieldLength == -1)
iMaxFieldLength = MAX_FIELD_LENGTH;
print(!" ");
print(!"Query result:");
if (!dbrResult)
print(!"\t- No result.");
else if (!db_num_rows(dbrResult))
print(!"\t- No rows.");
else {
new
iRow = 0,
iRows,
iFields = db_num_fields(dbrResult),
iField,
iLength,
bool:bHasMoreLines,
iPos,
iNextPos
;
if (iMaxFieldLength > MAX_FIELD_LENGTH) {
printf("\t- The longest possible field length is %d. Change MAX_FIELD_LENGTH for larger values.", MAX_FIELD_LENGTH);
iMaxFieldLength = MAX_FIELD_LENGTH;
}
if (iFields > MAX_FIELDS) {
printf("\t- There are %d, but only %d of them will be visible.", iFields, MAX_FIELDS);
print(!"\t- Increase MAX_FIELDS if you want to see all fields.");
iFields = MAX_FIELDS;
}
for (iField = 0; iField < iFields; iField++) {
db_field_name(dbrResult, iField, s_szBuffer, iMaxFieldLength - 1);
iPos = 0;
while (-1 != (iPos = strfind(s_szBuffer, "\r", _, iPos)))
s_szBuffer[iPos] = ' ';
iPos = 0;
while (-1 != (iPos = strfind(s_szBuffer, "\t", _, iPos))) {
s_szBuffer[iPos] = ' ';
strins(s_szBuffer, " ", iPos, iMaxFieldLength);
iPos += 4;
}
iPos = 0;
do {
iNextPos = strfind(s_szBuffer, "\n", _, iPos) + 1;
if (!iNextPos)
iLength = strlen(s_szBuffer[iPos]);
else
iLength = iNextPos - iPos - 1;
s_aiFieldMaxLength[iField] = min(iMaxFieldLength, max(iLength, s_aiFieldMaxLength[iField]));
} while ((iPos = iNextPos));
strpack(s_aaszFields[0][iField], s_szBuffer, iMaxFieldLength char);
}
do {
for (iField = 0; iField < iFields; iField++) {
db_get_field(dbrResult, iField, s_szBuffer, iMaxFieldLength - 1);
iPos = 0;
while (-1 != (iPos = strfind(s_szBuffer, "\r", _, iPos)))
s_szBuffer[iPos] = ' ';
iPos = 0;
while (-1 != (iPos = strfind(s_szBuffer, "\t", _, iPos))) {
s_szBuffer[iPos] = ' ';
strins(s_szBuffer, " ", iPos, iMaxFieldLength);
iPos += 4;
}
iPos = 0;
do {
iNextPos = strfind(s_szBuffer, "\n", _, iPos) + 1;
if (!iNextPos)
iLength = strlen(s_szBuffer[iPos]);
else
iLength = iNextPos - iPos - 1;
s_aiFieldMaxLength[iField] = min(iMaxFieldLength, max(iLength, s_aiFieldMaxLength[iField]));
} while ((iPos = iNextPos));
strpack(s_aaszFields[iRow + 1][iField], s_szBuffer, iMaxFieldLength char);
}
if (++iRow >= MAX_ROWS) {
iRows = iRow;
while (db_next_row(dbrResult))
iRows++;
printf("\t- Only the first %d rows are displayed; there are %d remaining.", MAX_ROWS, iRows);
break;
}
} while (db_next_row(dbrResult));
print(!" ");
for (iRows = iRow, iRow = 0; iRow <= iRows; iRow++) {
do {
bHasMoreLines = false;
s_szBuffer[0] = 0;
for (iField = 0; iField < iFields; iField++) {
if (iField)
strcat(s_szBuffer, " | ");
iLength = strlen(s_szBuffer);
if (-1 != (iPos = strfind(s_aaszFields[iRow][iField], "\n"))) {
strunpack(s_szBuffer[iLength], s_aaszFields[iRow][iField], strlen(s_szBuffer[iLength]) + iPos + 1);
strdel(s_aaszFields[iRow][iField], 0, iPos + 1);
bHasMoreLines = true;
} else {
if (s_aaszFields[iRow][iField]{0}) {
strunpack(s_szBuffer[iLength], s_aaszFields[iRow][iField], sizeof(s_szBuffer) - iLength);
s_aaszFields[iRow][iField]{0} = 0;
}
}
iLength = strlen(s_szBuffer[iLength]);
strcat(s_szBuffer, szcSpacePadding, strlen(s_szBuffer) + (s_aiFieldMaxLength[iField] - iLength + 1));
}
if (bHasMoreLines)
printf("\t| %s |", s_szBuffer);
} while (bHasMoreLines);
if (iRow == 0) {
printf("\t/ %s \\", s_szBuffer);
} else {
printf("\t| %s |", s_szBuffer);
}
if (iRow == iRows) {
s_szBuffer[0] = 0;
for (iField = 0; iField < iFields; iField++) {
if (iField)
strcat(s_szBuffer, "---");
strcat(s_szBuffer, szcDashPadding, strlen(s_szBuffer) + s_aiFieldMaxLength[iField] + 1);
}
printf("\t\\-%s-/", s_szBuffer);
} else {
s_szBuffer[0] = 0;
for (iField = 0; iField < iFields; iField++) {
if (iField)
strcat(s_szBuffer, "-|-");
strcat(s_szBuffer, szcDashPadding, strlen(s_szBuffer) + s_aiFieldMaxLength[iField] + 1);
}
printf("\t|-%s-|", s_szBuffer);
}
}
}
print(!" ");
}