Kohana 3 ORM compares 2 columns in where clause - kohana-3

I need to generate query like that :
SELECT * FROM `table1` WHERE `date1` < `date2`
I can't find how to compare 2 columns in kohana ORM. Here date2 is considered as text.
$foo = ORM::factory('model1')->where('date1','<','date2');
How can I write this line ?
Thanks!
More info:
I use this for the moment:
$query = DB::query(Database::SELECT, "SELECT id FROM table1 WHERE `date1` < `date2`");
$result = $query->execute();
$foo = array();
foreach ($result as $r) {
$foo[] = ORM::factory("model1", $r['id']);
}

If you don't want Kohana to modify the string, as it would with the 3rd argument in the DB where function, you can use the DB::expr() function which will leave what you pass unmodified. So with your example, you could use
$foo = ORM::factory('model1')->where('date1','<',DB::expr('date2'));

Related

Reuse code that contains variables in same script bash

I have a small block of code in bash like below
#!/bin/bash
query="select * from table where id = ${row_id}"
row_id=1
echo "$query"
row_id=3
echo "$query"
row_id=4
echo "$query"
expected output is below
select * from table where id = 1
select * from table where id = 3
select * from table where id = 5
But I am getting nothing as output
I know I am referencing variable before assigning it.
The idea here is to use reusable code instead of writing the same code at many places
How can I achieve what I want
You can create a function and call the function at various place by assign variable to it
#!/bin/bash
# create a function with variable and write your command
# here your command is print the query
my_function_name(){
arg1=$1
echo "select * from table where id = ${arg1}"
}
# assign varaible
row_id=1
# print the ourput of function when above variable is assigned
query=$(my_function_name "$row_id")
echo $query
# assign varaible
row_id=2
# print the ourput of function when above variable is assigned
query=$(my_function_name "$row_id")
echo $query
# assign varaible
row_id=3
# print the ourput of function when above variable is assigned
query=$(my_function_name "$row_id")
echo $query
you should be getting
select * from table where id =
select * from table where id =
select * from table where id =
and as you already mentioned the reason is
I know I am referencing variable before assigning it.
One way to implement this
$ for row_id in 1 3 5;
do
echo "select * from table where id = $row_id";
done
select * from table where id = 1
select * from table where id = 3
select * from table where id = 5
UPDATE
Based on the comment
Here if row_id is a random variable I get as part of another query then
how do I get the correct query as my output
which is different from the posted question, better to define a function
$ getquery() { echo "select * from table where id = $1"; }
$ getquery $RANDOM
select * from table where id = 12907

LinqtoExcel: Not able to use where clause to select a row data

I am using linqtoexcel for my automation project. This file contains different site URL and its credentails. If I use only one row I am able to login to that site. But if there are two rows then I am having problem in using where clause.
Please help if you can if you do not understood anything please ask question.
My excel Sheet
Below is my code.
string pathfile = #"..\..\Data.xlsx";
string sheetName = "Login";
var excelFile = new ExcelQueryFactory(pathfile);
var abc = from a in excelFile.Worksheet(sheetName).AsEnumerable() where Row.Field<String>("ID").Trim() == "2" select a;
PropertiesCollection.driver.Manage().Window.Maximize();
foreach (var a in abc)
{
PropertiesCollection.driver.Navigate().GoToUrl(a["URL"]);
}
foreach (var a in abc)
{
objLogin.Login(a["uname"], a["paswd"]);
}
I know I am wrong as per my code. Also I know, I have an error in Field<String>
Please guide what will the better way to use where clause in linqtoexcel.
This will help a specific row as per you condition. You just have to make changes in where clause.
string pathfile = #"..\..\Data.xlsx";
string sheetName = "Login";
var excelFile = new ExcelQueryFactory(pathfile);
var abc = from a in excelFile.Worksheet(sheetName).AsEnumerable()
where a["ID"] == "2"
select a;
PropertiesCollection.driver.Manage().Window.Maximize();
foreach (var a in abc)
{
PropertiesCollection.driver.Navigate().GoToUrl(a["URL"]);
}
foreach (var a in abc)
{
objLogin.Login(a["uname"], a["paswd"]);
}

Different approach for pagination - LIMIT when using SQLSRV

I'm trying to make an instant pagination using SQLSRV and PHP, I have successfully did this using MySQL but unable to do so when using SQL Server as it does not support LIMIT.
I have the following codes working in MySQL and I wanted to apply the same thing in sqlsrv but since this is not possible, I'm looking forward in creating a different approach(code) to achieve this, can someone give me an idea or a walkthrough to make this happen please, thanks in advanced.
if(isset($_POST['page'])):
$paged=$_POST['page'];
$sql="SELECT * FROM `member` ORDER BY `member`.`member_id` ASC";
if($paged>0){
$page_limit=$resultsPerPage*($paged-1);
$pagination_sql=" LIMIT $page_limit, $resultsPerPage";
}
else{
$pagination_sql=" FETCH 0 , $resultsPerPage";
}
$result=sqlsrv_query($sql.$pagination_sql);
Try the following code, I hope you find it helpful
$paged = filter_input(INPUT_POST, 'page', FILTER_SANITIZE_NUMBER_INT);
//Initialize these values
$Table = 'your_tbl_name'; //Table name
$IndexColumn = 'pk_col_name'; //Primary key column
$resultsPerPage = '10'; //Page size
$Where = ''; //Optional WHERE clause, may leave empty
$Order = ''; //Optional ORDER clause, may leave empty
$top = ($paged>0) ? $resultsPerPage * ($paged-1) : 0 ;
$limit = 'TOP ' . $resultsPerPage ;
$pagination_sql = "SELECT $limit *
FROM $Table
$Where ".(($Where=="")?" WHERE ":" AND ")." $IndexColumn NOT IN
(
SELECT $IndexColumn FROM
(
SELECT TOP $top *
FROM $Table
$Where
$Order
)
as [virtTable]
)
$Order";
$result=sqlsrv_query($conn, $pagination_sql);

Searching phpbb's 'topic_title' via MYSQL php, but exact match doesn't work

$sql = sprintf( "SELECT topic_title
FROM `phpbb_topics`
WHERE `topic_title` LIKE '%%%s%%' LIMIT 20"
, mysql_real_escape_string('match this title')
);
Which I run this query in phpMyAdmin the results are: (correct)
match this title
match this title 002
But when I run that same MYSQL query in PHP I get: (incorrect)
match this title 002
I have also tried MATCH AGAINST with the same result with both php and phpMyAdmin:
$sql = "SELECT topic_title
FROM phpbb_topics
WHERE MATCH (topic_title)
AGAINST('match this title' IN BOOLEAN MODE)";
The whole block of code im using to search with:
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("phpbb") or die(mysql_error());
$query = "match this title";
$query = "SELECT topic_title
FROM phpbb_topics
WHERE MATCH (topic_title)
AGAINST('$query' IN BOOLEAN MODE)";
// Doesn't work (these 2 both give the same result "match this title 002" and no the "match this title")
// $query = "SELECT * FROM `phpbb_topics`
// WHERE `topic_title`
// LIKE '%$query%'
// LIMIT 0, 30 "; // Doesn't work
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$topic_title = $row['topic_title'];
echo "$topic_title";
}
Any idea as to what i'm doing wrong?
I'v been searching all over the place and have found next to no help :(
The problem is that after you execute your query you fetch the first row, do nothing with it, enter the loop by fetching the second row and start printing results..
If you remove the first $row = mysql_fetch_array($result), (directly after $result = mysql_query($query) or die(mysql_error());) you should be fine.
Another comment; If you echo a variable you don't have to put any qoutes around it. And in the way you're doing it now, you won't get a newline between the results so you might want to change that line to echo $topic_title . "<br>";

How to use a string literal to specify a column name in SubSonic 3.0

Here is a statement in SubSonic which does a find using the strongly typed column ProductId:
var products = Product.Find(x => x.ProductID <= 10);
Is there a way to not use a strongly typed column name and instead specify the column name with a string literal like so:
var columnName = "SampleColumn";
var products = Product.Find(x => x[columnName] <= 10);
Or something similar?
You can't do this with linq but you could do it using a fluent query as follows:
string columnName = "SampleColumn";
List<Product> products = new Select()
.From<Product>()
.Where(columnName).IsLessThanOrEqualTo(10)
.ExecuteTypedList<Product>();

Resources