📄 mysql.cpp
字号:
{
}
virtual int
spi_ClearParameters(ShhCursorHandle hCursor)
{
return 0;
}
virtual int
spi_PasteValue(ShhCursorHandle hCursor, ShhDbExecutor* executor, int index, string& out, const char* value)
{
const char* p = value;
out.append('\'');
while(*p)
{
if (strchr("\'\"\\", *p)) out.append('\\');
else if (*p == '\n') { out.append("\\n"); p++; continue; }
else if (*p == '\r') { out.append("\\r"); p++; continue; }
out.append(*p);
p++;
}
out.append('\'');
return 0;
}
virtual int
spi_BindValue(ShhCursorHandle hCursor, ShhDbExecutor* executor, int index, const char* value)
{
return 0;
}
};
/*
ShhRecordSet::~ShhRecordSet()
{
if (m_result != NULL)
mysql_free_result(m_result);
}
bool ShhSQL::hasMethod(const char* method)
{
if (stricmp(method, "EXECUTE") == 0)
return true;
else
return false;
}
void ShhSQL::paste_value(string& out, ShhValue value)
{
switch (value.m_type)
{
case SHH_INT: out.appendf("%d", value.toInt()); return;
case SHH_FLOAT: out.appendf("%.15g", value.toFloat()); return;
case SHH_DATE:
{
string s; datetime dt; value.toDate(dt);
dt.export_common(s);
out.appendf("'%s'", s.cstr());
return;
}
case SHH_STRING:
{
string s; value.toString(s);
const char* p = s;
out.append('\'');
while(*p)
{
if (strchr("\'\"\\", *p)) out.append('\\');
else if (*p == '\n') { out.append("\\n"); p++; continue; }
else if (*p == '\r') { out.append("\\r"); p++; continue; }
out.append(*p);
p++;
}
out.append('\'');
return;
}
case SHH_OBJECT:
{
const char* type = (value.m_value.u_object != NULL) ?
value.m_value.u_object->getTypeName() : "NULL";
throw new ShhObjectException(2803, "Object <%s> cannot be inserted into SQL. Use explicit conversion");
}
default:
throw new ShhObjectException(2801, "Value of unknown type cannot be pasted into SQL");
}
}
void ShhSQL::substitute(ShhValueList* args, const char* sqlin, string &sqlout)
{
int argind = 0;
int argc = args ? args->size() : 0;
const char* in = sqlin;
char quote = '\0';
char comment = '\0';
sqlout.clear();
while(*in)
{
if (quote != '\0')
{
if (*in == '\\') { sqlout.append(*in); in++; }
else if (*in == quote) quote = '\0';
}
else if (*in == '\'' || *in == '\"')
{
quote = *in;
}
else if (*in == '?')
{
if (args == NULL || argind >= argc)
throw new ShhObjectException(2800, "this SQL requires at least %d parameter(s) - %d passed", argind+1, argc);
paste_value(sqlout, *(args->get(argind)));
in++;
argind++;
continue;
}
else if (*in == ':')
{
string name; in++;
while(*in && (isalnum(*in) || *in == '_')) name.append(*in++);
ShhVariable* var = m_module.findVariable(name);
if (var == NULL)
throw new ShhObjectException(2804, "variable '%s' for SQL substitution is not found", name.cstr());
paste_value(sqlout, var->value());
continue;
}
sqlout.append(*in);
in++;
}
}
ShhValue ShhSQL::execute(ShhValueList* args)
{
MYSQL* session = provide_session();
string sqlin, sqlout;
getProperty("STATEMENT").toString(sqlin);
if (sqlin.is_empty())
throw new ShhObjectException("SQL statement is not defined for object");
substitute(args, sqlin, sqlout);
if (mysql_query(session, sqlout) != 0)
{
int code = mysql_errno(session);
const char* msg = mysql_error(session);
throwSQL("SQL Query failed: MySQL error %d: %s", code, msg);
}
return ShhValue::null();
}
ShhValue ShhRecordSet::execute(ShhValueList* args)
{
ShhSQL::execute(args);
// release previous result in case of multiple executions
if (m_result != NULL) { mysql_free_result(m_result); m_result = NULL; }
m_result = mysql_store_result(m_connection->m_session);
if (m_result == NULL)
{
m_field_count = 0;
m_total_row_count = 0;
m_row_num = 0;
}
else
{
m_field_count = mysql_num_fields(m_result);
m_total_row_count = (int) mysql_num_rows(m_result);
m_row_num = 1;
}
return ShhValue::null();
}
ShhValue ShhRecordSet::method_more()
{
if (m_row_num < 0)
{
return ShhValue::null();
}
return ShhValue(m_row_num < m_total_row_count);
}
ShhValue ShhRecordSet::method_count()
{
if (m_result == NULL)
throw new ShhObjectException(2807, "Recordset was not executed", mysql_error(m_connection->m_session));
return ShhValue(m_total_row_count);
}
ShhValue ShhRecordSet::method_finished()
{
return ShhValue(m_row_num <= 0);
}
ShhValue ShhRecordSet::method_next()
{
MYSQL* session = provide_session();
m_changed_since_execute = true;
ShhValue zero(0);
if (m_result == NULL)
throw new ShhObjectException(2806, "Recordset was not executed", mysql_error(session));
MYSQL_ROW row = mysql_fetch_row(m_result);
if (row == NULL)
{
m_row_num = -1;
if (mysql_errno(session) != 0)
throw new ShhObjectException("fetch failed: %s", mysql_error(session));
else
{
return zero;
}
}
if (m_field_count == 0)
{
m_row_num = 0;
return zero;
}
unsigned long* lengths = mysql_fetch_lengths(m_result);
MYSQL_FIELD* fields = mysql_fetch_fields(m_result);
for(int i=0,n=m_field_count; i<n; i++)
{
MYSQL_FIELD* field = &fields[i];
const char* name = field->name;
unsigned int length = lengths[i];
ShhValue value("");
if (length > 0)
{
char* data = (char*) m_module.scratch().malloc(length+1);
memcpy(data, row[i], length);
data[length] = '\0';
value.m_value.u_string = data;
value.m_type = SHH_STRING;
}
setProperty(name, value);
}
m_row_num++;
return ShhValue(1);
}
ShhValue ShhRecordSet::method_rownum()
{
MYSQL* session = provide_session();
return ShhValue(m_row_num);
}
ShhValue ShhRecordSet::method_value(ShhValueList* args)
{
if (args == NULL || args->size() < 1)
{
throw new ShhObjectException("method VALUE() needs argument (field name)");
}
string name; args->get(0)->toString(name);
MYSQL* session = provide_session();
return getProperty(name);
}
*/
ShhDbDriver* create_mysql_driver()
{
return new MySQLDriver();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -