if I make a function :
def test
User.where(name: 'foo')
return 42
end
If I call test method the sql is not done.
So where method just construct the request but not execute it.
It's like that we can add where or others sql method like limit, order...etc
Apparently, each time, this method construct request sql but not execute it.
So where, in source code, SQL are execute? What trigger this?
I don't understand the current mechanism...
Related
It looks like with postgres, there are two ways to prepare and execute prepared statements. You can use the functions PQprepare and PQexecPrepared directly from libpq; or you can issue SQL statements: PREPARE and EXECUTE. The statement names are the same across either method so you can use PQPrepare to prepare a statement and then execute it by issuing an EXECUTE query (or use a PREPARE query and then execute it with PQexecPrepared).
So the two approaches (library functions vs SQL queries) are equivalent. However, it looks like when you use PQexecPrepared, the query column of pg_stat_activity is the underlying prepared statement with placeholders. So something like:
SELECT * from users where name in ($1, $2, $3);
But when you use an EXECUTE query, pg_stat_activity contains the SQL of the EXECUTE, like:
EXECUTE user_query('joe', 'bob', 'sally');
Questions
Is there a way to get the same output for the two different ways of executing prepared statements?
Is there a way to see both the query, and the bound parameters when executing prepared statements?
You are right that both ways to execute a prepared statement do the same thing under the hood, but since they are called in different ways on the SQL level, they look different in pg_stat_activity. There is no way to change that.
To get the statement and the parameters you must resort to the log file.
In the case of PQexecPrepared, you will see the statement as a LOG message and the parameters as its DETAIL if you turn on statement logging.
With PREPARE and EXECUTE, you have no choice but to find the PREPARE earlier in the session (both have the same session identifier, that is %c in log_line_prefix).
I issue SP from Jsp, code is here:
sql = "EXEC REP_INVOICES ?,?";
java.sql.PreparedStatement ps = cox.prepareStatement(sql);
ps.setEscapeProcessing(true);
ps.setInt(1, cycle);
ps.setInt(2, zone);
ps.execute();
Procedure runs several seconds and fills a table. I want to know when it is over to make outputs. Is it possible?
I has been ages since I did java.
However, there is JDBC and SQL libraries.
I think you are using the first. Here is a link to execute().
http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html
I guess the main question is this statement modal? After the execute() does the java wait until the call is finished? I think so.
Have the stored procedure return a value. Check it. After that, you whole table should be populated.
The only other alternative is to do paging. Load only a few records in the table at a time. Should produce quicker response times.
Here is the definition from the manual on return values and how to get them from a SPROC call.
execute
boolean execute()
Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL
statement. Some prepared statements return multiple results; the execute method handles these
complex statements as well as the simpler form of statements handled by the methods
executeQuery and executeUpdate.
The execute method returns a boolean to indicate the form of the first result. You must call
either the method getResultSet or getUpdateCount to retrieve the result; you must call
getMoreResults to move to any subsequent result(s).
Firstly, I apologise if this is a really stupid question.
I had a question about dealing correctly with SQL statements within Yii. I'll make a small example code.
public function actionCreate($id) {
$cmd = Yii::app()->db->createCommand();
$cmd->insert('table_1',array(
'user_id'=> (int) $id,
),'id=:id', array(':id'=>$id));
}
What's the correct way to confirm this query worked? Is it try/catch blocks?
The reason I ask is that could fail if it's passed a bad parameter, but on a couple of tables I have DB constraints that could also result in a failure, so I wanted to try and make sure I handled everything properly rather than blanket handle them.
From official document
Executing SQL Statements Once a database connection is established, SQL statements can be executed using CDbCommand. One
creates a CDbCommand instance by calling
CDbConnection::createCommand() with the specified SQL statement:
$connection=Yii::app()->db; // assuming you have configured a "db" connection
// If not, you may explicitly create a connection:
// $connection=new CDbConnection($dsn,$username,$password);
$command=$connection->createCommand($sql);
// if needed, the SQL statement may be updated as follows:
// $command->text=$newSQL;
A SQL statement is executed via CDbCommand in one of the following two ways:
And here it is
execute(): performs a non-query SQL statement, such as INSERT, UPDATE
and DELETE. If successful, it returns the number of rows that are
affected by the execution.
Btw, insert() is a low level method that's used internally by Active Record (AR). Why don't you simply use AR instead
By Yii gii, you automatically get model for table_1, and you can find, insert, update, delete from that. Example:
$model = new Table1ModelName;
$model->user_id= $id;
$model->name= $user_name;
...
$model->save();
There still has many workarounds and interesting things which you would like to study about
Yii Working Active Record
I'm using a LINQ to SQL class to define a query, and then calling the ToList() method, which executes the query. The ToList() call is wrapped in a 'using TransactionScope' block to achieve the 'NOLOCK' hint. The query is read-only, so I'm not sure if the call to TransactionScope.Complete() is necessary in this case. Here is the example I followed, which does not make this call: Getting LINQ to SQL and Entities to use NOLOCK
I'm thinking no. Complete() in C# is the same as calling Commit in SQL, but since this is a select statement there is nothing to commit. Any other residual constructs resulting from the TransactionScope will be disposed of at the end of the using { } block so I don't think Complete() is necessary in your case.
I am trying call a database-function from HQL. The HQL statement should just call the function and return its value, like this
select someFunction(:someParameter)
If I try to call select current_timestamp().
It fails with
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Exception of type 'Antlr.Runtime.MismatchedTreeNodeException' was thrown. near line 1, column 24 [select current_timestamp()]
I know that there is not much reason for retrieving the current timestamp. But I have created a few user-defined database-functions that I would like to unit-test by calling them from HQL.
It seems to me that it's not possible to write a HQL statement without a FROM and WHERE clause. Can this be true?
IQuery query = unitOfWork.Session.CreateQuery("select current_timestamp()");
var ts = query.UniqueResult();
This could work if you explicitly make it an SQL Query, not an HQL query:
var query = unitOfWork.Session.CreateSQLQuery("select current_timestamp()");
It doesn't make much sense to call a function like that. Either you have a stored procedure which should be called using other methods, or you have a function which is not related to the DB at all, and therefore should not be running via the database to begin with.
In any case, if the function doesn't work on a set of rows, can't you just add some dummy FROM clause, for example on a small static table?