SQL Server CE - IMAGE column - delphi

I know that IMAGE datatype is deprecated since a while, but for my project I don't have the choice.
The question is: can I have several IMAGE columns in a table in SQL Server CE?
Because when I make an insert (ADO in Delphi) with more than 1 IMAGE column, my program hangs when I call ExecSQL().
With just one IMAGE column, it works:
HSLmySQL.ADOQuery1.SQL.Clear();
HSLmySQL.ADOQuery1.SQL.Add( 'INSERT INTO ' + CONFIG_Table_Name + ' SET ' +
CONFIG_Table[ aCONFIG_PARAM ] + '=' + ':param_blob'; + ' ;' );
HSLmySQL.ADOQuery1.Parameters.ParamByName('param_blob').LoadFromStream( Mstream1, ftBlob );
HSLmySQL.ADOQuery1.ExecSQL();
But with more than one IMAGE column:
HSLmySQL.ADOQuery1.SQL.Clear();
HSLmySQL.ADOQuery1.SQL.Add( 'INSERT INTO ' + CONFIG_Table_Name + ' SET ' +
CONFIG_Table[ aCONFIG_PARAM ] + '=' + ':param_blob', +
CONFIG_Table[ aCONFIG_SCHEDULES ] + '=' + ':schedules_blob'; + '
;' );
HSLmySQL.ADOQuery1.Parameters.ParamByName('param_blob').LoadFromStream( Mstream1, ftBlob );
HSLmySQL.ADOQuery1.Parameters.ParamByName('schedules_blob').LoadFromStream( Mstream2, ftBlob );
HSLmySQL.ADOQuery1.ExecSQL(); // => Boom, no exceptions catch nothing ..
and my program hangs.

Related

How to make 3d model from code and export it as .obj file

I'm trying to create a 3d model from script and then i want it to store it as .obj .fbx or .dae format is there any way that I could make it from OpenCV or OpenGL?
def save_obj(vertex,index,uv=None):
with open("/pathto/file.obj", 'w') as the_file:
for v in vertex:
the_file.write(
'v ' + str(v[0]) + ' ' + str(v[1]) + ' ' + str(v[2]) + '\n')
if uv is not None:
for v in uv:
the_file.write(
'vt ' + str(v[0]) + ' ' + str(v[1]) + '\n')
for ind in index:
the_file.write('f ' + str(ind[0] + 1) + '/' + str(ind[0] + 1) +'/ ' + str(ind[1]+1) + '/'+ str(ind[1] + 1) + '/ ' + str(ind[2]+1)+ '/'+ str(ind[2] + 1) + '/' + '\n')

Zend 2 escaping single-quote

I am using Zend Framework 2 to generate the following escaped single-quote SQL query,
SELECT
`document`.*
FROM
`document`
WHERE
(
`document`.`document_taxon` LIKE '%Men\'s Health %' --escaped quote
AND `document`.`document_source_id` = ' 5 '
AND `document`.`document_published` = ' 1 '
AND `document`.`document_deleted` = ' 0 '
)
ORDER BY
`document_id` DESC
LIMIT 25 OFFSET 0
But I am getting this instead,
SELECT
`document`.*
FROM
`document`
WHERE
(
`document`.`document_taxon` LIKE '%Men's Health%'
AND `document`.`document_source_id` = ' 5 '
AND `document`.`document_published` = ' 1 '
AND `document`.`document_deleted` = ' 0 '
)
ORDER BY
`document_id` DESC
LIMIT 25 OFFSET 0
And here is my code
class DocumentTable extends TableGateway
{
....
$select=$this->getSql()->select();
$select->columns(array('*'));
$select->where
->NEST
->like('document_taxon', '%' . $label . '%')
->and
->equalTo('document_source_id', $sourceId)
->and
->equalTo('document_published', true)
->and
->equalTo('document_deleted', 0)
->UNNEST;
$select->order('document_id DESC');
$select->limit($limit);
$select->offset($offset);
...
}
I tried,
$this->getAdapter()->getPlatform()->quoteValue($string)
\Zend\Db\Sql\Expression("%". $label . "%")
str_replace("'", "\'", $label)
But I didn’t have much luck. I welcome any suggestion to solve this issue.
I worked it out. I was passing a normalized “label” value instead of the raw value. The above code snippet works fine.

Check if number of OrdersHistoryTotal in Metatrader 4 trade history changed

I would like to fire a function as soon as a trade was closed (= the OrdersHistoryTotal increased by at least 1).
Is there any handler in MQL4 for such scenarios?
In my particular setup I have the following function pushSocket which should only push data in case the OrdersHistoryTotal changed.
int i,hstTotal=OrdersHistoryTotal();
string historical_trades = "";
for(i=0;i<hstTotal;i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) continue;
historical_trades = historical_trades +
"historical_trades|" +
version + "|" +
DID + "|" +
AccountNumber() + "|" +
IntegerToString(OrderTicket()) + "," +
TimeToString(OrderOpenTime(), TIME_DATE|TIME_SECONDS) + "," +
TimeToString(OrderCloseTime(), TIME_DATE|TIME_SECONDS) + "," +
IntegerToString(OrderType()) + "," +
DoubleToString(OrderLots(),2) + "," +
OrderSymbol() + "," +
DoubleToString(OrderOpenPrice(),5) + "," +
DoubleToString(OrderClosePrice(),5) + "," +
DoubleToString(OrderStopLoss(),5) + "," +
DoubleToString(OrderTakeProfit(),5) + "," +
DoubleToString(OrderCommission(),2) + "," +
DoubleToString(OrderSwap(),2) + "," +
DoubleToString(OrderProfit(),2) + "," +
"<" + OrderComment() + ">|";
}
pushSocket.send(StringFormat("%s", historical_trades, true));
I tried to insert a counter to compare it but the counter is deleted every time on memory clean-up.. The above function is nested in a onTick function which executes every second.
There is a function OnTradeAction() in MQL5 that is called every time some trading action is done. But unfortunately this function is not available in MQL4. On the other hand, you can implement a function inside OnTick() that will check that HistoryTraderTotal() increased compared to the previously saved value, and do all the steps you like in that case. A bit more work but almost the same.
OnTimer(){
Mt4OnTradeAction();
otherLogic();
}
Mt4OnTradeAction(){
static int historyTradesTotal=0;
if(HistoryTradesTotal()==historyTradesTotal) return;
historyTradesTotal = HistoryTradesTotal();
processingTrade();
}

Passing in variables to SQL Server stored procedure, where clause

I am writing an application responsible for archiving data and we have the configuration in a database table
Id | TableName | ColumnName | RetentionAmountInDays
1 | DeviceData | MetricTime | 3
So when faced with this configuration, I should archive all data in the DeviceData table where the MetricTime value is before 3 days ago.
The reason I am doing this dynamically is the table names and column names differ (there would be multiple rows)
For each configuration this stored procedure is called
CREATE PROCEDURE GetDynamicDataForArchive
#TableName nvarchar(100),
#ColumnName nvarchar(100),
#OlderThanDate datetime2(7)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql nvarchar(1000);
SET #sql = 'SELECT * FROM ' + #TableName + ' WHERE ' + #ColumnName + ' < #OlderThanDate';
exec sp_executesql #sql;
END
And an example exec line
exec dbo.GetDynamicDataForArchive 'DeviceData', 'MetricTime', '2017-04-16 20:29:29.647'
This results in:
Conversion failed when converting date and/or time from character string.
So something is up with how I am passing in the datetime2 or how I am forming the where clause.
Replace this statement:
SET #sql = 'SELECT * FROM ' + #TableName + ' WHERE ' + #ColumnName + ' < #OlderThanDate'
by
SET #sql = 'SELECT * FROM ' + #TableName + ' WHERE [' + #ColumnName + '] < ''' + cast(#OlderThanDate as varchar(23)) + '''';
I don't particularly like having to convert the datetime to a varchar value though, perhaps there is a better way to do this(?).

How to get user name who updated the data from UI in MVC 5 and Entity Framework

We have a website in ASP.NET MVC 5 with Entity Framework.
When a logged in user made changes in UI (i.e. update the data) we save/update/delete the data in SQL Server as per operation performed by the user.
We also have a trigger for audit trailing.
With the trigger, table format for storing data is as below:
[AuditID]
[Type] -- Contains operation performed (Insert (I), Update (U), Delete (D))
[TableName]
[PK] -- Primary Key
[FieldName]
[OldValue]
[NewValue]
[UpdateDate]
[UserName]
We are storing SYSTEM_USER in [UserName] column.
If we have any solution to store the [UserName] who actually made changes from UI instead of system_user?
Do we have any approach to pass [UserName] from application (UI) to the trigger?
Please share your thoughts.
I have one solution - to add UpdatedBy column name in all the tables so that trigger can easily get the value of UpdatedBy column from magic tables or from main tables.
Please suggest best approach.
Below is the trigger used.
CREATE TRIGGER [ids].[tr_AuditEmploee]
ON Employee
FOR INSERT, UPDATE, DELETE
AS
DECLARE #bit INT,
#field INT,
#maxfield INT,
#char INT,
#fieldname VARCHAR(128),
#TableName VARCHAR(128),
#PKCols VARCHAR(1000),
#sql VARCHAR(2000),
#UpdateDate VARCHAR(21),
#UserName VARCHAR(128),
#Type CHAR(1),
#PKSelect VARCHAR(1000)
--You will need to change #TableName to match the table to be audited
SELECT #TableName = 'Employee'
-- date and user
SELECT
#UserName = SYSTEM_USER,
#UpdateDate = CONVERT(VARCHAR(8), GETDATE(), 112) + ' ' + CONVERT(VARCHAR(12), GETDATE(), 114)
-- Action
IF EXISTS (SELECT * FROM inserted)
IF EXISTS (SELECT * FROM deleted)
SELECT #Type = 'U'
ELSE
SELECT #Type = 'I'
ELSE
SELECT #Type = 'D'
-- get list of columns
SELECT * INTO #ins FROM inserted
SELECT * INTO #del FROM deleted
-- Get primary key columns for full outer join
SELECT
#PKCols = COALESCE(#PKCols + ' and', ' on')
+ ' i.' + c.COLUMN_NAME + ' = d.' + c.COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
WHERE
pk.TABLE_NAME = #TableName
AND CONSTRAINT_TYPE = 'PRIMARY KEY'
AND c.TABLE_NAME = pk.TABLE_NAME
AND c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
-- Get primary key select for insert
SELECT
#PKSelect = COALESCE(#PKSelect+'+','')
+ '''' + COLUMN_NAME
+ '=''+convert(varchar(100),
coalesce(i.' + COLUMN_NAME +', d.' + COLUMN_NAME + '))+'''''
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
WHERE
pk.TABLE_NAME = #TableName
AND CONSTRAINT_TYPE = 'PRIMARY KEY'
AND c.TABLE_NAME = pk.TABLE_NAME
AND c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
IF #PKCols IS NULL
BEGIN
RAISERROR('no PK on table %s', 16, -1, #TableName)
RETURN
END
SELECT
#field = 0,
#maxfield = MAX(ORDINAL_POSITION)
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = #TableName
WHILE #field < #maxfield
BEGIN
SELECT #field = MIN(ORDINAL_POSITION)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = #TableName
AND ORDINAL_POSITION > #field
SELECT #bit = (#field - 1 )% 8 + 1
SELECT #bit = POWER(2,#bit - 1)
SELECT #char = ((#field - 1) / 8) + 1
IF SUBSTRING(COLUMNS_UPDATED(),#char, 1) & #bit > 0 OR #Type IN ('I','D')
BEGIN
SELECT #fieldname = COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = #TableName
AND ORDINAL_POSITION = #field
SELECT #sql = '
insert [audit].[AuditEmployee] ( Type,
TableName,
PK,
FieldName,
OldValue,
NewValue,
UpdateDate,
UserName)
select ''' + #Type + ''','''
+ #TableName + ''',' + #PKSelect
+ ',''' + #fieldname + ''''
+ ',convert(varchar(1000),d.' + #fieldname + ')'
+ ',convert(varchar(1000),i.' + #fieldname + ')'
+ ',''' + #UpdateDate + ''''
+ ',''' + #UserName + ''''
+ ' from #ins i full outer join #del d'
+ #PKCols
+ ' where i.' + #fieldname + ' <> d.' + #fieldname
+ ' or (i.' + #fieldname + ' is null and d.' + #fieldname + ' is not null)'
+ ' or (i.' + #fieldname + ' is not null and d.' + #fieldname + ' is null)'
EXEC (#sql)
END
END

Resources