2024-04-27 21:08:04 +02:00
<?xml version="1.0" encoding="UTF-8" ?>
<class name= "PreparedStatement" inherits= "Reference" version= "4.4" >
<brief_description >
2024-04-27 23:02:05 +02:00
Represents a prepared statement for use with a [Database].
2024-04-27 21:08:04 +02:00
</brief_description>
<description >
2024-04-27 23:02:05 +02:00
Represents a prepared statement for use with a [Database].
Prepared statements are compiled and parametrized sql statements which can be used repeatedly.
[PreparedStatement]s are an alternative to [QueryBuilder].
Prepared statements comes with a lot of added security, as SQL injection attacks doesn't work with them (as long as you use their parameters). However note that [QueryBuilder] will escape string parameters which expect to have user input in the for you too, so if you use [QueryBuilder] yuo should also be safe.
Performance can differ compared to normal queries, but it seems like it depends on a lot of factors, so you will have to banchmark it yourself for heavy queries. It can be faster or slower.
A prepared statement looks similar:
[code]INSERT INTO table VALUES(?, ?, ?, ?);[/code]
And then you can substitute values into the [code]?[/code]-s.
Usge example:
[code]
var qb : QueryBuilder = DatabaseManager.ddb.get_connection().get_query_builder()
var ps : PreparedStatement = qb.create_prepared_statement()
ps.sql = "INSERT INTO 'data_table' VALUES(?, ?, ?, ?);"
ps.prepare()
print("Inserting 10 values!")
for i in range(10):
2024-04-27 23:06:38 +02:00
ps.reset()
2024-04-27 23:02:05 +02:00
2024-04-27 23:06:38 +02:00
ps.bind_text(1, "vc" + str(randi()))
ps.bind_text(2, "text" + str(randi()))
ps.bind_int(3, randi())
ps.bind_double(4, randf() * 100000)
ps.step()
2024-04-27 23:02:05 +02:00
[/code]
Usge example using [QueryBuilder]:
[code]
var qb : QueryBuilder = DatabaseManager.ddb.get_connection().get_query_builder()
qb.insert("data_table", "data_varchar,data_text,data_int,data_double").values()
qb.valph().valph().valph().valph().cvalues()
qb.end_command()
var ps : PreparedStatement = qb.create_prepared_statement()
ps.prepare()
print("Inserting 10 values!")
for i in range(10):
2024-04-27 23:06:38 +02:00
ps.reset()
2024-04-27 23:02:05 +02:00
2024-04-27 23:06:38 +02:00
ps.bind_text(1, "vc" + str(randi()))
ps.bind_text(2, "text" + str(randi()))
ps.bind_int(3, randi())
ps.bind_double(4, randf() * 100000)
ps.step()
2024-04-27 23:02:05 +02:00
[/code]
2024-04-27 21:08:04 +02:00
</description>
<tutorials >
</tutorials>
<methods >
<method name= "bind_blob" >
<return type= "int" enum= "Error" />
<argument index= "0" name= "index" type= "int" />
<argument index= "1" name= "value" type= "PoolByteArray" />
<description >
2024-04-27 23:02:05 +02:00
Bind a blob to the index-th parameter. (Indexed from 1)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_double" >
<return type= "int" enum= "Error" />
<argument index= "0" name= "index" type= "int" />
<argument index= "1" name= "value" type= "float" />
<description >
2024-04-27 23:02:05 +02:00
Bind a double to the index-th parameter. (Indexed from 1)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_float" >
<return type= "int" enum= "Error" />
<argument index= "0" name= "index" type= "int" />
<argument index= "1" name= "value" type= "float" />
<description >
2024-04-27 23:02:05 +02:00
Bind a float to the index-th parameter. (Indexed from 1)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_int" >
<return type= "int" enum= "Error" />
<argument index= "0" name= "index" type= "int" />
<argument index= "1" name= "value" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Bind an int to the index-th parameter. (Indexed from 1)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_int64" >
<return type= "int" enum= "Error" />
<argument index= "0" name= "index" type= "int" />
<argument index= "1" name= "value" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Bind a 64bit int to the index-th parameter. (Indexed from 1)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_null" >
<return type= "int" enum= "Error" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Bind null to the index-th parameter. (Indexed from 1)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_parameter_count" >
<return type= "int" />
<description >
2024-04-27 23:02:05 +02:00
The bindable parameter count in a query.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_parameter_index" >
<return type= "int" />
<argument index= "0" name= "name" type= "String" />
<description >
2024-04-27 23:02:05 +02:00
Some database backends support naming bind parameters using a special syntax.
Use this method to get back the index of a named parameter.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_parameter_name" >
<return type= "String" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Some database backends support naming bind parameters using a special syntax.
Use this method to get back the name of a parameter from it's index.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_text" >
<return type= "int" enum= "Error" />
<argument index= "0" name= "index" type= "int" />
<argument index= "1" name= "value" type= "String" />
<description >
2024-04-27 23:02:05 +02:00
Bind a [String] to the index-th parameter. (Indexed from 1)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_value" >
<return type= "int" enum= "Error" />
<argument index= "0" name= "index" type= "int" />
<argument index= "1" name= "value" type= "Variant" />
<description >
2024-04-27 23:02:05 +02:00
Bind a [Variant] to the index-th parameter. This mwthod will call other bind methods based on the value's type. (Indexed from 1)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "bind_zeroblob" >
<return type= "int" enum= "Error" />
<argument index= "0" name= "index" type= "int" />
<argument index= "1" name= "num" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Bind an empty blob with the size of num to the index-th parameter. (Indexed from 1)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "clear_bindings" >
<return type= "int" enum= "Error" />
<description >
2024-04-27 23:02:05 +02:00
Clear all bound parameters, effectively setting their values back to null.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_blob" >
<return type= "PoolByteArray" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Read a blob from the index-th column. (Indexed from 0)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_count" >
<return type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Return the number of columns in the result set.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_database_name" >
<return type= "String" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Returns the database's name from which the column is from if the database backend supports it.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_decltype" >
<return type= "String" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Returns the declared type of the given column.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_double" >
<return type= "float" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Read a blob from the index-th column. (Indexed from 0)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_float" >
<return type= "float" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Read a blob from the index-th column. (Indexed from 0)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_int" >
<return type= "int" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Read a blob from the index-th column. (Indexed from 0)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_int64" >
<return type= "int" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Read a blob from the index-th column. (Indexed from 0)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_name" >
<return type= "String" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Read a blob from the index-th column. (Indexed from 0)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_origin_name" >
<return type= "String" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Returns the column's origin name if the database backend supports it.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_table_name" >
<return type= "String" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Returns the column's table name if the database backend supports it.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_text" >
<return type= "String" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Read a blob from the index-th column. (Indexed from 0)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_type" >
<return type= "int" enum= "PreparedStatement.Type" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Returns the column's type.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "column_value" >
<return type= "Variant" />
<argument index= "0" name= "index" type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Read a [Variant] from the index-th column. (Indexed from 0)
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "data_count" >
<return type= "int" />
<description >
2024-04-27 23:02:05 +02:00
Returns the number of columns in the current row.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "finalize" >
<return type= "int" enum= "Error" />
<description >
2024-04-27 23:02:05 +02:00
Free the query from the database system. The destructor calls this automatically.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "get_connection" qualifiers= "const" >
<return type= "DatabaseConnection" />
<description >
2024-04-27 23:02:05 +02:00
Returns the owner connection.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "get_expanded_sql" >
<return type= "String" />
<description >
2024-04-27 23:02:05 +02:00
Returns the expanded version of the original sql, if the database backend supports it.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "get_normalized_sql" >
<return type= "String" />
<description >
2024-04-27 23:02:05 +02:00
Returns the normalized version of the original sql, if the database backend supports it.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "prepare" >
<return type= "int" enum= "Error" />
<description >
2024-04-27 23:02:05 +02:00
Compiles the sql query in [member sql].
You need to call this before trying to use your query. Subsequent calls update the stored query on the database server.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "reset" >
<return type= "int" enum= "Error" />
<description >
2024-04-27 23:02:05 +02:00
Reset the query. Call this before trying to use it again with different parameters.
2024-04-27 21:08:04 +02:00
</description>
</method>
<method name= "step" >
<return type= "int" enum= "Error" />
<description >
2024-04-27 23:02:05 +02:00
Step the query. When calling this the first time it runs the query. Subsequent calls read rows.
2024-04-27 21:08:04 +02:00
</description>
</method>
</methods>
<members >
<member name= "sql" type= "String" setter= "set_sql" getter= "get_sql" >
2024-04-27 23:02:05 +02:00
The sql statement to send to the database.
2024-04-27 21:08:04 +02:00
</member>
</members>
<constants >
<constant name= "TYPE_NULL" value= "0" enum= "Type" >
2024-04-27 23:02:05 +02:00
NULL type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_BLOB" value= "1" enum= "Type" >
2024-04-27 23:02:05 +02:00
BLOB type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_FLOAT" value= "2" enum= "Type" >
2024-04-27 23:02:05 +02:00
FLOAT type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_DOUBLE" value= "3" enum= "Type" >
2024-04-27 23:02:05 +02:00
DOUBLE type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_INT" value= "4" enum= "Type" >
2024-04-27 23:02:05 +02:00
INT type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_INT64" value= "5" enum= "Type" >
2024-04-27 23:02:05 +02:00
INT64 type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_TEXT" value= "6" enum= "Type" >
2024-04-27 23:02:05 +02:00
TEXT type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_VARCHAR" value= "7" enum= "Type" >
2024-04-27 23:02:05 +02:00
VARCHAR type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_VALUE" value= "8" enum= "Type" >
2024-04-27 23:02:05 +02:00
VALUE type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_BYTES" value= "9" enum= "Type" >
2024-04-27 23:02:05 +02:00
BYTES type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_TYPE" value= "10" enum= "Type" >
2024-04-27 23:02:05 +02:00
TYPE type.
2024-04-27 21:08:04 +02:00
</constant>
<constant name= "TYPE_UNKNOWN" value= "11" enum= "Type" >
2024-04-27 23:02:05 +02:00
UNKNOWN type.
2024-04-27 21:08:04 +02:00
</constant>
</constants>
</class>