Re: Why hibernated does not create tables automatically?

2015-02-12 Thread zhmt via Digitalmars-d-learn

how to update table schema automatically:

this()
{
mdb = new MysqlDB(connStr);

auto conn = mdb.lockConnection();
scope(exit) conn.close();
MysqlOrmUtil.updateTableSchema!(Customer)(conn);
MysqlOrmUtil.updateTableSchema!(Card)(conn);
MysqlOrmUtil.updateTableSchema!(Agent)(conn);
}


Re: Why hibernated does not create tables automatically?

2015-02-12 Thread zhmt via Digitalmars-d-learn

here is how to use:

@UniqueIndex("id",["id"])
class Card
{
@NotNull()
@Auto()
long id;
string pwd;
long agentId;
bool valid;
long rmb;
long createDate;
long soldDate;
long chargeDate;
}

public Card[] getAllCards(long page,long pageSize)
{
Connection conn = mdb.lockConnection();
scope(exit) conn.close();
string sql = "select * from Card";
Card[] ret = MysqlOrmUtil.exeQueryToObjArr!Card(sql,conn);
return ret;
}


public void createCards(long agentId,long rmb,long count)
{
long curTime = now();
Connection conn = mdb.lockConnection();
scope(exit) conn.close();

for(int i=0; i

Re: Why hibernated does not create tables automatically?

2015-02-12 Thread zhmt via Digitalmars-d-learn
finally ,I write a orm tool to replace hibernated, it is simple, 
and it is easy to use, and every thing in control.


It is free to copy ,improve.


module mysqlormutil;

import vibe.d;
import std.stdio;

import mysql.connection;
import mysql.db;


struct NotNull
{
}

struct Auto
{
}

struct Index
{
string name;
string [] cols;
}

struct UniqueIndex
{
string name;
string[] cols;
}

class TableIndexInfo
{
public string indexName;
public string[] cols;
public bool isUnique;
}

class MysqlOrmUtil
{
//更新表结构
public static void updateTableSchema(CLS)(Connection conn)
{
MysqlOrmInternalUtil.updateTableSchema!(CLS)(conn);
}

//生成replace语句,保存整个对象
public static string genSaveAllSql(CLS)(ref CLS obj)
{
return MysqlOrmInternalUtil.genSaveAllSql!(CLS)(obj);
}

	public static string genInsertSqlWithoutId(CLS)(string 
idColName,ref CLS obj)

{
		return 
MysqlOrmInternalUtil.genInsertSqlWithoutId!(CLS)(idColName,obj);

}

public static ulong exeSql(string sql,Connection conn)
{
return MysqlOrmInternalUtil.exeSql(sql,conn);
}

public static CLS exeQueryToObj(CLS)(string sql,Connection conn)
{
return MysqlOrmInternalUtil.exeQueryToObj!(CLS)(sql,conn);
}

	public static CLS exeQueryToStruct(CLS)(string sql,Connection 
conn)

{
return MysqlOrmInternalUtil.exeQueryToStruct!(CLS)(sql,conn);
}

	public static CLS[] exeQueryToObjArr(CLS)(string sql,Connection 
conn)

{
return MysqlOrmInternalUtil.exeQueryToObjArr!(CLS)(sql,conn);
}

	public static CLS[] exeQueryToStructArr(CLS)(string 
sql,Connection conn)

{
return MysqlOrmInternalUtil.exeQueryToStructArr!(CLS)(sql,conn);
}
}

class MysqlOrmInternalUtil
{
__gshared static string[string] dToMysql ;
shared static this()
{
dToMysql["int"] = "int";
dToMysql["long"] = "bigint(20)";
dToMysql["string"] = "varchar(128)";
dToMysql["bool"] = "tinyint(1)";
}

public static string getMysqlType(string dtype)
{
return dToMysql[dtype];
}

	public static CLS[] exeQueryToObjArr(CLS)(string sql,Connection 
conn)

{
Command cmd =  Command(conn);
cmd.sql = sql;
ResultSet  rs = cmd.execSQLResult();
CLS[] ret = resultSetToObjArray!(CLS)(rs);
return ret;
}

	public static CLS[] exeQueryToStructArr(CLS)(string 
sql,Connection conn)

{
Command cmd =  Command(conn);
cmd.sql = sql;
ResultSet  rs = cmd.execSQLResult();
CLS[] ret = resultSetToStructArray(rs);
return ret;
}

public static CLS exeQueryToObj(CLS)(string sql,Connection conn)
{
Command cmd =  Command(conn);
cmd.sql = sql;
ResultSet  rs = cmd.execSQLResult();
CLS ret = new CLS;
resultSetToObj!(CLS)(rs);
return ret;
}

	public static CLS exeQueryToStruct(CLS)(string sql,Connection 
conn)

{
Command cmd = new Command(conn);
cmd.sql = sql;
ResultSet  rs = cmd.execSQLResult();
CLS ret;
resultSetToStruct!(CLS)(rs);
return ret;
}

public static ulong exeSql(string sql,Connection conn)
{
Command cmd =  Command(conn);
cmd.sql = sql;
ulong ret;
cmd.execSQL(ret);
return ret;
}

public static CLS[] resultSetToObjArray(CLS)(ref ResultSet rs)
{
CLS[] arr;
foreach(Row row ; rs)
{
CLS obj = new CLS;
rowToObjOrStruct!(CLS)(rs,row,obj);
arr ~= obj;
}
return arr;
}

public static CLS resultSetToObj(CLS)(ref ResultSet rs)
{
if(rs.length == 0)
{
return null;
}

CLS ret = new CLS;
resultSetToObjOrStruct!(CLS)(rs,ret);
return ret;
}

public static CLS[] resultSetToStructArray(CLS)(ref ResultSet rs)
{
CLS[] arr;
foreach(Row row ; rs)
{
CLS obj;
rowToObjOrStruct(row,obj);
arr ~= obj;
}
return arr;
}

Re: Why hibernated does not create tables automatically?

2015-02-06 Thread zhmt via Digitalmars-d-learn

Hi,Arjan,Thx for your replies, I have tried your suggestion,

MySQLDriver driver = new MySQLDriver();
	string url = MySQLDriver.generateUrl("10.211.55.10", 3306, 
"test");
	string[string] params = MySQLDriver.setUserAndPassword("root", 
"xxx");

auto ds = new ConnectionPoolDataSourceImpl(driver, url, params);

EntityMetaData schema = new SchemaInfoImpl!(Card);
Dialect dialect = new MySQLDialect();
auto factory = new SessionFactoryImpl(schema, dialect, ds);

auto conn = ds.getConnection();
scope(exit) conn.close();
auto db = factory.getDBMetaData();
db.updateDBSchema(conn, true, true);

Card c = new Card;
auto s = factory.openSession();
scope(exit) s.close();
s.save(c);

The table is not created. The same code works about two weeks 
ago, but fails today, it make me confused.


Re: Why hibernated does not create tables automatically?

2015-02-06 Thread Arjan via Digitalmars-d-learn

On Friday, 6 February 2015 at 09:42:09 UTC, zhmt wrote:

class Card
{
import hibernated.core;

@Id
@Generated
long id;
@UniqueKey
string pwd;
}

MySQLDriver driver = new MySQLDriver();
	string url = MySQLDriver.generateUrl("10.211.55.10", 3306, 
"test");
	string[string] params = MySQLDriver.setUserAndPassword("root", 
"xxx");
	auto ds = new ConnectionPoolDataSourceImpl(driver, url, 
params);


EntityMetaData schema = new SchemaInfoImpl!(Card);
Dialect dialect = new MySQLDialect();
auto factory = new SessionFactoryImpl(schema, dialect, ds);


Card c = new Card;
auto s = factory.openSession();
scope(exit) s.close();
s.save(c);


Here is the simplest example, but it complains the same error.

OK,
Before the call
"auto s = factory.openSession();"
Does de mysql db has a db called 'test' and a table called 'card' 
with columns named 'id' and 'pwd'? If not call:

"factory.getDBMetaData().updateDBSchema( conn, true, true );"
verify the table is created in mysql.
After create call:
"factory.openSession();"
...
If things are still failing take a look here:
https://github.com/buggins/hibernated/blob/master/hdtest/source/htestmain.d
This is a simple program testing some basic functions of 
hibernated. Try it with sqlite first after that add a mysql 
section and try again.


Re: Why hibernated does not create tables automatically?

2015-02-06 Thread zhmt via Digitalmars-d-learn
I have submit an issue on github, hope that the author could help 
me.


Re: Why hibernated does not create tables automatically?

2015-02-06 Thread zhmt via Digitalmars-d-learn

class Card
{
import hibernated.core;

@Id
@Generated
long id;
@UniqueKey
string pwd;
}

MySQLDriver driver = new MySQLDriver();
	string url = MySQLDriver.generateUrl("10.211.55.10", 3306, 
"test");
	string[string] params = MySQLDriver.setUserAndPassword("root", 
"xxx");

auto ds = new ConnectionPoolDataSourceImpl(driver, url, params);

EntityMetaData schema = new SchemaInfoImpl!(Card);
Dialect dialect = new MySQLDialect();
auto factory = new SessionFactoryImpl(schema, dialect, ds);


Card c = new Card;
auto s = factory.openSession();
scope(exit) s.close();
s.save(c);


Here is the simplest example, but it complains the same error.


Re: Why hibernated does not create tables automatically?

2015-02-06 Thread Arjan via Digitalmars-d-learn

On Friday, 6 February 2015 at 08:53:12 UTC, zhmt wrote:
The app compiles fine, but It throw an exception when I try to 
save data to mysql :


hibernated.type.MappingException@../../../zhmt/.dub/packages/hibernated-0.2.19/source/hibernated/metadata.d(3332): 
Cannot find entity by class ezsockacount.Dao.Customer


My initialization code is something like:

MySQLDriver driver = new MySQLDriver();
		string url = MySQLDriver.generateUrl("10.211.55.10", 3306, 
"test");
		string[string] params = 
MySQLDriver.setUserAndPassword("root", "xxx");

ds = new ConnectionPoolDataSourceImpl(driver, url, params);

		EntityMetaData schema = new 
SchemaInfoImpl!(Customer,Card,Agent);

Dialect dialect = new MySQLDialect();
factory = new SessionFactoryImpl(schema, dialect, ds);

Connection conn = ds.getConnection();
scope(exit) conn.close();

DBInfo db = factory.getDBMetaData();
db.updateDBSchema(conn, false, true);



I checked the "static EntityInfo [] entities; in 
SchemaInfoImpl",

the length of entites is 0.

And tables in mysql is not created automatically either.

I found the point of this question, but dont know how to 
resolve it?


Will anybody help me?

Thx ahead!!


IFAIK partial creation does not work. (bug?)
Use db.updateDBSchema( conn, true, true) make sure none of the 
entities exist in the database. Otherwise it will fail.


hth.


Why hibernated does not create tables automatically?

2015-02-06 Thread zhmt via Digitalmars-d-learn
The app compiles fine, but It throw an exception when I try to 
save data to mysql :


hibernated.type.MappingException@../../../zhmt/.dub/packages/hibernated-0.2.19/source/hibernated/metadata.d(3332): 
Cannot find entity by class ezsockacount.Dao.Customer


My initialization code is something like:

MySQLDriver driver = new MySQLDriver();
		string url = MySQLDriver.generateUrl("10.211.55.10", 3306, 
"test");
		string[string] params = MySQLDriver.setUserAndPassword("root", 
"xxx");

ds = new ConnectionPoolDataSourceImpl(driver, url, params);

		EntityMetaData schema = new 
SchemaInfoImpl!(Customer,Card,Agent);

Dialect dialect = new MySQLDialect();
factory = new SessionFactoryImpl(schema, dialect, ds);

Connection conn = ds.getConnection();
scope(exit) conn.close();

DBInfo db = factory.getDBMetaData();
db.updateDBSchema(conn, false, true);



I checked the "static EntityInfo [] entities; in SchemaInfoImpl",
the length of entites is 0.

And tables in mysql is not created automatically either.

I found the point of this question, but dont know how to resolve 
it?


Will anybody help me?

Thx ahead!!