diff options
author | Sean Dague | 2007-09-07 12:37:05 +0000 |
---|---|---|
committer | Sean Dague | 2007-09-07 12:37:05 +0000 |
commit | 6a45a1ce9c7be613ebc7f16372acf27c991a245e (patch) | |
tree | 1baf266ca85789c400c5b281f4a059050f8c9c68 /OpenSim/Framework/Data.SQLite/SQLiteUserData.cs | |
parent | minor change to CONTRIBUTORS, and a quick test of the (diff) | |
download | opensim-SC_OLD-6a45a1ce9c7be613ebc7f16372acf27c991a245e.zip opensim-SC_OLD-6a45a1ce9c7be613ebc7f16372acf27c991a245e.tar.gz opensim-SC_OLD-6a45a1ce9c7be613ebc7f16372acf27c991a245e.tar.bz2 opensim-SC_OLD-6a45a1ce9c7be613ebc7f16372acf27c991a245e.tar.xz |
factor out common methods to SQLiteBase
Diffstat (limited to 'OpenSim/Framework/Data.SQLite/SQLiteUserData.cs')
-rw-r--r-- | OpenSim/Framework/Data.SQLite/SQLiteUserData.cs | 170 |
1 files changed, 1 insertions, 169 deletions
diff --git a/OpenSim/Framework/Data.SQLite/SQLiteUserData.cs b/OpenSim/Framework/Data.SQLite/SQLiteUserData.cs index b09354d..26c832d 100644 --- a/OpenSim/Framework/Data.SQLite/SQLiteUserData.cs +++ b/OpenSim/Framework/Data.SQLite/SQLiteUserData.cs | |||
@@ -39,7 +39,7 @@ namespace OpenSim.Framework.Data.SQLite | |||
39 | /// <summary> | 39 | /// <summary> |
40 | /// A User storage interface for the DB4o database system | 40 | /// A User storage interface for the DB4o database system |
41 | /// </summary> | 41 | /// </summary> |
42 | public class SQLiteUserData : IUserData | 42 | public class SQLiteUserData : SQLiteBase, IUserData |
43 | { | 43 | { |
44 | /// <summary> | 44 | /// <summary> |
45 | /// The database manager | 45 | /// The database manager |
@@ -284,12 +284,6 @@ namespace OpenSim.Framework.Data.SQLite | |||
284 | * | 284 | * |
285 | **********************************************************************/ | 285 | **********************************************************************/ |
286 | 286 | ||
287 | private void createCol(DataTable dt, string name, System.Type type) | ||
288 | { | ||
289 | DataColumn col = new DataColumn(name, type); | ||
290 | dt.Columns.Add(col); | ||
291 | } | ||
292 | |||
293 | private DataTable createUsersTable() | 287 | private DataTable createUsersTable() |
294 | { | 288 | { |
295 | DataTable users = new DataTable("users"); | 289 | DataTable users = new DataTable("users"); |
@@ -477,98 +471,6 @@ namespace OpenSim.Framework.Data.SQLite | |||
477 | 471 | ||
478 | /*********************************************************************** | 472 | /*********************************************************************** |
479 | * | 473 | * |
480 | * SQL Statement Creation Functions | ||
481 | * | ||
482 | * These functions create SQL statements for update, insert, and create. | ||
483 | * They can probably be factored later to have a db independant | ||
484 | * portion and a db specific portion | ||
485 | * | ||
486 | **********************************************************************/ | ||
487 | |||
488 | private SqliteCommand createInsertCommand(string table, DataTable dt) | ||
489 | { | ||
490 | /** | ||
491 | * This is subtle enough to deserve some commentary. | ||
492 | * Instead of doing *lots* and *lots of hardcoded strings | ||
493 | * for database definitions we'll use the fact that | ||
494 | * realistically all insert statements look like "insert | ||
495 | * into A(b, c) values(:b, :c) on the parameterized query | ||
496 | * front. If we just have a list of b, c, etc... we can | ||
497 | * generate these strings instead of typing them out. | ||
498 | */ | ||
499 | string[] cols = new string[dt.Columns.Count]; | ||
500 | for (int i = 0; i < dt.Columns.Count; i++) { | ||
501 | DataColumn col = dt.Columns[i]; | ||
502 | cols[i] = col.ColumnName; | ||
503 | } | ||
504 | |||
505 | string sql = "insert into " + table + "("; | ||
506 | sql += String.Join(", ", cols); | ||
507 | // important, the first ':' needs to be here, the rest get added in the join | ||
508 | sql += ") values (:"; | ||
509 | sql += String.Join(", :", cols); | ||
510 | sql += ")"; | ||
511 | SqliteCommand cmd = new SqliteCommand(sql); | ||
512 | |||
513 | // this provides the binding for all our parameters, so | ||
514 | // much less code than it used to be | ||
515 | foreach (DataColumn col in dt.Columns) | ||
516 | { | ||
517 | cmd.Parameters.Add(createSqliteParameter(col.ColumnName, col.DataType)); | ||
518 | } | ||
519 | return cmd; | ||
520 | } | ||
521 | |||
522 | private SqliteCommand createUpdateCommand(string table, string pk, DataTable dt) | ||
523 | { | ||
524 | string sql = "update " + table + " set "; | ||
525 | string subsql = ""; | ||
526 | foreach (DataColumn col in dt.Columns) | ||
527 | { | ||
528 | if (subsql.Length > 0) | ||
529 | { // a map function would rock so much here | ||
530 | subsql += ", "; | ||
531 | } | ||
532 | subsql += col.ColumnName + "= :" + col.ColumnName; | ||
533 | } | ||
534 | sql += subsql; | ||
535 | sql += " where " + pk; | ||
536 | SqliteCommand cmd = new SqliteCommand(sql); | ||
537 | |||
538 | // this provides the binding for all our parameters, so | ||
539 | // much less code than it used to be | ||
540 | |||
541 | foreach (DataColumn col in dt.Columns) | ||
542 | { | ||
543 | cmd.Parameters.Add(createSqliteParameter(col.ColumnName, col.DataType)); | ||
544 | } | ||
545 | return cmd; | ||
546 | } | ||
547 | |||
548 | |||
549 | private string defineTable(DataTable dt) | ||
550 | { | ||
551 | string sql = "create table " + dt.TableName + "("; | ||
552 | string subsql = ""; | ||
553 | foreach (DataColumn col in dt.Columns) | ||
554 | { | ||
555 | if (subsql.Length > 0) | ||
556 | { // a map function would rock so much here | ||
557 | subsql += ",\n"; | ||
558 | } | ||
559 | subsql += col.ColumnName + " " + sqliteType(col.DataType); | ||
560 | if(col == dt.PrimaryKey[0]) | ||
561 | { | ||
562 | subsql += " primary key"; | ||
563 | } | ||
564 | } | ||
565 | sql += subsql; | ||
566 | sql += ")"; | ||
567 | return sql; | ||
568 | } | ||
569 | |||
570 | /*********************************************************************** | ||
571 | * | ||
572 | * Database Binding functions | 474 | * Database Binding functions |
573 | * | 475 | * |
574 | * These will be db specific due to typing, and minor differences | 476 | * These will be db specific due to typing, and minor differences |
@@ -576,27 +478,6 @@ namespace OpenSim.Framework.Data.SQLite | |||
576 | * | 478 | * |
577 | **********************************************************************/ | 479 | **********************************************************************/ |
578 | 480 | ||
579 | ///<summary> | ||
580 | /// This is a convenience function that collapses 5 repetitive | ||
581 | /// lines for defining SqliteParameters to 2 parameters: | ||
582 | /// column name and database type. | ||
583 | /// | ||
584 | /// It assumes certain conventions like :param as the param | ||
585 | /// name to replace in parametrized queries, and that source | ||
586 | /// version is always current version, both of which are fine | ||
587 | /// for us. | ||
588 | ///</summary> | ||
589 | ///<returns>a built sqlite parameter</returns> | ||
590 | private SqliteParameter createSqliteParameter(string name, System.Type type) | ||
591 | { | ||
592 | SqliteParameter param = new SqliteParameter(); | ||
593 | param.ParameterName = ":" + name; | ||
594 | param.DbType = dbtypeFromType(type); | ||
595 | param.SourceColumn = name; | ||
596 | param.SourceVersion = DataRowVersion.Current; | ||
597 | return param; | ||
598 | } | ||
599 | |||
600 | private void setupUserCommands(SqliteDataAdapter da, SqliteConnection conn) | 481 | private void setupUserCommands(SqliteDataAdapter da, SqliteConnection conn) |
601 | { | 482 | { |
602 | da.InsertCommand = createInsertCommand("users", ds.Tables["users"]); | 483 | da.InsertCommand = createInsertCommand("users", ds.Tables["users"]); |
@@ -634,54 +515,5 @@ namespace OpenSim.Framework.Data.SQLite | |||
634 | return true; | 515 | return true; |
635 | } | 516 | } |
636 | 517 | ||
637 | /*********************************************************************** | ||
638 | * | ||
639 | * Type conversion functions | ||
640 | * | ||
641 | **********************************************************************/ | ||
642 | |||
643 | private DbType dbtypeFromType(Type type) | ||
644 | { | ||
645 | if (type == typeof(System.String)) { | ||
646 | return DbType.String; | ||
647 | } else if (type == typeof(System.Int32)) { | ||
648 | return DbType.Int32; | ||
649 | } else if (type == typeof(System.UInt32)) { | ||
650 | return DbType.UInt32; | ||
651 | } else if (type == typeof(System.Int64)) { | ||
652 | return DbType.Int64; | ||
653 | } else if (type == typeof(System.UInt64)) { | ||
654 | return DbType.UInt64; | ||
655 | } else if (type == typeof(System.Double)) { | ||
656 | return DbType.Double; | ||
657 | } else if (type == typeof(System.Byte[])) { | ||
658 | return DbType.Binary; | ||
659 | } else { | ||
660 | return DbType.String; | ||
661 | } | ||
662 | } | ||
663 | |||
664 | // this is something we'll need to implement for each db | ||
665 | // slightly differently. | ||
666 | private string sqliteType(Type type) | ||
667 | { | ||
668 | if (type == typeof(System.String)) { | ||
669 | return "varchar(255)"; | ||
670 | } else if (type == typeof(System.Int32)) { | ||
671 | return "integer"; | ||
672 | } else if (type == typeof(System.UInt32)) { | ||
673 | return "integer"; | ||
674 | } else if (type == typeof(System.Int64)) { | ||
675 | return "varchar(255)"; | ||
676 | } else if (type == typeof(System.UInt64)) { | ||
677 | return "varchar(255)"; | ||
678 | } else if (type == typeof(System.Double)) { | ||
679 | return "float"; | ||
680 | } else if (type == typeof(System.Byte[])) { | ||
681 | return "blob"; | ||
682 | } else { | ||
683 | return "string"; | ||
684 | } | ||
685 | } | ||
686 | } | 518 | } |
687 | } | 519 | } |