Re: Swedish letters fuck up parsing into SQL querry

2020-03-24 Thread Anders S via Digitalmars-d-learn

On Tuesday, 24 March 2020 at 14:10:19 UTC, WebFreak001 wrote:

On Tuesday, 24 March 2020 at 11:15:24 UTC, matheus wrote:

On Monday, 23 March 2020 at 15:41:50 UTC, Adam D. Ruppe wrote:

On Monday, 23 March 2020 at 15:15:12 UTC, Anders S wrote:

I'm creating a connection to the db and conn.exec(sql)


It depends on the library but it is almost always easier to 
do it right than to do it the way you are.


like with my lib it is

db.query("update celldata set name = ?", new_name);


I'm not the OP but I have a question, isn't this passive to 
SQL injection too, or your LIB will handle this somehow?


If is the later could you please point the code on GitHub?

Matheus.


https://github.com/mysql-d/mysql-native/blob/8f9cb4cd9904ade43af006f96e5e03eebe7a7c19/source/mysql/protocol/comms.d#L494

it's builtin into mysql


Ahhh, thanks need to dig into this and learn.

Thanks guys for all the responses. Got plenty of leads to dig 
into, also issues I have to consider to be a better coder ;)

Thks again


Re: Swedish letters fuck up parsing into SQL querry

2020-03-23 Thread Anders S via Digitalmars-d-learn

On Monday, 23 March 2020 at 15:07:31 UTC, Adam D. Ruppe wrote:

On Monday, 23 March 2020 at 14:26:46 UTC, Anders S wrote:
do you mean I should loop through each pos till 
strlen(cellTab[CellIndex].name) to find "\0"?


strlen is ok, that gives the answer itself. Just slice to that.

cellTab[CellIndex].name[0 .. 
strlen(cellTab[CellIndex].name.ptr)]


could do it. or

size_t end = 0;
foreach(idx, ch; cellTab[CellIndex].name)
   if(ch == 0) {
end = idx;
break;
   }

auto name = cellTab[CellIndex].name[0 .. end];


anything like that


How do you suggest I do the querry build then?


how are you running it? using a lib or just generating a .sql 
file?


Hi,
I'm creating a connection to the db and conn.exec(sql)
I think I'll try the foreach to find out if it works  ( 
tomorrow )





Re: Swedish letters fuck up parsing into SQL querry

2020-03-23 Thread Anders S via Digitalmars-d-learn

On Monday, 23 March 2020 at 14:58:03 UTC, bauss wrote:

On Monday, 23 March 2020 at 14:26:46 UTC, Anders S wrote:

On Monday, 23 March 2020 at 13:53:50 UTC, Adam D. Ruppe wrote:
My first thought is to!string(cellTab[CellIndex].name) is 
wrong, if it is a char[20] you should be scanning it to find 
the length and slicing. Maybe [0 .. name.indexOf("\0")] or 
whatever.


You also shouldn't be building a query by concatenation.


Hi, thks

do you mean I should loop through each pos till 
strlen(cellTab[CellIndex].name) to find "\0"?


How do you suggest I do the querry build then?


This is open to sql injection.

I thought we were rid of this in this day and age.

Use prepared statements.


Yes true however I'm in early development and want to get a red 
line working, then take care of the issues ;)


Re: Swedish letters fuck up parsing into SQL querry

2020-03-23 Thread Anders S via Digitalmars-d-learn

On Monday, 23 March 2020 at 13:53:50 UTC, Adam D. Ruppe wrote:
My first thought is to!string(cellTab[CellIndex].name) is 
wrong, if it is a char[20] you should be scanning it to find 
the length and slicing. Maybe [0 .. name.indexOf("\0")] or 
whatever.


You also shouldn't be building a query by concatenation.


Hi, thks

do you mean I should loop through each pos till 
strlen(cellTab[CellIndex].name) to find "\0"?


How do you suggest I do the querry build then?


Swedish letters fuck up parsing into SQL querry

2020-03-23 Thread Anders S via Digitalmars-d-learn

Hi guys,

I'm trying to read a name from a struct iorequest where the name 
is char name[20]
The struct is received through a FIFO pipe and message is going 
into a mysql database to update specific post there.


Now my problem is that all works fine to read and stop with  '\0' 
termination till I receive a Swedish character, ie åäö. Then the 
string gets crazy and reads all 20 chars no matter what.


Any ideas how to read all chars including åäö?

Using "~ to!string(name) ~" to build the SQL querry string as 
below


int extract_Cell_From_IOREQ(int CellIndex){
 auto sql ="UPDATE celldata set
name='"~ to!string(cellTab[CellIndex].name) ~"', 
...


Re: How to concat UUID into a SQL query string to MariaDB

2019-08-27 Thread Anders S via Digitalmars-d-learn

On Tuesday, 27 August 2019 at 08:30:50 UTC, Jani Hur wrote:

On Tuesday, 27 August 2019 at 08:08:05 UTC, Anders S wrote:

Any ideas?


+ is not a string concatenation. Try ~ instead:

auto x = "aa" ~ "bb" ~ "cc";


Hi again, the auto declaration worked as I expected my 
catenations should with the string


So thanks




Re: How to concat UUID into a SQL query string to MariaDB

2019-08-27 Thread Anders S via Digitalmars-d-learn

On Tuesday, 27 August 2019 at 08:30:50 UTC, Jani Hur wrote:

On Tuesday, 27 August 2019 at 08:08:05 UTC, Anders S wrote:

Any ideas?


+ is not a string concatenation. Try ~ instead:

auto x = "aa" ~ "bb" ~ "cc";


Hi thanks for answer, but didn't help. Got this error instead :

Error: cannot implicitly convert expression "UPDATE guirequest 
SET done_request = SYSDATE() WHERE request_id=" ~ 
cast(const(char)[])hash[cast(uint)i] ~ ";" of type char[] to 
string



Notice you use auto x  Am I better of using something like

auto sql_respons ="UPDATE guirequest SET done_request = SYSDATE() 
WHERE request_id=" ~ hash[i] ~ ";";


How to concat UUID into a SQL query string to MariaDB

2019-08-27 Thread Anders S via Digitalmars-d-learn

Hi guys,

Using MariaDB to communicate between appz via FIFO pipe

Now I stumbled on the next problem, how to extract UUID from 
database into an UPDATE query that is a string ( the sql variable 
).


Got in a loop:

char [16][10] hash;

for(i =0; i < count; i++){

auto hash1 = row[0];

hash[i] = hash1.get!(string);

... do some FIFO Pipe work

sql = "UPDATE guirequest SET done_request = SYSDATE() WHERE 
request_id=" + hash[i] + ";";


Get this error :Error: invalid array operation "UPDATE guirequest 
SET done_request = SYSDATE() WHERE request_id=" + 
hash[cast(uint)i] (possible missing [])


Any ideas?

/a


Re: How do I extract and convert datatypes from ResultRange

2019-08-26 Thread Anders S via Digitalmars-d-learn

On Monday, 26 August 2019 at 13:58:34 UTC, Olivier Pisano wrote:

On Monday, 26 August 2019 at 13:49:21 UTC, Anders S wrote:

[...]


I don't have a compiler accessible right now, but according to 
https://dlang.org/phobos/std_variant.html :


int i;
if (row[2].peek!int !is null)
i = row[2].get!int



Hi Olivier, thanks man that did it.
/a


How do I extract and convert datatypes from ResultRange

2019-08-26 Thread Anders S via Digitalmars-d-learn

Hi guys,

I'm trying to read a post of different datatypes from MariaDB 
table into another message and pass it on into a FIFO pipe to an 
other application.


My code :
   string sql = "SELECT * FROM guirequest WHERE read_request = 
-1;";

ResultRange range = conn.query(sql);
Row row = range.front;
int i, count = (to!int(range.rowCount));
writeln("rowCount ", range.rowCount, "\n"); // sofar so good

The problem occurs when I wish to extract row data into string, 
int or short datatypes.

It complains on  int i = row[2]; with
Error: cannot implicitly convert expression row.opIndex(2u) of 
type VariantN!24u to int


Same for other datatypes.

Any ideas?
/a


Re: How do I execute a sql-file inside D code

2019-08-26 Thread Anders S via Digitalmars-d-learn

On Tuesday, 20 August 2019 at 13:10:55 UTC, Andre Pany wrote:

On Tuesday, 20 August 2019 at 11:33:33 UTC, Anders S wrote:
I'm creating an application that connect to a database and 
write data from another application. Now when I start the 
application I want it to check if the database exists and if 
not create the database and it's tables.


I have everything working IF the database and tables exist.

Use this code to check
conn.exec("CREATE DATABASE IF NOT EXISTS boxweb;");

however haven't found a way to run the sql file that create 
the tables. The file is in the source folder


Any ideas ?


You need to use an additional library to communicate with the 
database. The library to use depends on the type of database 
(Oracle, DB2, sqlite, Hana, postgres,...)


Which database type do you target?

Kind regards
Andre


I'm using MariaDB on std port 3306
/a




Re: How do I execute a sql-file inside D code

2019-08-26 Thread Anders S via Digitalmars-d-learn

On Thursday, 22 August 2019 at 13:39:00 UTC, XavierAP wrote:

On Tuesday, 20 August 2019 at 11:33:33 UTC, Anders S wrote:


Use this code to check
conn.exec("CREATE DATABASE IF NOT EXISTS boxweb;");

however haven't found a way to run the sql file that create 
the tables. The file is in the source folder


I understand you're using some API to some SQL implementation 
which allows you to run SQL commands from strings, but not from 
files which is what you want?


Just read the file into a string with the D std lib:

import std:file;
conn.exec( readText(fileName) );

https://dlang.org/phobos/std_file.html#.readText


Thanks XavierAP, that did the trick
/a


How do I execute a sql-file inside D code

2019-08-20 Thread Anders S via Digitalmars-d-learn
I'm creating an application that connect to a database and write 
data from another application. Now when I start the application I 
want it to check if the database exists and if not create the 
database and it's tables.


I have everything working IF the database and tables exist.

Use this code to check
conn.exec("CREATE DATABASE IF NOT EXISTS boxweb;");

however haven't found a way to run the sql file that create the 
tables. The file is in the source folder


Any ideas ?


Re: How do I filter out data from mongodb in D-code

2017-10-10 Thread Anders S via Digitalmars-d

On Tuesday, 10 October 2017 at 09:43:10 UTC, Anders S wrote:
I'm working on a middleware application that reads array of 
data from a POSIX pipe and insert data into the db if any 
position in the array has changed.
This is where my problem lays, in order not to duplicate data I 
want to fetch the latest data/document in the array of 
documents.


To clarify,
This is where my problem lays, in order not to duplicate data I 
want to fetch the >latest data/document in the array of 
documents.
refers to latest inserted data/document in the mongodb 
collection. Also that the insert of document that changed include 
a timestamp like this

dateAdded: new Date()


How do I filter out data from mongodb in D-code

2017-10-10 Thread Anders S via Digitalmars-d

Hi,

I'm working on a middleware application that reads array of data 
from a POSIX pipe and insert data into the db if any position in 
the array has changed.
This is where my problem lays, in order not to duplicate data I 
want to fetch the latest data/document in the array of documents.

I'm using MS Code and dub to code and compile.

This is the code:
Collection ct = mongo.boxweb.celltab;
int i = 1;
for(;i < 10; i++ ){
   auto cell = 
ct.find({"number":i}).sort({_id:-1}).limit(2).pretty();
   if (insert...


   i++;

Where "number" is the document nr in the array

I get these errors
source/app.d(166,54): Error: found : when expecting ; following 
statement
source/app.d(166,56): Error: found } when expecting ; following 
statement

source/app.d(166,57): Error: found ) instead of statement
dmd failed with exit code 1.

Any ideas?
Isn't there a way to markup code in the forum message to separate 
from the bodytext?


/anders


Re: not callable error

2016-12-16 Thread Anders S via Digitalmars-d-learn

On Friday, 4 November 2016 at 23:26:40 UTC, lobo wrote:

On Friday, 4 November 2016 at 14:37:04 UTC, bluphantom91 wrote:

On Friday, 4 November 2016 at 02:59:49 UTC, Paul Backus wrote:
On Friday, 4 November 2016 at 02:28:17 UTC, bluphantom91 
wrote:

Hello,



Am I just using getc the wrong way?


Try something like this:

...
ch = getc(file.getFP);
...

https://dlang.org/phobos/std_stdio.html#.File.getFP

bye,
lobo


Hi, I having equal problems and it looks like you get the 
solution. so is it posible to get the full code listing from 
import  to the counting that is working?

Just as a conclusive answer for others aswell to read.
/anders


Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-30 Thread Anders S via Digitalmars-d-learn

On Wednesday, 30 November 2016 at 13:47:06 UTC, Anders S wrote:
On Wednesday, 30 November 2016 at 12:41:24 UTC, Stefan Koch 
wrote:

On Wednesday, 30 November 2016 at 10:20:35 UTC, Anders S wrote:

int [1] argv;   /* list of arguments */

Is that supposed to be a VLAIS ?
That will not port to D.

It would be helpful If you could share the code and state the 
intent.


Hi,
No to VLAIS (Variable length array in structure). All known 
size of arrays and structures.

testing from terminal with writing to using:
Echo "testing pipe and textformat" > .pipes/1234
and reading with
cat .pipes/1234

that works just fine ;)
/anders


Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-30 Thread Anders S via Digitalmars-d-learn

On Wednesday, 30 November 2016 at 12:41:24 UTC, Stefan Koch wrote:

On Wednesday, 30 November 2016 at 10:20:35 UTC, Anders S wrote:

int [1] argv;   /* list of arguments */

Is that supposed to be a VLAIS ?
That will not port to D.

It would be helpful If you could share the code and state the 
intent.


Hi,
No to VLAIS (Variable length array in structure). All known size 
of arrays and structures.
My intent is to find a way to write a request for data (the 
IOREQ.fc) to an application. The IOREQ is my message struct and 
the c-code on the other end of the pipe use same struct to 
identify request. to keep it backward compatible I need to keep 
IOREQ.
The c-code application will switch out request and return the 
answer with char[] of data using the (IOREQ.src) dlang's created 
pipe. i.e. the pipes is oneway "streets"


The purpose is to port a c-application to use dlang with a web ui

Since I'm new to dlang but know c-code I need to ask stupid level 
questions ;)
Also I'm experimenting on OS X but it is intended for linux x86 
in production, for now.


Only requirement is the struct IOREQ as sending structure and 
char[] as receiving structure, using FIFO pipe's. Outgoing pipe 
is fixed named and returning is based on mypid().


In pseudo code (don't have any working code yet)

open pipe and if doesn't exist create it, to receiver
open own pipe for reading.
create ioreq *io;
create spec sized char buffer
point io to start of buffer
add request to io.fc
add return pipe to io.src
write in pipe
read pipe for answer, into char array
close pipe
disassemble response into various struct data depending on 
request. Here I simply try by returning the sent ioreq and echo 
out the fc and src.


/anders



Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-30 Thread Anders S via Digitalmars-d-learn

On Wednesday, 30 November 2016 at 07:16:38 UTC, Anders S wrote:

On Tuesday, 29 November 2016 at 23:33:19 UTC, Ali Çehreli wrote:

On 11/29/2016 07:30 AM, Anders S wrote:



Ali



Thanks you all guys, and the cast (IOREQ *) ... did the trick!!
I'll have a look at your other comments aswell on struct a.s.o.
/anders


Hi again, still have problem. It works when using plain text but 
not like this:

(Hope you can identify my errors ;) )

import std.range, std.stdio, std.conv;
import core.stdc.string;
import core.stdc.stdio;
import core.sys.posix.sys.stat;
import core.sys.posix.unistd;
import core.sys.posix.fcntl;
import core.sys.posix.sys.stat;

extern (C) uint read(int, void *, uint);
extern (C) uint write(int, void *, ulong);

struct IOREQ {
short   fc; /* function code */
short   rs; /* return code */
int size;   /* size of this request, including */
short   src;/* source */
int [1] argv;   /* list of arguments */
 };

void main(string[] args) {
   //f;
   int fd = -1;
   IOREQ *io;
   fd = mkfifo("/Users/anders/pipes/8556",666 );
   char [1024]rbuf;
   char [1024]sbuf;
   sbuf = "You must use the cast keyword, the pointer to 
first element of an array is .ptr, and I think you meant sbuf:

io = cast(IOREQ *)sbuf.ptr;";

io = cast (IOREQ *) sbuf;
io.fc = 501;
io.rs = 1234;
io.src = 8556;
writeln("\nio.fc :", io.fc);
   writeln("io.rs :", io.rs);
   writeln("\nio.src :", io.src);
writeln ("\nio :",io);
   writeln("Skrev ", write(fd, cast (void*) sbuf, 
sbuf.length));
   writeln("läste ", read(fd, cast (void*) rbuf, 
sbuf.length));


   writeln("\nrbuf :", rbuf);
   io = cast(IOREQ *) rbuf;
   writeln("\nrio.fc :", io.fc);
   writeln("\nrio.rs :", io.rs);
   writeln("\nrio.src :", io.src);
   //unlink("/Users/anders/pipes/8556");

}




Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-29 Thread Anders S via Digitalmars-d-learn

On Tuesday, 29 November 2016 at 23:33:19 UTC, Ali Çehreli wrote:

On 11/29/2016 07:30 AM, Anders S wrote:

> INTargv[1];/* list of arguments */

In addition to what Nemanja Boric wrote, the recommended array 
syntax in D is the following:


INT[1] argv;

>   char sbuf[1024];
>   io = (IOREQ *)buf;  // Not accepted in dlang

You must use the cast keyword, the pointer to first element of 
an array is .ptr, and I think you meant sbuf:


io = cast(IOREQ *)sbuf.ptr;

>   st = write(fd, sbuf, 1024);  //

You can use the .length property of arrays:

st = write(fd, sbuf, sbuf.length);

Ali



Thanks you all guys, and the cast (IOREQ *) ... did the trick!!
I'll have a look at your other comments aswell on struct a.s.o.
/anders




Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-29 Thread Anders S via Digitalmars-d-learn

Hi guys,

I want to write into a fifo pipe using write( ...)
Now a gather my data into my own struct IOREQ so in order to 
write I have to cast into an char buffer.


My problem in dlang is that it doesn't accept the casting (IOREQ 
*) I get:
Error: Cannot implicitly convert expression () of type char 
[1024] to IOREQ*


define FC_GETSTATUS 501;
typedef struct {
SHORT   fc; /* function code */
SHORT   rs; /* return code */
INT size;  /* size of this request, 
including header */
SHORT   src;/* source */

INT argv[1];/* list of arguments */
} IOREQ;

int WritePipe(int fd, int mess, int argc)
{
  struct IOREQ * io; // my array of data
  char sbuf[1024];
  io = (IOREQ *)buf;  // Not accepted in dlang
  io.fc = FC_GETSTATUS;
  io.src = getpid();// works

. // add more data

  st = write(fd, sbuf, 1024);  //
  return st;
}



Re: How can I concatenate a string, a char array and an int

2016-11-29 Thread Anders S via Digitalmars-d-learn

Thanks guys for a really quick answer !!
OK, a little bit awkward to use but getting there
posting a new question about char * to struct ;)

Thanks
/anders





How can I concatenate a string, a char array and an int

2016-11-29 Thread Anders S via Digitalmars-d-learn

Hi guys,

just started to get into Dlang, comming from C and C++ I like to 
use methods like there if possible.


Now I want to catenate something like this, but don't get it to 
work

in standard C i code:
   char str[80];
   sprintf(str, "This is a number = %f", 3.14356);

Now in Dlang and import core.stdc.string and
code:
char [80] str;
sprintf(str, "This is a number = %d", 314356);
writefln("%s", str);

but get error
Error: function core.stdc.stdio.sprintf (char* s, const(char*) 
format, ...) is not callable using argument types (char[80], 
string, int)


Nor does this work
   char [50] temp = "This is a number";
   string greeting5= temp~" "~314356;
   writefln("%s",greeting5);

result in error:
Error: incompatible types for ((cast(const(char)[])temp ~ " ") ~ 
(314356)): 'char[]' and 'int'



Any ideas or hints?
/anders