FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Extend DBLIB to MiniD for access database

 
Post new topic   Reply to topic     Forum Index -> MiniD
View previous topic :: View next topic  
Author Message
ideage



Joined: 12 Jul 2006
Posts: 63
Location: china

PostPosted: Mon Nov 20, 2006 12:54 am    Post subject: Extend DBLIB to MiniD for access database Reply with quote

Code:
module minid.dblib;

import minid.state;
import minid.types;
import std.stdio;

import dbi.all;
import dbi.sqlite.all;
import dbi.mysql.all;
import std.stdio;
import std.c.stdio;
//Retrieve,execsql
// retrieve as table,update table
// curtable,curcolumn,
class DbLib
{
   Database db;
   char[] _params,_username,_password;
   
   this(char[] dbtype,char[] params, char[] username = null, char[] password = null)
   {
      //if (dbtype=="sqlite")   db= new SqliteDatabase();      
      if (dbtype=="mysql") db = new MysqlDatabase(); else    db= new SqliteDatabase();      
      //assert(db);
      //writefln(dbtype);
      _params = params;
      _username = username;
      _password = password;
   }
   
   int execute(MDState s)
   {
      char[] sql = s.getStringParam(0).asUTF8();
                  
      try
      {            
         db.connect(_params,_username,_password);    
         db.execute(sql);               
         db.close();      
      }
      catch(Exception e)
      {
         throw new MDRuntimeException(s, e.toString());
      }            
      return 0;
   }
      
   int queryFetchOne(MDState s)
   {
      char[] sql = s.getStringParam(0).asUTF8();      
      //writefln(sql);      
      char[][] vs;
      MDArray data;            
      try
      {            
         db.connect(_params,_username,_password);        
         Row row = db.queryFetchOne(sql);
         //writefln(row.getdata());   
         data = new MDArray(vs.length);
         for (int idx = 0; idx < vs.length; idx++) {
               data[idx] =   new MDString(vs[idx]);   
         }         
         s.push(data);               
         db.close();      
      }
      catch(Exception e)
      {
         throw new MDRuntimeException(s, e.toString());
      }            
      return 1;
   }
   
   int queryFetchAll(MDState s)
   {
      char[] sql = s.getStringParam(0).asUTF8();
      //writefln(sql);            
      char[][] vs;
      MDArray data;
      
      try
      {            
         db.connect(_params,_username,_password);    
         Row[] rows = db.queryFetchAll(sql);
         //writefln(rows.length);
         data = new MDArray(rows.length);
         foreach (int i,Row row; rows)
         {   
            vs = row.getData();         
            MDArray drow = new MDArray(vs.length);
            for (int idx = 0; idx < vs.length; idx++) {
                  drow[idx] =   new MDString(vs[idx]);   
            }
            data[i] = drow;            
         }
         
         s.push(data);               
         db.close();      
      }
      catch(Exception e)
      {
         throw new MDRuntimeException(s, e.toString());
      }            
      return 1;
   }   
}

public void init(MDState s,char[] dbtype,char[] params, char[] username = null, char[] password = null)
{
   DbLib lib = new DbLib(dbtype,params,username,password);
   
   MDTable dbLib = MDTable.create
   (
      "execute",       new MDClosure(s, &lib.execute,      "dblib.execute"),
      "queryFetchAll",new MDClosure(s, &lib.queryFetchAll,      "dblib.queryFetchAll"),
      "queryFetchOne",new MDClosure(s, &lib.queryFetchOne,      "dblib.queryFetchOne")
   );

   s.setGlobal("dblib"d, dbLib);
   
}


please suggest to me!! thanks
________
og kush pictures


Last edited by ideage on Wed Feb 02, 2011 5:02 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
ideage



Joined: 12 Jul 2006
Posts: 63
Location: china

PostPosted: Mon Nov 20, 2006 1:11 am    Post subject: Reply with quote

TO Jarrett Billingsley:

How extend MDTable to wrap dbi.Row[] ?

How D access MiniD each other(Variable,Function,Class)?
________
Mercedes-Benz 300


Last edited by ideage on Wed Feb 02, 2011 5:02 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Mon Nov 20, 2006 4:21 pm    Post subject: Reply with quote

Wow; this is great! Thanks for showing such interest in MiniD Smile I'll try it out tonight when I have some time. I've got class soon Confused

Quote:
How extend MDTable to wrap dbi.Row[] ?

How D access MiniD each other(Variable,Function,Class)?


I'm sorry, I'm not sure I understand you. Can you give some examples of what you want to do?
Back to top
View user's profile Send private message
ideage



Joined: 12 Jul 2006
Posts: 63
Location: china

PostPosted: Mon Nov 20, 2006 6:34 pm    Post subject: Reply with quote

Quote:

Quote:
How extend MDTable to wrap dbi.Row[] ?

How D access MiniD each other(Variable,Function,Class)?


I'm sorry, I'm not sure I understand you. Can you give some examples of what you want to do?


If someone use MiniD,He can acess variable in *.MD script? and access Function or class?


I Access variable in MiniD script:
in D code:
Code:

        dchar[] w = "Width";
   state.setGlobal(w,100);      
   MDClosure cl = new MDClosure(state, compileFile("simple.md"));   
   try
   {
      state.easyCall(cl, 0u);
   }
   catch(MDException e)
   {
      writefln("error: ", e);
      writefln(state.getTracebackString());
   }   
   MDValue* sl = state.getGlobal(f);    
   writefln (sl.asString());

in simple.md:
Code:

writefln(Width);
Width = 200;


when execute,it output:
100
200
________
herbalaire reviews


Last edited by ideage on Wed Feb 02, 2011 5:02 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Tue Nov 21, 2006 3:10 pm    Post subject: Reply with quote

OK, yes, that's right. Anything that you want to be accessible from both D code and MiniD code has to be a MiniD global, since locals in MiniD code will disappear.

Now, to access MiniD functions and classes from D, it's just an extension of that.

Say you have the MiniD code:

Code:
function Foo(x, y)
{
   writefln("Foo got: ", x, ", ", y);
}


Then, in D, you can access this:

Code:
MDClosure cl = new MDClosure(state, compileFile(`simple.md`));

// This is required so that the function gets set as a global.
state.easyCall(cl, 0u);

MDClosure func = state.getGlobal("Foo").asFunction();
state.easyCall(func, 0u, 3, 4);


This will output:

Code:
Foo got: 3, 4


Notice that you have to execute the loaded file in order for the function "Foo" to be set as a global. Then you can just get the global and call it with state.easyCall().

As for MiniD classes in D.. I plan on really changing how classes and instances are created very soon, so if I documented how to use them, it might be outdated in a couple weeks.

Lastly, I plan on working on a binding library which should make it much easier to use MiniD code from D and to expose D functions and classes to MiniD. So a lot of this might become obsolete as well Smile
Back to top
View user's profile Send private message
ideage



Joined: 12 Jul 2006
Posts: 63
Location: china

PostPosted: Tue Nov 21, 2006 9:38 pm    Post subject: Reply with quote

Quote:

Code:


MDClosure cl = new MDClosure(state, compileFile(`simple.md`));

// This is required so that the function gets set as a global.
state.easyCall(cl, 0u);

MDClosure func = state.getGlobal("Foo").asFunction();
state.easyCall(func, 0u, 3, 4);



aha,Thanks,I see,If i want return a value,How do it? Now,i use a MiniD global value only.
I find push function in State,but no Pop funtion!

When I use ansi code,MiniD work good,then i use chinese in code,It fail.
I save my source files in utf8 encoding.

Database is main way for programer, i think dblib is a standard lib,OK?

How compile *.MD source to bin format ?like Lua,compile then protect source?
________
T platform


Last edited by ideage on Wed Feb 02, 2011 5:02 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Fri Nov 24, 2006 11:18 am    Post subject: Reply with quote

Quote:
If i want return a value,How do it? Now,i use a MiniD global value only.
I find push function in State,but no Pop funtion!


Right now there are some bugs in the state which don't allow return values to native functions. I just found out about these this week, so I hope to have them fixed soon.

Quote:
When I use ansi code,MiniD work good,then i use chinese in code,It fail.
I save my source files in utf8 encoding.


This is probably a bug in the compiler. Could you send me the source code that's giving you the errors?

Quote:
Database is main way for programer, i think dblib is a standard lib,OK?


Sure, I think it's great to use other dsource projects as well Smile

Quote:
How compile *.MD source to bin format ?like Lua,compile then protect source?


Another feature I hope to put in very soon Smile
Back to top
View user's profile Send private message
ideage



Joined: 12 Jul 2006
Posts: 63
Location: china

PostPosted: Sat Nov 25, 2006 12:18 am    Post subject: Reply with quote

Thank you for that answer the question particularity. I wait for answer a long time.

Quote:
This is probably a bug in the compiler. Could you send me the source code that's giving you the errors?


err: simple.md(3:14): Invalid ASCII character 0xe5

simple.md:
WindowTitle="????????"~KeyEvent;

Expect you improve MiniD!
________
Ford U platform specifications


Last edited by ideage on Wed Feb 02, 2011 5:02 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Sun Nov 26, 2006 8:16 am    Post subject: Reply with quote

Make sure you put a BOM at the beginning of your UTF-8 source code files. The compiler will default to ASCII if it doesn't find a BOM at the beginning of the file. Your text editor or source code editor should have an option to include a BOM when saving the file.

Although, I might just change the compiler to default to UTF-8 if it doesn't find a BOM..
Back to top
View user's profile Send private message
ideage



Joined: 12 Jul 2006
Posts: 63
Location: china

PostPosted: Mon Nov 27, 2006 12:40 am    Post subject: Reply with quote

thanks,It is OK when my save a BOM at beginning of the source.
________
Roy Abernethy
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> MiniD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group